altos: Start with packet slave running. Turn off in pad mode.
[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
362                         } else
363 #endif
364                                 if (!ao_flight_force_idle
365 #if HAS_ACCEL
366                                     && ao_flight_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
367 #endif
368                                         )
369                         {
370                                 /* Set pad mode - we can fly! */
371                                 ao_flight_state = ao_flight_pad;
372
373 #if HAS_USB
374                                 /* Disable the USB controller in flight mode
375                                  * to save power
376                                  */
377                                 ao_usb_disable();
378 #endif
379
380                                 /* Disable packet mode in pad state */
381                                 ao_packet_slave_stop();
382
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                                 /* signal successful initialization by turning off the LED */
394                                 ao_led_off(AO_LED_RED);
395                         }
396                         /* wakeup threads due to state change */
397                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
398
399                         break;
400                 case ao_flight_pad:
401
402 #if HAS_ACCEL
403                         /* Trim velocity
404                          *
405                          * Once a second, remove any velocity from
406                          * a second ago
407                          */
408                         if ((int16_t) (ao_flight_tick - ao_old_vel_tick) >= AO_SEC_TO_TICKS(1)) {
409                                 ao_old_vel_tick = ao_flight_tick;
410                                 ao_flight_vel -= ao_old_vel;
411                                 ao_old_vel = ao_flight_vel;
412                         }
413 #endif
414                         /* pad to boost:
415                          *
416                          * accelerometer: > 2g AND velocity > 5m/s
417                          *             OR
418                          * barometer: > 20m vertical motion
419                          *
420                          * The accelerometer should always detect motion before
421                          * the barometer, but we use both to make sure this
422                          * transition is detected
423                          */
424                         if (
425 #if HAS_ACCEL
426                                 (ao_flight_accel < ao_ground_accel - ACCEL_BOOST &&
427                                  ao_flight_vel > ACCEL_VEL_BOOST) ||
428 #endif
429                             ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
430                         {
431 #if HAS_ACCEL
432                                 ao_flight_state = ao_flight_boost;
433 #else
434                                 ao_flight_state = ao_flight_coast;
435 #endif
436                                 ao_launch_tick = ao_flight_tick;
437
438                                 /* start logging data */
439                                 ao_log_start();
440
441                                 /* Increase telemetry rate */
442                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
443
444                                 /* disable RDF beacon */
445                                 ao_rdf_set(0);
446
447 #if HAS_GPS
448                                 /* Record current GPS position by waking up GPS log tasks */
449                                 ao_wakeup(&ao_gps_data);
450                                 ao_wakeup(&ao_gps_tracking_data);
451 #endif
452
453                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
454                                 break;
455                         }
456                         break;
457 #if HAS_ACCEL
458                 case ao_flight_boost:
459
460                         /* boost to fast:
461                          *
462                          * accelerometer: start to fall at > 1/4 G
463                          *              OR
464                          * time: boost for more than 15 seconds
465                          *
466                          * Detects motor burn out by the switch from acceleration to
467                          * deceleration, or by waiting until the maximum burn duration
468                          * (15 seconds) has past.
469                          */
470                         if (ao_flight_accel > ao_ground_accel + ACCEL_COAST ||
471                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
472                         {
473                                 ao_flight_state = ao_flight_fast;
474                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
475                                 break;
476                         }
477                         break;
478                 case ao_flight_fast:
479
480                         /* fast to coast:
481                          *
482                          * accelerometer: integrated velocity < 200 m/s
483                          *               OR
484                          * barometer: fall at least 500m from max altitude
485                          *
486                          * This extra state is required to avoid mis-detecting
487                          * apogee due to mach transitions.
488                          *
489                          * XXX this is essentially a single-detector test
490                          * as the 500m altitude change would likely result
491                          * in a loss of the rocket. More data on precisely
492                          * how big a pressure change the mach transition
493                          * generates would be useful here.
494                          */
495                         if (ao_flight_vel < ACCEL_VEL_MACH ||
496                             ao_flight_pres > ao_min_pres + BARO_COAST)
497                         {
498                                 /* set min velocity to current velocity for
499                                  * apogee detect
500                                  */
501                                 ao_min_vel = abs(ao_flight_vel);
502                                 ao_flight_state = ao_flight_coast;
503                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
504                         }
505                         break;
506 #endif
507                 case ao_flight_coast:
508
509                         /* apogee detect: coast to drogue deploy:
510                          *
511                          * barometer: fall at least 10m
512                          *
513                          * It would be nice to use the accelerometer
514                          * to detect apogee as well, but tests have
515                          * shown that flights far from vertical would
516                          * grossly mis-detect apogee. So, for now,
517                          * we'll trust to a single sensor for this test
518                          */
519                         if (ao_flight_pres > ao_min_pres + BARO_APOGEE)
520                         {
521                                 /* ignite the drogue charge */
522                                 ao_ignite(ao_igniter_drogue);
523
524                                 /* slow down the telemetry system */
525                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
526
527                                 /* slow down the ADC sample rate */
528                                 ao_timer_set_adc_interval(10);
529
530                                 /*
531                                  * Start recording min/max accel and pres for a while
532                                  * to figure out when the rocket has landed
533                                  */
534                                 /* Set the 'last' limits to max range to prevent
535                                  * early resting detection
536                                  */
537 #if HAS_ACCEL
538                                 ao_interval_min_accel = 0;
539                                 ao_interval_max_accel = 0x7fff;
540 #endif
541                                 ao_interval_min_pres = 0;
542                                 ao_interval_max_pres = 0x7fff;
543
544                                 /* initialize interval values */
545                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
546
547                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
548 #if HAS_ACCEL
549                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
550 #endif
551
552                                 /* and enter drogue state */
553                                 ao_flight_state = ao_flight_drogue;
554                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
555                         }
556
557                         break;
558                 case ao_flight_drogue:
559
560                         /* drogue to main deploy:
561                          *
562                          * barometer: reach main deploy altitude
563                          *
564                          * Would like to use the accelerometer for this test, but
565                          * the orientation of the flight computer is unknown after
566                          * drogue deploy, so we ignore it. Could also detect
567                          * high descent rate using the pressure sensor to
568                          * recognize drogue deploy failure and eject the main
569                          * at that point. Perhaps also use the drogue sense lines
570                          * to notice continutity?
571                          */
572                         if (ao_flight_pres >= ao_main_pres)
573                         {
574                                 ao_ignite(ao_igniter_main);
575                                 ao_flight_state = ao_flight_main;
576                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
577                         }
578
579                         /* fall through... */
580                 case ao_flight_main:
581
582                         /* drogue/main to land:
583                          *
584                          * accelerometer: value stable
585                          *                           AND
586                          * barometer: altitude stable and within 1000m of the launch altitude
587                          */
588
589                         if (ao_flight_pres < ao_interval_cur_min_pres)
590                                 ao_interval_cur_min_pres = ao_flight_pres;
591                         if (ao_flight_pres > ao_interval_cur_max_pres)
592                                 ao_interval_cur_max_pres = ao_flight_pres;
593 #if HAS_ACCEL
594                         if (ao_flight_accel < ao_interval_cur_min_accel)
595                                 ao_interval_cur_min_accel = ao_flight_accel;
596                         if (ao_flight_accel > ao_interval_cur_max_accel)
597                                 ao_interval_cur_max_accel = ao_flight_accel;
598 #endif
599
600                         if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
601                                 ao_interval_max_pres = ao_interval_cur_max_pres;
602                                 ao_interval_min_pres = ao_interval_cur_min_pres;
603                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
604 #if HAS_ACCEL
605                                 ao_interval_max_accel = ao_interval_cur_max_accel;
606                                 ao_interval_min_accel = ao_interval_cur_min_accel;
607                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
608 #endif
609                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
610
611                                 if (
612 #if HAS_ACCEL
613                                         (uint16_t) (ao_interval_max_accel - ao_interval_min_accel) < (uint16_t) ACCEL_INT_LAND &&
614 #endif
615                                     ao_flight_pres > ao_ground_pres - BARO_LAND &&
616                                     (uint16_t) (ao_interval_max_pres - ao_interval_min_pres) < (uint16_t) BARO_INT_LAND)
617                                 {
618                                         ao_flight_state = ao_flight_landed;
619
620                                         /* turn off the ADC capture */
621                                         ao_timer_set_adc_interval(0);
622                                         /* Enable RDF beacon */
623                                         ao_rdf_set(1);
624
625                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
626                                 }
627                         }
628                         break;
629                 case ao_flight_landed:
630                         break;
631                 }
632         }
633 }
634
635 static __xdata struct ao_task   flight_task;
636
637 void
638 ao_flight_init(void)
639 {
640         ao_flight_state = ao_flight_startup;
641         ao_add_task(&flight_task, ao_flight, "flight");
642 }