2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 /* Main flight thread. */
23 enum ao_flight_state ao_flight_state; /* current flight state */
24 AO_TICK_TYPE ao_launch_tick; /* time of launch detect */
27 * track min/max data over a long interval to detect
30 AO_TICK_TYPE ao_interval_end;
31 alt_t ao_interval_min_height;
32 alt_t ao_interval_max_height;
34 uint8_t ao_flight_force_idle;
36 /* Landing is detected by getting constant readings from both pressure and accelerometer
37 * for a fairly long time (AO_INTERVAL_TICKS)
39 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
45 ao_flight_state = ao_flight_startup;
49 * Process ADC samples, just looping
50 * until the sensors are calibrated.
55 switch (ao_flight_state) {
56 case ao_flight_startup:
57 if (ao_flight_force_idle) {
59 ao_flight_state = ao_flight_idle;
61 ao_flight_state = ao_flight_pad;
62 /* Disable packet mode in pad state */
63 ao_packet_slave_stop();
65 /* Turn on telemetry system */
67 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
69 /* signal successful initialization by turning off the LED */
70 ao_led_off(AO_LED_RED);
72 /* wakeup threads due to state change */
73 ao_wakeup(&ao_flight_state);
76 if (ao_height> AO_M_TO_HEIGHT(20)) {
77 ao_flight_state = ao_flight_drogue;
78 ao_launch_tick = ao_sample_tick;
80 /* start logging data */
83 ao_wakeup(&ao_flight_state);
86 case ao_flight_drogue:
87 /* drogue/main to land:
89 * barometer: altitude stable
92 if (ao_height < ao_interval_min_height)
93 ao_interval_min_height = ao_height;
94 if (ao_height > ao_interval_max_height)
95 ao_interval_max_height = ao_height;
97 if ((AO_TICK_SIGNED) (ao_sample_tick - ao_interval_end) >= 0) {
98 if (ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
100 ao_flight_state = ao_flight_landed;
102 /* turn off the ADC capture */
103 ao_timer_set_adc_interval(0);
104 ao_wakeup(&ao_flight_state);
106 ao_interval_min_height = ao_interval_max_height = ao_height;
107 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
114 static struct ao_task flight_task;
117 ao_flight_nano_init(void)
119 ao_flight_state = ao_flight_startup;
120 ao_add_task(&flight_task, ao_flight_nano, "flight");