From dd73c9b441f7672fb9982c4caeb5178df30f5d2b Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 27 May 2012 16:47:30 -0600 Subject: altos: Split out mm-specific versions of sampling code Avoid breaking telemetrum (too much) by splitting this stuff apart. Signed-off-by: Keith Packard --- src/core/ao_data.h | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 src/core/ao_data.h (limited to 'src/core/ao_data.h') diff --git a/src/core/ao_data.h b/src/core/ao_data.h new file mode 100644 index 00000000..d28730a8 --- /dev/null +++ b/src/core/ao_data.h @@ -0,0 +1,191 @@ +/* + * Copyright © 2012 Keith Packard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +#ifndef _AO_DATA_H_ +#define _AO_DATA_H_ + +#if HAS_MS5607 +#include +#endif + +#if HAS_MPU6000 +#include +#endif + +struct ao_data { + uint16_t tick; +#if HAS_ADC + struct ao_adc adc; +#endif +#if HAS_ACCEL_REF + uint16_t accel_ref; +#endif +#if HAS_MS5607 + struct ao_ms5607_sample ms5607; +#endif +#if HAS_MPU6000 + struct ao_mpu6000_sample mpu6000; +#endif +}; + +#define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1)) +#define ao_data_ring_prev(n) (((n) - 1) & (AO_DATA_RING - 1)) + +extern volatile __xdata struct ao_data ao_data_ring[AO_DATA_RING]; +extern volatile __data uint8_t ao_data_head; + +#if HAS_MS5607 + +typedef int32_t pres_t; +typedef int32_t alt_t; + +static inline pres_t ao_data_pres(struct ao_data *packet) +{ + struct ao_ms5607_value value; + + ao_ms5607_convert(&packet->ms5607, &value); + return value.pres; +} + +#define pres_to_altitude(p) ao_pa_to_altitude(p) + +#else + +typedef int16_t pres_t; +typedef int16_t alt_t; + +#define ao_data_pres(packet) ((packet)->adc.pres) +#define pres_to_altitude(p) ao_pres_to_altitude(p) + +#endif + +/* + * Need a few macros to pull data from the sensors: + * + * ao_data_accel_sample - pull raw sensor and convert to normalized values + * ao_data_accel - pull normalized value (lives in the same memory) + * ao_data_set_accel - store normalized value back in the sensor location + * ao_data_accel_invert - flip rocket ends for positive acceleration + */ + +#if HAS_MPU6000 + +typedef int16_t accel_t; + +/* MPU6000 is hooked up so that positive y is positive acceleration */ +#define ao_data_accel(packet) ((packet)->mpu6000.accel_y) +#define ao_data_accel_sample(packet) (-ao_data_accel(packet)) +#define ao_data_set_accel(packet, accel) ((packet)->mpu6000.accel_y = (accel)) +#define ao_data_accel_invert(a) (-(a)) + +#else + +typedef int16_t accel_t; +#define ao_data_accel(packet) ((packet)->adc.accel) +#define ao_data_set_accel(packet, accel) ((packet)->adc.accel = (accel)) +#define ao_data_accel_invert(a) (0x7fff -(a)) + +/* + * Ok, the math here is a bit tricky. + * + * ao_sample_accel: ADC output for acceleration + * ao_accel_ref: ADC output for the 5V reference. + * ao_cook_accel: Corrected acceleration value + * Vcc: 3.3V supply to the CC1111 + * Vac: 5V supply to the accelerometer + * accel: input voltage to accelerometer ADC pin + * ref: input voltage to 5V reference ADC pin + * + * + * Measured acceleration is ratiometric to Vcc: + * + * ao_sample_accel accel + * ------------ = ----- + * 32767 Vcc + * + * Measured 5v reference is also ratiometric to Vcc: + * + * ao_accel_ref ref + * ------------ = ----- + * 32767 Vcc + * + * + * ao_accel_ref = 32767 * (ref / Vcc) + * + * Acceleration is measured ratiometric to the 5V supply, + * so what we want is: + * + * ao_cook_accel accel + * ------------- = ----- + * 32767 ref + * + * + * accel Vcc + * = ----- * --- + * Vcc ref + * + * ao_sample_accel 32767 + * = ------------ * ------------ + * 32767 ao_accel_ref + * + * Multiply through by 32767: + * + * ao_sample_accel * 32767 + * ao_cook_accel = -------------------- + * ao_accel_ref + * + * Now, the tricky part. Getting this to compile efficiently + * and keeping all of the values in-range. + * + * First off, we need to use a shift of 16 instead of * 32767 as SDCC + * does the obvious optimizations for byte-granularity shifts: + * + * ao_cook_accel = (ao_sample_accel << 16) / ao_accel_ref + * + * Next, lets check our input ranges: + * + * 0 <= ao_sample_accel <= 0x7fff (singled ended ADC conversion) + * 0x7000 <= ao_accel_ref <= 0x7fff (the 5V ref value is close to 0x7fff) + * + * Plugging in our input ranges, we get an output range of 0 - 0x12490, + * which is 17 bits. That won't work. If we take the accel ref and shift + * by a bit, we'll change its range: + * + * 0xe000 <= ao_accel_ref<<1 <= 0xfffe + * + * ao_cook_accel = (ao_sample_accel << 16) / (ao_accel_ref << 1) + * + * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It + * is, however, one bit too large for our signed computations. So, we + * take the result and shift that by a bit: + * + * ao_cook_accel = ((ao_sample_accel << 16) / (ao_accel_ref << 1)) >> 1 + * + * This finally creates an output range of 0 - 0x4924. As the ADC only + * provides 11 bits of data, we haven't actually lost any precision, + * just dropped a bit of noise off the low end. + */ +#if HAS_ACCEL_REF +#define ao_data_accel_sample(packet) \ + ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->accel_ref << 1))) >> 1) +#else +#define ao_data_accel(packet) ((packet)->adc.accel) +#endif /* HAS_ACCEL_REF */ + +#endif /* else some other pressure sensor */ + +#endif /* _AO_DATA_H_ */ -- cgit v1.2.3 From ce8153472069ed56b24ac36f297ac569d1f767d4 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 27 May 2012 17:24:09 -0600 Subject: altos: Make telemetrum-v1.1 compile with new ao_data structure Signed-off-by: Keith Packard --- src/cc1111/ao_adc.c | 36 ++++++++++++++++-------------------- src/cc1111/ao_arch.h | 3 +-- src/cc1111/ao_ignite.c | 9 +++++---- src/core/ao_adc.h | 28 +++------------------------- src/core/ao_data.h | 2 +- src/core/ao_log_big.c | 24 ++++++++++++------------ src/core/ao_telemetry.c | 28 ++++++++++++++-------------- src/product/Makefile.telemetrum | 4 ++-- src/stm/ao_adc_stm.c | 8 ++++++++ 9 files changed, 62 insertions(+), 80 deletions(-) (limited to 'src/core/ao_data.h') diff --git a/src/cc1111/ao_adc.c b/src/cc1111/ao_adc.c index 1688eceb..1e3d8cab 100644 --- a/src/cc1111/ao_adc.c +++ b/src/cc1111/ao_adc.c @@ -16,13 +16,9 @@ */ #include "ao.h" -#include "ao_pins.h" -volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING]; -#if HAS_ACCEL_REF -volatile __xdata uint16_t ao_accel_ref[AO_ADC_RING]; -#endif -volatile __data uint8_t ao_adc_head; +volatile __xdata struct ao_data ao_data_ring[AO_DATA_RING]; +volatile __data uint8_t ao_data_head; void ao_adc_poll(void) @@ -39,14 +35,14 @@ ao_adc_poll(void) } void -ao_adc_get(__xdata struct ao_adc *packet) +ao_data_get(__xdata struct ao_data *packet) { #if HAS_FLIGHT - uint8_t i = ao_adc_ring_prev(ao_sample_adc); + uint8_t i = ao_data_ring_prev(ao_sample_data); #else - uint8_t i = ao_adc_ring_prev(ao_adc_head); + uint8_t i = ao_data_ring_prev(ao_data_head); #endif - ao_xmemcpy(packet, &ao_adc_ring[i], sizeof (struct ao_adc)); + ao_xmemcpy(packet, (void __xdata *) &ao_data_ring[i], sizeof (struct ao_data)); } void @@ -60,14 +56,14 @@ ao_adc_isr(void) __interrupt 1 /* TeleMetrum readings */ #if HAS_ACCEL_REF if (sequence == 2) { - a = (uint8_t __xdata *) (&ao_accel_ref[ao_adc_head]); + a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].accel_ref); sequence = 0; } else #endif { if (sequence == ADCCON3_ECH_TEMP) sequence = 2; - a = (uint8_t __xdata *) (&ao_adc_ring[ao_adc_head].accel + sequence); + a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].adc.accel + sequence); sequence++; } #define GOT_ADC @@ -87,7 +83,7 @@ ao_adc_isr(void) __interrupt 1 #if IGNITE_ON_P0 /* TeleMini readings */ - a = (uint8_t __xdata *) (&ao_adc_ring[ao_adc_head].pres); + a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].pres); #ifdef TELEMINI_V_1_0 switch (sequence) { case 0: @@ -149,20 +145,20 @@ ao_adc_isr(void) __interrupt 1 else { /* record this conversion series */ - ao_adc_ring[ao_adc_head].tick = ao_time(); - ao_adc_head = ao_adc_ring_next(ao_adc_head); - ao_wakeup(DATA_TO_XDATA(&ao_adc_head)); + ao_data_ring[ao_data_head].tick = ao_time(); + ao_data_head = ao_data_ring_next(ao_data_head); + ao_wakeup(DATA_TO_XDATA(&ao_data_head)); } } static void ao_adc_dump(void) __reentrant { - static __xdata struct ao_adc packet; - ao_adc_get(&packet); + static __xdata struct ao_data packet; + ao_data_get(&packet); printf("tick: %5u accel: %5d pres: %5d temp: %5d batt: %5d drogue: %5d main: %5d\n", - packet.tick, packet.accel, packet.pres, packet.temp, - packet.v_batt, packet.sense_d, packet.sense_m); + packet.tick, packet.adc.accel, packet.adc.pres, packet.adc.temp, + packet.adc.v_batt, packet.adc.sense_d, packet.adc.sense_m); } __code struct ao_cmds ao_adc_cmds[] = { diff --git a/src/cc1111/ao_arch.h b/src/cc1111/ao_arch.h index 9d0643b4..704ae4f9 100644 --- a/src/cc1111/ao_arch.h +++ b/src/cc1111/ao_arch.h @@ -199,7 +199,6 @@ extern __code __at (0x00a6) uint32_t ao_radio_cal; #define ao_arch_critical(b) __critical { b } struct ao_adc { - uint16_t tick; /* tick when the sample was read */ int16_t accel; /* accelerometer */ int16_t pres; /* pressure sensor */ int16_t temp; /* temperature sensor */ @@ -208,7 +207,7 @@ struct ao_adc { int16_t sense_m; /* main continuity sense */ }; -#define AO_ADC_RING 32 +#define AO_DATA_RING 32 /* ao_button.c */ #ifdef HAS_BUTTON diff --git a/src/cc1111/ao_ignite.c b/src/cc1111/ao_ignite.c index dd529088..cf07dfe8 100644 --- a/src/cc1111/ao_ignite.c +++ b/src/cc1111/ao_ignite.c @@ -16,6 +16,7 @@ */ #include "ao.h" +#include __xdata struct ao_ignition ao_ignition[2]; @@ -29,12 +30,12 @@ ao_ignite(enum ao_igniter igniter) __critical enum ao_igniter_status ao_igniter_status(enum ao_igniter igniter) { - __xdata struct ao_adc adc; + __xdata struct ao_data packet; __pdata int16_t value; __pdata uint8_t request, firing, fired; __critical { - ao_adc_get(&adc); + ao_data_get(&packet); request = ao_ignition[igniter].request; fired = ao_ignition[igniter].fired; firing = ao_ignition[igniter].firing; @@ -45,10 +46,10 @@ ao_igniter_status(enum ao_igniter igniter) value = (AO_IGNITER_CLOSED>>1); switch (igniter) { case ao_igniter_drogue: - value = adc.sense_d; + value = packet.adc.sense_d; break; case ao_igniter_main: - value = adc.sense_m; + value = packet.adc.sense_m; break; } if (value < AO_IGNITER_OPEN) diff --git a/src/core/ao_adc.h b/src/core/ao_adc.h index 8ec740c4..0dd87080 100644 --- a/src/core/ao_adc.h +++ b/src/core/ao_adc.h @@ -18,29 +18,7 @@ #ifndef _AO_ADC_H_ #define _AO_ADC_H_ -/* - * One set of samples read from the A/D converter or telemetry - */ - -#if AO_ADC_RING -/* - * ao_adc.c - */ - -#define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1)) -#define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1)) - - -/* - * A/D data is stored in a ring, with the next sample to be written - * at ao_adc_head - */ -extern volatile __xdata struct ao_adc ao_adc_ring[AO_ADC_RING]; -extern volatile __data uint8_t ao_adc_head; -#if HAS_ACCEL_REF -extern volatile __xdata uint16_t ao_accel_ref[AO_ADC_RING]; -#endif -#endif +#include /* Trigger a conversion sequence (called from the timer interrupt) */ void @@ -50,9 +28,9 @@ ao_adc_poll(void); void ao_adc_sleep(void); -/* Get a copy of the last complete A/D sample set */ +/* Get a copy of the last complete sample set */ void -ao_adc_get(__xdata struct ao_adc *packet); +ao_data_get(__xdata struct ao_data *packet); /* Initialize the A/D converter */ void diff --git a/src/core/ao_data.h b/src/core/ao_data.h index d28730a8..bd7e2d54 100644 --- a/src/core/ao_data.h +++ b/src/core/ao_data.h @@ -96,7 +96,7 @@ typedef int16_t accel_t; typedef int16_t accel_t; #define ao_data_accel(packet) ((packet)->adc.accel) -#define ao_data_set_accel(packet, accel) ((packet)->adc.accel = (accel)) +#define ao_data_set_accel(packet, a) ((packet)->adc.accel = (a)) #define ao_data_accel_invert(a) (0x7fff -(a)) /* diff --git a/src/core/ao_log_big.c b/src/core/ao_log_big.c index 43b3aa0c..50eca069 100644 --- a/src/core/ao_log_big.c +++ b/src/core/ao_log_big.c @@ -60,7 +60,7 @@ ao_log_dump_check_data(void) return 1; } -static __data uint8_t ao_log_adc_pos; +static __data uint8_t ao_log_data_pos; /* a hack to make sure that ao_log_records fill the eeprom block in even units */ typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ; @@ -94,17 +94,17 @@ ao_log(void) /* Write the whole contents of the ring to the log * when starting up. */ - ao_log_adc_pos = ao_adc_ring_next(ao_sample_adc); - next_other = next_sensor = ao_adc_ring[ao_log_adc_pos].tick; + ao_log_data_pos = ao_data_ring_next(ao_sample_data); + next_other = next_sensor = ao_data_ring[ao_log_data_pos].tick; ao_log_state = ao_flight_startup; for (;;) { /* Write samples to EEPROM */ - while (ao_log_adc_pos != ao_sample_adc) { - log.tick = ao_adc_ring[ao_log_adc_pos].tick; + while (ao_log_data_pos != ao_sample_data) { + log.tick = ao_data_ring[ao_log_data_pos].tick; if ((int16_t) (log.tick - next_sensor) >= 0) { log.type = AO_LOG_SENSOR; - log.u.sensor.accel = ao_adc_ring[ao_log_adc_pos].accel; - log.u.sensor.pres = ao_adc_ring[ao_log_adc_pos].pres; + log.u.sensor.accel = ao_data_ring[ao_log_data_pos].adc.accel; + log.u.sensor.pres = ao_data_ring[ao_log_data_pos].adc.pres; ao_log_data(&log); if (ao_log_state <= ao_flight_coast) next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT; @@ -113,16 +113,16 @@ ao_log(void) } if ((int16_t) (log.tick - next_other) >= 0) { log.type = AO_LOG_TEMP_VOLT; - log.u.temp_volt.temp = ao_adc_ring[ao_log_adc_pos].temp; - log.u.temp_volt.v_batt = ao_adc_ring[ao_log_adc_pos].v_batt; + log.u.temp_volt.temp = ao_data_ring[ao_log_data_pos].adc.temp; + log.u.temp_volt.v_batt = ao_data_ring[ao_log_data_pos].adc.v_batt; ao_log_data(&log); log.type = AO_LOG_DEPLOY; - log.u.deploy.drogue = ao_adc_ring[ao_log_adc_pos].sense_d; - log.u.deploy.main = ao_adc_ring[ao_log_adc_pos].sense_m; + log.u.deploy.drogue = ao_data_ring[ao_log_data_pos].adc.sense_d; + log.u.deploy.main = ao_data_ring[ao_log_data_pos].adc.sense_m; ao_log_data(&log); next_other = log.tick + AO_OTHER_INTERVAL; } - ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos); + ao_log_data_pos = ao_data_ring_next(ao_log_data_pos); } /* Write state change to EEPROM */ if (ao_flight_state != ao_log_state) { diff --git a/src/core/ao_telemetry.c b/src/core/ao_telemetry.c index 46d72609..800adfcb 100644 --- a/src/core/ao_telemetry.c +++ b/src/core/ao_telemetry.c @@ -53,23 +53,23 @@ static __xdata union ao_telemetry_all telemetry; static void ao_send_sensor(void) { - uint8_t sample = ao_adc_ring_prev(ao_sample_adc); + uint8_t sample = ao_data_ring_prev(ao_sample_data); - telemetry.generic.tick = ao_adc_ring[sample].tick; + telemetry.generic.tick = ao_data_ring[sample].tick; telemetry.generic.type = AO_TELEMETRY_SENSOR; telemetry.sensor.state = ao_flight_state; #if HAS_ACCEL - telemetry.sensor.accel = ao_adc_ring[sample].accel; + telemetry.sensor.accel = ao_data_ring[sample].adc.accel; #else telemetry.sensor.accel = 0; #endif - telemetry.sensor.pres = ao_adc_ring[sample].pres; - telemetry.sensor.temp = ao_adc_ring[sample].temp; - telemetry.sensor.v_batt = ao_adc_ring[sample].v_batt; + telemetry.sensor.pres = ao_data_ring[sample].adc.pres; + telemetry.sensor.temp = ao_data_ring[sample].adc.temp; + telemetry.sensor.v_batt = ao_data_ring[sample].adc.v_batt; #if HAS_IGNITE - telemetry.sensor.sense_d = ao_adc_ring[sample].sense_d; - telemetry.sensor.sense_m = ao_adc_ring[sample].sense_m; + telemetry.sensor.sense_d = ao_data_ring[sample].adc.sense_d; + telemetry.sensor.sense_m = ao_data_ring[sample].adc.sense_m; #else telemetry.sensor.sense_d = 0; telemetry.sensor.sense_m = 0; @@ -99,19 +99,19 @@ static uint8_t ao_baro_sample; static void ao_send_baro(void) { - uint8_t sample = ao_sample_adc; - uint8_t samples = (sample - ao_baro_sample) & (AO_ADC_RING - 1); + uint8_t sample = ao_sample_data; + uint8_t samples = (sample - ao_baro_sample) & (AO_DATA_RING - 1); if (samples > 12) { - ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_ADC_RING - 1); + ao_baro_sample = (ao_baro_sample + (samples - 12)) & (AO_DATA_RING - 1); samples = 12; } - telemetry.generic.tick = ao_adc_ring[sample].tick; + telemetry.generic.tick = ao_data_ring[sample].tick; telemetry.generic.type = AO_TELEMETRY_BARO; telemetry.baro.samples = samples; for (sample = 0; sample < samples; sample++) { - telemetry.baro.baro[sample] = ao_adc_ring[ao_baro_sample].pres; - ao_baro_sample = ao_adc_ring_next(ao_baro_sample); + telemetry.baro.baro[sample] = ao_data_ring[ao_baro_sample].adc.pres; + ao_baro_sample = ao_data_ring_next(ao_baro_sample); } ao_radio_send(&telemetry, sizeof (telemetry)); } diff --git a/src/product/Makefile.telemetrum b/src/product/Makefile.telemetrum index e4f55f88..b1111053 100644 --- a/src/product/Makefile.telemetrum +++ b/src/product/Makefile.telemetrum @@ -34,8 +34,8 @@ CORE_SRC = \ ao_stdio.c \ ao_storage.c \ ao_task.c \ - ao_flight.c \ - ao_sample.c \ + ao_flight_mm.c \ + ao_sample_mm.c \ ao_kalman.c \ ao_log.c \ ao_log_big.c \ diff --git a/src/stm/ao_adc_stm.c b/src/stm/ao_adc_stm.c index 02187205..ea9e25e4 100644 --- a/src/stm/ao_adc_stm.c +++ b/src/stm/ao_adc_stm.c @@ -104,14 +104,22 @@ ao_adc_poll(void) void ao_adc_get(__xdata struct ao_adc *packet) { +#if HAS_FLIGHT + uint8_t i = ao_data_ring_prev(ao_sample_data); +#else uint8_t i = ao_data_ring_prev(ao_data_head); +#endif memcpy(packet, (void *) &ao_data_ring[i].adc, sizeof (struct ao_adc)); } void ao_data_get(__xdata struct ao_data *packet) { +#if HAS_FLIGHT + uint8_t i = ao_data_ring_prev(ao_sample_data); +#else uint8_t i = ao_data_ring_prev(ao_data_head); +#endif memcpy(packet, (void *) &ao_data_ring[i], sizeof (struct ao_data)); } -- cgit v1.2.3 From 98e992b65d366d9f79d7d2dd2dd4d1886dd1d9c4 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 27 May 2012 17:25:17 -0600 Subject: altos: Make telemetrum-v1.0 build with new ao_data structure Signed-off-by: Keith Packard --- src/core/ao_data.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/ao_data.h') diff --git a/src/core/ao_data.h b/src/core/ao_data.h index bd7e2d54..bb26c4c0 100644 --- a/src/core/ao_data.h +++ b/src/core/ao_data.h @@ -183,7 +183,7 @@ typedef int16_t accel_t; #define ao_data_accel_sample(packet) \ ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->accel_ref << 1))) >> 1) #else -#define ao_data_accel(packet) ((packet)->adc.accel) +#define ao_data_accel_sample(packet) ((packet)->adc.accel) #endif /* HAS_ACCEL_REF */ #endif /* else some other pressure sensor */ -- cgit v1.2.3 From c04af7533bd3fd3f3260338c0753fde966131720 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 1 Jun 2012 23:07:38 -0700 Subject: altos: Add support for MegaAccel daughter card. Switches all acceleration computation to using the MegaAccel accelerometer to ensure support for high-g flights. MPU6000 values continue to be logged as normal Signed-off-by: Keith Packard --- src/cc1111/ao_adc.c | 2 +- src/cc1111/ao_arch.h | 1 + src/core/ao_data.h | 7 ++----- src/megametrum-v0.1/ao_pins.h | 28 ++++++++++++++++++++++++---- src/stm/ao_adc_stm.c | 25 +++++++++++++++++++++++-- 5 files changed, 51 insertions(+), 12 deletions(-) (limited to 'src/core/ao_data.h') diff --git a/src/cc1111/ao_adc.c b/src/cc1111/ao_adc.c index 95643956..ce827e25 100644 --- a/src/cc1111/ao_adc.c +++ b/src/cc1111/ao_adc.c @@ -56,7 +56,7 @@ ao_adc_isr(void) __interrupt 1 /* TeleMetrum readings */ #if HAS_ACCEL_REF if (sequence == 2) { - a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].accel_ref); + a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].adc.accel_ref); sequence = 0; } else #endif diff --git a/src/cc1111/ao_arch.h b/src/cc1111/ao_arch.h index 704ae4f9..44116b81 100644 --- a/src/cc1111/ao_arch.h +++ b/src/cc1111/ao_arch.h @@ -205,6 +205,7 @@ struct ao_adc { int16_t v_batt; /* battery voltage */ int16_t sense_d; /* drogue continuity sense */ int16_t sense_m; /* main continuity sense */ + int16_t accel_ref; /* acceleration reference */ }; #define AO_DATA_RING 32 diff --git a/src/core/ao_data.h b/src/core/ao_data.h index bb26c4c0..83f8df59 100644 --- a/src/core/ao_data.h +++ b/src/core/ao_data.h @@ -31,9 +31,6 @@ struct ao_data { #if HAS_ADC struct ao_adc adc; #endif -#if HAS_ACCEL_REF - uint16_t accel_ref; -#endif #if HAS_MS5607 struct ao_ms5607_sample ms5607; #endif @@ -82,7 +79,7 @@ typedef int16_t alt_t; * ao_data_accel_invert - flip rocket ends for positive acceleration */ -#if HAS_MPU6000 +#if HAS_MPU6000 && !HAS_HIGHG_ACCEL typedef int16_t accel_t; @@ -181,7 +178,7 @@ typedef int16_t accel_t; */ #if HAS_ACCEL_REF #define ao_data_accel_sample(packet) \ - ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->accel_ref << 1))) >> 1) + ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->adc.accel_ref << 1))) >> 1) #else #define ao_data_accel_sample(packet) ((packet)->adc.accel) #endif /* HAS_ACCEL_REF */ diff --git a/src/megametrum-v0.1/ao_pins.h b/src/megametrum-v0.1/ao_pins.h index ff0edaa9..75b22045 100644 --- a/src/megametrum-v0.1/ao_pins.h +++ b/src/megametrum-v0.1/ao_pins.h @@ -98,6 +98,7 @@ #define HAS_FLIGHT 1 #define HAS_ADC 1 #define HAS_ACCEL 1 +#define HAS_ACCEL_REF 1 #define HAS_LOG 1 #define AO_DATA_RING 32 @@ -107,6 +108,8 @@ struct ao_adc { int16_t sense[AO_ADC_NUM_SENSE]; int16_t v_batt; int16_t v_pbatt; + int16_t accel_ref; + int16_t accel; int16_t temp; }; @@ -142,13 +145,22 @@ struct ao_adc { #define AO_ADC_V_PBATT_PORT stm_gpiob #define AO_ADC_V_PBATT_PIN 1 +#define AO_ADC_ACCEL_REF 10 +#define AO_ADC_ACCEL_REF_PORT stm_gpioc +#define AO_ADC_ACCEL_REF_PIN 0 + +#define AO_ADC_ACCEL 11 +#define AO_ADC_ACCEL_PORT stm_gpioc +#define AO_ADC_ACCEL_PIN 1 + #define AO_ADC_TEMP 16 #define AO_ADC_RCC_AHBENR ((1 << STM_RCC_AHBENR_GPIOAEN) | \ (1 << STM_RCC_AHBENR_GPIOEEN) | \ - (1 << STM_RCC_AHBENR_GPIOBEN)) + (1 << STM_RCC_AHBENR_GPIOBEN) | \ + (1 << STM_RCC_AHBENR_GPIOCEN)) -#define AO_NUM_ADC_PIN (AO_ADC_NUM_SENSE + 2) +#define AO_NUM_ADC_PIN (AO_ADC_NUM_SENSE + 4) #define AO_ADC_PIN0_PORT AO_ADC_SENSE_A_PORT #define AO_ADC_PIN0_PIN AO_ADC_SENSE_A_PIN @@ -166,8 +178,12 @@ struct ao_adc { #define AO_ADC_PIN6_PIN AO_ADC_V_BATT_PIN #define AO_ADC_PIN7_PORT AO_ADC_V_PBATT_PORT #define AO_ADC_PIN7_PIN AO_ADC_V_PBATT_PIN +#define AO_ADC_PIN8_PORT AO_ADC_ACCEL_REF_PORT +#define AO_ADC_PIN8_PIN AO_ADC_ACCEL_REF_PIN +#define AO_ADC_PIN9_PORT AO_ADC_ACCEL_PORT +#define AO_ADC_PIN9_PIN AO_ADC_ACCEL_PIN -#define AO_NUM_ADC (AO_ADC_NUM_SENSE + 3) +#define AO_NUM_ADC (AO_ADC_NUM_SENSE + 5) #define AO_ADC_SQ1 AO_ADC_SENSE_A #define AO_ADC_SQ2 AO_ADC_SENSE_B @@ -177,7 +193,9 @@ struct ao_adc { #define AO_ADC_SQ6 AO_ADC_SENSE_F #define AO_ADC_SQ7 AO_ADC_V_BATT #define AO_ADC_SQ8 AO_ADC_V_PBATT -#define AO_ADC_SQ9 AO_ADC_TEMP +#define AO_ADC_SQ9 AO_ADC_ACCEL_REF +#define AO_ADC_SQ10 AO_ADC_ACCEL +#define AO_ADC_SQ11 AO_ADC_TEMP /* * Pressure sensor settings @@ -230,4 +248,6 @@ struct ao_adc { #define AO_MPU6000_INT_PIN 13 #define AO_MPU6000_I2C_INDEX STM_I2C_INDEX(1) +#define HAS_HIGHG_ACCEL 1 + #endif /* _AO_PINS_H_ */ diff --git a/src/stm/ao_adc_stm.c b/src/stm/ao_adc_stm.c index ea9e25e4..24a1fdd0 100644 --- a/src/stm/ao_adc_stm.c +++ b/src/stm/ao_adc_stm.c @@ -82,7 +82,7 @@ ao_adc_poll(void) stm_adc.sr = 0; ao_dma_set_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1), &stm_adc.dr, - (void *) (&ao_data_ring[ao_data_head].tick + 1), + (void *) (&ao_data_ring[ao_data_head].adc), AO_NUM_ADC, (0 << STM_DMA_CCR_MEM2MEM) | (STM_DMA_CCR_PL_HIGH << STM_DMA_CCR_PL) | @@ -180,6 +180,15 @@ ao_adc_init(void) #ifdef AO_ADC_PIN9_PORT stm_moder_set(&AO_ADC_PIN9_PORT, AO_ADC_PIN9_PIN, STM_MODER_ANALOG); #endif +#ifdef AO_ADC_PIN10_PORT + stm_moder_set(&AO_ADC_PIN10_PORT, AO_ADC_PIN10_PIN, STM_MODER_ANALOG); +#endif +#ifdef AO_ADC_PIN11_PORT + stm_moder_set(&AO_ADC_PIN11_PORT, AO_ADC_PIN11_PIN, STM_MODER_ANALOG); +#endif +#ifdef AO_ADC_PIN12_PORT + stm_moder_set(&AO_ADC_PIN12_PORT, AO_ADC_PIN12_PIN, STM_MODER_ANALOG); +#endif stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_ADC1EN); @@ -240,7 +249,19 @@ ao_adc_init(void) #if AO_NUM_ADC > 8 stm_adc.sqr4 |= (AO_ADC_SQ9 << 10); #endif - +#if AO_NUM_ADC > 9 + stm_adc.sqr4 |= (AO_ADC_SQ10 << 15); +#endif +#if AO_NUM_ADC > 10 + stm_adc.sqr4 |= (AO_ADC_SQ11 << 20); +#endif +#if AO_NUM_ADC > 11 + stm_adc.sqr4 |= (AO_ADC_SQ12 << 25); +#endif +#if AO_NUM_ADC > 12 +#error "need to finish stm_adc.sqr settings" +#endif + /* Turn ADC on */ stm_adc.cr2 = AO_ADC_CR2_VAL; -- cgit v1.2.3 From 9aa7993ee31bdfd6890ad7262a0375c07464ee76 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 2 Jun 2012 17:09:00 -0700 Subject: altos: Intgrate hmc5883 sensor into adc ring Creates a task to poll the mag sensor and place the data into the sensor data ring. Signed-off-by: Keith Packard --- src/core/ao_data.h | 7 +++++++ src/core/ao_log_mega.c | 4 ++++ src/drivers/ao_hmc5883.c | 47 ++++++++++++++++++++++++++----------------- src/drivers/ao_hmc5883.h | 4 ++++ src/megametrum-v0.1/ao_pins.h | 1 + src/stm/ao_adc_stm.c | 5 +++++ 6 files changed, 50 insertions(+), 18 deletions(-) (limited to 'src/core/ao_data.h') diff --git a/src/core/ao_data.h b/src/core/ao_data.h index 83f8df59..502df6c9 100644 --- a/src/core/ao_data.h +++ b/src/core/ao_data.h @@ -26,6 +26,10 @@ #include #endif +#if HAS_HMC5883 +#include +#endif + struct ao_data { uint16_t tick; #if HAS_ADC @@ -37,6 +41,9 @@ struct ao_data { #if HAS_MPU6000 struct ao_mpu6000_sample mpu6000; #endif +#if HAS_HMC5883 + struct ao_hmc5883_sample hmc5883; +#endif }; #define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1)) diff --git a/src/core/ao_log_mega.c b/src/core/ao_log_mega.c index 404e6bf7..68e2af49 100644 --- a/src/core/ao_log_mega.c +++ b/src/core/ao_log_mega.c @@ -120,6 +120,10 @@ ao_log(void) log.u.sensor.gyro_x = ao_data_ring[ao_log_data_pos].mpu6000.gyro_x; log.u.sensor.gyro_y = ao_data_ring[ao_log_data_pos].mpu6000.gyro_y; log.u.sensor.gyro_z = ao_data_ring[ao_log_data_pos].mpu6000.gyro_z; + log.u.sensor.mag_x = ao_data_ring[ao_log_data_pos].hmc5883.x; + log.u.sensor.mag_y = ao_data_ring[ao_log_data_pos].hmc5883.y; + log.u.sensor.mag_z = ao_data_ring[ao_log_data_pos].hmc5883.z; + log.u.sensor.accel = ao_data_ring[ao_log_data_pos].adc.accel; ao_log_mega(&log); if (ao_log_state <= ao_flight_coast) next_sensor = log.tick + AO_SENSOR_INTERVAL_ASCENT; diff --git a/src/drivers/ao_hmc5883.c b/src/drivers/ao_hmc5883.c index f50376c8..1bc914e6 100644 --- a/src/drivers/ao_hmc5883.c +++ b/src/drivers/ao_hmc5883.c @@ -19,6 +19,8 @@ #include #include +uint8_t ao_hmc5883_valid; + static uint8_t ao_hmc5883_configured; static uint8_t ao_hmc5883_addr; @@ -57,7 +59,7 @@ static void ao_hmc5883_isr(void) { ao_exti_disable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN); - ++ao_hmc5883_done; + ao_hmc5883_done = 1; ao_wakeup(&ao_hmc5883_done); } @@ -77,8 +79,6 @@ ao_hmc5883_sample(struct ao_hmc5883_sample *sample) ao_sleep(&ao_hmc5883_done); sei(); - printf ("done %d\n", ao_hmc5883_done); - ao_hmc5883_read(HMC5883_X_MSB, (uint8_t *) sample, sizeof (struct ao_hmc5883_sample)); #if __BYTE_ORDER == __LITTLE_ENDIAN /* byte swap */ @@ -94,6 +94,7 @@ ao_hmc5883_setup(void) { uint8_t d; uint8_t present; + if (ao_hmc5883_configured) return 1; @@ -103,7 +104,7 @@ ao_hmc5883_setup(void) ao_i2c_put(AO_HMC5883_I2C_INDEX); if (!present) - return 0; + ao_panic(AO_PANIC_SELF_TEST); ao_hmc5883_reg_write(HMC5883_CONFIG_A, (HMC5883_CONFIG_A_MA_8 << HMC5883_CONFIG_A_MA) | @@ -117,24 +118,32 @@ ao_hmc5883_setup(void) return 1; } +struct ao_hmc5883_sample ao_hmc5883_current; + +static void +ao_hmc5883(void) +{ + ao_hmc5883_setup(); + for (;;) { + struct ao_hmc5883_sample ao_hmc5883_next; + + ao_hmc5883_sample(&ao_hmc5883_next); + ao_arch_critical( + ao_hmc5883_current = ao_hmc5883_next; + ao_hmc5883_valid = 1; + ); + ao_delay(0); + } +} + +static struct ao_task ao_hmc5883_task; + static void ao_hmc5883_show(void) { - uint8_t addr, data; - struct ao_hmc5883_sample sample; + struct ao_hmc5883_sample sample; - if (!ao_hmc5883_setup()) { - printf("hmc5883 not present\n"); - return; - } -#if 0 - for (addr = 0; addr <= 12; addr++) { - ao_hmc5883_read(addr, &data, 1); - printf ("hmc5883 register %2d: %02x\n", - addr, data); - } -#endif - ao_hmc5883_sample(&sample); + sample = ao_hmc5883_current; printf ("X: %d Y: %d Z: %d\n", sample.x, sample.y, sample.z); } @@ -147,6 +156,7 @@ void ao_hmc5883_init(void) { ao_hmc5883_configured = 0; + ao_hmc5883_valid = 0; ao_enable_port(AO_HMC5883_INT_PORT); ao_exti_setup(&AO_HMC5883_INT_PORT, @@ -154,5 +164,6 @@ ao_hmc5883_init(void) AO_EXTI_MODE_FALLING | AO_EXTI_MODE_PULL_UP, ao_hmc5883_isr); + ao_add_task(&ao_hmc5883_task, ao_hmc5883, "hmc5883"); ao_cmd_register(&ao_hmc5883_cmds[0]); } diff --git a/src/drivers/ao_hmc5883.h b/src/drivers/ao_hmc5883.h index 55690978..8d726510 100644 --- a/src/drivers/ao_hmc5883.h +++ b/src/drivers/ao_hmc5883.h @@ -75,10 +75,14 @@ #define HMC5883_ID_B 11 #define HMC5883_ID_C 12 +extern uint8_t ao_hmc5883_valid; + struct ao_hmc5883_sample { int16_t x, y, z; }; +extern struct ao_hmc5883_sample ao_hmc5883_current; + void ao_hmc5883_init(void); diff --git a/src/megametrum-v0.1/ao_pins.h b/src/megametrum-v0.1/ao_pins.h index 75b22045..d6394d9b 100644 --- a/src/megametrum-v0.1/ao_pins.h +++ b/src/megametrum-v0.1/ao_pins.h @@ -235,6 +235,7 @@ struct ao_adc { * Mag sensor (hmc5883) */ +#define HAS_HMC5883 1 #define AO_HMC5883_INT_PORT stm_gpioc #define AO_HMC5883_INT_PIN 12 #define AO_HMC5883_I2C_INDEX STM_I2C_INDEX(1) diff --git a/src/stm/ao_adc_stm.c b/src/stm/ao_adc_stm.c index 24a1fdd0..71299de9 100644 --- a/src/stm/ao_adc_stm.c +++ b/src/stm/ao_adc_stm.c @@ -62,6 +62,11 @@ static void ao_adc_done(int index) step = 0; ao_data_ring[ao_data_head].ms5607 = ao_ms5607_current; #endif +#if HAS_HMC5883 + if (!ao_hmc5883_valid) + step = 0; + ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current; +#endif if (step) { ao_data_head = ao_data_ring_next(ao_data_head); ao_wakeup((void *) &ao_data_head); -- cgit v1.2.3 From d2bd95edb6f77daeb1e8f043c4a239c248728e0c Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 21 Jun 2012 09:45:42 -0700 Subject: altos: Add full MM telemetry Create two new telemetry packets to hold all of the MM data. This patch also splits the telemetry structures out of ao.h Signed-off-by: Keith Packard --- src/core/ao.h | 177 +----------------------------------------------- src/core/ao_data.h | 33 +++++---- src/core/ao_log_mega.c | 4 +- src/core/ao_sample_mm.c | 5 +- src/core/ao_telemetry.c | 135 +++++++++++++++++++++++++++++------- src/stm/ao_adc_stm.c | 2 +- 6 files changed, 138 insertions(+), 218 deletions(-) (limited to 'src/core/ao_data.h') diff --git a/src/core/ao.h b/src/core/ao.h index 3dae8b40..204c8530 100644 --- a/src/core/ao.h +++ b/src/core/ao.h @@ -325,182 +325,7 @@ ao_spi_slave_init(void); void ao_spi_slave(void); -/* - * ao_telemetry.c - */ -#define AO_MAX_CALLSIGN 8 -#define AO_MAX_VERSION 8 -#if LEGACY_MONITOR -#define AO_MAX_TELEMETRY 128 -#else -#define AO_MAX_TELEMETRY 32 -#endif - -struct ao_telemetry_generic { - uint16_t serial; /* 0 */ - uint16_t tick; /* 2 */ - uint8_t type; /* 4 */ - uint8_t payload[27]; /* 5 */ - /* 32 */ -}; - -#define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01 -#define AO_TELEMETRY_SENSOR_TELEMINI 0x02 -#define AO_TELEMETRY_SENSOR_TELENANO 0x03 -#define AO_TELEMETRY_SENSOR_MEGAMETRUM 0x08 - -struct ao_telemetry_sensor { - uint16_t serial; /* 0 */ - uint16_t tick; /* 2 */ - uint8_t type; /* 4 */ - - uint8_t state; /* 5 flight state */ - int16_t accel; /* 6 accelerometer (TM only) */ - int16_t pres; /* 8 pressure sensor */ - int16_t temp; /* 10 temperature sensor */ - int16_t v_batt; /* 12 battery voltage */ - int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */ - int16_t sense_m; /* 16 main continuity sense (TM/Tm) */ - - int16_t acceleration; /* 18 m/s² * 16 */ - int16_t speed; /* 20 m/s * 16 */ - int16_t height; /* 22 m */ - - int16_t ground_pres; /* 24 average pres on pad */ - int16_t ground_accel; /* 26 average accel on pad */ - int16_t accel_plus_g; /* 28 accel calibration at +1g */ - int16_t accel_minus_g; /* 30 accel calibration at -1g */ - /* 32 */ -}; - -#define AO_TELEMETRY_CONFIGURATION 0x04 - -struct ao_telemetry_configuration { - uint16_t serial; /* 0 */ - uint16_t tick; /* 2 */ - uint8_t type; /* 4 */ - - uint8_t device; /* 5 device type */ - uint16_t flight; /* 6 flight number */ - uint8_t config_major; /* 8 Config major version */ - uint8_t config_minor; /* 9 Config minor version */ - uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */ - uint16_t main_deploy; /* 12 Main deploy alt in meters */ - uint16_t flight_log_max; /* 14 Maximum flight log size in kB */ - char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */ - char version[AO_MAX_VERSION]; /* 24 Software version */ - /* 32 */ -}; - -#define AO_TELEMETRY_LOCATION 0x05 - -#define AO_GPS_MODE_NOT_VALID 'N' -#define AO_GPS_MODE_AUTONOMOUS 'A' -#define AO_GPS_MODE_DIFFERENTIAL 'D' -#define AO_GPS_MODE_ESTIMATED 'E' -#define AO_GPS_MODE_MANUAL 'M' -#define AO_GPS_MODE_SIMULATED 'S' - -struct ao_telemetry_location { - uint16_t serial; /* 0 */ - uint16_t tick; /* 2 */ - uint8_t type; /* 4 */ - - uint8_t flags; /* 5 Number of sats and other flags */ - int16_t altitude; /* 6 GPS reported altitude (m) */ - int32_t latitude; /* 8 latitude (degrees * 10⁷) */ - int32_t longitude; /* 12 longitude (degrees * 10⁷) */ - uint8_t year; /* 16 (- 2000) */ - uint8_t month; /* 17 (1-12) */ - uint8_t day; /* 18 (1-31) */ - uint8_t hour; /* 19 (0-23) */ - uint8_t minute; /* 20 (0-59) */ - uint8_t second; /* 21 (0-59) */ - uint8_t pdop; /* 22 (m * 5) */ - uint8_t hdop; /* 23 (m * 5) */ - uint8_t vdop; /* 24 (m * 5) */ - uint8_t mode; /* 25 */ - uint16_t ground_speed; /* 26 cm/s */ - int16_t climb_rate; /* 28 cm/s */ - uint8_t course; /* 30 degrees / 2 */ - uint8_t unused[1]; /* 31 */ - /* 32 */ -}; - -#define AO_TELEMETRY_SATELLITE 0x06 - -struct ao_telemetry_satellite_info { - uint8_t svid; - uint8_t c_n_1; -}; - -struct ao_telemetry_satellite { - uint16_t serial; /* 0 */ - uint16_t tick; /* 2 */ - uint8_t type; /* 4 */ - uint8_t channels; /* 5 number of reported sats */ - - struct ao_telemetry_satellite_info sats[12]; /* 6 */ - uint8_t unused[2]; /* 30 */ - /* 32 */ -}; - -#define AO_TELEMETRY_COMPANION 0x07 - -#define AO_COMPANION_MAX_CHANNELS 12 - -struct ao_telemetry_companion { - uint16_t serial; /* 0 */ - uint16_t tick; /* 2 */ - uint8_t type; /* 4 */ - uint8_t board_id; /* 5 */ - - uint8_t update_period; /* 6 */ - uint8_t channels; /* 7 */ - uint16_t companion_data[AO_COMPANION_MAX_CHANNELS]; /* 8 */ - /* 32 */ -}; - -/* #define AO_SEND_ALL_BARO */ - -#define AO_TELEMETRY_BARO 0x80 - -/* - * This packet allows the full sampling rate baro - * data to be captured over the RF link so that the - * flight software can be tested using 'real' data. - * - * Along with this telemetry packet, the flight - * code is modified to send full-rate telemetry all the time - * and never send an RDF tone; this ensure that the full radio - * link is available. - */ -struct ao_telemetry_baro { - uint16_t serial; /* 0 */ - uint16_t tick; /* 2 */ - uint8_t type; /* 4 */ - uint8_t samples; /* 5 number samples */ - - int16_t baro[12]; /* 6 samples */ - /* 32 */ -}; - -union ao_telemetry_all { - struct ao_telemetry_generic generic; - struct ao_telemetry_sensor sensor; - struct ao_telemetry_configuration configuration; - struct ao_telemetry_location location; - struct ao_telemetry_satellite satellite; - struct ao_telemetry_companion companion; - struct ao_telemetry_baro baro; -}; - -struct ao_telemetry_all_recv { - union ao_telemetry_all telemetry; - int8_t rssi; - uint8_t status; -}; - +#include /* * ao_gps.c */ diff --git a/src/core/ao_data.h b/src/core/ao_data.h index 502df6c9..fdc49ca2 100644 --- a/src/core/ao_data.h +++ b/src/core/ao_data.h @@ -36,7 +36,8 @@ struct ao_data { struct ao_adc adc; #endif #if HAS_MS5607 - struct ao_ms5607_sample ms5607; + struct ao_ms5607_sample ms5607_raw; + struct ao_ms5607_value ms5607_cooked; #endif #if HAS_MPU6000 struct ao_mpu6000_sample mpu6000; @@ -57,25 +58,24 @@ extern volatile __data uint8_t ao_data_head; typedef int32_t pres_t; typedef int32_t alt_t; -static inline pres_t ao_data_pres(struct ao_data *packet) -{ - struct ao_ms5607_value value; +#define ao_data_pres_cook(packet) ao_ms5607_convert(&packet->ms5607_raw, &packet->ms5607_cooked) - ao_ms5607_convert(&packet->ms5607, &value); - return value.pres; -} +#define ao_data_pres(packet) ((packet)->ms5607_cooked.pres) +#define ao_data_temp(packet) ((packet)->ms5607_cooked.temp) #define pres_to_altitude(p) ao_pa_to_altitude(p) -#else +#else /* HAS_MS5607 */ typedef int16_t pres_t; typedef int16_t alt_t; #define ao_data_pres(packet) ((packet)->adc.pres) +#define ao_data_temp(packet) ((packet)->adc.temp) #define pres_to_altitude(p) ao_pres_to_altitude(p) +#define ao_data_pres_cook(p) -#endif +#endif /* else HAS_MS5607 */ /* * Need a few macros to pull data from the sensors: @@ -92,11 +92,11 @@ typedef int16_t accel_t; /* MPU6000 is hooked up so that positive y is positive acceleration */ #define ao_data_accel(packet) ((packet)->mpu6000.accel_y) -#define ao_data_accel_sample(packet) (-ao_data_accel(packet)) +#define ao_data_accel_cook(packet) (-(packet)->mpu6000.accel_y) #define ao_data_set_accel(packet, accel) ((packet)->mpu6000.accel_y = (accel)) #define ao_data_accel_invert(a) (-(a)) -#else +#else /* HAS_MPU6000 && !HAS_HIGHG_ACCEL */ typedef int16_t accel_t; #define ao_data_accel(packet) ((packet)->adc.accel) @@ -183,13 +183,18 @@ typedef int16_t accel_t; * provides 11 bits of data, we haven't actually lost any precision, * just dropped a bit of noise off the low end. */ + #if HAS_ACCEL_REF -#define ao_data_accel_sample(packet) \ + +#define ao_data_accel_cook(packet) \ ((uint16_t) ((((uint32_t) (packet)->adc.accel << 16) / ((packet)->adc.accel_ref << 1))) >> 1) + #else -#define ao_data_accel_sample(packet) ((packet)->adc.accel) + +#define ao_data_accel_cook(packet) ((packet)->adc.accel) + #endif /* HAS_ACCEL_REF */ -#endif /* else some other pressure sensor */ +#endif /* else some other accel sensor */ #endif /* _AO_DATA_H_ */ diff --git a/src/core/ao_log_mega.c b/src/core/ao_log_mega.c index 68e2af49..e7c2b0d9 100644 --- a/src/core/ao_log_mega.c +++ b/src/core/ao_log_mega.c @@ -112,8 +112,8 @@ ao_log(void) log.tick = ao_data_ring[ao_log_data_pos].tick; if ((int16_t) (log.tick - next_sensor) >= 0) { log.type = AO_LOG_SENSOR; - log.u.sensor.pres = ao_data_ring[ao_log_data_pos].ms5607.pres; - log.u.sensor.temp = ao_data_ring[ao_log_data_pos].ms5607.temp; + log.u.sensor.pres = ao_data_ring[ao_log_data_pos].ms5607_raw.pres; + log.u.sensor.temp = ao_data_ring[ao_log_data_pos].ms5607_raw.temp; log.u.sensor.accel_x = ao_data_ring[ao_log_data_pos].mpu6000.accel_x; log.u.sensor.accel_y = ao_data_ring[ao_log_data_pos].mpu6000.accel_y; log.u.sensor.accel_z = ao_data_ring[ao_log_data_pos].mpu6000.accel_z; diff --git a/src/core/ao_sample_mm.c b/src/core/ao_sample_mm.c index 8cfadc40..ac11eef0 100644 --- a/src/core/ao_sample_mm.c +++ b/src/core/ao_sample_mm.c @@ -101,11 +101,14 @@ ao_sample(void) /* Capture a sample */ ao_data = (struct ao_data *) &ao_data_ring[ao_sample_data]; ao_sample_tick = ao_data->tick; + + ao_data_pres_cook(ao_data); ao_sample_pres = ao_data_pres(ao_data); ao_sample_alt = pres_to_altitude(ao_sample_pres); ao_sample_height = ao_sample_alt - ao_ground_height; + #if HAS_ACCEL - ao_sample_accel = ao_data_accel_sample(ao_data); + ao_sample_accel = ao_data_accel_cook(ao_data); if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP) ao_sample_accel = ao_data_accel_invert(ao_sample_accel); ao_data_set_accel(ao_data, ao_sample_accel); diff --git a/src/core/ao_telemetry.c b/src/core/ao_telemetry.c index 5857c44d..9000a149 100644 --- a/src/core/ao_telemetry.c +++ b/src/core/ao_telemetry.c @@ -19,16 +19,6 @@ #include "ao_product.h" static __pdata uint16_t ao_telemetry_interval; -static __pdata int8_t ao_telemetry_config_max; -static __pdata int8_t ao_telemetry_config_cur; -#if HAS_GPS -static __pdata int8_t ao_telemetry_loc_cur; -static __pdata int8_t ao_telemetry_sat_cur; -#endif -#if HAS_COMPANION -static __pdata int8_t ao_telemetry_companion_max; -static __pdata int8_t ao_telemetry_companion_cur; -#endif static __pdata uint8_t ao_rdf = 0; static __pdata uint16_t ao_rdf_time; @@ -36,7 +26,7 @@ static __pdata uint16_t ao_rdf_time; #define AO_RDF_LENGTH_MS 500 #if defined(MEGAMETRUM) -#define AO_TELEMETRY_SENSOR AO_TELEMETRY_SENSOR_MEGAMETRUM +#define AO_SEND_MEGA 1 #endif #if defined(TELEMETRUM_V_0_1) || defined(TELEMETRUM_V_0_2) || defined(TELEMETRUM_V_1_0) || defined(TELEMETRUM_V_1_1) || defined(TELEBALLOON_V_1_1) || defined(TELEMETRUM_V_1_2) @@ -53,6 +43,7 @@ static __pdata uint16_t ao_rdf_time; static __xdata union ao_telemetry_all telemetry; +#if defined AO_TELEMETRY_SENSOR /* Send sensor packet */ static void ao_send_sensor(void) @@ -96,10 +87,78 @@ ao_send_sensor(void) ao_radio_send(&telemetry, sizeof (telemetry)); } +#endif -static uint8_t ao_baro_sample; + +#ifdef AO_SEND_MEGA +/* Send mega sensor packet */ +static void +ao_send_mega_sensor(void) +{ + __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)]; + + telemetry.generic.tick = packet->tick; + telemetry.generic.type = AO_TELEMETRY_MEGA_SENSOR; + + telemetry.mega_sensor.accel = ao_data_accel(packet); + telemetry.mega_sensor.pres = ao_data_pres(packet); + telemetry.mega_sensor.temp = ao_data_temp(packet); + + telemetry.mega_sensor.accel_x = packet->mpu6000.accel_x; + telemetry.mega_sensor.accel_y = packet->mpu6000.accel_y; + telemetry.mega_sensor.accel_z = packet->mpu6000.accel_z; + + telemetry.mega_sensor.gyro_x = packet->mpu6000.gyro_x; + telemetry.mega_sensor.gyro_y = packet->mpu6000.gyro_y; + telemetry.mega_sensor.gyro_z = packet->mpu6000.gyro_z; + + telemetry.mega_sensor.mag_x = packet->hmc5883.x; + telemetry.mega_sensor.mag_y = packet->hmc5883.y; + telemetry.mega_sensor.mag_z = packet->hmc5883.z; + + ao_radio_send(&telemetry, sizeof (telemetry)); +} + +static __pdata int8_t ao_telemetry_mega_data_max; +static __pdata int8_t ao_telemetry_mega_data_cur; + +/* Send mega data packet */ +static void +ao_send_mega_data(void) +{ + if (--ao_telemetry_mega_data_cur <= 0) { + __xdata struct ao_data *packet = (__xdata struct ao_data *) &ao_data_ring[ao_data_ring_prev(ao_sample_data)]; + uint8_t i; + + telemetry.generic.tick = packet->tick; + telemetry.generic.type = AO_TELEMETRY_MEGA_DATA; + + telemetry.mega_data.state = ao_flight_state; + telemetry.mega_data.v_batt = packet->adc.v_batt; + telemetry.mega_data.v_pyro = packet->adc.v_pbatt; + + /* XXX figure out right shift value; 4 might suffice */ + for (i = 0; i < AO_ADC_NUM_SENSE; i++) + telemetry.mega_data.sense[i] = packet->adc.sense[i] >> 8; + + telemetry.mega_data.ground_pres = ao_ground_pres; + telemetry.mega_data.ground_accel = ao_ground_accel; + telemetry.mega_data.accel_plus_g = ao_config.accel_plus_g; + telemetry.mega_data.accel_minus_g = ao_config.accel_minus_g; + + telemetry.mega_data.acceleration = ao_accel; + telemetry.mega_data.speed = ao_speed; + telemetry.mega_data.height = ao_height; + + ao_radio_send(&telemetry, sizeof (telemetry)); + ao_telemetry_mega_data_cur = ao_telemetry_mega_data_max; + } +} +#endif /* AO_SEND_MEGA */ #ifdef AO_SEND_ALL_BARO +static uint8_t ao_baro_sample; + static void ao_send_baro(void) { @@ -121,6 +180,9 @@ ao_send_baro(void) } #endif +static __pdata int8_t ao_telemetry_config_max; +static __pdata int8_t ao_telemetry_config_cur; + static void ao_send_configuration(void) { @@ -146,6 +208,10 @@ ao_send_configuration(void) } #if HAS_GPS + +static __pdata int8_t ao_telemetry_loc_cur; +static __pdata int8_t ao_telemetry_sat_cur; + static void ao_send_location(void) { @@ -181,6 +247,10 @@ ao_send_satellite(void) #endif #if HAS_COMPANION + +static __pdata int8_t ao_telemetry_companion_max; +static __pdata int8_t ao_telemetry_companion_cur; + static void ao_send_companion(void) { @@ -223,7 +293,13 @@ ao_telemetry(void) #ifdef AO_SEND_ALL_BARO ao_send_baro(); #endif +#ifdef AO_SEND_MEGA + ao_send_mega_sensor(); + ao_send_mega_data(); +#else ao_send_sensor(); +#endif + #if HAS_COMPANION if (ao_companion_running) ao_send_companion(); @@ -257,31 +333,42 @@ ao_telemetry(void) void ao_telemetry_set_interval(uint16_t interval) { + uint8_t cur = 0; ao_telemetry_interval = interval; + +#if AO_SEND_MEGA + if (interval > 1) + ao_telemetry_mega_data_max = 1; + else + ao_telemetry_mega_data_max = 2; + if (ao_telemetry_mega_data_max > cur) + cur++; + ao_telemetry_mega_data_cur = cur; +#endif #if HAS_COMPANION if (!ao_companion_setup.update_period) ao_companion_setup.update_period = AO_SEC_TO_TICKS(1); ao_telemetry_companion_max = ao_companion_setup.update_period / interval; - ao_telemetry_companion_cur = 1; + if (ao_telemetry_companion_max > cur) + cur++; + ao_telemetry_companion_cur = cur; #endif ao_telemetry_config_max = AO_SEC_TO_TICKS(1) / interval; #if HAS_COMPANION - ao_telemetry_config_cur = ao_telemetry_companion_cur; - if (ao_telemetry_config_max > ao_telemetry_config_cur) - ao_telemetry_config_cur++; -#else - ao_telemetry_config_cur = 1; + if (ao_telemetry_config_max > cur) + cur++; + ao_telemetry_config_cur = cur; #endif #if HAS_GPS - ao_telemetry_loc_cur = ao_telemetry_config_cur; - if (ao_telemetry_config_max > ao_telemetry_loc_cur) - ao_telemetry_loc_cur++; - ao_telemetry_sat_cur = ao_telemetry_loc_cur; - if (ao_telemetry_config_max > ao_telemetry_sat_cur) - ao_telemetry_sat_cur++; + if (ao_telemetry_config_max > cur) + cur++; + ao_telemetry_loc_cur = cur; + if (ao_telemetry_config_max > cur) + cur++; + ao_telemetry_sat_cur = cur; #endif ao_wakeup(&telemetry); } diff --git a/src/stm/ao_adc_stm.c b/src/stm/ao_adc_stm.c index 71299de9..ed1d20c9 100644 --- a/src/stm/ao_adc_stm.c +++ b/src/stm/ao_adc_stm.c @@ -60,7 +60,7 @@ static void ao_adc_done(int index) #if HAS_MS5607 if (!ao_ms5607_valid) step = 0; - ao_data_ring[ao_data_head].ms5607 = ao_ms5607_current; + ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current; #endif #if HAS_HMC5883 if (!ao_hmc5883_valid) -- cgit v1.2.3 From f89e7de20374141b367205aa517a08ee203bfaf3 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 26 Aug 2012 09:53:16 -0700 Subject: altos: Trigger sample complete when all data are ready This has each sensor mark a bit in the current data record which is then sent for processing when all of the data are present. Signed-off-by: Keith Packard --- src/core/ao_data.h | 50 +++++++++++++++++++++++++++++++++++++++++ src/drivers/ao_hmc5883.c | 23 ++++++++----------- src/drivers/ao_hmc5883.h | 4 ---- src/drivers/ao_mma655x.c | 23 ++++++++++++++++--- src/drivers/ao_mpu6000.c | 30 +++++++++---------------- src/drivers/ao_mpu6000.h | 3 --- src/drivers/ao_ms5607.c | 26 ++++++++++----------- src/drivers/ao_ms5607.h | 3 --- src/megametrum-v0.1/Makefile | 2 ++ src/megametrum-v0.1/ao_pins.h | 2 +- src/megametrum-v0.1/stlink-pins | 8 +++++++ src/stm-demo/Makefile | 1 + src/stm/ao_adc_stm.c | 42 +--------------------------------- src/stm/ao_timer.c | 19 +++++++++------- 14 files changed, 126 insertions(+), 110 deletions(-) (limited to 'src/core/ao_data.h') diff --git a/src/core/ao_data.h b/src/core/ao_data.h index fdc49ca2..3b66ef5d 100644 --- a/src/core/ao_data.h +++ b/src/core/ao_data.h @@ -18,18 +18,42 @@ #ifndef _AO_DATA_H_ #define _AO_DATA_H_ +#if HAS_ADC +#define AO_DATA_ADC (1 << 0) +#else +#define AO_DATA_ADC 0 +#endif + #if HAS_MS5607 #include +#define AO_DATA_MS5607 (1 << 1) +#else +#define AO_DATA_MS5607 (1 << 1) #endif #if HAS_MPU6000 #include +#define AO_DATA_MPU6000 (1 << 2) +#else +#define AO_DATA_MPU6000 0 #endif #if HAS_HMC5883 #include +#define AO_DATA_HMC5883 (1 << 3) +#else +#define AO_DATA_HMC5883 0 +#endif + +#if HAS_MMA655X +#include +#define AO_DATA_MMA655X (1 << 4) +#else +#define AO_DATA_MMA655X 0 #endif +#define AO_DATA_ALL (AO_DATA_ADC|AO_DATA_MS5607|AO_DATA_MPU6000|AO_DATA_HMC5883|AO_DATA_MMA655X) + struct ao_data { uint16_t tick; #if HAS_ADC @@ -45,6 +69,9 @@ struct ao_data { #if HAS_HMC5883 struct ao_hmc5883_sample hmc5883; #endif +#if HAS_MMA655X + uint16_t mma655x; +#endif }; #define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1)) @@ -52,6 +79,29 @@ struct ao_data { extern volatile __xdata struct ao_data ao_data_ring[AO_DATA_RING]; extern volatile __data uint8_t ao_data_head; +extern volatile __data uint8_t ao_data_present; +extern volatile __data uint8_t ao_data_count; + +/* + * Mark a section of data as ready, check for data complete + */ +#define AO_DATA_PRESENT(bit) do { \ + if ((ao_data_present |= (bit)) == AO_DATA_ALL) { \ + ao_data_ring[ao_data_head].tick = ao_tick_count; \ + ao_data_head = ao_data_ring_next(ao_data_head); \ + ao_data_present = 0; \ + ao_wakeup((void *) &ao_data_head); \ + } \ + } while (0); + +/* + * Wait for data to be completed by looking at the + * indicated bit + */ +#define AO_DATA_WAIT() do { \ + ao_sleep((void *) &ao_data_count); \ + } while (0) + #if HAS_MS5607 diff --git a/src/drivers/ao_hmc5883.c b/src/drivers/ao_hmc5883.c index dbeb66b8..ade6c263 100644 --- a/src/drivers/ao_hmc5883.c +++ b/src/drivers/ao_hmc5883.c @@ -19,7 +19,7 @@ #include #include -uint8_t ao_hmc5883_valid; +#if HAS_HMC5883 static uint8_t ao_hmc5883_configured; @@ -123,21 +123,16 @@ ao_hmc5883_setup(void) return 1; } -struct ao_hmc5883_sample ao_hmc5883_current; - static void ao_hmc5883(void) { ao_hmc5883_setup(); for (;;) { - struct ao_hmc5883_sample ao_hmc5883_next; - - ao_hmc5883_sample(&ao_hmc5883_next); + ao_hmc5883_sample((struct ao_hmc5883_sample *) &ao_data_ring[ao_data_head].hmc5883); ao_arch_critical( - ao_hmc5883_current = ao_hmc5883_next; - ao_hmc5883_valid = 1; + AO_DATA_PRESENT(AO_DATA_HMC5883); + AO_DATA_WAIT(); ); - ao_delay(0); } } @@ -146,11 +141,10 @@ static struct ao_task ao_hmc5883_task; static void ao_hmc5883_show(void) { - struct ao_hmc5883_sample sample; - - sample = ao_hmc5883_current; + struct ao_data sample; + ao_data_get(&sample); printf ("X: %d Y: %d Z: %d missed irq: %lu\n", - sample.x, sample.y, sample.z, ao_hmc5883_missed_irq); + sample.hmc5883.x, sample.hmc5883.y, sample.hmc5883.z, ao_hmc5883_missed_irq); } static const struct ao_cmds ao_hmc5883_cmds[] = { @@ -162,7 +156,6 @@ void ao_hmc5883_init(void) { ao_hmc5883_configured = 0; - ao_hmc5883_valid = 0; ao_enable_port(AO_HMC5883_INT_PORT); ao_exti_setup(AO_HMC5883_INT_PORT, @@ -173,3 +166,5 @@ ao_hmc5883_init(void) ao_add_task(&ao_hmc5883_task, ao_hmc5883, "hmc5883"); ao_cmd_register(&ao_hmc5883_cmds[0]); } + +#endif diff --git a/src/drivers/ao_hmc5883.h b/src/drivers/ao_hmc5883.h index 8d726510..55690978 100644 --- a/src/drivers/ao_hmc5883.h +++ b/src/drivers/ao_hmc5883.h @@ -75,14 +75,10 @@ #define HMC5883_ID_B 11 #define HMC5883_ID_C 12 -extern uint8_t ao_hmc5883_valid; - struct ao_hmc5883_sample { int16_t x, y, z; }; -extern struct ao_hmc5883_sample ao_hmc5883_current; - void ao_hmc5883_init(void); diff --git a/src/drivers/ao_mma655x.c b/src/drivers/ao_mma655x.c index e4e41627..cd304d80 100644 --- a/src/drivers/ao_mma655x.c +++ b/src/drivers/ao_mma655x.c @@ -18,8 +18,9 @@ #include #include +#if HAS_MMA655X + static uint8_t mma655x_configured; -uint8_t ao_mma655x_valid; static void ao_mma655x_start(void) { @@ -197,14 +198,30 @@ __code struct ao_cmds ao_mma655x_cmds[] = { { 0, NULL }, }; +static void +ao_mma655x(void) +{ + ao_mma655x_setup(); + for (;;) { + ao_data_ring[ao_data_head].mma655x = ao_mma655x_value(); + ao_arch_critical( + AO_DATA_PRESENT(AO_DATA_MMA655X); + AO_DATA_WAIT(); + ); + } +} + +static __xdata struct ao_task ao_mma655x_task; + void ao_mma655x_init(void) { mma655x_configured = 0; - ao_mma655x_valid = 0; ao_cmd_register(&ao_mma655x_cmds[0]); ao_spi_init_cs(AO_MMA655X_CS_GPIO, (1 << AO_MMA655X_CS)); -// ao_add_task(&ao_mma655x_task, ao_mma655x, "mma655x"); + ao_add_task(&ao_mma655x_task, ao_mma655x, "mma655x"); } + +#endif diff --git a/src/drivers/ao_mpu6000.c b/src/drivers/ao_mpu6000.c index a1c32d4d..e8c80f12 100644 --- a/src/drivers/ao_mpu6000.c +++ b/src/drivers/ao_mpu6000.c @@ -240,22 +240,17 @@ ao_mpu6000_setup(void) ao_mpu6000_configured = 1; } -struct ao_mpu6000_sample ao_mpu6000_current; -uint8_t ao_mpu6000_valid; - static void ao_mpu6000(void) { ao_mpu6000_setup(); for (;;) { - struct ao_mpu6000_sample ao_mpu6000_next; - ao_mpu6000_sample(&ao_mpu6000_next); + ao_mpu6000_sample((struct ao_mpu6000_sample *) &ao_data_ring[ao_data_head].mpu6000); ao_arch_critical( - ao_mpu6000_current = ao_mpu6000_next; - ao_mpu6000_valid = 1; + AO_DATA_PRESENT(AO_DATA_MPU6000); + AO_DATA_WAIT(); ); - ao_delay(0); } } @@ -264,18 +259,16 @@ static struct ao_task ao_mpu6000_task; static void ao_mpu6000_show(void) { - struct ao_mpu6000_sample sample; + struct ao_data sample; - ao_arch_critical( - sample = ao_mpu6000_current; - ); + ao_data_get(&sample); printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d\n", - sample.accel_x, - sample.accel_y, - sample.accel_z, - sample.gyro_x, - sample.gyro_y, - sample.gyro_z); + sample.mpu6000.accel_x, + sample.mpu6000.accel_y, + sample.mpu6000.accel_z, + sample.mpu6000.gyro_x, + sample.mpu6000.gyro_y, + sample.mpu6000.gyro_z); } static const struct ao_cmds ao_mpu6000_cmds[] = { @@ -287,7 +280,6 @@ void ao_mpu6000_init(void) { ao_mpu6000_configured = 0; - ao_mpu6000_valid = 0; ao_add_task(&ao_mpu6000_task, ao_mpu6000, "mpu6000"); ao_cmd_register(&ao_mpu6000_cmds[0]); diff --git a/src/drivers/ao_mpu6000.h b/src/drivers/ao_mpu6000.h index fc7af1e0..ca76b081 100644 --- a/src/drivers/ao_mpu6000.h +++ b/src/drivers/ao_mpu6000.h @@ -155,9 +155,6 @@ struct ao_mpu6000_sample { int16_t gyro_z; }; -extern struct ao_mpu6000_sample ao_mpu6000_current; -extern uint8_t ao_mpu6000_valid; - void ao_mpu6000_init(void); diff --git a/src/drivers/ao_ms5607.c b/src/drivers/ao_ms5607.c index 1b55b7fd..ec0d2202 100644 --- a/src/drivers/ao_ms5607.c +++ b/src/drivers/ao_ms5607.c @@ -19,6 +19,8 @@ #include #include "ao_ms5607.h" +#if HAS_MS5607 + static struct ao_ms5607_prom ms5607_prom; static uint8_t ms5607_configured; @@ -201,22 +203,17 @@ ao_ms5607_convert(struct ao_ms5607_sample *sample, struct ao_ms5607_value *value value->temp = TEMP; } -struct ao_ms5607_sample ao_ms5607_current; -uint8_t ao_ms5607_valid; - static void ao_ms5607(void) { ao_ms5607_setup(); for (;;) { - static struct ao_ms5607_sample ao_ms5607_next; - ao_ms5607_sample(&ao_ms5607_next); + ao_ms5607_sample((struct ao_ms5607_sample *) &ao_data_ring[ao_data_head].ms5607_raw); ao_arch_critical( - ao_ms5607_current = ao_ms5607_next; - ao_ms5607_valid = 1; + AO_DATA_PRESENT(AO_DATA_MS5607); + AO_DATA_WAIT(); ); - ao_delay(0); } } @@ -238,13 +235,13 @@ ao_ms5607_info(void) static void ao_ms5607_dump(void) { - struct ao_ms5607_sample sample; + struct ao_data sample; struct ao_ms5607_value value; - sample = ao_ms5607_current; - ao_ms5607_convert(&sample, &value); - printf ("Pressure: %8u %8d\n", sample.pres, value.pres); - printf ("Temperature: %8u %8d\n", sample.temp, value.temp); + ao_data_get(&sample); + ao_ms5607_convert(&sample.ms5607_raw, &value); + printf ("Pressure: %8u %8d\n", sample.ms5607_raw.pres, value.pres); + printf ("Temperature: %8u %8d\n", sample.ms5607_raw.temp, value.temp); printf ("Altitude: %ld\n", ao_pa_to_altitude(value.pres)); } @@ -257,7 +254,6 @@ void ao_ms5607_init(void) { ms5607_configured = 0; - ao_ms5607_valid = 0; ao_cmd_register(&ao_ms5607_cmds[0]); ao_spi_init_cs(AO_MS5607_CS_GPIO, (1 << AO_MS5607_CS)); @@ -279,3 +275,5 @@ ao_ms5607_init(void) AO_MS5607_MISO, STM_MODER_ALTERNATE); } + +#endif diff --git a/src/drivers/ao_ms5607.h b/src/drivers/ao_ms5607.h index fa3b1c5b..e9c364d9 100644 --- a/src/drivers/ao_ms5607.h +++ b/src/drivers/ao_ms5607.h @@ -51,9 +51,6 @@ struct ao_ms5607_sample { uint32_t temp; /* raw 24 bit sensor */ }; -extern uint8_t ao_ms5607_valid; -extern struct ao_ms5607_sample ao_ms5607_current; - struct ao_ms5607_value { int32_t pres; /* in Pa * 10 */ int32_t temp; /* in °C * 100 */ diff --git a/src/megametrum-v0.1/Makefile b/src/megametrum-v0.1/Makefile index 1dfebca0..a93f6f17 100644 --- a/src/megametrum-v0.1/Makefile +++ b/src/megametrum-v0.1/Makefile @@ -56,8 +56,10 @@ ALTOS_SRC = \ ao_cc1120.c \ ao_fec_tx.c \ ao_fec_rx.c \ + ao_data.c \ ao_ms5607.c \ ao_mma655x.c \ + ao_hmc5883.c \ ao_adc_stm.c \ ao_beep_stm.c \ ao_storage.c \ diff --git a/src/megametrum-v0.1/ao_pins.h b/src/megametrum-v0.1/ao_pins.h index 6256d2f6..8b631ae9 100644 --- a/src/megametrum-v0.1/ao_pins.h +++ b/src/megametrum-v0.1/ao_pins.h @@ -309,7 +309,7 @@ struct ao_adc { * mma655x */ -#define HAS_MMA655X 1 +#define HAS_MMA655X 0 #define AO_MMA655X_SPI_INDEX AO_SPI_1_PA5_PA6_PA7 #define AO_MMA655X_CS_GPIO (&stm_gpiod) #define AO_MMA655X_CS 4 diff --git a/src/megametrum-v0.1/stlink-pins b/src/megametrum-v0.1/stlink-pins index 71042acc..e6094372 100644 --- a/src/megametrum-v0.1/stlink-pins +++ b/src/megametrum-v0.1/stlink-pins @@ -34,6 +34,14 @@ GND 3 7 JTMS 4 8 NRST 5 2 +TL debug connector: + + TL ST +GND 1 3 +NRST 2 5 +SWDIO 3 4 +SWCLK 4 2 + MegaAccel: Jumpers diff --git a/src/stm-demo/Makefile b/src/stm-demo/Makefile index 340967fc..09c9c3ca 100644 --- a/src/stm-demo/Makefile +++ b/src/stm-demo/Makefile @@ -32,6 +32,7 @@ ALTOS_SRC = \ ao_dma_stm.c \ ao_spi_stm.c \ ao_adc_stm.c \ + ao_data.c \ ao_i2c_stm.c \ ao_usb_stm.c \ ao_exti_stm.c \ diff --git a/src/stm/ao_adc_stm.c b/src/stm/ao_adc_stm.c index 7564c7fa..18ca6ea0 100644 --- a/src/stm/ao_adc_stm.c +++ b/src/stm/ao_adc_stm.c @@ -17,15 +17,6 @@ #include #include -#if HAS_MPU6000 -#include -#endif -#if HAS_MS5607 -#include -#endif - -volatile __xdata struct ao_data ao_data_ring[AO_DATA_RING]; -volatile __data uint8_t ao_data_head; static uint8_t ao_adc_ready; @@ -50,27 +41,7 @@ static uint8_t ao_adc_ready; */ static void ao_adc_done(int index) { - uint8_t step = 1; - ao_data_ring[ao_data_head].tick = ao_time(); -#if HAS_MPU6000 - if (!ao_mpu6000_valid) - step = 0; - ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current; -#endif -#if HAS_MS5607 - if (!ao_ms5607_valid) - step = 0; - ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current; -#endif -#if HAS_HMC5883 - if (!ao_hmc5883_valid) - step = 0; - ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current; -#endif - if (step) { - ao_data_head = ao_data_ring_next(ao_data_head); - ao_wakeup((void *) &ao_data_head); - } + AO_DATA_PRESENT(AO_DATA_ADC); ao_dma_done_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1)); ao_adc_ready = 1; } @@ -117,17 +88,6 @@ ao_adc_get(__xdata struct ao_adc *packet) memcpy(packet, (void *) &ao_data_ring[i].adc, sizeof (struct ao_adc)); } -void -ao_data_get(__xdata struct ao_data *packet) -{ -#if HAS_FLIGHT - uint8_t i = ao_data_ring_prev(ao_sample_data); -#else - uint8_t i = ao_data_ring_prev(ao_data_head); -#endif - memcpy(packet, (void *) &ao_data_ring[i], sizeof (struct ao_data)); -} - static void ao_adc_dump(void) __reentrant { diff --git a/src/stm/ao_timer.c b/src/stm/ao_timer.c index adec7aad..78228e65 100644 --- a/src/stm/ao_timer.c +++ b/src/stm/ao_timer.c @@ -37,9 +37,9 @@ ao_delay(uint16_t ticks) ao_sleep(&ao_forever); } -#if HAS_ADC -volatile __data uint8_t ao_adc_interval = 1; -volatile __data uint8_t ao_adc_count; +#if AO_DATA_ALL +volatile __data uint8_t ao_data_interval = 1; +volatile __data uint8_t ao_data_count; #endif void @@ -51,10 +51,13 @@ void stm_tim6_isr(void) if (stm_tim6.sr & (1 << STM_TIM67_SR_UIF)) { stm_tim6.sr = 0; ++ao_tick_count; -#if HAS_ADC - if (++ao_adc_count == ao_adc_interval) { - ao_adc_count = 0; +#if AO_DATA_ALL + if (++ao_data_count == ao_data_interval) { + ao_data_count = 0; ao_adc_poll(); +#if (AO_DATA_ALL & ~(AO_DATA_ADC)) + ao_wakeup((void *) &ao_data_count); +#endif } #endif } @@ -64,8 +67,8 @@ void stm_tim6_isr(void) void ao_timer_set_adc_interval(uint8_t interval) __critical { - ao_adc_interval = interval; - ao_adc_count = 0; + ao_data_interval = interval; + ao_data_count = 0; } #endif -- cgit v1.2.3 From e053da3e7cb5a4c9ebbffd245cb5d83932183b22 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 9 Sep 2012 13:11:57 -0700 Subject: altos: Allow products without MS5607 The define for a missing MS5607 was wrong, so anything using the fancy multi-sensor data code would break without an MS5607 in place. Signed-off-by: Keith Packard --- src/core/ao_data.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/ao_data.h') diff --git a/src/core/ao_data.h b/src/core/ao_data.h index 3b66ef5d..5412febe 100644 --- a/src/core/ao_data.h +++ b/src/core/ao_data.h @@ -28,7 +28,7 @@ #include #define AO_DATA_MS5607 (1 << 1) #else -#define AO_DATA_MS5607 (1 << 1) +#define AO_DATA_MS5607 0 #endif #if HAS_MPU6000 -- cgit v1.2.3 From 27ab744c6eec9243b7aa14161eec2fbf7003531e Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sun, 9 Sep 2012 13:46:23 -0700 Subject: altos: Clean up flight data definitions These just shuffle the various definitions of data macros around to make the include files more sensible looking. Signed-off-by: Keith Packard --- src/core/ao_data.h | 64 +++++++++++++++++++++++++++++++++++------------ src/core/ao_sample.c | 2 ++ src/core/ao_sample.h | 17 +++++++------ src/test/ao_flight_test.c | 22 ++++++++-------- 4 files changed, 72 insertions(+), 33 deletions(-) (limited to 'src/core/ao_data.h') diff --git a/src/core/ao_data.h b/src/core/ao_data.h index 5412febe..2b9ef5ac 100644 --- a/src/core/ao_data.h +++ b/src/core/ao_data.h @@ -95,15 +95,19 @@ extern volatile __data uint8_t ao_data_count; } while (0); /* - * Wait for data to be completed by looking at the - * indicated bit + * Wait until it is time to write a sensor sample; this is + * signaled by the timer tick */ #define AO_DATA_WAIT() do { \ ao_sleep((void *) &ao_data_count); \ } while (0) +#if !HAS_BARO && HAS_MS5607 -#if HAS_MS5607 +/* Either an MS5607 or an MS5611 hooked to a SPI port + */ + +#define HAS_BARO 1 typedef int32_t pres_t; typedef int32_t alt_t; @@ -115,7 +119,11 @@ typedef int32_t alt_t; #define pres_to_altitude(p) ao_pa_to_altitude(p) -#else /* HAS_MS5607 */ +#endif + +#if !HAS_BARO && HAS_ADC + +#define HAS_BARO 1 typedef int16_t pres_t; typedef int16_t alt_t; @@ -125,7 +133,7 @@ typedef int16_t alt_t; #define pres_to_altitude(p) ao_pres_to_altitude(p) #define ao_data_pres_cook(p) -#endif /* else HAS_MS5607 */ +#endif /* * Need a few macros to pull data from the sensors: @@ -136,17 +144,12 @@ typedef int16_t alt_t; * ao_data_accel_invert - flip rocket ends for positive acceleration */ -#if HAS_MPU6000 && !HAS_HIGHG_ACCEL - -typedef int16_t accel_t; - -/* MPU6000 is hooked up so that positive y is positive acceleration */ -#define ao_data_accel(packet) ((packet)->mpu6000.accel_y) -#define ao_data_accel_cook(packet) (-(packet)->mpu6000.accel_y) -#define ao_data_set_accel(packet, accel) ((packet)->mpu6000.accel_y = (accel)) -#define ao_data_accel_invert(a) (-(a)) +#if HAS_ACCEL -#else /* HAS_MPU6000 && !HAS_HIGHG_ACCEL */ +/* This section is for an analog accelerometer hooked to one of the ADC pins. As + * those are 5V parts, this also requires that the 5V supply be hooked to to anothe ADC + * pin so that the both can be measured to correct for changes between the 3.3V and 5V rails + */ typedef int16_t accel_t; #define ao_data_accel(packet) ((packet)->adc.accel) @@ -245,6 +248,35 @@ typedef int16_t accel_t; #endif /* HAS_ACCEL_REF */ -#endif /* else some other accel sensor */ +#endif /* HAS_ACCEL */ + +#if !HAS_ACCEL && HAS_MMA655X + +#define HAS_ACCEL 1 + +typedef int16_t accel_t; + +/* MMA655X is hooked up so that positive values represent negative acceleration */ + +#define ao_data_accel(packet) ((packet)->mma655x) +#define ao_data_accel_cook(packet) ((packet)->mma655x) +#define ao_data_set_accel(packet, accel) ((packet)->mma655x = (accel)) +#define ao_data_accel_invert(accel) (4095 - (accel)) + +#endif + +#if !HAS_ACCEL && HAS_MPU6000 + +#define HAS_ACCEL 1 + +typedef int16_t accel_t; + +/* MPU6000 is hooked up so that positive y is positive acceleration */ +#define ao_data_accel(packet) ((packet)->mpu6000.accel_y) +#define ao_data_accel_cook(packet) (-(packet)->mpu6000.accel_y) +#define ao_data_set_accel(packet, accel) ((packet)->mpu6000.accel_y = (accel)) +#define ao_data_accel_invert(a) (-(a)) + +#endif #endif /* _AO_DATA_H_ */ diff --git a/src/core/ao_sample.c b/src/core/ao_sample.c index 6461def0..985c0940 100644 --- a/src/core/ao_sample.c +++ b/src/core/ao_sample.c @@ -134,10 +134,12 @@ ao_sample(void) ao_data = (struct ao_data *) &ao_data_ring[ao_sample_data]; ao_sample_tick = ao_data->tick; +#if HAS_BARO ao_data_pres_cook(ao_data); ao_sample_pres = ao_data_pres(ao_data); ao_sample_alt = pres_to_altitude(ao_sample_pres); ao_sample_height = ao_sample_alt - ao_ground_height; +#endif #if HAS_ACCEL ao_sample_accel = ao_data_accel_cook(ao_data); diff --git a/src/core/ao_sample.h b/src/core/ao_sample.h index 189b2019..9336bdf9 100644 --- a/src/core/ao_sample.h +++ b/src/core/ao_sample.h @@ -69,7 +69,11 @@ /* * Above this height, the baro sensor doesn't work */ +#if HAS_MS5607 +#define AO_MAX_BARO_HEIGHT 30000 +#else #define AO_MAX_BARO_HEIGHT 12000 +#endif /* * Above this speed, baro measurements are unreliable @@ -90,20 +94,19 @@ #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16)) extern __pdata uint16_t ao_sample_tick; /* time of last data */ -extern __pdata pres_t ao_sample_pres; /* most recent pressure sensor reading */ -extern __pdata alt_t ao_sample_alt; /* MSL of ao_sample_pres */ -extern __pdata alt_t ao_sample_height; /* AGL of ao_sample_pres */ extern __data uint8_t ao_sample_adc; /* Ring position of last processed sample */ extern __data uint8_t ao_sample_data; /* Ring position of last processed sample */ -#if HAS_ACCEL -extern __pdata accel_t ao_sample_accel; /* most recent accel sensor reading */ -#endif - +#if HAS_BARO +extern __pdata pres_t ao_sample_pres; /* most recent pressure sensor reading */ +extern __pdata alt_t ao_sample_alt; /* MSL of ao_sample_pres */ +extern __pdata alt_t ao_sample_height; /* AGL of ao_sample_pres */ extern __pdata pres_t ao_ground_pres; /* startup pressure */ extern __pdata alt_t ao_ground_height; /* MSL of ao_ground_pres */ +#endif #if HAS_ACCEL +extern __pdata accel_t ao_sample_accel; /* most recent accel sensor reading */ extern __pdata accel_t ao_ground_accel; /* startup acceleration */ extern __pdata accel_t ao_accel_2g; /* factory accel calibration */ extern __pdata int32_t ao_accel_scale; /* sensor to m/s² conversion */ diff --git a/src/test/ao_flight_test.c b/src/test/ao_flight_test.c index a4ef8dc0..b9e291ce 100644 --- a/src/test/ao_flight_test.c +++ b/src/test/ao_flight_test.c @@ -54,6 +54,15 @@ struct ao_adc { #define __code #define __reentrant +#define HAS_FLIGHT 1 +#define HAS_IGNITE 1 +#define HAS_USB 1 +#define HAS_GPS 1 +#ifndef HAS_ACCEL +#define HAS_ACCEL 1 +#define HAS_ACCEL_REF 0 +#endif + #include #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5)) @@ -200,24 +209,17 @@ struct ao_config ao_config; #define DATA_TO_XDATA(x) (x) -#define HAS_FLIGHT 1 -#define HAS_IGNITE 1 -#define HAS_ADC 1 -#define HAS_USB 1 -#define HAS_GPS 1 -#ifndef HAS_ACCEL -#define HAS_ACCEL 1 -#define HAS_ACCEL_REF 0 -#endif #define GRAVITY 9.80665 extern int16_t ao_ground_accel, ao_flight_accel; extern int16_t ao_accel_2g; +typedef int16_t accel_t; + extern uint16_t ao_sample_tick; extern int16_t ao_sample_height; -extern int16_t ao_sample_accel; +extern accel_t ao_sample_accel; extern int32_t ao_accel_scale; extern int16_t ao_ground_height; extern int16_t ao_sample_alt; -- cgit v1.2.3