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