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