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;
67 static ao_v_t ao_interval_min_height;
68 static ao_v_t ao_interval_max_height;
70 static ao_v_t ao_coast_avg_accel;
73 uint8_t ao_flight_force_idle;
75 /* We also have a clock, which can be used to sanity check things in
76 * case of other failures
79 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
81 /* Landing is detected by getting constant readings from both pressure and accelerometer
82 * for a fairly long time (AO_INTERVAL_TICKS)
84 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(10)
86 #define abs(a) ((a) < 0 ? -(a) : (a))
92 ao_flight_state = ao_flight_startup;
96 * Process ADC samples, just looping
97 * until the sensors are calibrated.
102 switch (ao_flight_state) {
103 case ao_flight_startup:
105 /* Check to see what mode we should go to.
106 * - Invalid mode if accel cal appears to be out
107 * - pad mode if we're upright,
108 * - idle mode otherwise
111 if (ao_config.accel_plus_g == 0 ||
112 ao_config.accel_minus_g == 0 ||
113 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP
115 || ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP
116 || ao_ground_height < -1000 || ao_ground_height > 7000
120 /* Detected an accel value outside -1.5g to 1.5g
121 * (or uncalibrated values), so we go into invalid mode
123 ao_flight_state = ao_flight_invalid;
125 #if HAS_RADIO && PACKET_HAS_SLAVE
126 /* Turn on packet system in invalid mode on TeleMetrum */
127 ao_packet_slave_start();
131 if (!ao_flight_force_idle
133 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
137 /* Set pad mode - we can fly! */
138 ao_flight_state = ao_flight_pad;
139 #if HAS_USB && !HAS_FLIGHT_DEBUG && !HAS_SAMPLE_PROFILE && !DEBUG
140 /* Disable the USB controller in flight mode
144 if (!ao_fake_flight_active)
149 #if !HAS_ACCEL && PACKET_HAS_SLAVE
150 /* Disable packet mode in pad state on TeleMini */
151 ao_packet_slave_stop();
155 /* Turn on telemetry system */
157 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
160 /* signal successful initialization by turning off the LED */
161 ao_led_off(AO_LED_RED);
165 ao_flight_state = ao_flight_idle;
166 #if HAS_SENSOR_ERRORS
167 if (ao_sensor_errors)
168 ao_flight_state = ao_flight_invalid;
171 #if HAS_ACCEL && HAS_RADIO && PACKET_HAS_SLAVE
172 /* Turn on packet system in idle mode on TeleMetrum */
173 ao_packet_slave_start();
177 /* signal successful initialization by turning off the LED */
178 ao_led_off(AO_LED_RED);
181 /* wakeup threads due to state change */
182 ao_wakeup(&ao_flight_state);
188 * barometer: > 20m vertical motion
190 * accelerometer: > 2g AND velocity > 5m/s
192 * The accelerometer should always detect motion before
193 * the barometer, but we use both to make sure this
194 * transition is detected. If the device
195 * doesn't have an accelerometer, then ignore the
196 * speed and acceleration as they are quite noisy
199 if (ao_height > AO_M_TO_HEIGHT(20)
201 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
202 ao_speed > AO_MS_TO_SPEED(5))
206 ao_flight_state = ao_flight_boost;
207 ao_launch_tick = ao_boost_tick = ao_sample_tick;
209 /* start logging data */
215 /* Increase telemetry rate */
216 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
218 /* disable RDF beacon */
223 /* Record current GPS position by waking up GPS log tasks */
224 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
225 ao_wakeup(&ao_gps_new);
228 ao_wakeup(&ao_flight_state);
231 case ao_flight_boost:
235 * accelerometer: start to fall at > 1/4 G
237 * time: boost for more than 15 seconds
239 * Detects motor burn out by the switch from acceleration to
240 * deceleration, or by waiting until the maximum burn duration
241 * (15 seconds) has past.
243 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5)) ||
244 (int16_t) (ao_sample_tick - ao_boost_tick) > BOOST_TICKS_MAX)
247 ao_flight_state = ao_flight_fast;
248 ao_coast_avg_accel = ao_accel;
250 ao_flight_state = ao_flight_coast;
253 ao_wakeup(&ao_flight_state);
259 * This is essentially the same as coast,
260 * but the barometer is being ignored as
261 * it may be unreliable.
263 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
265 ao_flight_state = ao_flight_coast;
266 ao_wakeup(&ao_flight_state);
271 case ao_flight_coast:
274 * By customer request - allow the user
275 * to lock out apogee detection for a specified
278 if (ao_config.apogee_lockout) {
279 if ((int16_t) (ao_sample_tick - ao_launch_tick) <
280 AO_SEC_TO_TICKS(ao_config.apogee_lockout))
284 /* apogee detect: coast to drogue deploy:
288 * Also make sure the model altitude is tracking
289 * the measured altitude reasonably closely; otherwise
290 * we're probably transsonic.
292 #define AO_ERROR_BOUND 100
296 && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < AO_ERROR_BOUND)
301 /* ignite the drogue charge */
302 ao_ignite(ao_igniter_drogue);
306 /* slow down the telemetry system */
307 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
309 /* Turn the RDF beacon back on */
313 /* and enter drogue state */
314 ao_flight_state = ao_flight_drogue;
315 ao_wakeup(&ao_flight_state);
320 ao_coast_avg_accel = ao_coast_avg_accel + ((ao_accel - ao_coast_avg_accel) >> 5);
321 if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
322 ao_boost_tick = ao_sample_tick;
323 ao_flight_state = ao_flight_boost;
324 ao_wakeup(&ao_flight_state);
330 case ao_flight_drogue:
332 /* drogue to main deploy:
334 * barometer: reach main deploy altitude
336 * Would like to use the accelerometer for this test, but
337 * the orientation of the flight computer is unknown after
338 * drogue deploy, so we ignore it. Could also detect
339 * high descent rate using the pressure sensor to
340 * recognize drogue deploy failure and eject the main
341 * at that point. Perhaps also use the drogue sense lines
342 * to notice continutity?
344 if (ao_height <= ao_config.main_deploy)
347 ao_ignite(ao_igniter_main);
351 * Start recording min/max height
352 * to figure out when the rocket has landed
355 /* initialize interval values */
356 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
358 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
360 ao_flight_state = ao_flight_main;
361 ao_wakeup(&ao_flight_state);
365 /* fall through... */
370 * barometer: altitude stable
373 if (ao_avg_height < ao_interval_min_height)
374 ao_interval_min_height = ao_avg_height;
375 if (ao_avg_height > ao_interval_max_height)
376 ao_interval_max_height = ao_avg_height;
378 if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
379 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
381 ao_flight_state = ao_flight_landed;
384 /* turn off the ADC capture */
385 ao_timer_set_adc_interval(0);
388 ao_wakeup(&ao_flight_state);
390 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
391 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
397 printf ("angle %4d pitch %7d yaw %7d roll %7d\n",
399 ((ao_sample_pitch << 9) - ao_ground_pitch) >> 9,
400 ((ao_sample_yaw << 9) - ao_ground_yaw) >> 9,
401 ((ao_sample_roll << 9) - ao_ground_roll) >> 9);
405 #endif /* HAS_FLIGHT_DEBUG */
413 static inline int int_part(ao_v_t i) { return i >> 4; }
414 static inline int frac_part(ao_v_t i) { return ((i & 0xf) * 100 + 8) / 16; }
422 accel = ((ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale) >> 16;
425 printf ("sample:\n");
426 printf (" tick %d\n", ao_sample_tick);
427 printf (" raw pres %d\n", ao_sample_pres);
429 printf (" raw accel %d\n", ao_sample_accel);
431 printf (" ground pres %d\n", ao_ground_pres);
432 printf (" ground alt %d\n", ao_ground_height);
434 printf (" raw accel %d\n", ao_sample_accel);
435 printf (" groundaccel %d\n", ao_ground_accel);
436 printf (" accel_2g %d\n", ao_accel_2g);
439 printf (" alt %d\n", ao_sample_alt);
440 printf (" height %d\n", ao_sample_height);
442 printf (" accel %d.%02d\n", int_part(accel), frac_part(accel));
446 printf ("kalman:\n");
447 printf (" height %d\n", ao_height);
448 printf (" speed %d.%02d\n", int_part(ao_speed), frac_part(ao_speed));
449 printf (" accel %d.%02d\n", int_part(ao_accel), frac_part(ao_accel));
450 printf (" max_height %d\n", ao_max_height);
451 printf (" avg_height %d\n", ao_avg_height);
452 printf (" error_h %d\n", ao_error_h);
454 printf (" error_avg %d\n", ao_error_h_sq_avg);
461 ao_flight_state = ao_flight_test;
463 ao_flight_state = ao_flight_idle;
466 uint8_t ao_orient_test;
469 ao_orient_test_select(void)
471 ao_orient_test = !ao_orient_test;
474 const struct ao_cmds ao_flight_cmds[] = {
475 { ao_flight_dump, "F\0Dump flight status" },
476 { ao_gyro_test, "G\0Test gyro code" },
477 { ao_orient_test_select,"O\0Test orientation code" },
482 static struct ao_task flight_task;
487 ao_flight_state = ao_flight_startup;
489 ao_cmd_register(&ao_flight_cmds[0]);
491 ao_add_task(&flight_task, ao_flight, "flight");