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))
109 ao_flight_state = ao_flight_startup;
113 * Process ADC samples, just looping
114 * until the sensors are calibrated.
119 switch (ao_flight_state) {
120 case ao_flight_startup:
122 /* Check to see what mode we should go to.
123 * - Invalid mode if accel cal appears to be out
124 * - pad mode if we're upright,
125 * - idle mode otherwise
128 if (ao_config.accel_plus_g == 0 ||
129 ao_config.accel_minus_g == 0 ||
130 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
131 ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP
133 || ao_ground_height < -1000 ||
134 ao_ground_height > 7000
138 /* Detected an accel value outside -1.5g to 1.5g
139 * (or uncalibrated values), so we go into invalid mode
141 ao_flight_state = ao_flight_invalid;
143 #if HAS_RADIO && PACKET_HAS_SLAVE
144 /* Turn on packet system in invalid mode on TeleMetrum */
145 ao_packet_slave_start();
149 if (!ao_flight_force_idle
151 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
155 /* Set pad mode - we can fly! */
156 ao_flight_state = ao_flight_pad;
157 #if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE && !DEBUG
158 /* Disable the USB controller in flight mode
162 if (!ao_fake_flight_active)
167 #if !HAS_ACCEL && PACKET_HAS_SLAVE
168 /* Disable packet mode in pad state on TeleMini */
169 ao_packet_slave_stop();
173 /* Turn on telemetry system */
175 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
178 /* signal successful initialization by turning off the LED */
179 ao_led_off(AO_LED_RED);
183 ao_flight_state = ao_flight_idle;
184 #if HAS_SENSOR_ERRORS
185 if (ao_sensor_errors)
186 ao_flight_state = ao_flight_invalid;
189 #if HAS_ACCEL && HAS_RADIO && PACKET_HAS_SLAVE
190 /* Turn on packet system in idle mode on TeleMetrum */
191 ao_packet_slave_start();
195 /* signal successful initialization by turning off the LED */
196 ao_led_off(AO_LED_RED);
199 /* wakeup threads due to state change */
200 ao_wakeup(&ao_flight_state);
205 printf("accel %ld speed %ld\n", ao_accel, ao_speed);
211 * barometer: > 20m vertical motion
213 * accelerometer: > 2g AND velocity > 5m/s
215 * The accelerometer should always detect motion before
216 * the barometer, but we use both to make sure this
217 * transition is detected. If the device
218 * doesn't have an accelerometer, then ignore the
219 * speed and acceleration as they are quite noisy
222 if (ao_height > AO_M_TO_HEIGHT(20)
224 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
225 ao_speed > AO_MS_TO_SPEED(5))
229 ao_flight_state = ao_flight_boost;
230 ao_wakeup(&ao_flight_state);
232 ao_launch_tick = ao_boost_tick = ao_sample_tick;
234 /* start logging data */
240 /* Increase telemetry rate */
241 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
243 /* disable RDF beacon */
248 /* Record current GPS position by waking up GPS log tasks */
249 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
250 ao_wakeup(&ao_gps_new);
254 case ao_flight_boost:
258 * accelerometer: start to fall at > 1/4 G
260 * time: boost for more than 15 seconds
262 * Detects motor burn out by the switch from acceleration to
263 * deceleration, or by waiting until the maximum burn duration
264 * (15 seconds) has past.
266 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5)) ||
267 (int16_t) (ao_sample_tick - ao_boost_tick) > BOOST_TICKS_MAX)
271 ao_flight_state = ao_flight_fast;
273 ao_flight_state = ao_flight_coast;
275 /* Initialize landing detection interval values */
276 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
278 init_bounds(ao_sample_accel_along, ao_interval_min_accel_along, ao_interval_max_accel_along);
279 init_bounds(ao_sample_accel_across, ao_interval_min_accel_across, ao_interval_max_accel_across);
280 init_bounds(ao_sample_accel_through, ao_interval_min_accel_through, ao_interval_max_accel_through);
282 ao_coast_avg_accel = ao_accel;
284 ao_flight_state = ao_flight_coast;
286 ao_wakeup(&ao_flight_state);
290 #if HAS_ACCEL && HAS_BARO
293 * This is essentially the same as coast,
294 * but the barometer is being ignored as
295 * it may be unreliable.
297 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
299 ao_flight_state = ao_flight_coast;
300 ao_wakeup(&ao_flight_state);
305 case ao_flight_coast:
309 * By customer request - allow the user
310 * to lock out apogee detection for a specified
313 if (ao_config.apogee_lockout) {
314 if ((int16_t) (ao_sample_tick - ao_launch_tick) <
315 AO_SEC_TO_TICKS(ao_config.apogee_lockout))
319 /* apogee detect: coast to drogue deploy:
323 * Also make sure the model altitude is tracking
324 * the measured altitude reasonably closely; otherwise
325 * we're probably transsonic.
327 #define AO_ERROR_BOUND 100
331 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < AO_ERROR_BOUND)
335 /* enter drogue state */
336 ao_flight_state = ao_flight_drogue;
337 ao_wakeup(&ao_flight_state);
339 /* slow down the telemetry system */
340 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
342 /* Turn the RDF beacon back on */
347 #else /* not HAS_BARO */
350 * accel: values stable
352 check_bounds(ao_sample_accel_along, ao_interval_min_accel_along, ao_interval_max_accel_along);
353 check_bounds(ao_sample_accel_across, ao_interval_min_accel_across, ao_interval_max_accel_across);
354 check_bounds(ao_sample_accel_through, ao_interval_min_accel_through, ao_interval_max_accel_through);
356 #define MAX_QUIET_ACCEL 2
358 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
359 if (ao_interval_max_accel_along - ao_interval_min_accel_along <= ao_data_accel_to_sample(MAX_QUIET_ACCEL) &&
360 ao_interval_max_accel_across - ao_interval_min_accel_across <= ao_data_accel_to_sample(MAX_QUIET_ACCEL) &&
361 ao_interval_max_accel_through - ao_interval_min_accel_through <= ao_data_accel_to_sample(MAX_QUIET_ACCEL))
363 ao_flight_state = ao_flight_landed;
364 ao_wakeup(&ao_flight_state);
366 /* turn off the ADC capture */
367 ao_timer_set_adc_interval(0);
371 /* Reset interval values */
372 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
374 init_bounds(ao_sample_accel_along, ao_interval_min_accel_along, ao_interval_max_accel_along);
375 init_bounds(ao_sample_accel_across, ao_interval_min_accel_across, ao_interval_max_accel_across);
376 init_bounds(ao_sample_accel_through, ao_interval_min_accel_through, ao_interval_max_accel_through);
384 ao_coast_avg_accel = ao_coast_avg_accel + ((ao_accel - ao_coast_avg_accel) >> 5);
385 if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
386 ao_boost_tick = ao_sample_tick;
387 ao_flight_state = ao_flight_boost;
388 ao_wakeup(&ao_flight_state);
395 case ao_flight_drogue:
397 /* drogue to main deploy:
399 * barometer: reach main deploy altitude
401 * Would like to use the accelerometer for this test, but
402 * the orientation of the flight computer is unknown after
403 * drogue deploy, so we ignore it. Could also detect
404 * high descent rate using the pressure sensor to
405 * recognize drogue deploy failure and eject the main
406 * at that point. Perhaps also use the drogue sense lines
407 * to notice continutity?
409 if (ao_height <= ao_config.main_deploy)
411 ao_flight_state = ao_flight_main;
412 ao_wakeup(&ao_flight_state);
415 * Start recording min/max height
416 * to figure out when the rocket has landed
419 /* initialize interval values */
420 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
422 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
426 /* fall through... */
431 * barometer: altitude stable
433 if (ao_avg_height < ao_interval_min_height)
434 ao_interval_min_height = ao_avg_height;
435 if (ao_avg_height > ao_interval_max_height)
436 ao_interval_max_height = ao_avg_height;
438 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
439 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
441 ao_flight_state = ao_flight_landed;
442 ao_wakeup(&ao_flight_state);
444 /* turn off the ADC capture */
445 ao_timer_set_adc_interval(0);
448 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
449 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
452 #endif /* HAS_BARO */
456 printf ("angle %4d pitch %7d yaw %7d roll %7d\n",
458 ((ao_sample_pitch << 9) - ao_ground_pitch) >> 9,
459 ((ao_sample_yaw << 9) - ao_ground_yaw) >> 9,
460 ((ao_sample_roll << 9) - ao_ground_roll) >> 9);
464 #endif /* HAS_FLIGHT_DEBUG */
472 static inline int int_part(ao_v_t i) { return i >> 4; }
473 static inline int frac_part(ao_v_t i) { return ((i & 0xf) * 100 + 8) / 16; }
481 accel = ((ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale) >> 16;
484 printf ("sample:\n");
485 printf (" tick %d\n", ao_sample_tick);
487 printf (" raw pres %d\n", ao_sample_pres);
490 printf (" raw accel %d\n", ao_sample_accel);
493 printf (" ground pres %d\n", ao_ground_pres);
494 printf (" ground alt %d\n", ao_ground_height);
497 printf (" raw accel %d\n", ao_sample_accel);
498 printf (" groundaccel %d\n", ao_ground_accel);
499 printf (" accel_2g %d\n", ao_accel_2g);
503 printf (" alt %d\n", ao_sample_alt);
504 printf (" height %d\n", ao_sample_height);
508 printf (" accel %d.%02d\n", int_part(accel), frac_part(accel));
512 printf ("kalman:\n");
513 printf (" height %d\n", ao_height);
514 printf (" speed %d.%02d\n", int_part(ao_speed), frac_part(ao_speed));
515 printf (" accel %d.%02d\n", int_part(ao_accel), frac_part(ao_accel));
516 printf (" max_height %d\n", ao_max_height);
517 printf (" avg_height %d\n", ao_avg_height);
518 printf (" error_h %d\n", ao_error_h);
520 printf (" error_avg %d\n", ao_error_h_sq_avg);
527 ao_flight_state = ao_flight_test;
529 ao_flight_state = ao_flight_idle;
532 uint8_t ao_orient_test;
535 ao_orient_test_select(void)
537 ao_orient_test = !ao_orient_test;
540 const struct ao_cmds ao_flight_cmds[] = {
541 { ao_flight_dump, "F\0Dump flight status" },
542 { ao_gyro_test, "G\0Test gyro code" },
543 { ao_orient_test_select,"O\0Test orientation code" },
548 static struct ao_task flight_task;
553 ao_flight_state = ao_flight_startup;
555 ao_cmd_register(&ao_flight_cmds[0]);
557 ao_add_task(&flight_task, ao_flight, "flight");