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;
47 __xdata uint8_t ao_flight_force_idle;
49 /* We also have a clock, which can be used to sanity check things in
50 * case of other failures
53 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
55 /* Landing is detected by getting constant readings from both pressure and accelerometer
56 * for a fairly long time (AO_INTERVAL_TICKS)
58 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
60 #define abs(a) ((a) < 0 ? -(a) : (a))
66 ao_flight_state = ao_flight_startup;
70 * Process ADC samples, just looping
71 * until the sensors are calibrated.
76 switch (ao_flight_state) {
77 case ao_flight_startup:
79 /* Check to see what mode we should go to.
80 * - Invalid mode if accel cal appears to be out
81 * - pad mode if we're upright,
82 * - idle mode otherwise
85 if (ao_config.accel_plus_g == 0 ||
86 ao_config.accel_minus_g == 0 ||
87 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
88 ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
90 /* Detected an accel value outside -1.5g to 1.5g
91 * (or uncalibrated values), so we go into invalid mode
93 ao_flight_state = ao_flight_invalid;
97 if (!ao_flight_force_idle
99 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
103 /* Set pad mode - we can fly! */
104 ao_flight_state = ao_flight_pad;
106 /* Disable the USB controller in flight mode
112 /* Disable packet mode in pad state */
113 ao_packet_slave_stop();
115 /* Turn on telemetry system */
117 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
119 /* signal successful initialization by turning off the LED */
120 ao_led_off(AO_LED_RED);
123 ao_flight_state = ao_flight_idle;
125 /* signal successful initialization by turning off the LED */
126 ao_led_off(AO_LED_RED);
128 /* wakeup threads due to state change */
129 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
136 * barometer: > 20m vertical motion
138 * accelerometer: > 2g AND velocity > 5m/s
140 * The accelerometer should always detect motion before
141 * the barometer, but we use both to make sure this
142 * transition is detected. If the device
143 * doesn't have an accelerometer, then ignore the
144 * speed and acceleration as they are quite noisy
147 if (ao_height > AO_M_TO_HEIGHT(20)
149 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
150 ao_speed > AO_MS_TO_SPEED(5))
154 ao_flight_state = ao_flight_boost;
155 ao_launch_tick = ao_sample_tick;
157 /* start logging data */
160 /* Increase telemetry rate */
161 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
163 /* disable RDF beacon */
167 /* Record current GPS position by waking up GPS log tasks */
168 ao_wakeup(&ao_gps_data);
169 ao_wakeup(&ao_gps_tracking_data);
172 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
176 case ao_flight_boost:
180 * accelerometer: start to fall at > 1/4 G
182 * time: boost for more than 15 seconds
184 * Detects motor burn out by the switch from acceleration to
185 * deceleration, or by waiting until the maximum burn duration
186 * (15 seconds) has past.
188 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
189 (int16_t) (ao_sample_tick - ao_launch_tick) > BOOST_TICKS_MAX)
192 ao_flight_state = ao_flight_fast;
194 ao_flight_state = ao_flight_coast;
196 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
203 * This is essentially the same as coast,
204 * but the barometer is being ignored as
205 * it may be unreliable.
207 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
209 ao_flight_state = ao_flight_coast;
210 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
215 case ao_flight_coast:
217 /* apogee detect: coast to drogue deploy:
221 * Also make sure the model altitude is tracking
222 * the measured altitude reasonably closely; otherwise
223 * we're probably transsonic.
227 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
231 /* ignite the drogue charge */
232 ao_ignite(ao_igniter_drogue);
234 /* slow down the telemetry system */
235 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
237 /* Turn the RDF beacon back on */
241 * Start recording min/max height
242 * to figure out when the rocket has landed
245 /* initialize interval values */
246 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
248 ao_interval_min_height = ao_interval_max_height = ao_height;
250 /* and enter drogue state */
251 ao_flight_state = ao_flight_drogue;
252 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
256 case ao_flight_drogue:
258 /* drogue to main deploy:
260 * barometer: reach main deploy altitude
262 * Would like to use the accelerometer for this test, but
263 * the orientation of the flight computer is unknown after
264 * drogue deploy, so we ignore it. Could also detect
265 * high descent rate using the pressure sensor to
266 * recognize drogue deploy failure and eject the main
267 * at that point. Perhaps also use the drogue sense lines
268 * to notice continutity?
270 if (ao_height <= ao_config.main_deploy)
272 ao_ignite(ao_igniter_main);
273 ao_flight_state = ao_flight_main;
274 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
277 /* fall through... */
280 /* drogue/main to land:
282 * barometer: altitude stable and within 1000m of the launch altitude
285 if (ao_height < ao_interval_min_height)
286 ao_interval_min_height = ao_height;
287 if (ao_height > ao_interval_max_height)
288 ao_interval_max_height = ao_height;
290 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
291 if (ao_height < AO_M_TO_HEIGHT(1000) &&
292 ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
294 ao_flight_state = ao_flight_landed;
296 /* turn off the ADC capture */
297 ao_timer_set_adc_interval(0);
299 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
301 ao_interval_min_height = ao_interval_max_height = ao_height;
302 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
305 case ao_flight_landed:
311 static __xdata struct ao_task flight_task;
316 ao_flight_state = ao_flight_startup;
317 ao_add_task(&flight_task, ao_flight, "flight");