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_boost_tick; /* time of launch detect */
40 * track min/max data over a long interval to detect
43 static __data uint16_t ao_interval_end;
44 static __data int16_t ao_interval_min_height;
45 static __data int16_t ao_interval_max_height;
46 static __data int16_t ao_coast_avg_accel;
48 __pdata uint8_t ao_flight_force_idle;
50 /* We also have a clock, which can be used to sanity check things in
51 * case of other failures
54 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
56 /* Landing is detected by getting constant readings from both pressure and accelerometer
57 * for a fairly long time (AO_INTERVAL_TICKS)
59 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(10)
61 #define abs(a) ((a) < 0 ? -(a) : (a))
67 ao_flight_state = ao_flight_startup;
71 * Process ADC samples, just looping
72 * until the sensors are calibrated.
77 switch (ao_flight_state) {
78 case ao_flight_startup:
80 /* Check to see what mode we should go to.
81 * - Invalid mode if accel cal appears to be out
82 * - pad mode if we're upright,
83 * - idle mode otherwise
86 if (ao_config.accel_plus_g == 0 ||
87 ao_config.accel_minus_g == 0 ||
88 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
89 ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
91 /* Detected an accel value outside -1.5g to 1.5g
92 * (or uncalibrated values), so we go into invalid mode
94 ao_flight_state = ao_flight_invalid;
96 /* Turn on packet system in invalid mode on TeleMetrum */
97 ao_packet_slave_start();
100 if (!ao_flight_force_idle
102 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
106 /* Set pad mode - we can fly! */
107 ao_flight_state = ao_flight_pad;
109 /* Disable the USB controller in flight mode
116 /* Disable packet mode in pad state on TeleMini */
117 ao_packet_slave_stop();
120 /* Turn on telemetry system */
122 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
124 /* signal successful initialization by turning off the LED */
125 ao_led_off(AO_LED_RED);
128 ao_flight_state = ao_flight_idle;
131 /* Turn on packet system in idle mode on TeleMetrum */
132 ao_packet_slave_start();
135 /* signal successful initialization by turning off the LED */
136 ao_led_off(AO_LED_RED);
138 /* wakeup threads due to state change */
139 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
146 * barometer: > 20m vertical motion
148 * accelerometer: > 2g AND velocity > 5m/s
150 * The accelerometer should always detect motion before
151 * the barometer, but we use both to make sure this
152 * transition is detected. If the device
153 * doesn't have an accelerometer, then ignore the
154 * speed and acceleration as they are quite noisy
157 if (ao_height > AO_M_TO_HEIGHT(20)
159 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
160 ao_speed > AO_MS_TO_SPEED(5))
164 ao_flight_state = ao_flight_boost;
165 ao_boost_tick = ao_sample_tick;
167 /* start logging data */
170 /* Increase telemetry rate */
171 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
173 /* disable RDF beacon */
177 /* Record current GPS position by waking up GPS log tasks */
178 ao_wakeup(&ao_gps_data);
179 ao_wakeup(&ao_gps_tracking_data);
182 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
185 case ao_flight_boost:
189 * accelerometer: start to fall at > 1/4 G
191 * time: boost for more than 15 seconds
193 * Detects motor burn out by the switch from acceleration to
194 * deceleration, or by waiting until the maximum burn duration
195 * (15 seconds) has past.
197 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
198 (int16_t) (ao_sample_tick - ao_boost_tick) > BOOST_TICKS_MAX)
201 ao_flight_state = ao_flight_fast;
202 ao_coast_avg_accel = ao_accel;
204 ao_flight_state = ao_flight_coast;
206 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
212 * This is essentially the same as coast,
213 * but the barometer is being ignored as
214 * it may be unreliable.
216 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
218 ao_flight_state = ao_flight_coast;
219 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
224 case ao_flight_coast:
226 /* apogee detect: coast to drogue deploy:
230 * Also make sure the model altitude is tracking
231 * the measured altitude reasonably closely; otherwise
232 * we're probably transsonic.
236 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
240 /* ignite the drogue charge */
241 ao_ignite(ao_igniter_drogue);
243 /* slow down the telemetry system */
244 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
246 /* Turn the RDF beacon back on */
249 /* and enter drogue state */
250 ao_flight_state = ao_flight_drogue;
251 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
256 ao_coast_avg_accel = ao_coast_avg_accel - (ao_coast_avg_accel >> 6) + (ao_accel >> 6);
257 if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
258 ao_boost_tick = ao_sample_tick;
259 ao_flight_state = ao_flight_boost;
260 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
266 case ao_flight_drogue:
268 /* drogue to main deploy:
270 * barometer: reach main deploy altitude
272 * Would like to use the accelerometer for this test, but
273 * the orientation of the flight computer is unknown after
274 * drogue deploy, so we ignore it. Could also detect
275 * high descent rate using the pressure sensor to
276 * recognize drogue deploy failure and eject the main
277 * at that point. Perhaps also use the drogue sense lines
278 * to notice continutity?
280 if (ao_height <= ao_config.main_deploy)
282 ao_ignite(ao_igniter_main);
285 * Start recording min/max height
286 * to figure out when the rocket has landed
289 /* initialize interval values */
290 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
292 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
294 ao_flight_state = ao_flight_main;
295 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
299 /* fall through... */
304 * barometer: altitude stable
307 if (ao_avg_height < ao_interval_min_height)
308 ao_interval_min_height = ao_avg_height;
309 if (ao_avg_height > ao_interval_max_height)
310 ao_interval_max_height = ao_avg_height;
312 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
313 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
315 ao_flight_state = ao_flight_landed;
317 /* turn off the ADC capture */
318 ao_timer_set_adc_interval(0);
320 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
322 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
323 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
326 case ao_flight_landed:
332 static __xdata struct ao_task flight_task;
337 ao_flight_state = ao_flight_startup;
338 ao_add_task(&flight_task, ao_flight, "flight");