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;
94 /* Turn on packet system in invalid mode on TeleMetrum */
95 ao_packet_slave_start();
98 if (!ao_flight_force_idle
100 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
104 /* Set pad mode - we can fly! */
105 ao_flight_state = ao_flight_pad;
107 /* Disable the USB controller in flight mode
114 /* Disable packet mode in pad state on TeleMini */
115 ao_packet_slave_stop();
118 /* Turn on telemetry system */
120 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
122 /* signal successful initialization by turning off the LED */
123 ao_led_off(AO_LED_RED);
126 ao_flight_state = ao_flight_idle;
129 /* Turn on packet system in idle mode on TeleMetrum */
130 ao_packet_slave_start();
133 /* signal successful initialization by turning off the LED */
134 ao_led_off(AO_LED_RED);
136 /* wakeup threads due to state change */
137 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
144 * barometer: > 20m vertical motion
146 * accelerometer: > 2g AND velocity > 5m/s
148 * The accelerometer should always detect motion before
149 * the barometer, but we use both to make sure this
150 * transition is detected. If the device
151 * doesn't have an accelerometer, then ignore the
152 * speed and acceleration as they are quite noisy
155 if (ao_height > AO_M_TO_HEIGHT(20)
157 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
158 ao_speed > AO_MS_TO_SPEED(5))
162 ao_flight_state = ao_flight_boost;
163 ao_launch_tick = ao_sample_tick;
165 /* start logging data */
168 /* Increase telemetry rate */
169 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
171 /* disable RDF beacon */
175 /* Record current GPS position by waking up GPS log tasks */
176 ao_wakeup(&ao_gps_data);
177 ao_wakeup(&ao_gps_tracking_data);
180 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
183 case ao_flight_boost:
187 * accelerometer: start to fall at > 1/4 G
189 * time: boost for more than 15 seconds
191 * Detects motor burn out by the switch from acceleration to
192 * deceleration, or by waiting until the maximum burn duration
193 * (15 seconds) has past.
195 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
196 (int16_t) (ao_sample_tick - ao_launch_tick) > BOOST_TICKS_MAX)
199 ao_flight_state = ao_flight_fast;
201 ao_flight_state = ao_flight_coast;
203 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
209 * This is essentially the same as coast,
210 * but the barometer is being ignored as
211 * it may be unreliable.
213 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
215 ao_flight_state = ao_flight_coast;
216 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
220 case ao_flight_coast:
222 /* apogee detect: coast to drogue deploy:
226 * Also make sure the model altitude is tracking
227 * the measured altitude reasonably closely; otherwise
228 * we're probably transsonic.
232 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
236 /* ignite the drogue charge */
237 ao_ignite(ao_igniter_drogue);
239 /* slow down the telemetry system */
240 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
242 /* Turn the RDF beacon back on */
245 /* and enter drogue state */
246 ao_flight_state = ao_flight_drogue;
247 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
251 case ao_flight_drogue:
253 /* drogue to main deploy:
255 * barometer: reach main deploy altitude
257 * Would like to use the accelerometer for this test, but
258 * the orientation of the flight computer is unknown after
259 * drogue deploy, so we ignore it. Could also detect
260 * high descent rate using the pressure sensor to
261 * recognize drogue deploy failure and eject the main
262 * at that point. Perhaps also use the drogue sense lines
263 * to notice continutity?
265 if (ao_height <= ao_config.main_deploy)
267 ao_ignite(ao_igniter_main);
270 * Start recording min/max height
271 * to figure out when the rocket has landed
274 /* initialize interval values */
275 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
277 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
279 ao_flight_state = ao_flight_main;
280 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
284 /* fall through... */
289 * barometer: altitude stable
292 if (ao_avg_height < ao_interval_min_height)
293 ao_interval_min_height = ao_avg_height;
294 if (ao_avg_height > ao_interval_max_height)
295 ao_interval_max_height = ao_avg_height;
297 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
298 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
300 ao_flight_state = ao_flight_landed;
302 /* turn off the ADC capture */
303 ao_timer_set_adc_interval(0);
305 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
307 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
308 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
311 case ao_flight_landed:
317 static __xdata struct ao_task flight_task;
322 ao_flight_state = ao_flight_startup;
323 ao_add_task(&flight_task, ao_flight, "flight");