88f0544f83a076ce4a702a966c23c0703c3376a9
[fw/altos] / src / ao_flight.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #ifndef AO_FLIGHT_TEST
19 #include "ao.h"
20 #endif
21
22 #ifndef HAS_ACCEL
23 #error Please define HAS_ACCEL
24 #endif
25
26 #ifndef HAS_GPS
27 #error Please define HAS_GPS
28 #endif
29
30 #ifndef HAS_USB
31 #error Please define HAS_USB
32 #endif
33
34 /* Main flight thread. */
35
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 __xdata int16_t                 ao_ground_pres;         /* startup pressure */
40 __pdata uint16_t                ao_launch_tick;         /* time of launch detect */
41 #if HAS_ACCEL
42 __pdata int16_t                 ao_ground_accel;        /* startup acceleration */
43 #endif
44
45 /*
46  * track min/max data over a long interval to detect
47  * resting
48  */
49 __pdata uint16_t                ao_interval_end;
50 __pdata int16_t                 ao_interval_min_height;
51 __pdata int16_t                 ao_interval_max_height;
52
53 __data uint8_t ao_flight_adc;
54 __pdata int16_t ao_raw_pres;
55 __xdata uint8_t ao_flight_force_idle;
56
57 #if HAS_ACCEL
58 __pdata int16_t ao_raw_accel, ao_raw_accel_prev;
59 __pdata int16_t ao_accel_2g;
60
61 /* Accelerometer calibration
62  *
63  * We're sampling the accelerometer through a resistor divider which
64  * consists of 5k and 10k resistors. This multiplies the values by 2/3.
65  * That goes into the cc1111 A/D converter, which is running at 11 bits
66  * of precision with the bits in the MSB of the 16 bit value. Only positive
67  * values are used, so values should range from 0-32752 for 0-3.3V. The
68  * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
69  * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
70  * for a final computation of:
71  *
72  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
73  *
74  * Zero g was measured at 16000 (we would expect 16384).
75  * Note that this value is only require to tell if the
76  * rocket is standing upright. Once that is determined,
77  * the value of the accelerometer is averaged for 100 samples
78  * to find the resting accelerometer value, which is used
79  * for all further flight computations
80  */
81
82 #define GRAVITY 9.80665
83
84 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
85
86 #endif
87
88 /*
89  * Barometer calibration
90  *
91  * We directly sample the barometer. The specs say:
92  *
93  * Pressure range: 15-115 kPa
94  * Voltage at 115kPa: 2.82
95  * Output scale: 27mV/kPa
96  *
97  * If we want to detect launch with the barometer, we need
98  * a large enough bump to not be fooled by noise. At typical
99  * launch elevations (0-2000m), a 200Pa pressure change cooresponds
100  * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
101  * As all of our calculations are done in 16 bits, we'll actually see a change
102  * of 16 times this though
103  *
104  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
105  */
106
107 /* We also have a clock, which can be used to sanity check things in
108  * case of other failures
109  */
110
111 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
112
113 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
114 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
115 #define from_fix(x)     ((x) >> 16)
116
117 #include "ao_kalman.h"
118
119 __pdata int16_t                 ao_ground_height;
120 __pdata int16_t                 ao_height;
121 __pdata int16_t                 ao_speed;
122 __pdata int16_t                 ao_accel;
123 __pdata int16_t                 ao_max_height;
124
125 static __pdata int32_t          ao_k_height;
126 static __pdata int32_t          ao_k_speed;
127 static __pdata int32_t          ao_k_accel;
128
129 #define AO_K_STEP_100           to_fix16(0.01)
130 #define AO_K_STEP_2_2_100       to_fix16(0.00005)
131
132 #define AO_K_STEP_10            to_fix16(0.1)
133 #define AO_K_STEP_2_2_10        to_fix16(0.005)
134
135 /*
136  * Above this height, the baro sensor doesn't work
137  */
138 #define AO_MAX_BARO_HEIGHT      12000
139
140 /*
141  * Above this speed, baro measurements are unreliable
142  */
143 #define AO_MAX_BARO_SPEED       200
144
145 static void
146 ao_kalman_predict(void)
147 {
148 #ifdef AO_FLIGHT_TEST
149         if (ao_flight_tick - ao_flight_prev_tick > 5) {
150                 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_10 +
151                                 (int32_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
152                 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_10;
153
154                 return;
155         }
156         if (ao_flight_debug) {
157                 printf ("predict speed %g + (%g * %g) = %g\n",
158                         ao_k_speed / (65536.0 * 16.0), ao_accel / 16.0, AO_K_STEP_100 / 65536.0,
159                         (ao_k_speed + (int32_t) ao_accel * AO_K_STEP_100) / (65536.0 * 16.0));
160         }
161 #endif
162         ao_k_height += ((int32_t) ao_speed * AO_K_STEP_100 +
163                         (int32_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
164         ao_k_speed += (int32_t) ao_accel * AO_K_STEP_100;
165 }
166
167 static __pdata int16_t ao_error_h;
168 static __pdata int16_t ao_raw_alt;
169 static __pdata int16_t ao_raw_height;
170 static __pdata int16_t ao_error_h_sq_avg;
171
172 static void
173 ao_kalman_err_height(void)
174 {
175         int16_t e;
176         int16_t height_distrust;
177 #if HAS_ACCEL
178         int16_t speed_distrust;
179 #endif
180
181         ao_error_h = ao_raw_height - (int16_t) (ao_k_height >> 16);
182
183         e = ao_error_h;
184         if (e < 0)
185                 e = -e;
186         if (e > 127)
187                 e = 127;
188 #if HAS_ACCEL
189         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 2;
190         ao_error_h_sq_avg += (e * e) >> 2;
191 #else
192         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
193         ao_error_h_sq_avg += (e * e) >> 4;
194 #endif
195
196         height_distrust = ao_raw_height - AO_MAX_BARO_HEIGHT;
197 #if HAS_ACCEL
198         /* speed is stored * 16, but we need to ramp between 200 and 328, so
199          * we want to multiply by 2. The result is a shift by 3.
200          */
201         speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
202         if (speed_distrust <= 0)
203                 speed_distrust = 0;
204         else if (speed_distrust > height_distrust)
205                 height_distrust = speed_distrust;
206 #endif
207         if (height_distrust <= 0)
208                 height_distrust = 0;
209
210         if (height_distrust) {
211 #ifdef AO_FLIGHT_TEST
212                 int     old_ao_error_h = ao_error_h;
213 #endif
214                 if (height_distrust > 0x100)
215                         height_distrust = 0x100;
216                 ao_error_h = (int16_t) (((int32_t) ao_error_h * (0x100 - height_distrust)) >> 8);
217 #ifdef AO_FLIGHT_TEST
218                 if (ao_flight_debug) {
219                         printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
220                                (double) (ao_raw_height - AO_MAX_BARO_HEIGHT),
221                                (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
222                                height_distrust / 256.0,
223                                old_ao_error_h, ao_error_h);
224                 }
225 #endif
226         }
227 }
228
229 static void
230 ao_kalman_correct_baro(void)
231 {
232         ao_kalman_err_height();
233 #ifdef AO_FLIGHT_TEST
234         if (ao_flight_tick - ao_flight_prev_tick > 5) {
235                 ao_k_height += (int32_t) AO_BARO_K0_10 * ao_error_h;
236                 ao_k_speed  += (int32_t) AO_BARO_K1_10 * ao_error_h;
237                 ao_k_accel  += (int32_t) AO_BARO_K2_10 * ao_error_h;
238                 return;
239         }
240 #endif
241         ao_k_height += (int32_t) AO_BARO_K0_100 * ao_error_h;
242         ao_k_speed  += (int32_t) AO_BARO_K1_100 * ao_error_h;
243         ao_k_accel  += (int32_t) AO_BARO_K2_100 * ao_error_h;
244 }
245
246 #if HAS_ACCEL
247 static __pdata int16_t ao_error_a;
248 static __pdata int32_t ao_accel_scale;
249
250 static void
251 ao_kalman_err_accel(void)
252 {
253         int32_t accel;
254
255         accel = (ao_ground_accel - ao_raw_accel) * ao_accel_scale;
256
257         /* Can't use ao_accel here as it is the pre-prediction value still */
258         ao_error_a = (accel - ao_k_accel) >> 16;
259 }
260
261 static void
262 ao_kalman_correct_both(void)
263 {
264         ao_kalman_err_height();
265         ao_kalman_err_accel();
266
267 #ifdef AO_FLIGHT_TEST
268         if (ao_flight_tick - ao_flight_prev_tick > 5) {
269                 if (ao_flight_debug) {
270                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
271                                 ao_k_speed / (65536.0 * 16.0),
272                                 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
273                                 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
274                                 (ao_k_speed +
275                                  (int32_t) AO_BOTH_K10_10 * ao_error_h +
276                                  (int32_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
277                 }
278                 ao_k_height +=
279                         (int32_t) AO_BOTH_K00_10 * ao_error_h +
280                         (int32_t) AO_BOTH_K01_10 * ao_error_a;
281                 ao_k_speed +=
282                         (int32_t) AO_BOTH_K10_10 * ao_error_h +
283                         (int32_t) AO_BOTH_K11_10 * ao_error_a;
284                 ao_k_accel +=
285                         (int32_t) AO_BOTH_K20_10 * ao_error_h +
286                         (int32_t) AO_BOTH_K21_10 * ao_error_a;
287                 return;
288         }
289         if (ao_flight_debug) {
290                 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
291                         ao_k_speed / (65536.0 * 16.0),
292                         (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
293                         (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
294                         (ao_k_speed +
295                          (int32_t) AO_BOTH_K10_100 * ao_error_h +
296                          (int32_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
297         }
298 #endif
299         ao_k_height +=
300                 (int32_t) AO_BOTH_K00_100 * ao_error_h +
301                 (int32_t) AO_BOTH_K01_100 * ao_error_a;
302         ao_k_speed +=
303                 (int32_t) AO_BOTH_K10_100 * ao_error_h +
304                 (int32_t) AO_BOTH_K11_100 * ao_error_a;
305         ao_k_accel +=
306                 (int32_t) AO_BOTH_K20_100 * ao_error_h +
307                 (int32_t) AO_BOTH_K21_100 * ao_error_a;
308 }
309
310 #ifdef FORCE_ACCEL
311 static void
312 ao_kalman_correct_accel(void)
313 {
314         ao_kalman_err_accel();
315
316         if (ao_flight_tick - ao_flight_prev_tick > 5) {
317                 ao_k_height +=(int32_t) AO_ACCEL_K0_10 * ao_error_a;
318                 ao_k_speed  += (int32_t) AO_ACCEL_K1_10 * ao_error_a;
319                 ao_k_accel  += (int32_t) AO_ACCEL_K2_10 * ao_error_a;
320                 return;
321         }
322         ao_k_height += (int32_t) AO_ACCEL_K0_100 * ao_error_a;
323         ao_k_speed  += (int32_t) AO_ACCEL_K1_100 * ao_error_a;
324         ao_k_accel  += (int32_t) AO_ACCEL_K2_100 * ao_error_a;
325 }
326 #endif
327 #endif /* HAS_ACCEL */
328
329 __xdata int32_t ao_raw_pres_sum;
330
331 #ifdef HAS_ACCEL
332 __xdata int32_t ao_raw_accel_sum;
333 #endif
334
335 /* Landing is detected by getting constant readings from both pressure and accelerometer
336  * for a fairly long time (AO_INTERVAL_TICKS)
337  */
338 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(5)
339
340 #define abs(a)  ((a) < 0 ? -(a) : (a))
341
342 void
343 ao_flight(void)
344 {
345         __pdata static uint16_t nsamples = 0;
346
347         ao_flight_adc = ao_adc_head;
348         ao_raw_pres = 0;
349 #if HAS_ACCEL
350         ao_raw_accel_prev = 0;
351         ao_raw_accel = 0;
352 #endif
353         ao_flight_tick = 0;
354         for (;;) {
355                 ao_wakeup(DATA_TO_XDATA(&ao_flight_adc));
356                 ao_sleep(DATA_TO_XDATA(&ao_adc_head));
357                 while (ao_flight_adc != ao_adc_head) {
358                         __xdata struct ao_adc *ao_adc;
359                         ao_flight_prev_tick = ao_flight_tick;
360
361                         /* Capture a sample */
362                         ao_adc = &ao_adc_ring[ao_flight_adc];
363                         ao_flight_tick = ao_adc->tick;
364                         ao_raw_pres = ao_adc->pres;
365                         ao_raw_alt = ao_pres_to_altitude(ao_raw_pres);
366                         ao_raw_height = ao_raw_alt - ao_ground_height;
367 #if HAS_ACCEL
368                         ao_raw_accel = ao_adc->accel;
369 #if HAS_ACCEL_REF
370                         /*
371                          * Ok, the math here is a bit tricky.
372                          *
373                          * ao_raw_accel:  ADC output for acceleration
374                          * ao_accel_ref:  ADC output for the 5V reference.
375                          * ao_cook_accel: Corrected acceleration value
376                          * Vcc:           3.3V supply to the CC1111
377                          * Vac:           5V supply to the accelerometer
378                          * accel:         input voltage to accelerometer ADC pin
379                          * ref:           input voltage to 5V reference ADC pin
380                          *
381                          *
382                          * Measured acceleration is ratiometric to Vcc:
383                          *
384                          *     ao_raw_accel   accel
385                          *     ------------ = -----
386                          *        32767        Vcc
387                          *
388                          * Measured 5v reference is also ratiometric to Vcc:
389                          *
390                          *     ao_accel_ref    ref
391                          *     ------------ = -----
392                          *        32767        Vcc
393                          *
394                          *
395                          *      ao_accel_ref = 32767 * (ref / Vcc)
396                          *
397                          * Acceleration is measured ratiometric to the 5V supply,
398                          * so what we want is:
399                          *
400                          *      ao_cook_accel    accel
401                          *      ------------- =  -----
402                          *          32767         ref
403                          *
404                          *
405                          *                      accel    Vcc
406                          *                    = ----- *  ---
407                          *                       Vcc     ref
408                          *
409                          *                      ao_raw_accel       32767
410                          *                    = ------------ *  ------------
411                          *                         32737        ao_accel_ref
412                          *
413                          * Multiply through by 32767:
414                          *
415                          *                      ao_raw_accel * 32767
416                          *      ao_cook_accel = --------------------
417                          *                          ao_accel_ref
418                          *
419                          * Now, the tricky part. Getting this to compile efficiently
420                          * and keeping all of the values in-range.
421                          *
422                          * First off, we need to use a shift of 16 instead of * 32767 as SDCC
423                          * does the obvious optimizations for byte-granularity shifts:
424                          *
425                          *      ao_cook_accel = (ao_raw_accel << 16) / ao_accel_ref
426                          *
427                          * Next, lets check our input ranges:
428                          *
429                          *      0 <= ao_raw_accel <= 0x7fff             (singled ended ADC conversion)
430                          *      0x7000 <= ao_accel_ref <= 0x7fff        (the 5V ref value is close to 0x7fff)
431                          *
432                          * Plugging in our input ranges, we get an output range of 0 - 0x12490,
433                          * which is 17 bits. That won't work. If we take the accel ref and shift
434                          * by a bit, we'll change its range:
435                          *
436                          *      0xe000 <= ao_accel_ref<<1 <= 0xfffe
437                          *
438                          *      ao_cook_accel = (ao_raw_accel << 16) / (ao_accel_ref << 1)
439                          *
440                          * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
441                          * is, however, one bit too large for our signed computations. So, we
442                          * take the result and shift that by a bit:
443                          *
444                          *      ao_cook_accel = ((ao_raw_accel << 16) / (ao_accel_ref << 1)) >> 1
445                          *
446                          * This finally creates an output range of 0 - 0x4924. As the ADC only
447                          * provides 11 bits of data, we haven't actually lost any precision,
448                          * just dropped a bit of noise off the low end.
449                          */
450                         ao_raw_accel = (uint16_t) ((((uint32_t) ao_raw_accel << 16) / (ao_accel_ref[ao_flight_adc] << 1))) >> 1;
451                         ao_adc->accel = ao_raw_accel;
452 #endif
453 #endif
454
455                         if (ao_flight_state > ao_flight_idle) {
456                                 ao_kalman_predict();
457 #if HAS_ACCEL
458                                 if (ao_flight_state <= ao_flight_coast) {
459 #ifdef FORCE_ACCEL
460                                         ao_kalman_correct_accel();
461 #else
462                                         ao_kalman_correct_both();
463 #endif
464                                 } else
465 #endif
466                                         ao_kalman_correct_baro();
467                                 ao_height = from_fix(ao_k_height);
468                                 ao_speed = from_fix(ao_k_speed);
469                                 ao_accel = from_fix(ao_k_accel);
470                                 if (ao_height > ao_max_height)
471                                         ao_max_height = ao_height;
472                         }
473                         ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
474                 }
475
476                 switch (ao_flight_state) {
477                 case ao_flight_startup:
478
479                         /* startup state:
480                          *
481                          * Collect 512 samples of acceleration and pressure
482                          * data and average them to find the resting values
483                          */
484                         if (nsamples < 512) {
485 #if HAS_ACCEL
486                                 ao_raw_accel_sum += ao_raw_accel;
487 #endif
488                                 ao_raw_pres_sum += ao_raw_pres;
489                                 ++nsamples;
490                                 continue;
491                         }
492                         ao_config_get();
493 #if HAS_ACCEL
494                         ao_ground_accel = ao_raw_accel_sum >> 9;
495                         ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
496                         ao_accel_scale = to_fix32(GRAVITY * 2 * 16) / ao_accel_2g;
497 #endif
498                         ao_ground_pres = ao_raw_pres_sum >> 9;
499                         ao_ground_height = ao_pres_to_altitude(ao_ground_pres);
500
501                         /* Check to see what mode we should go to.
502                          *  - Invalid mode if accel cal appears to be out
503                          *  - pad mode if we're upright,
504                          *  - idle mode otherwise
505                          */
506 #if HAS_ACCEL
507                         if (ao_config.accel_plus_g == 0 ||
508                             ao_config.accel_minus_g == 0 ||
509                             ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
510                             ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
511                         {
512                                 /* Detected an accel value outside -1.5g to 1.5g
513                                  * (or uncalibrated values), so we go into invalid mode
514                                  */
515                                 ao_flight_state = ao_flight_invalid;
516
517                         } else
518 #endif
519                                 if (!ao_flight_force_idle
520 #if HAS_ACCEL
521                                     && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
522 #endif
523                                         )
524                         {
525                                 /* Set pad mode - we can fly! */
526                                 ao_flight_state = ao_flight_pad;
527 #if HAS_USB
528                                 /* Disable the USB controller in flight mode
529                                  * to save power
530                                  */
531                                 ao_usb_disable();
532 #endif
533
534                                 /* Disable packet mode in pad state */
535                                 ao_packet_slave_stop();
536
537                                 /* Turn on telemetry system */
538                                 ao_rdf_set(1);
539                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
540
541                                 /* signal successful initialization by turning off the LED */
542                                 ao_led_off(AO_LED_RED);
543                         } else {
544                                 /* Set idle mode */
545                                 ao_flight_state = ao_flight_idle;
546  
547                                 /* signal successful initialization by turning off the LED */
548                                 ao_led_off(AO_LED_RED);
549                         }
550                         /* wakeup threads due to state change */
551                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
552
553                         break;
554                 case ao_flight_pad:
555
556                         /* pad to boost:
557                          *
558                          * barometer: > 20m vertical motion
559                          *             OR
560                          * accelerometer: > 2g AND velocity > 5m/s
561                          *
562                          * The accelerometer should always detect motion before
563                          * the barometer, but we use both to make sure this
564                          * transition is detected. If the device
565                          * doesn't have an accelerometer, then ignore the
566                          * speed and acceleration as they are quite noisy
567                          * on the pad.
568                          */
569                         if (ao_height > AO_M_TO_HEIGHT(20)
570 #if HAS_ACCEL
571                             || (ao_accel > AO_MSS_TO_ACCEL(20) &&
572                                 ao_speed > AO_MS_TO_SPEED(5))
573 #endif
574                                 )
575                         {
576                                 ao_flight_state = ao_flight_boost;
577                                 ao_launch_tick = ao_flight_tick;
578
579                                 /* start logging data */
580                                 ao_log_start();
581
582                                 /* Increase telemetry rate */
583                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
584
585                                 /* disable RDF beacon */
586                                 ao_rdf_set(0);
587
588 #if HAS_GPS
589                                 /* Record current GPS position by waking up GPS log tasks */
590                                 ao_wakeup(&ao_gps_data);
591                                 ao_wakeup(&ao_gps_tracking_data);
592 #endif
593
594                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
595                                 break;
596                         }
597                         break;
598                 case ao_flight_boost:
599
600                         /* boost to fast:
601                          *
602                          * accelerometer: start to fall at > 1/4 G
603                          *              OR
604                          * time: boost for more than 15 seconds
605                          *
606                          * Detects motor burn out by the switch from acceleration to
607                          * deceleration, or by waiting until the maximum burn duration
608                          * (15 seconds) has past.
609                          */
610                         if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
611                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
612                         {
613 #if HAS_ACCEL
614                                 ao_flight_state = ao_flight_fast;
615 #else
616                                 ao_flight_state = ao_flight_coast;
617 #endif
618                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
619                                 break;
620                         }
621                         break;
622 #if HAS_ACCEL
623                 case ao_flight_fast:
624                         /*
625                          * This is essentially the same as coast,
626                          * but the barometer is being ignored as
627                          * it may be unreliable.
628                          */
629                         if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
630                         {
631                                 ao_flight_state = ao_flight_coast;
632                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
633                                 break;
634                         }
635                         break;
636 #endif
637                 case ao_flight_coast:
638
639                         /* apogee detect: coast to drogue deploy:
640                          *
641                          * speed: < 0
642                          *
643                          * Also make sure the model altitude is tracking
644                          * the measured altitude reasonably closely; otherwise
645                          * we're probably transsonic.
646                          */
647                         if (ao_speed < 0
648 #if !HAS_ACCEL
649                             && (ao_raw_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
650 #endif
651                                 )
652                         {
653                                 /* ignite the drogue charge */
654                                 ao_ignite(ao_igniter_drogue);
655
656                                 /* slow down the telemetry system */
657                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
658
659                                 /*
660                                  * Start recording min/max height
661                                  * to figure out when the rocket has landed
662                                  */
663
664                                 /* initialize interval values */
665                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
666
667                                 ao_interval_min_height = ao_interval_max_height = ao_height;
668
669                                 /* and enter drogue state */
670                                 ao_flight_state = ao_flight_drogue;
671                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
672                         }
673
674                         break;
675                 case ao_flight_drogue:
676
677                         /* drogue to main deploy:
678                          *
679                          * barometer: reach main deploy altitude
680                          *
681                          * Would like to use the accelerometer for this test, but
682                          * the orientation of the flight computer is unknown after
683                          * drogue deploy, so we ignore it. Could also detect
684                          * high descent rate using the pressure sensor to
685                          * recognize drogue deploy failure and eject the main
686                          * at that point. Perhaps also use the drogue sense lines
687                          * to notice continutity?
688                          */
689                         if (ao_height <= ao_config.main_deploy)
690                         {
691                                 ao_ignite(ao_igniter_main);
692                                 ao_flight_state = ao_flight_main;
693                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
694                         }
695
696                         /* fall through... */
697                 case ao_flight_main:
698
699                         /* drogue/main to land:
700                          *
701                          * barometer: altitude stable and within 1000m of the launch altitude
702                          */
703
704                         if (ao_height < ao_interval_min_height)
705                                 ao_interval_min_height = ao_height;
706                         if (ao_height > ao_interval_max_height)
707                                 ao_interval_max_height = ao_height;
708
709                         if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
710                                 if (ao_height < AO_M_TO_HEIGHT(1000) &&
711                                     ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
712                                 {
713                                         ao_flight_state = ao_flight_landed;
714
715                                         /* turn off the ADC capture */
716                                         ao_timer_set_adc_interval(0);
717                                         /* Enable RDF beacon */
718                                         ao_rdf_set(1);
719
720                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
721                                 }
722                                 ao_interval_min_height = ao_interval_max_height = ao_height;
723                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
724                         }
725                         break;
726                 case ao_flight_landed:
727                         break;
728                 }
729         }
730 }
731
732 static __xdata struct ao_task   flight_task;
733
734 void
735 ao_flight_init(void)
736 {
737         ao_flight_state = ao_flight_startup;
738         ao_add_task(&flight_task, ao_flight, "flight");
739 }