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