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; version 2 of the License.
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.
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.
20 /* Main flight thread. */
22 __pdata enum ao_flight_state ao_flight_state; /* current flight state */
23 __pdata uint16_t ao_launch_tick; /* time of launch detect */
26 * track min/max data over a long interval to detect
29 __pdata uint16_t ao_interval_end;
30 __pdata int16_t ao_interval_min_height;
31 __pdata int16_t ao_interval_max_height;
33 __pdata uint8_t ao_flight_force_idle;
35 /* Landing is detected by getting constant readings from both pressure and accelerometer
36 * for a fairly long time (AO_INTERVAL_TICKS)
38 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
44 ao_flight_state = ao_flight_startup;
48 * Process ADC samples, just looping
49 * until the sensors are calibrated.
54 switch (ao_flight_state) {
55 case ao_flight_startup:
56 if (ao_flight_force_idle) {
58 ao_flight_state = ao_flight_idle;
60 ao_flight_state = ao_flight_pad;
61 /* Disable packet mode in pad state */
62 ao_packet_slave_stop();
64 /* Turn on telemetry system */
66 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
68 /* signal successful initialization by turning off the LED */
69 ao_led_off(AO_LED_RED);
71 /* wakeup threads due to state change */
72 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
75 if (ao_height> AO_M_TO_HEIGHT(20)) {
76 ao_flight_state = ao_flight_drogue;
77 ao_launch_tick = ao_sample_tick;
79 /* start logging data */
82 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
85 case ao_flight_drogue:
86 /* drogue/main to land:
88 * barometer: altitude stable
91 if (ao_height < ao_interval_min_height)
92 ao_interval_min_height = ao_height;
93 if (ao_height > ao_interval_max_height)
94 ao_interval_max_height = ao_height;
96 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
97 if (ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
99 ao_flight_state = ao_flight_landed;
101 /* turn off the ADC capture */
102 ao_timer_set_adc_interval(0);
103 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
105 ao_interval_min_height = ao_interval_max_height = ao_height;
106 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
113 static __xdata struct ao_task flight_task;
116 ao_flight_nano_init(void)
118 ao_flight_state = ao_flight_startup;
119 ao_add_task(&flight_task, ao_flight_nano, "flight");