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
24 #error Please define HAS_ACCEL
28 #error Please define HAS_GPS
32 #error Please define HAS_USB
36 #define HAS_TELEMETRY HAS_RADIO
39 /* Main flight thread. */
41 __pdata enum ao_flight_state ao_flight_state; /* current flight state */
42 __pdata uint16_t ao_boost_tick; /* time of launch detect */
43 __pdata uint16_t ao_motor_number; /* number of motors burned so far */
46 * track min/max data over a long interval to detect
49 static __data uint16_t ao_interval_end;
50 static __data int16_t ao_interval_min_height;
51 static __data int16_t ao_interval_max_height;
53 static __data int16_t ao_coast_avg_accel;
56 __pdata uint8_t ao_flight_force_idle;
58 /* We also have a clock, which can be used to sanity check things in
59 * case of other failures
62 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
64 /* Landing is detected by getting constant readings from both pressure and accelerometer
65 * for a fairly long time (AO_INTERVAL_TICKS)
67 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(10)
69 #define abs(a) ((a) < 0 ? -(a) : (a))
75 ao_flight_state = ao_flight_startup;
79 * Process ADC samples, just looping
80 * until the sensors are calibrated.
85 switch (ao_flight_state) {
86 case ao_flight_startup:
88 /* Check to see what mode we should go to.
89 * - Invalid mode if accel cal appears to be out
90 * - pad mode if we're upright,
91 * - idle mode otherwise
94 if (ao_config.accel_plus_g == 0 ||
95 ao_config.accel_minus_g == 0 ||
96 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
97 ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP ||
98 ao_ground_height < -1000 ||
99 ao_ground_height > 7000)
101 /* Detected an accel value outside -1.5g to 1.5g
102 * (or uncalibrated values), so we go into invalid mode
104 ao_flight_state = ao_flight_invalid;
106 #if HAS_RADIO && PACKET_HAS_SLAVE
107 /* Turn on packet system in invalid mode on TeleMetrum */
108 ao_packet_slave_start();
112 if (!ao_flight_force_idle
114 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
118 /* Set pad mode - we can fly! */
119 ao_flight_state = ao_flight_pad;
120 #if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE
121 /* Disable the USB controller in flight mode
127 #if !HAS_ACCEL && PACKET_HAS_SLAVE
128 /* Disable packet mode in pad state on TeleMini */
129 ao_packet_slave_stop();
133 /* Turn on telemetry system */
135 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
137 /* signal successful initialization by turning off the LED */
138 ao_led_off(AO_LED_RED);
141 ao_flight_state = ao_flight_idle;
143 #if HAS_ACCEL && HAS_RADIO && PACKET_HAS_SLAVE
144 /* Turn on packet system in idle mode on TeleMetrum */
145 ao_packet_slave_start();
148 /* signal successful initialization by turning off the LED */
149 ao_led_off(AO_LED_RED);
151 /* wakeup threads due to state change */
152 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
159 * barometer: > 20m vertical motion
161 * accelerometer: > 2g AND velocity > 5m/s
163 * The accelerometer should always detect motion before
164 * the barometer, but we use both to make sure this
165 * transition is detected. If the device
166 * doesn't have an accelerometer, then ignore the
167 * speed and acceleration as they are quite noisy
170 if (ao_height > AO_M_TO_HEIGHT(20)
172 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
173 ao_speed > AO_MS_TO_SPEED(5))
177 ao_flight_state = ao_flight_boost;
178 ao_boost_tick = ao_sample_tick;
180 /* start logging data */
184 /* Increase telemetry rate */
185 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
187 /* disable RDF beacon */
192 /* Record current GPS position by waking up GPS log tasks */
193 ao_wakeup(&ao_gps_data);
194 ao_wakeup(&ao_gps_tracking_data);
197 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
200 case ao_flight_boost:
204 * accelerometer: start to fall at > 1/4 G
206 * time: boost for more than 15 seconds
208 * Detects motor burn out by the switch from acceleration to
209 * deceleration, or by waiting until the maximum burn duration
210 * (15 seconds) has past.
212 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
213 (int16_t) (ao_sample_tick - ao_boost_tick) > BOOST_TICKS_MAX)
216 ao_flight_state = ao_flight_fast;
217 ao_coast_avg_accel = ao_accel;
219 ao_flight_state = ao_flight_coast;
222 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
228 * This is essentially the same as coast,
229 * but the barometer is being ignored as
230 * it may be unreliable.
232 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
234 ao_flight_state = ao_flight_coast;
235 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
240 case ao_flight_coast:
243 * By customer request - allow the user
244 * to lock out apogee detection for a specified
247 if (ao_config.apogee_lockout) {
248 if ((ao_sample_tick - ao_boost_tick) <
249 AO_SEC_TO_TICKS(ao_config.apogee_lockout))
253 /* apogee detect: coast to drogue deploy:
257 * Also make sure the model altitude is tracking
258 * the measured altitude reasonably closely; otherwise
259 * we're probably transsonic.
263 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
268 /* ignite the drogue charge */
269 ao_ignite(ao_igniter_drogue);
273 /* slow down the telemetry system */
274 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
276 /* Turn the RDF beacon back on */
280 /* and enter drogue state */
281 ao_flight_state = ao_flight_drogue;
282 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
287 ao_coast_avg_accel = ao_coast_avg_accel - (ao_coast_avg_accel >> 6) + (ao_accel >> 6);
288 if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
289 ao_boost_tick = ao_sample_tick;
290 ao_flight_state = ao_flight_boost;
291 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
297 case ao_flight_drogue:
299 /* drogue to main deploy:
301 * barometer: reach main deploy altitude
303 * Would like to use the accelerometer for this test, but
304 * the orientation of the flight computer is unknown after
305 * drogue deploy, so we ignore it. Could also detect
306 * high descent rate using the pressure sensor to
307 * recognize drogue deploy failure and eject the main
308 * at that point. Perhaps also use the drogue sense lines
309 * to notice continutity?
311 if (ao_height <= ao_config.main_deploy)
314 ao_ignite(ao_igniter_main);
318 * Start recording min/max height
319 * to figure out when the rocket has landed
322 /* initialize interval values */
323 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
325 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
327 ao_flight_state = ao_flight_main;
328 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
332 /* fall through... */
337 * barometer: altitude stable
340 if (ao_avg_height < ao_interval_min_height)
341 ao_interval_min_height = ao_avg_height;
342 if (ao_avg_height > ao_interval_max_height)
343 ao_interval_max_height = ao_avg_height;
345 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
346 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
348 ao_flight_state = ao_flight_landed;
350 /* turn off the ADC capture */
351 ao_timer_set_adc_interval(0);
353 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
355 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
356 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
366 static inline int int_part(int16_t i) { return i >> 4; }
367 static inline int frac_part(int16_t i) { return ((i & 0xf) * 100 + 8) / 16; }
375 accel = ((ao_ground_accel - ao_sample_accel) * ao_accel_scale) >> 16;
378 printf ("sample:\n");
379 printf (" tick %d\n", ao_sample_tick);
380 printf (" raw pres %d\n", ao_sample_pres);
382 printf (" raw accel %d\n", ao_sample_accel);
384 printf (" ground pres %d\n", ao_ground_pres);
385 printf (" ground alt %d\n", ao_ground_height);
387 printf (" raw accel %d\n", ao_sample_accel);
388 printf (" groundaccel %d\n", ao_ground_accel);
389 printf (" accel_2g %d\n", ao_accel_2g);
392 printf (" alt %d\n", ao_sample_alt);
393 printf (" height %d\n", ao_sample_height);
395 printf (" accel %d.%02d\n", int_part(accel), frac_part(accel));
399 printf ("kalman:\n");
400 printf (" height %d\n", ao_height);
401 printf (" speed %d.%02d\n", int_part(ao_speed), frac_part(ao_speed));
402 printf (" accel %d.%02d\n", int_part(ao_accel), frac_part(ao_accel));
403 printf (" max_height %d\n", ao_max_height);
404 printf (" avg_height %d\n", ao_avg_height);
405 printf (" error_h %d\n", ao_error_h);
406 printf (" error_avg %d\n", ao_error_h_sq_avg);
409 __code struct ao_cmds ao_flight_cmds[] = {
410 { ao_flight_dump, "F\0Dump flight status" },
415 static __xdata struct ao_task flight_task;
420 ao_flight_state = ao_flight_startup;
422 ao_cmd_register(&ao_flight_cmds[0]);
424 ao_add_task(&flight_task, ao_flight, "flight");