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 */
37 __pdata uint16_t ao_launch_tick; /* time of launch detect */
40 * track min/max data over a long interval to detect
43 __pdata uint16_t ao_interval_end;
44 __pdata int16_t ao_interval_min_height;
45 __pdata int16_t ao_interval_max_height;
46 __pdata uint8_t ao_flight_force_idle;
48 /* We also have a clock, which can be used to sanity check things in
49 * case of other failures
52 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
54 /* Landing is detected by getting constant readings from both pressure and accelerometer
55 * for a fairly long time (AO_INTERVAL_TICKS)
57 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(10)
59 #define abs(a) ((a) < 0 ? -(a) : (a))
65 ao_flight_state = ao_flight_startup;
69 * Process ADC samples, just looping
70 * until the sensors are calibrated.
75 switch (ao_flight_state) {
76 case ao_flight_startup:
78 /* Check to see what mode we should go to.
79 * - Invalid mode if accel cal appears to be out
80 * - pad mode if we're upright,
81 * - idle mode otherwise
84 if (ao_config.accel_plus_g == 0 ||
85 ao_config.accel_minus_g == 0 ||
86 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
87 ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
89 /* Detected an accel value outside -1.5g to 1.5g
90 * (or uncalibrated values), so we go into invalid mode
92 ao_flight_state = ao_flight_invalid;
96 if (!ao_flight_force_idle
98 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
102 /* Set pad mode - we can fly! */
103 ao_flight_state = ao_flight_pad;
105 /* Disable the USB controller in flight mode
111 /* Disable packet mode in pad state */
112 ao_packet_slave_stop();
114 /* Turn on telemetry system */
116 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
118 /* signal successful initialization by turning off the LED */
119 ao_led_off(AO_LED_RED);
122 ao_flight_state = ao_flight_idle;
124 /* signal successful initialization by turning off the LED */
125 ao_led_off(AO_LED_RED);
127 /* wakeup threads due to state change */
128 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
135 * barometer: > 20m vertical motion
137 * accelerometer: > 2g AND velocity > 5m/s
139 * The accelerometer should always detect motion before
140 * the barometer, but we use both to make sure this
141 * transition is detected. If the device
142 * doesn't have an accelerometer, then ignore the
143 * speed and acceleration as they are quite noisy
146 if (ao_height > AO_M_TO_HEIGHT(20)
148 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
149 ao_speed > AO_MS_TO_SPEED(5))
153 ao_flight_state = ao_flight_boost;
154 ao_launch_tick = ao_sample_tick;
156 /* start logging data */
159 /* Increase telemetry rate */
160 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
162 /* disable RDF beacon */
166 /* Record current GPS position by waking up GPS log tasks */
167 ao_wakeup(&ao_gps_data);
168 ao_wakeup(&ao_gps_tracking_data);
171 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
174 case ao_flight_boost:
178 * accelerometer: start to fall at > 1/4 G
180 * time: boost for more than 15 seconds
182 * Detects motor burn out by the switch from acceleration to
183 * deceleration, or by waiting until the maximum burn duration
184 * (15 seconds) has past.
186 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
187 (int16_t) (ao_sample_tick - ao_launch_tick) > BOOST_TICKS_MAX)
190 ao_flight_state = ao_flight_fast;
192 ao_flight_state = ao_flight_coast;
194 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
200 * This is essentially the same as coast,
201 * but the barometer is being ignored as
202 * it may be unreliable.
204 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
206 ao_flight_state = ao_flight_coast;
207 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
211 case ao_flight_coast:
213 /* apogee detect: coast to drogue deploy:
217 * Also make sure the model altitude is tracking
218 * the measured altitude reasonably closely; otherwise
219 * we're probably transsonic.
223 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
227 /* ignite the drogue charge */
228 ao_ignite(ao_igniter_drogue);
230 /* slow down the telemetry system */
231 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
233 /* Turn the RDF beacon back on */
236 /* and enter drogue state */
237 ao_flight_state = ao_flight_drogue;
238 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
242 case ao_flight_drogue:
244 /* drogue to main deploy:
246 * barometer: reach main deploy altitude
248 * Would like to use the accelerometer for this test, but
249 * the orientation of the flight computer is unknown after
250 * drogue deploy, so we ignore it. Could also detect
251 * high descent rate using the pressure sensor to
252 * recognize drogue deploy failure and eject the main
253 * at that point. Perhaps also use the drogue sense lines
254 * to notice continutity?
256 if (ao_height <= ao_config.main_deploy)
258 ao_ignite(ao_igniter_main);
261 * Start recording min/max height
262 * to figure out when the rocket has landed
265 /* initialize interval values */
266 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
268 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
270 ao_flight_state = ao_flight_main;
271 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
275 /* fall through... */
280 * barometer: altitude stable
283 if (ao_avg_height < ao_interval_min_height)
284 ao_interval_min_height = ao_avg_height;
285 if (ao_avg_height > ao_interval_max_height)
286 ao_interval_max_height = ao_avg_height;
288 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
289 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
291 ao_flight_state = ao_flight_landed;
293 /* turn off the ADC capture */
294 ao_timer_set_adc_interval(0);
296 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
298 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
299 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
302 case ao_flight_landed:
308 static __xdata struct ao_task flight_task;
313 ao_flight_state = ao_flight_startup;
314 ao_add_task(&flight_task, ao_flight, "flight");