2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 #ifndef AO_FLIGHT_TEST
23 #error Please define HAS_ACCEL
27 #error Please define HAS_GPS
31 #error Please define HAS_USB
34 /* Main flight thread. */
36 __pdata enum ao_flight_state ao_flight_state; /* current flight state */
37 __pdata uint16_t ao_flight_tick; /* time of last data */
38 __pdata uint16_t ao_flight_prev_tick; /* time of previous data */
39 __pdata int16_t ao_flight_pres; /* filtered pressure */
40 __pdata int16_t ao_ground_pres; /* startup pressure */
41 __pdata int16_t ao_min_pres; /* minimum recorded pressure */
42 __pdata uint16_t ao_launch_tick; /* time of launch detect */
43 __pdata int16_t ao_main_pres; /* pressure to eject main */
45 __pdata int16_t ao_flight_accel; /* filtered acceleration */
46 __pdata int16_t ao_ground_accel; /* startup acceleration */
50 * track min/max data over a long interval to detect
53 __pdata uint16_t ao_interval_end;
54 __pdata int16_t ao_interval_cur_min_pres;
55 __pdata int16_t ao_interval_cur_max_pres;
56 __pdata int16_t ao_interval_min_pres;
57 __pdata int16_t ao_interval_max_pres;
59 __pdata int16_t ao_interval_cur_min_accel;
60 __pdata int16_t ao_interval_cur_max_accel;
61 __pdata int16_t ao_interval_min_accel;
62 __pdata int16_t ao_interval_max_accel;
65 __data uint8_t ao_flight_adc;
66 __pdata int16_t ao_raw_pres;
67 __xdata uint8_t ao_flight_force_idle;
70 __pdata int16_t ao_raw_accel, ao_raw_accel_prev;
71 __pdata int16_t ao_accel_2g;
73 /* Accelerometer calibration
75 * We're sampling the accelerometer through a resistor divider which
76 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
77 * That goes into the cc1111 A/D converter, which is running at 11 bits
78 * of precision with the bits in the MSB of the 16 bit value. Only positive
79 * values are used, so values should range from 0-32752 for 0-3.3V. The
80 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
81 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
82 * for a final computation of:
84 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
86 * Zero g was measured at 16000 (we would expect 16384).
87 * Note that this value is only require to tell if the
88 * rocket is standing upright. Once that is determined,
89 * the value of the accelerometer is averaged for 100 samples
90 * to find the resting accelerometer value, which is used
91 * for all further flight computations
94 #define GRAVITY 9.80665
95 /* convert m/s to velocity count */
96 #define VEL_MPS_TO_COUNT(mps) (((int32_t) (((mps) / GRAVITY) * (AO_HERTZ/2))) * (int32_t) ao_accel_2g)
98 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
99 #define ACCEL_BOOST ao_accel_2g
100 #define ACCEL_COAST (ao_accel_2g >> 3)
101 #define ACCEL_INT_LAND (ao_accel_2g >> 3)
102 #define ACCEL_VEL_MACH VEL_MPS_TO_COUNT(200)
103 #define ACCEL_VEL_BOOST VEL_MPS_TO_COUNT(5)
108 * Barometer calibration
110 * We directly sample the barometer. The specs say:
112 * Pressure range: 15-115 kPa
113 * Voltage at 115kPa: 2.82
114 * Output scale: 27mV/kPa
116 * If we want to detect launch with the barometer, we need
117 * a large enough bump to not be fooled by noise. At typical
118 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
119 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
120 * As all of our calculations are done in 16 bits, we'll actually see a change
121 * of 16 times this though
123 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
127 #define BARO_LAUNCH (BARO_kPa / 5) /* .2kPa, or about 20m */
128 #define BARO_APOGEE (BARO_kPa / 10) /* .1kPa, or about 10m */
129 #define BARO_COAST (BARO_kPa * 5) /* 5kpa, or about 500m */
130 #define BARO_MAIN (BARO_kPa) /* 1kPa, or about 100m */
131 #define BARO_INT_LAND (BARO_kPa / 20) /* .05kPa, or about 5m */
132 #define BARO_LAND (BARO_kPa * 10) /* 10kPa or about 1000m */
134 /* We also have a clock, which can be used to sanity check things in
135 * case of other failures
138 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
141 /* This value is scaled in a weird way. It's a running total of accelerometer
142 * readings minus the ground accelerometer reading. That means it measures
143 * velocity, and quite accurately too. As it gets updated 100 times a second,
146 __pdata int32_t ao_flight_vel;
147 __pdata int32_t ao_min_vel;
148 __pdata int32_t ao_old_vel;
149 __pdata int16_t ao_old_vel_tick;
150 __xdata int32_t ao_raw_accel_sum;
153 __xdata int32_t ao_raw_pres_sum;
155 /* Landing is detected by getting constant readings from both pressure and accelerometer
156 * for a fairly long time (AO_INTERVAL_TICKS)
158 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
160 #define abs(a) ((a) < 0 ? -(a) : (a))
165 __pdata static uint16_t nsamples = 0;
167 ao_flight_adc = ao_adc_head;
170 ao_raw_accel_prev = 0;
175 ao_wakeup(DATA_TO_XDATA(&ao_flight_adc));
176 ao_sleep(DATA_TO_XDATA(&ao_adc_head));
177 while (ao_flight_adc != ao_adc_head) {
179 __pdata uint8_t ticks;
180 __pdata int16_t ao_vel_change;
182 __xdata struct ao_adc *ao_adc;
183 ao_flight_prev_tick = ao_flight_tick;
185 /* Capture a sample */
186 ao_adc = &ao_adc_ring[ao_flight_adc];
187 ao_flight_tick = ao_adc->tick;
188 ao_raw_pres = ao_adc->pres;
189 ao_flight_pres -= ao_flight_pres >> 4;
190 ao_flight_pres += ao_raw_pres >> 4;
193 ao_raw_accel = ao_adc->accel;
196 * Ok, the math here is a bit tricky.
198 * ao_raw_accel: ADC output for acceleration
199 * ao_accel_ref: ADC output for the 5V reference.
200 * ao_cook_accel: Corrected acceleration value
201 * Vcc: 3.3V supply to the CC1111
202 * Vac: 5V supply to the accelerometer
203 * accel: input voltage to accelerometer ADC pin
204 * ref: input voltage to 5V reference ADC pin
207 * Measured acceleration is ratiometric to Vcc:
210 * ------------ = -----
213 * Measured 5v reference is also ratiometric to Vcc:
216 * ------------ = -----
220 * ao_accel_ref = 32767 * (ref / Vcc)
222 * Acceleration is measured ratiometric to the 5V supply,
223 * so what we want is:
225 * ao_cook_accel accel
226 * ------------- = -----
235 * = ------------ * ------------
238 * Multiply through by 32767:
240 * ao_raw_accel * 32767
241 * ao_cook_accel = --------------------
244 * Now, the tricky part. Getting this to compile efficiently
245 * and keeping all of the values in-range.
247 * First off, we need to use a shift of 16 instead of * 32767 as SDCC
248 * does the obvious optimizations for byte-granularity shifts:
250 * ao_cook_accel = (ao_raw_accel << 16) / ao_accel_ref
252 * Next, lets check our input ranges:
254 * 0 <= ao_raw_accel <= 0x7fff (singled ended ADC conversion)
255 * 0x7000 <= ao_accel_ref <= 0x7fff (the 5V ref value is close to 0x7fff)
257 * Plugging in our input ranges, we get an output range of 0 - 0x12490,
258 * which is 17 bits. That won't work. If we take the accel ref and shift
259 * by a bit, we'll change its range:
261 * 0xe000 <= ao_accel_ref<<1 <= 0xfffe
263 * ao_cook_accel = (ao_raw_accel << 16) / (ao_accel_ref << 1)
265 * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
266 * is, however, one bit too large for our signed computations. So, we
267 * take the result and shift that by a bit:
269 * ao_cook_accel = ((ao_raw_accel << 16) / (ao_accel_ref << 1)) >> 1
271 * This finally creates an output range of 0 - 0x4924. As the ADC only
272 * provides 11 bits of data, we haven't actually lost any precision,
273 * just dropped a bit of noise off the low end.
275 ao_raw_accel = (uint16_t) ((((uint32_t) ao_raw_accel << 16) / (ao_accel_ref[ao_flight_adc] << 1))) >> 1;
276 ao_adc->accel = ao_raw_accel;
279 ao_flight_accel -= ao_flight_accel >> 4;
280 ao_flight_accel += ao_raw_accel >> 4;
283 * The accelerometer is mounted so that
284 * acceleration yields negative values
285 * while deceleration yields positive values,
286 * so subtract instead of add.
288 ticks = ao_flight_tick - ao_flight_prev_tick;
289 ao_vel_change = ao_ground_accel - (((ao_raw_accel + 1) >> 1) + ((ao_raw_accel_prev + 1) >> 1));
290 ao_raw_accel_prev = ao_raw_accel;
292 /* one is a common interval */
294 ao_flight_vel += (int32_t) ao_vel_change;
296 ao_flight_vel += (int32_t) ao_vel_change * (int32_t) ticks;
299 ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
302 if (ao_flight_pres < ao_min_pres)
303 ao_min_pres = ao_flight_pres;
305 if (ao_flight_vel >= 0) {
306 if (ao_flight_vel < ao_min_vel)
307 ao_min_vel = ao_flight_vel;
309 if (-ao_flight_vel < ao_min_vel)
310 ao_min_vel = -ao_flight_vel;
314 switch (ao_flight_state) {
315 case ao_flight_startup:
319 * Collect 512 samples of acceleration and pressure
320 * data and average them to find the resting values
322 if (nsamples < 512) {
324 ao_raw_accel_sum += ao_raw_accel;
326 ao_raw_pres_sum += ao_raw_pres;
331 ao_ground_accel = ao_raw_accel_sum >> 9;
333 ao_ground_pres = ao_raw_pres_sum >> 9;
334 ao_min_pres = ao_ground_pres;
336 ao_main_pres = ao_altitude_to_pres(ao_pres_to_altitude(ao_ground_pres) + ao_config.main_deploy);
338 ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
341 ao_old_vel = ao_flight_vel;
342 ao_old_vel_tick = ao_flight_tick;
345 /* Check to see what mode we should go to.
346 * - Invalid mode if accel cal appears to be out
347 * - pad mode if we're upright,
348 * - idle mode otherwise
352 if (ao_config.accel_plus_g == 0 ||
353 ao_config.accel_minus_g == 0 ||
354 ao_flight_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
355 ao_flight_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
357 /* Detected an accel value outside -1.5g to 1.5g
358 * (or uncalibrated values), so we go into invalid mode
360 ao_flight_state = ao_flight_invalid;
364 if (!ao_flight_force_idle
366 && ao_flight_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
370 /* Set pad mode - we can fly! */
371 ao_flight_state = ao_flight_pad;
374 /* Disable the USB controller in flight mode
380 /* Disable packet mode in pad state */
381 ao_packet_slave_stop();
383 /* Turn on telemetry system */
385 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
387 /* signal successful initialization by turning off the LED */
388 ao_led_off(AO_LED_RED);
391 ao_flight_state = ao_flight_idle;
393 /* signal successful initialization by turning off the LED */
394 ao_led_off(AO_LED_RED);
396 /* wakeup threads due to state change */
397 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
405 * Once a second, remove any velocity from
408 if ((int16_t) (ao_flight_tick - ao_old_vel_tick) >= AO_SEC_TO_TICKS(1)) {
409 ao_old_vel_tick = ao_flight_tick;
410 ao_flight_vel -= ao_old_vel;
411 ao_old_vel = ao_flight_vel;
416 * accelerometer: > 2g AND velocity > 5m/s
418 * barometer: > 20m vertical motion
420 * The accelerometer should always detect motion before
421 * the barometer, but we use both to make sure this
422 * transition is detected
426 (ao_flight_accel < ao_ground_accel - ACCEL_BOOST &&
427 ao_flight_vel > ACCEL_VEL_BOOST) ||
429 ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
432 ao_flight_state = ao_flight_boost;
434 ao_flight_state = ao_flight_coast;
436 ao_launch_tick = ao_flight_tick;
438 /* start logging data */
441 /* Increase telemetry rate */
442 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
444 /* disable RDF beacon */
448 /* Record current GPS position by waking up GPS log tasks */
449 ao_wakeup(&ao_gps_data);
450 ao_wakeup(&ao_gps_tracking_data);
453 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
458 case ao_flight_boost:
462 * accelerometer: start to fall at > 1/4 G
464 * time: boost for more than 15 seconds
466 * Detects motor burn out by the switch from acceleration to
467 * deceleration, or by waiting until the maximum burn duration
468 * (15 seconds) has past.
470 if (ao_flight_accel > ao_ground_accel + ACCEL_COAST ||
471 (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
473 ao_flight_state = ao_flight_fast;
474 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
482 * accelerometer: integrated velocity < 200 m/s
484 * barometer: fall at least 500m from max altitude
486 * This extra state is required to avoid mis-detecting
487 * apogee due to mach transitions.
489 * XXX this is essentially a single-detector test
490 * as the 500m altitude change would likely result
491 * in a loss of the rocket. More data on precisely
492 * how big a pressure change the mach transition
493 * generates would be useful here.
495 if (ao_flight_vel < ACCEL_VEL_MACH ||
496 ao_flight_pres > ao_min_pres + BARO_COAST)
498 /* set min velocity to current velocity for
501 ao_min_vel = abs(ao_flight_vel);
502 ao_flight_state = ao_flight_coast;
503 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
507 case ao_flight_coast:
509 /* apogee detect: coast to drogue deploy:
511 * barometer: fall at least 10m
513 * It would be nice to use the accelerometer
514 * to detect apogee as well, but tests have
515 * shown that flights far from vertical would
516 * grossly mis-detect apogee. So, for now,
517 * we'll trust to a single sensor for this test
519 if (ao_flight_pres > ao_min_pres + BARO_APOGEE)
521 /* ignite the drogue charge */
522 ao_ignite(ao_igniter_drogue);
524 /* slow down the telemetry system */
525 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
527 /* slow down the ADC sample rate */
528 ao_timer_set_adc_interval(10);
531 * Start recording min/max accel and pres for a while
532 * to figure out when the rocket has landed
534 /* Set the 'last' limits to max range to prevent
535 * early resting detection
538 ao_interval_min_accel = 0;
539 ao_interval_max_accel = 0x7fff;
541 ao_interval_min_pres = 0;
542 ao_interval_max_pres = 0x7fff;
544 /* initialize interval values */
545 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
547 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
549 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
552 /* and enter drogue state */
553 ao_flight_state = ao_flight_drogue;
554 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
558 case ao_flight_drogue:
560 /* drogue to main deploy:
562 * barometer: reach main deploy altitude
564 * Would like to use the accelerometer for this test, but
565 * the orientation of the flight computer is unknown after
566 * drogue deploy, so we ignore it. Could also detect
567 * high descent rate using the pressure sensor to
568 * recognize drogue deploy failure and eject the main
569 * at that point. Perhaps also use the drogue sense lines
570 * to notice continutity?
572 if (ao_flight_pres >= ao_main_pres)
574 ao_ignite(ao_igniter_main);
575 ao_flight_state = ao_flight_main;
576 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
579 /* fall through... */
582 /* drogue/main to land:
584 * accelerometer: value stable
586 * barometer: altitude stable and within 1000m of the launch altitude
589 if (ao_flight_pres < ao_interval_cur_min_pres)
590 ao_interval_cur_min_pres = ao_flight_pres;
591 if (ao_flight_pres > ao_interval_cur_max_pres)
592 ao_interval_cur_max_pres = ao_flight_pres;
594 if (ao_flight_accel < ao_interval_cur_min_accel)
595 ao_interval_cur_min_accel = ao_flight_accel;
596 if (ao_flight_accel > ao_interval_cur_max_accel)
597 ao_interval_cur_max_accel = ao_flight_accel;
600 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
601 ao_interval_max_pres = ao_interval_cur_max_pres;
602 ao_interval_min_pres = ao_interval_cur_min_pres;
603 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
605 ao_interval_max_accel = ao_interval_cur_max_accel;
606 ao_interval_min_accel = ao_interval_cur_min_accel;
607 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
609 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
613 (uint16_t) (ao_interval_max_accel - ao_interval_min_accel) < (uint16_t) ACCEL_INT_LAND &&
615 ao_flight_pres > ao_ground_pres - BARO_LAND &&
616 (uint16_t) (ao_interval_max_pres - ao_interval_min_pres) < (uint16_t) BARO_INT_LAND)
618 ao_flight_state = ao_flight_landed;
620 /* turn off the ADC capture */
621 ao_timer_set_adc_interval(0);
622 /* Enable RDF beacon */
625 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
629 case ao_flight_landed:
635 static __xdata struct ao_task flight_task;
640 ao_flight_state = ao_flight_startup;
641 ao_add_task(&flight_task, ao_flight, "flight");