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