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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #ifndef AO_FLIGHT_TEST
24 #if HAS_MPU6000 || HAS_MPU9250
25 #include <ao_quaternion.h>
29 #error Please define HAS_ACCEL
33 #error Please define HAS_GPS
37 #error Please define HAS_USB
41 #include <ao_fake_flight.h>
45 #define HAS_TELEMETRY HAS_RADIO
48 /* Main flight thread. */
50 enum ao_flight_state ao_flight_state; /* current flight state */
51 uint16_t ao_boost_tick; /* time of most recent boost detect */
52 uint16_t ao_launch_tick; /* time of first boost detect */
53 uint16_t ao_motor_number; /* number of motors burned so far */
56 /* Any sensor can set this to mark the flight computer as 'broken' */
57 uint8_t ao_sensor_errors;
61 * track min/max data over a long interval to detect
64 static uint16_t ao_interval_end;
65 static ao_v_t ao_interval_min_height;
66 static ao_v_t ao_interval_max_height;
68 static ao_v_t ao_coast_avg_accel;
71 uint8_t ao_flight_force_idle;
73 /* We also have a clock, which can be used to sanity check things in
74 * case of other failures
77 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
79 /* Landing is detected by getting constant readings from both pressure and accelerometer
80 * for a fairly long time (AO_INTERVAL_TICKS)
82 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(10)
84 #define abs(a) ((a) < 0 ? -(a) : (a))
90 ao_flight_state = ao_flight_startup;
94 * Process ADC samples, just looping
95 * until the sensors are calibrated.
100 switch (ao_flight_state) {
101 case ao_flight_startup:
103 /* Check to see what mode we should go to.
104 * - Invalid mode if accel cal appears to be out
105 * - pad mode if we're upright,
106 * - idle mode otherwise
109 if (ao_config.accel_plus_g == 0 ||
110 ao_config.accel_minus_g == 0 ||
111 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP
113 || ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP
114 || ao_ground_height < -1000 || ao_ground_height > 7000
118 /* Detected an accel value outside -1.5g to 1.5g
119 * (or uncalibrated values), so we go into invalid mode
121 ao_flight_state = ao_flight_invalid;
123 #if HAS_RADIO && PACKET_HAS_SLAVE
124 /* Turn on packet system in invalid mode on TeleMetrum */
125 ao_packet_slave_start();
129 if (!ao_flight_force_idle
131 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
135 /* Set pad mode - we can fly! */
136 ao_flight_state = ao_flight_pad;
137 #if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE && !DEBUG
138 /* Disable the USB controller in flight mode
142 if (!ao_fake_flight_active)
147 #if !HAS_ACCEL && PACKET_HAS_SLAVE
148 /* Disable packet mode in pad state on TeleMini */
149 ao_packet_slave_stop();
153 /* Turn on telemetry system */
155 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
158 /* signal successful initialization by turning off the LED */
159 ao_led_off(AO_LED_RED);
163 ao_flight_state = ao_flight_idle;
164 #if HAS_SENSOR_ERRORS
165 if (ao_sensor_errors)
166 ao_flight_state = ao_flight_invalid;
169 #if HAS_ACCEL && HAS_RADIO && PACKET_HAS_SLAVE
170 /* Turn on packet system in idle mode on TeleMetrum */
171 ao_packet_slave_start();
175 /* signal successful initialization by turning off the LED */
176 ao_led_off(AO_LED_RED);
179 /* wakeup threads due to state change */
180 ao_wakeup(&ao_flight_state);
186 * barometer: > 20m vertical motion
188 * accelerometer: > 2g AND velocity > 5m/s
190 * The accelerometer should always detect motion before
191 * the barometer, but we use both to make sure this
192 * transition is detected. If the device
193 * doesn't have an accelerometer, then ignore the
194 * speed and acceleration as they are quite noisy
197 if (ao_height > AO_M_TO_HEIGHT(20)
199 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
200 ao_speed > AO_MS_TO_SPEED(5))
204 ao_flight_state = ao_flight_boost;
205 ao_launch_tick = ao_boost_tick = ao_sample_tick;
207 /* start logging data */
213 /* Increase telemetry rate */
214 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
216 /* disable RDF beacon */
221 /* Record current GPS position by waking up GPS log tasks */
222 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
223 ao_wakeup(&ao_gps_new);
226 ao_wakeup(&ao_flight_state);
229 case ao_flight_boost:
233 * accelerometer: start to fall at > 1/4 G
235 * time: boost for more than 15 seconds
237 * Detects motor burn out by the switch from acceleration to
238 * deceleration, or by waiting until the maximum burn duration
239 * (15 seconds) has past.
241 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5)) ||
242 (int16_t) (ao_sample_tick - ao_boost_tick) > BOOST_TICKS_MAX)
245 ao_flight_state = ao_flight_fast;
246 ao_coast_avg_accel = ao_accel;
248 ao_flight_state = ao_flight_coast;
251 ao_wakeup(&ao_flight_state);
257 * This is essentially the same as coast,
258 * but the barometer is being ignored as
259 * it may be unreliable.
261 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
263 ao_flight_state = ao_flight_coast;
264 ao_wakeup(&ao_flight_state);
269 case ao_flight_coast:
272 * By customer request - allow the user
273 * to lock out apogee detection for a specified
276 if (ao_config.apogee_lockout) {
277 if ((int16_t) (ao_sample_tick - ao_launch_tick) <
278 AO_SEC_TO_TICKS(ao_config.apogee_lockout))
282 /* apogee detect: coast to drogue deploy:
286 * Also make sure the model altitude is tracking
287 * the measured altitude reasonably closely; otherwise
288 * we're probably transsonic.
290 #define AO_ERROR_BOUND 100
294 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < AO_ERROR_BOUND)
299 /* ignite the drogue charge */
300 ao_ignite(ao_igniter_drogue);
304 /* slow down the telemetry system */
305 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
307 /* Turn the RDF beacon back on */
311 /* and enter drogue state */
312 ao_flight_state = ao_flight_drogue;
313 ao_wakeup(&ao_flight_state);
318 ao_coast_avg_accel = ao_coast_avg_accel + ((ao_accel - ao_coast_avg_accel) >> 5);
319 if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
320 ao_boost_tick = ao_sample_tick;
321 ao_flight_state = ao_flight_boost;
322 ao_wakeup(&ao_flight_state);
328 case ao_flight_drogue:
330 /* drogue to main deploy:
332 * barometer: reach main deploy altitude
334 * Would like to use the accelerometer for this test, but
335 * the orientation of the flight computer is unknown after
336 * drogue deploy, so we ignore it. Could also detect
337 * high descent rate using the pressure sensor to
338 * recognize drogue deploy failure and eject the main
339 * at that point. Perhaps also use the drogue sense lines
340 * to notice continutity?
342 if (ao_height <= ao_config.main_deploy)
345 ao_ignite(ao_igniter_main);
349 * Start recording min/max height
350 * to figure out when the rocket has landed
353 /* initialize interval values */
354 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
356 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
358 ao_flight_state = ao_flight_main;
359 ao_wakeup(&ao_flight_state);
363 /* fall through... */
368 * barometer: altitude stable
371 if (ao_avg_height < ao_interval_min_height)
372 ao_interval_min_height = ao_avg_height;
373 if (ao_avg_height > ao_interval_max_height)
374 ao_interval_max_height = ao_avg_height;
376 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
377 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
379 ao_flight_state = ao_flight_landed;
382 /* turn off the ADC capture */
383 ao_timer_set_adc_interval(0);
386 ao_wakeup(&ao_flight_state);
388 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
389 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
395 printf ("angle %4d pitch %7d yaw %7d roll %7d\n",
397 ((ao_sample_pitch << 9) - ao_ground_pitch) >> 9,
398 ((ao_sample_yaw << 9) - ao_ground_yaw) >> 9,
399 ((ao_sample_roll << 9) - ao_ground_roll) >> 9);
403 #endif /* HAS_FLIGHT_DEBUG */
411 static inline int int_part(ao_v_t i) { return i >> 4; }
412 static inline int frac_part(ao_v_t i) { return ((i & 0xf) * 100 + 8) / 16; }
420 accel = ((ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale) >> 16;
423 printf ("sample:\n");
424 printf (" tick %d\n", ao_sample_tick);
425 printf (" raw pres %d\n", ao_sample_pres);
427 printf (" raw accel %d\n", ao_sample_accel);
429 printf (" ground pres %d\n", ao_ground_pres);
430 printf (" ground alt %d\n", ao_ground_height);
432 printf (" raw accel %d\n", ao_sample_accel);
433 printf (" groundaccel %d\n", ao_ground_accel);
434 printf (" accel_2g %d\n", ao_accel_2g);
437 printf (" alt %d\n", ao_sample_alt);
438 printf (" height %d\n", ao_sample_height);
440 printf (" accel %d.%02d\n", int_part(accel), frac_part(accel));
444 printf ("kalman:\n");
445 printf (" height %d\n", ao_height);
446 printf (" speed %d.%02d\n", int_part(ao_speed), frac_part(ao_speed));
447 printf (" accel %d.%02d\n", int_part(ao_accel), frac_part(ao_accel));
448 printf (" max_height %d\n", ao_max_height);
449 printf (" avg_height %d\n", ao_avg_height);
450 printf (" error_h %d\n", ao_error_h);
452 printf (" error_avg %d\n", ao_error_h_sq_avg);
459 ao_flight_state = ao_flight_test;
461 ao_flight_state = ao_flight_idle;
464 uint8_t ao_orient_test;
467 ao_orient_test_select(void)
469 ao_orient_test = !ao_orient_test;
472 const struct ao_cmds ao_flight_cmds[] = {
473 { ao_flight_dump, "F\0Dump flight status" },
474 { ao_gyro_test, "G\0Test gyro code" },
475 { ao_orient_test_select,"O\0Test orientation code" },
480 static struct ao_task flight_task;
485 ao_flight_state = ao_flight_startup;
487 ao_cmd_register(&ao_flight_cmds[0]);
489 ao_add_task(&flight_task, ao_flight, "flight");