4c65344f27b4bcd29d918432e87d71438390e04d
[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       300
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         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
189         ao_error_h_sq_avg += (e * e) >> 4;
190
191         height_distrust = ao_raw_height - AO_MAX_BARO_HEIGHT;
192 #if HAS_ACCEL
193         speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> 4;
194         if (speed_distrust <= 0)
195                 speed_distrust = 0;
196         else if (speed_distrust > height_distrust)
197                 height_distrust = speed_distrust;
198 #endif
199         if (height_distrust <= 0)
200                 height_distrust = 0;
201
202         if (height_distrust) {
203 #ifdef AO_FLIGHT_TEST
204                 int     old_ao_error_h = ao_error_h;
205 #endif
206                 if (height_distrust > 0x100)
207                         height_distrust = 0x100;
208                 ao_error_h = (int16_t) ((int32_t) ao_error_h * (0x100 - height_distrust)) >> 8;
209 #ifdef AO_FLIGHT_TEST
210                 if (ao_flight_debug) {
211                         printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
212                                (double) (ao_raw_height - AO_MAX_BARO_HEIGHT),
213                                (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
214                                height_distrust / 256.0,
215                                old_ao_error_h, ao_error_h);
216                 }
217 #endif
218         }
219 }
220
221 static void
222 ao_kalman_correct_baro(void)
223 {
224         ao_kalman_err_height();
225 #ifdef AO_FLIGHT_TEST
226         if (ao_flight_tick - ao_flight_prev_tick > 5) {
227                 ao_k_height += (int32_t) AO_BARO_K0_10 * ao_error_h;
228                 ao_k_speed  += (int32_t) AO_BARO_K1_10 * ao_error_h;
229                 ao_k_accel  += (int32_t) AO_BARO_K2_10 * ao_error_h;
230                 return;
231         }
232 #endif
233         ao_k_height += (int32_t) AO_BARO_K0_100 * ao_error_h;
234         ao_k_speed  += (int32_t) AO_BARO_K1_100 * ao_error_h;
235         ao_k_accel  += (int32_t) AO_BARO_K2_100 * ao_error_h;
236 }
237
238 #if HAS_ACCEL
239 static __pdata int16_t ao_error_a;
240 static __pdata int32_t ao_accel_scale;
241
242 static void
243 ao_kalman_err_accel(void)
244 {
245         int32_t accel;
246
247         accel = (ao_ground_accel - ao_raw_accel) * ao_accel_scale;
248
249         /* Can't use ao_accel here as it is the pre-prediction value still */
250         ao_error_a = (accel - ao_k_accel) >> 16;
251 }
252
253 static void
254 ao_kalman_correct_both(void)
255 {
256         ao_kalman_err_height();
257         ao_kalman_err_accel();
258
259 #ifdef AO_FLIGHT_TEST
260         if (ao_flight_tick - ao_flight_prev_tick > 5) {
261                 if (ao_flight_debug) {
262                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
263                                 ao_k_speed / (65536.0 * 16.0),
264                                 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
265                                 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
266                                 (ao_k_speed +
267                                  (int32_t) AO_BOTH_K10_10 * ao_error_h +
268                                  (int32_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
269                 }
270                 ao_k_height +=
271                         (int32_t) AO_BOTH_K00_10 * ao_error_h +
272                         (int32_t) (AO_BOTH_K01_10 >> 4) * ao_error_a;
273                 ao_k_speed +=
274                         ((int32_t) AO_BOTH_K10_10 << 4) * ao_error_h +
275                         (int32_t) AO_BOTH_K11_10 * ao_error_a;
276                 ao_k_accel +=
277                         ((int32_t) AO_BOTH_K20_10 << 4) * ao_error_h +
278                         (int32_t) AO_BOTH_K21_10 * ao_error_a;
279                 return;
280         }
281         if (ao_flight_debug) {
282                 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
283                         ao_k_speed / (65536.0 * 16.0),
284                         (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
285                         (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
286                         (ao_k_speed +
287                          (int32_t) AO_BOTH_K10_100 * ao_error_h +
288                          (int32_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
289         }
290 #endif
291         ao_k_height +=
292                 (int32_t) AO_BOTH_K00_100 * ao_error_h +
293                 (int32_t) AO_BOTH_K01_100 * ao_error_a;
294         ao_k_speed +=
295                 (int32_t) AO_BOTH_K10_100 * ao_error_h +
296                 (int32_t) AO_BOTH_K11_100 * ao_error_a;
297         ao_k_accel +=
298                 (int32_t) AO_BOTH_K20_100 * ao_error_h +
299                 (int32_t) AO_BOTH_K21_100 * ao_error_a;
300 }
301
302 #ifdef FORCE_ACCEL
303 static void
304 ao_kalman_correct_accel(void)
305 {
306         ao_kalman_err_accel();
307
308         if (ao_flight_tick - ao_flight_prev_tick > 5) {
309                 ao_k_height +=(int32_t) AO_ACCEL_K0_10 * ao_error_a;
310                 ao_k_speed  += (int32_t) AO_ACCEL_K1_10 * ao_error_a;
311                 ao_k_accel  += (int32_t) AO_ACCEL_K2_10 * ao_error_a;
312                 return;
313         }
314         ao_k_height += (int32_t) AO_ACCEL_K0_100 * ao_error_a;
315         ao_k_speed  += (int32_t) AO_ACCEL_K1_100 * ao_error_a;
316         ao_k_accel  += (int32_t) AO_ACCEL_K2_100 * ao_error_a;
317 }
318 #endif
319 #endif /* HAS_ACCEL */
320
321 __xdata int32_t ao_raw_pres_sum;
322
323 #ifdef HAS_ACCEL
324 __xdata int32_t ao_raw_accel_sum;
325 #endif
326
327 /* Landing is detected by getting constant readings from both pressure and accelerometer
328  * for a fairly long time (AO_INTERVAL_TICKS)
329  */
330 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(5)
331
332 #define abs(a)  ((a) < 0 ? -(a) : (a))
333
334 void
335 ao_flight(void)
336 {
337         __pdata static uint16_t nsamples = 0;
338
339         ao_flight_adc = ao_adc_head;
340         ao_raw_pres = 0;
341 #if HAS_ACCEL
342         ao_raw_accel_prev = 0;
343         ao_raw_accel = 0;
344 #endif
345         ao_flight_tick = 0;
346         for (;;) {
347                 ao_wakeup(DATA_TO_XDATA(&ao_flight_adc));
348                 ao_sleep(DATA_TO_XDATA(&ao_adc_head));
349                 while (ao_flight_adc != ao_adc_head) {
350                         __xdata struct ao_adc *ao_adc;
351                         ao_flight_prev_tick = ao_flight_tick;
352
353                         /* Capture a sample */
354                         ao_adc = &ao_adc_ring[ao_flight_adc];
355                         ao_flight_tick = ao_adc->tick;
356                         ao_raw_pres = ao_adc->pres;
357                         ao_raw_alt = ao_pres_to_altitude(ao_raw_pres);
358                         ao_raw_height = ao_raw_alt - ao_ground_height;
359 #if HAS_ACCEL
360                         ao_raw_accel = ao_adc->accel;
361 #if HAS_ACCEL_REF
362                         /*
363                          * Ok, the math here is a bit tricky.
364                          *
365                          * ao_raw_accel:  ADC output for acceleration
366                          * ao_accel_ref:  ADC output for the 5V reference.
367                          * ao_cook_accel: Corrected acceleration value
368                          * Vcc:           3.3V supply to the CC1111
369                          * Vac:           5V supply to the accelerometer
370                          * accel:         input voltage to accelerometer ADC pin
371                          * ref:           input voltage to 5V reference ADC pin
372                          *
373                          *
374                          * Measured acceleration is ratiometric to Vcc:
375                          *
376                          *     ao_raw_accel   accel
377                          *     ------------ = -----
378                          *        32767        Vcc
379                          *
380                          * Measured 5v reference is also ratiometric to Vcc:
381                          *
382                          *     ao_accel_ref    ref
383                          *     ------------ = -----
384                          *        32767        Vcc
385                          *
386                          *
387                          *      ao_accel_ref = 32767 * (ref / Vcc)
388                          *
389                          * Acceleration is measured ratiometric to the 5V supply,
390                          * so what we want is:
391                          *
392                          *      ao_cook_accel    accel
393                          *      ------------- =  -----
394                          *          32767         ref
395                          *
396                          *
397                          *                      accel    Vcc
398                          *                    = ----- *  ---
399                          *                       Vcc     ref
400                          *
401                          *                      ao_raw_accel       32767
402                          *                    = ------------ *  ------------
403                          *                         32737        ao_accel_ref
404                          *
405                          * Multiply through by 32767:
406                          *
407                          *                      ao_raw_accel * 32767
408                          *      ao_cook_accel = --------------------
409                          *                          ao_accel_ref
410                          *
411                          * Now, the tricky part. Getting this to compile efficiently
412                          * and keeping all of the values in-range.
413                          *
414                          * First off, we need to use a shift of 16 instead of * 32767 as SDCC
415                          * does the obvious optimizations for byte-granularity shifts:
416                          *
417                          *      ao_cook_accel = (ao_raw_accel << 16) / ao_accel_ref
418                          *
419                          * Next, lets check our input ranges:
420                          *
421                          *      0 <= ao_raw_accel <= 0x7fff             (singled ended ADC conversion)
422                          *      0x7000 <= ao_accel_ref <= 0x7fff        (the 5V ref value is close to 0x7fff)
423                          *
424                          * Plugging in our input ranges, we get an output range of 0 - 0x12490,
425                          * which is 17 bits. That won't work. If we take the accel ref and shift
426                          * by a bit, we'll change its range:
427                          *
428                          *      0xe000 <= ao_accel_ref<<1 <= 0xfffe
429                          *
430                          *      ao_cook_accel = (ao_raw_accel << 16) / (ao_accel_ref << 1)
431                          *
432                          * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
433                          * is, however, one bit too large for our signed computations. So, we
434                          * take the result and shift that by a bit:
435                          *
436                          *      ao_cook_accel = ((ao_raw_accel << 16) / (ao_accel_ref << 1)) >> 1
437                          *
438                          * This finally creates an output range of 0 - 0x4924. As the ADC only
439                          * provides 11 bits of data, we haven't actually lost any precision,
440                          * just dropped a bit of noise off the low end.
441                          */
442                         ao_raw_accel = (uint16_t) ((((uint32_t) ao_raw_accel << 16) / (ao_accel_ref[ao_flight_adc] << 1))) >> 1;
443                         ao_adc->accel = ao_raw_accel;
444 #endif
445 #endif
446
447                         if (ao_flight_state > ao_flight_idle) {
448                                 ao_kalman_predict();
449 #if HAS_ACCEL
450                                 if (ao_flight_state <= ao_flight_coast) {
451 #ifdef FORCE_ACCEL
452                                         ao_kalman_correct_accel();
453 #else
454                                         ao_kalman_correct_both();
455 #endif
456                                 } else
457 #endif
458                                         ao_kalman_correct_baro();
459                                 ao_height = from_fix(ao_k_height);
460                                 ao_speed = from_fix(ao_k_speed);
461                                 ao_accel = from_fix(ao_k_accel);
462                                 if (ao_height > ao_max_height)
463                                         ao_max_height = ao_height;
464                         }
465                         ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
466                 }
467
468                 switch (ao_flight_state) {
469                 case ao_flight_startup:
470
471                         /* startup state:
472                          *
473                          * Collect 512 samples of acceleration and pressure
474                          * data and average them to find the resting values
475                          */
476                         if (nsamples < 512) {
477 #if HAS_ACCEL
478                                 ao_raw_accel_sum += ao_raw_accel;
479 #endif
480                                 ao_raw_pres_sum += ao_raw_pres;
481                                 ++nsamples;
482                                 continue;
483                         }
484                         ao_config_get();
485 #if HAS_ACCEL
486                         ao_ground_accel = ao_raw_accel_sum >> 9;
487                         ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
488                         ao_accel_scale = to_fix32(GRAVITY * 2 * 16) / ao_accel_2g;
489 #endif
490                         ao_ground_pres = ao_raw_pres_sum >> 9;
491                         ao_ground_height = ao_pres_to_altitude(ao_ground_pres);
492
493                         /* Check to see what mode we should go to.
494                          *  - Invalid mode if accel cal appears to be out
495                          *  - pad mode if we're upright,
496                          *  - idle mode otherwise
497                          */
498 #if HAS_ACCEL
499                         if (ao_config.accel_plus_g == 0 ||
500                             ao_config.accel_minus_g == 0 ||
501                             ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
502                             ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
503                         {
504                                 /* Detected an accel value outside -1.5g to 1.5g
505                                  * (or uncalibrated values), so we go into invalid mode
506                                  */
507                                 ao_flight_state = ao_flight_invalid;
508
509                         } else
510 #endif
511                                 if (!ao_flight_force_idle
512 #if HAS_ACCEL
513                                     && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
514 #endif
515                                         )
516                         {
517                                 /* Set pad mode - we can fly! */
518                                 ao_flight_state = ao_flight_pad;
519 #if HAS_USB
520                                 /* Disable the USB controller in flight mode
521                                  * to save power
522                                  */
523                                 ao_usb_disable();
524 #endif
525
526                                 /* Disable packet mode in pad state */
527                                 ao_packet_slave_stop();
528
529                                 /* Turn on telemetry system */
530                                 ao_rdf_set(1);
531                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
532
533                                 /* signal successful initialization by turning off the LED */
534                                 ao_led_off(AO_LED_RED);
535                         } else {
536                                 /* Set idle mode */
537                                 ao_flight_state = ao_flight_idle;
538  
539                                 /* signal successful initialization by turning off the LED */
540                                 ao_led_off(AO_LED_RED);
541                         }
542                         /* wakeup threads due to state change */
543                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
544
545                         break;
546                 case ao_flight_pad:
547
548                         /* pad to boost:
549                          *
550                          * barometer: > 20m vertical motion
551                          *             OR
552                          * accelerometer: > 2g AND velocity > 5m/s
553                          *
554                          * The accelerometer should always detect motion before
555                          * the barometer, but we use both to make sure this
556                          * transition is detected. If the device
557                          * doesn't have an accelerometer, then ignore the
558                          * speed and acceleration as they are quite noisy
559                          * on the pad.
560                          */
561                         if (ao_height > AO_M_TO_HEIGHT(20)
562 #if HAS_ACCEL
563                             || (ao_accel > AO_MSS_TO_ACCEL(20) &&
564                                 ao_speed > AO_MS_TO_SPEED(5))
565 #endif
566                                 )
567                         {
568                                 ao_flight_state = ao_flight_boost;
569                                 ao_launch_tick = ao_flight_tick;
570
571                                 /* start logging data */
572                                 ao_log_start();
573
574                                 /* Increase telemetry rate */
575                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
576
577                                 /* disable RDF beacon */
578                                 ao_rdf_set(0);
579
580 #if HAS_GPS
581                                 /* Record current GPS position by waking up GPS log tasks */
582                                 ao_wakeup(&ao_gps_data);
583                                 ao_wakeup(&ao_gps_tracking_data);
584 #endif
585
586                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
587                                 break;
588                         }
589                         break;
590                 case ao_flight_boost:
591
592                         /* boost to fast:
593                          *
594                          * accelerometer: start to fall at > 1/4 G
595                          *              OR
596                          * time: boost for more than 15 seconds
597                          *
598                          * Detects motor burn out by the switch from acceleration to
599                          * deceleration, or by waiting until the maximum burn duration
600                          * (15 seconds) has past.
601                          */
602                         if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
603                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
604                         {
605                                 ao_flight_state = ao_flight_fast;
606                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
607                                 break;
608                         }
609                         break;
610                 case ao_flight_fast:
611                         /*
612                          * This is essentially the same as coast,
613                          * but the barometer is being ignored as
614                          * it may be unreliable.
615                          */
616                         if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) {
617                                 ao_flight_state = ao_flight_coast;
618                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
619                                 break;
620                         }
621                         break;
622                 case ao_flight_coast:
623
624                         /* apogee detect: coast to drogue deploy:
625                          *
626                          * speed: < 0
627                          *
628                          * Also make sure the model altitude is tracking
629                          * the measured altitude reasonably closely; otherwise
630                          * we're probably transsonic.
631                          */
632                         if (ao_speed < 0 && (ao_raw_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100))
633                         {
634                                 /* ignite the drogue charge */
635                                 ao_ignite(ao_igniter_drogue);
636
637                                 /* slow down the telemetry system */
638                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
639
640                                 /*
641                                  * Start recording min/max height
642                                  * to figure out when the rocket has landed
643                                  */
644
645                                 /* initialize interval values */
646                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
647
648                                 ao_interval_min_height = ao_interval_max_height = ao_height;
649
650                                 /* and enter drogue state */
651                                 ao_flight_state = ao_flight_drogue;
652                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
653                         }
654
655                         break;
656                 case ao_flight_drogue:
657
658                         /* drogue to main deploy:
659                          *
660                          * barometer: reach main deploy altitude
661                          *
662                          * Would like to use the accelerometer for this test, but
663                          * the orientation of the flight computer is unknown after
664                          * drogue deploy, so we ignore it. Could also detect
665                          * high descent rate using the pressure sensor to
666                          * recognize drogue deploy failure and eject the main
667                          * at that point. Perhaps also use the drogue sense lines
668                          * to notice continutity?
669                          */
670                         if (ao_height <= ao_config.main_deploy)
671                         {
672                                 ao_ignite(ao_igniter_main);
673                                 ao_flight_state = ao_flight_main;
674                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
675                         }
676
677                         /* fall through... */
678                 case ao_flight_main:
679
680                         /* drogue/main to land:
681                          *
682                          * barometer: altitude stable and within 1000m of the launch altitude
683                          */
684
685                         if (ao_height < ao_interval_min_height)
686                                 ao_interval_min_height = ao_height;
687                         if (ao_height > ao_interval_max_height)
688                                 ao_interval_max_height = ao_height;
689
690                         if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
691                                 if (ao_height < AO_M_TO_HEIGHT(1000) &&
692                                     ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
693                                 {
694                                         ao_flight_state = ao_flight_landed;
695
696                                         /* turn off the ADC capture */
697                                         ao_timer_set_adc_interval(0);
698                                         /* Enable RDF beacon */
699                                         ao_rdf_set(1);
700
701                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
702                                 }
703                                 ao_interval_min_height = ao_interval_max_height = ao_height;
704                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
705                         }
706                         break;
707                 case ao_flight_landed:
708                         break;
709                 }
710         }
711 }
712
713 static __xdata struct ao_task   flight_task;
714
715 void
716 ao_flight_init(void)
717 {
718         ao_flight_state = ao_flight_startup;
719         ao_add_task(&flight_task, ao_flight, "flight");
720 }