altos: Baro-only boards must not detect launch on accel or speed data
[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 HAS_ACCEL
492                         /*
493                          * With an accelerometer, either to detect launch
494                          */
495                         if ((ao_k_accel > to_fix32(20) &&
496                              ao_k_speed > to_fix32(5)) ||
497                             ao_k_height > to_fix32(20))
498 #else
499                         /*
500                          * Without an accelerometer, the barometer is far too
501                          * noisy to rely on speed or acceleration data
502                          */
503                         if (ao_k_height > to_fix32(20))
504 #endif
505 #else
506                         if (
507 #if HAS_ACCEL
508                                 (ao_flight_accel < ao_ground_accel - ACCEL_BOOST &&
509                                  ao_flight_vel > ACCEL_VEL_BOOST) ||
510 #endif
511                             ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
512 #endif
513                         {
514 #if HAS_ACCEL || USE_KALMAN
515                                 ao_flight_state = ao_flight_boost;
516 #else
517                                 ao_flight_state = ao_flight_coast;
518 #endif
519                                 ao_launch_tick = ao_flight_tick;
520
521                                 /* start logging data */
522                                 ao_log_start();
523
524                                 /* Increase telemetry rate */
525                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
526
527                                 /* disable RDF beacon */
528                                 ao_rdf_set(0);
529
530 #if HAS_GPS
531                                 /* Record current GPS position by waking up GPS log tasks */
532                                 ao_wakeup(&ao_gps_data);
533                                 ao_wakeup(&ao_gps_tracking_data);
534 #endif
535
536                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
537                                 break;
538                         }
539                         break;
540 #if HAS_ACCEL || USE_KALMAN
541                 case ao_flight_boost:
542
543                         /* boost to fast:
544                          *
545                          * accelerometer: start to fall at > 1/4 G
546                          *              OR
547                          * time: boost for more than 15 seconds
548                          *
549                          * Detects motor burn out by the switch from acceleration to
550                          * deceleration, or by waiting until the maximum burn duration
551                          * (15 seconds) has past.
552                          */
553 #if USE_KALMAN
554                         if ((ao_k_accel < to_fix32(-10) && ao_k_height > to_fix32(100)) ||
555                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
556 #else
557                         if (ao_flight_accel > ao_ground_accel + ACCEL_COAST ||
558                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
559 #endif
560                         {
561                                 ao_flight_state = ao_flight_fast;
562                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
563                                 break;
564                         }
565                         break;
566                 case ao_flight_fast:
567
568                         /* fast to coast:
569                          *
570                          * accelerometer: integrated velocity < 200 m/s
571                          *               OR
572                          * barometer: fall at least 500m from max altitude
573                          *
574                          * This extra state is required to avoid mis-detecting
575                          * apogee due to mach transitions.
576                          *
577                          * XXX this is essentially a single-detector test
578                          * as the 500m altitude change would likely result
579                          * in a loss of the rocket. More data on precisely
580                          * how big a pressure change the mach transition
581                          * generates would be useful here.
582                          */
583 #if USE_KALMAN
584                         if (ao_k_speed < to_fix32(200) ||
585                             ao_k_height < ao_k_max_height - to_fix32(500))
586 #else
587                         if (ao_flight_vel < ACCEL_VEL_MACH ||
588                             ao_flight_pres > ao_min_pres + BARO_COAST)
589 #endif
590                         {
591 #if HAS_ACCEL
592                                 /* set min velocity to current velocity for
593                                  * apogee detect
594                                  */
595                                 ao_min_vel = abs(ao_flight_vel);
596 #endif
597                                 ao_flight_state = ao_flight_coast;
598                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
599                         }
600                         break;
601 #endif /* HAS_ACCEL */
602                 case ao_flight_coast:
603
604 #if USE_KALMAN
605                         /* apogee detect: coast to drogue deploy:
606                          *
607                          * speed: < 0
608                          */
609                         if (ao_k_speed < 0)
610 #else
611                         /* apogee detect: coast to drogue deploy:
612                          *
613                          * barometer: fall at least 10m
614                          *
615                          * It would be nice to use the accelerometer
616                          * to detect apogee as well, but tests have
617                          * shown that flights far from vertical would
618                          * grossly mis-detect apogee. So, for now,
619                          * we'll trust to a single sensor for this test
620                          */
621                         if (ao_flight_pres > ao_min_pres + BARO_APOGEE)
622 #endif
623                         {
624                                 /* ignite the drogue charge */
625                                 ao_ignite(ao_igniter_drogue);
626
627                                 /* slow down the telemetry system */
628                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
629
630                                 /* slow down the ADC sample rate */
631                                 ao_timer_set_adc_interval(10);
632
633                                 /*
634                                  * Start recording min/max accel and pres for a while
635                                  * to figure out when the rocket has landed
636                                  */
637                                 /* Set the 'last' limits to max range to prevent
638                                  * early resting detection
639                                  */
640 #if HAS_ACCEL
641                                 ao_interval_min_accel = 0;
642                                 ao_interval_max_accel = 0x7fff;
643 #endif
644                                 ao_interval_min_pres = 0;
645                                 ao_interval_max_pres = 0x7fff;
646
647                                 /* initialize interval values */
648                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
649
650                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
651 #if HAS_ACCEL
652                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
653 #endif
654
655                                 /* and enter drogue state */
656                                 ao_flight_state = ao_flight_drogue;
657                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
658                         }
659
660                         break;
661                 case ao_flight_drogue:
662
663                         /* drogue to main deploy:
664                          *
665                          * barometer: reach main deploy altitude
666                          *
667                          * Would like to use the accelerometer for this test, but
668                          * the orientation of the flight computer is unknown after
669                          * drogue deploy, so we ignore it. Could also detect
670                          * high descent rate using the pressure sensor to
671                          * recognize drogue deploy failure and eject the main
672                          * at that point. Perhaps also use the drogue sense lines
673                          * to notice continutity?
674                          */
675 #if USE_KALMAN
676                         if (from_fix(ao_k_height) < ao_config.main_deploy)
677 #else
678                         if (ao_flight_pres >= ao_main_pres)
679 #endif
680                         {
681                                 ao_ignite(ao_igniter_main);
682                                 ao_flight_state = ao_flight_main;
683                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
684                         }
685
686                         /* fall through... */
687                 case ao_flight_main:
688
689                         /* drogue/main to land:
690                          *
691                          * accelerometer: value stable
692                          *                           AND
693                          * barometer: altitude stable and within 1000m of the launch altitude
694                          */
695
696                         if (ao_flight_pres < ao_interval_cur_min_pres)
697                                 ao_interval_cur_min_pres = ao_flight_pres;
698                         if (ao_flight_pres > ao_interval_cur_max_pres)
699                                 ao_interval_cur_max_pres = ao_flight_pres;
700 #if HAS_ACCEL
701                         if (ao_flight_accel < ao_interval_cur_min_accel)
702                                 ao_interval_cur_min_accel = ao_flight_accel;
703                         if (ao_flight_accel > ao_interval_cur_max_accel)
704                                 ao_interval_cur_max_accel = ao_flight_accel;
705 #endif
706
707                         if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
708                                 ao_interval_max_pres = ao_interval_cur_max_pres;
709                                 ao_interval_min_pres = ao_interval_cur_min_pres;
710                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
711 #if HAS_ACCEL
712                                 ao_interval_max_accel = ao_interval_cur_max_accel;
713                                 ao_interval_min_accel = ao_interval_cur_min_accel;
714                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
715 #endif
716                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
717
718                                 if (
719 #if HAS_ACCEL
720                                         (uint16_t) (ao_interval_max_accel - ao_interval_min_accel) < (uint16_t) ACCEL_INT_LAND &&
721 #endif
722                                     ao_flight_pres > ao_ground_pres - BARO_LAND &&
723                                     (uint16_t) (ao_interval_max_pres - ao_interval_min_pres) < (uint16_t) BARO_INT_LAND)
724                                 {
725                                         ao_flight_state = ao_flight_landed;
726
727                                         /* turn off the ADC capture */
728                                         ao_timer_set_adc_interval(0);
729                                         /* Enable RDF beacon */
730                                         ao_rdf_set(1);
731
732                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
733                                 }
734                         }
735                         break;
736                 case ao_flight_landed:
737                         break;
738                 }
739         }
740 }
741
742 static __xdata struct ao_task   flight_task;
743
744 void
745 ao_flight_init(void)
746 {
747         ao_flight_state = ao_flight_startup;
748         ao_add_task(&flight_task, ao_flight, "flight");
749 }