2 * Copyright © 2009 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.
18 #ifndef AO_FLIGHT_TEST
23 #error Please define HAS_ACCEL
27 #error Please define HAS_GPS
31 #error Please define HAS_USB
34 /* Main flight thread. */
36 __pdata enum ao_flight_state ao_flight_state; /* current flight state */
38 __pdata uint8_t ao_flight_force_idle;
39 __pdata uint16_t ao_motor_number;
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:
58 /* Check to see what mode we should go to.
59 * - Invalid mode if accel cal appears to be out
60 * - pad mode if we're upright,
61 * - idle mode otherwise
64 if (ao_config.accel_plus_g == 0 ||
65 ao_config.accel_minus_g == 0 ||
66 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
67 ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
69 /* Detected an accel value outside -1.5g to 1.5g
70 * (or uncalibrated values), so we go into invalid mode
72 ao_flight_state = ao_flight_invalid;
74 #if HAS_RADIO && PACKET_HAS_SLAVE
75 /* Turn on packet system in invalid mode on TeleMetrum */
76 ao_packet_slave_start();
80 if (!ao_flight_force_idle
82 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
86 /* Set pad mode - we can fly! */
87 ao_flight_state = ao_flight_drogue;
89 /* Disable the USB controller in flight mode
95 /* Disable packet mode in pad state */
96 ao_packet_slave_stop();
98 /* Turn on telemetry system */
100 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_BALLOON);
102 /* signal successful initialization by turning off the LED */
103 ao_led_off(AO_LED_RED);
105 /* start logging data */
109 /* Record current GPS position by waking up GPS log tasks */
110 ao_wakeup(&ao_gps_data);
111 ao_wakeup(&ao_gps_tracking_data);
114 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
118 ao_flight_state = ao_flight_idle;
120 /* signal successful initialization by turning off the LED */
121 ao_led_off(AO_LED_RED);
123 /* wakeup threads due to state change */
124 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
127 case ao_flight_drogue:
134 static __xdata struct ao_task flight_task;
139 ao_flight_state = ao_flight_startup;
140 ao_add_task(&flight_task, ao_flight, "flight");