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 #include <ao_flight.h>
26 #if HAS_MPU6000 || HAS_MPU9250
27 #include <ao_quaternion.h>
31 #error Please define HAS_ACCEL
35 #error Please define HAS_GPS
39 #error Please define HAS_USB
43 #include <ao_fake_flight.h>
47 #define HAS_TELEMETRY HAS_RADIO
50 /* Main flight thread. */
52 enum ao_flight_state ao_flight_state; /* current flight state */
53 uint16_t ao_boost_tick; /* time of most recent boost detect */
54 uint16_t ao_launch_tick; /* time of first boost detect */
55 uint16_t ao_motor_number; /* number of motors burned so far */
58 /* Any sensor can set this to mark the flight computer as 'broken' */
59 uint8_t ao_sensor_errors;
63 * track min/max data over a long interval to detect
66 static uint16_t ao_interval_end;
68 static ao_v_t ao_interval_min_height;
69 static ao_v_t ao_interval_max_height;
71 static accel_t ao_interval_min_accel_along, ao_interval_max_accel_along;
72 static accel_t ao_interval_min_accel_across, ao_interval_max_accel_across;
73 static accel_t ao_interval_min_accel_through, ao_interval_max_accel_through;
76 static ao_v_t ao_coast_avg_accel;
79 #define init_bounds(_cur, _min, _max) do { \
83 #define check_bounds(_cur, _min, _max) do { \
90 uint8_t ao_flight_force_idle;
92 /* We also have a clock, which can be used to sanity check things in
93 * case of other failures
96 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
98 /* Landing is detected by getting constant readings from both pressure and accelerometer
99 * for a fairly long time (AO_INTERVAL_TICKS)
101 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(10)
103 #define abs(a) ((a) < 0 ? -(a) : (a))
105 static bool accel_plus_g_failed;
106 static bool accel_minus_g_failed;
107 static bool accel_plus_failed;
108 static bool accel_minus_failed;
110 static char *btos(bool x) { return x? "true" : "false"; }
116 ao_flight_state = ao_flight_startup;
120 * Process ADC samples, just looping
121 * until the sensors are calibrated.
126 switch (ao_flight_state) {
127 case ao_flight_startup:
129 /* Check to see what mode we should go to.
130 * - Invalid mode if accel cal appears to be out
131 * - pad mode if we're upright,
132 * - idle mode otherwise
135 if (ao_config.accel_plus_g == 0 ||
136 ao_config.accel_minus_g == 0 ||
137 ao_ground_accel < (accel_t) ao_config.accel_plus_g - ACCEL_NOSE_UP ||
138 ao_ground_accel > (accel_t) ao_config.accel_minus_g + ACCEL_NOSE_UP
140 || ao_ground_height < -1000 ||
141 ao_ground_height > 7000
145 if (ao_config.accel_plus_g == 0)
146 accel_plus_g_failed = true;
147 if (ao_config.accel_minus_g == 0)
148 accel_minus_g_failed = true;
149 if (ao_ground_accel < (accel_t) ao_config.accel_plus_g - ACCEL_NOSE_UP)
150 accel_plus_failed = true;
151 if (ao_ground_accel > (accel_t) ao_config.accel_minus_g + ACCEL_NOSE_UP)
152 accel_minus_failed = true;
153 /* Detected an accel value outside -1.5g to 1.5g
154 * (or uncalibrated values), so we go into invalid mode
156 ao_flight_state = ao_flight_invalid;
158 #if HAS_RADIO && PACKET_HAS_SLAVE
159 /* Turn on packet system in invalid mode on TeleMetrum */
160 ao_packet_slave_start();
164 if (!ao_flight_force_idle
166 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
170 /* Set pad mode - we can fly! */
171 ao_flight_state = ao_flight_pad;
172 #if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE && !DEBUG
173 /* Disable the USB controller in flight mode
177 if (!ao_fake_flight_active)
182 #if !HAS_ACCEL && PACKET_HAS_SLAVE
183 /* Disable packet mode in pad state on TeleMini */
184 ao_packet_slave_stop();
188 /* Turn on telemetry system */
190 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
193 /* signal successful initialization by turning off the LED */
194 ao_led_off(AO_LED_RED);
198 ao_flight_state = ao_flight_idle;
199 #if HAS_SENSOR_ERRORS
200 if (ao_sensor_errors)
201 ao_flight_state = ao_flight_invalid;
204 #if HAS_ACCEL && HAS_RADIO && PACKET_HAS_SLAVE
205 /* Turn on packet system in idle mode on TeleMetrum */
206 ao_packet_slave_start();
210 /* signal successful initialization by turning off the LED */
211 ao_led_off(AO_LED_RED);
214 /* wakeup threads due to state change */
215 ao_wakeup(&ao_flight_state);
219 case ao_flight_invalid:
220 printf("+g? %s -g? %s +? %s -? %s +g %d -g %d ga %d +g-NU %d -g+NU %d\n",
221 btos(accel_plus_g_failed),
222 btos(accel_minus_g_failed),
223 btos(accel_plus_failed),
224 btos(accel_minus_failed),
225 ao_config.accel_plus_g,
226 ao_config.accel_minus_g,
228 ao_config.accel_plus_g - ACCEL_NOSE_UP,
229 ao_config.accel_minus_g + ACCEL_NOSE_UP);
233 printf("+g %d ga %d sa %d accel %ld speed %ld\n", ao_config.accel_plus_g, ao_ground_accel, ao_sample_accel, ao_accel, ao_speed);
240 * barometer: > 20m vertical motion
242 * accelerometer: > 2g AND velocity > 5m/s
244 * The accelerometer should always detect motion before
245 * the barometer, but we use both to make sure this
246 * transition is detected. If the device
247 * doesn't have an accelerometer, then ignore the
248 * speed and acceleration as they are quite noisy
251 if (ao_height > AO_M_TO_HEIGHT(20)
253 || (ao_accel > AO_MSS_TO_ACCEL(20)
254 && ao_speed > AO_MS_TO_SPEED(5))
258 ao_flight_state = ao_flight_boost;
259 ao_wakeup(&ao_flight_state);
261 ao_launch_tick = ao_boost_tick = ao_sample_tick;
263 /* start logging data */
269 /* Increase telemetry rate */
270 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
272 /* disable RDF beacon */
277 /* Record current GPS position by waking up GPS log tasks */
278 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
279 ao_wakeup(&ao_gps_new);
283 case ao_flight_boost:
287 * accelerometer: start to fall at > 1/4 G
289 * time: boost for more than 15 seconds
291 * Detects motor burn out by the switch from acceleration to
292 * deceleration, or by waiting until the maximum burn duration
293 * (15 seconds) has past.
295 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5)) ||
296 (int16_t) (ao_sample_tick - ao_boost_tick) > BOOST_TICKS_MAX)
300 ao_flight_state = ao_flight_fast;
302 ao_flight_state = ao_flight_coast;
304 /* Initialize landing detection interval values */
305 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
307 init_bounds(ao_sample_accel_along, ao_interval_min_accel_along, ao_interval_max_accel_along);
308 init_bounds(ao_sample_accel_across, ao_interval_min_accel_across, ao_interval_max_accel_across);
309 init_bounds(ao_sample_accel_through, ao_interval_min_accel_through, ao_interval_max_accel_through);
311 ao_coast_avg_accel = ao_accel;
313 ao_flight_state = ao_flight_coast;
315 ao_wakeup(&ao_flight_state);
319 #if HAS_ACCEL && HAS_BARO
322 * This is essentially the same as coast,
323 * but the barometer is being ignored as
324 * it may be unreliable.
326 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
328 ao_flight_state = ao_flight_coast;
329 ao_wakeup(&ao_flight_state);
334 case ao_flight_coast:
338 * By customer request - allow the user
339 * to lock out apogee detection for a specified
342 if (ao_config.apogee_lockout) {
343 if ((int16_t) (ao_sample_tick - ao_launch_tick) <
344 AO_SEC_TO_TICKS(ao_config.apogee_lockout))
348 /* apogee detect: coast to drogue deploy:
352 * Also make sure the model altitude is tracking
353 * the measured altitude reasonably closely; otherwise
354 * we're probably transsonic.
356 #define AO_ERROR_BOUND 100
360 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < AO_ERROR_BOUND)
364 /* enter drogue state */
365 ao_flight_state = ao_flight_drogue;
366 ao_wakeup(&ao_flight_state);
368 /* slow down the telemetry system */
369 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
371 /* Turn the RDF beacon back on */
376 #else /* not HAS_BARO */
379 * accel: values stable
381 check_bounds(ao_sample_accel_along, ao_interval_min_accel_along, ao_interval_max_accel_along);
382 check_bounds(ao_sample_accel_across, ao_interval_min_accel_across, ao_interval_max_accel_across);
383 check_bounds(ao_sample_accel_through, ao_interval_min_accel_through, ao_interval_max_accel_through);
385 #define MAX_QUIET_ACCEL 2
387 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
388 if (ao_interval_max_accel_along - ao_interval_min_accel_along <= ao_data_accel_to_sample(MAX_QUIET_ACCEL) &&
389 ao_interval_max_accel_across - ao_interval_min_accel_across <= ao_data_accel_to_sample(MAX_QUIET_ACCEL) &&
390 ao_interval_max_accel_through - ao_interval_min_accel_through <= ao_data_accel_to_sample(MAX_QUIET_ACCEL))
392 ao_flight_state = ao_flight_landed;
393 ao_wakeup(&ao_flight_state);
395 /* turn off the ADC capture */
396 ao_timer_set_adc_interval(0);
400 /* Reset interval values */
401 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
403 init_bounds(ao_sample_accel_along, ao_interval_min_accel_along, ao_interval_max_accel_along);
404 init_bounds(ao_sample_accel_across, ao_interval_min_accel_across, ao_interval_max_accel_across);
405 init_bounds(ao_sample_accel_through, ao_interval_min_accel_through, ao_interval_max_accel_through);
413 ao_coast_avg_accel = ao_coast_avg_accel + ((ao_accel - ao_coast_avg_accel) >> 5);
414 if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
415 ao_boost_tick = ao_sample_tick;
416 ao_flight_state = ao_flight_boost;
417 ao_wakeup(&ao_flight_state);
424 case ao_flight_drogue:
426 /* drogue to main deploy:
428 * barometer: reach main deploy altitude
430 * Would like to use the accelerometer for this test, but
431 * the orientation of the flight computer is unknown after
432 * drogue deploy, so we ignore it. Could also detect
433 * high descent rate using the pressure sensor to
434 * recognize drogue deploy failure and eject the main
435 * at that point. Perhaps also use the drogue sense lines
436 * to notice continutity?
438 if (ao_height <= ao_config.main_deploy)
440 ao_flight_state = ao_flight_main;
441 ao_wakeup(&ao_flight_state);
444 * Start recording min/max height
445 * to figure out when the rocket has landed
448 /* initialize interval values */
449 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
451 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
455 /* fall through... */
460 * barometer: altitude stable
462 if (ao_avg_height < ao_interval_min_height)
463 ao_interval_min_height = ao_avg_height;
464 if (ao_avg_height > ao_interval_max_height)
465 ao_interval_max_height = ao_avg_height;
467 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
468 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
470 ao_flight_state = ao_flight_landed;
471 ao_wakeup(&ao_flight_state);
473 /* turn off the ADC capture */
474 ao_timer_set_adc_interval(0);
477 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
478 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
481 #endif /* HAS_BARO */
485 printf ("angle %4d pitch %7d yaw %7d roll %7d\n",
487 ((ao_sample_pitch << 9) - ao_ground_pitch) >> 9,
488 ((ao_sample_yaw << 9) - ao_ground_yaw) >> 9,
489 ((ao_sample_roll << 9) - ao_ground_roll) >> 9);
493 #endif /* HAS_FLIGHT_DEBUG */
501 static inline int int_part(ao_v_t i) { return i >> 4; }
502 static inline int frac_part(ao_v_t i) { return ((i & 0xf) * 100 + 8) / 16; }
510 accel = ((ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale) >> 16;
513 printf ("sample:\n");
514 printf (" tick %d\n", ao_sample_tick);
516 printf (" raw pres %d\n", ao_sample_pres);
519 printf (" raw accel %d\n", ao_sample_accel);
522 printf (" ground pres %d\n", ao_ground_pres);
523 printf (" ground alt %d\n", ao_ground_height);
526 printf (" raw accel %d\n", ao_sample_accel);
527 printf (" groundaccel %d\n", ao_ground_accel);
528 printf (" accel_2g %d\n", ao_accel_2g);
532 printf (" alt %d\n", ao_sample_alt);
533 printf (" height %d\n", ao_sample_height);
537 printf (" accel %d.%02d\n", int_part(accel), frac_part(accel));
541 printf ("kalman:\n");
542 printf (" height %d\n", ao_height);
543 printf (" speed %d.%02d\n", int_part(ao_speed), frac_part(ao_speed));
544 printf (" accel %d.%02d\n", int_part(ao_accel), frac_part(ao_accel));
545 printf (" max_height %d\n", ao_max_height);
546 printf (" avg_height %d\n", ao_avg_height);
547 printf (" error_h %d\n", ao_error_h);
549 printf (" error_avg %d\n", ao_error_h_sq_avg);
556 ao_flight_state = ao_flight_test;
558 ao_flight_state = ao_flight_idle;
561 uint8_t ao_orient_test;
564 ao_orient_test_select(void)
566 ao_orient_test = !ao_orient_test;
569 const struct ao_cmds ao_flight_cmds[] = {
570 { ao_flight_dump, "F\0Dump flight status" },
571 { ao_gyro_test, "G\0Test gyro code" },
572 { ao_orient_test_select,"O\0Test orientation code" },
577 static struct ao_task flight_task;
582 ao_flight_state = ao_flight_startup;
584 ao_cmd_register(&ao_flight_cmds[0]);
586 ao_add_task(&flight_task, ao_flight, "flight");