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
23 #error Please define HAS_ACCEL
27 #error Please define HAS_GPS
31 #error Please define HAS_USB
34 /* Main flight thread. */
36 __pdata enum ao_flight_state ao_flight_state; /* current flight state */
37 __pdata uint16_t ao_flight_tick; /* time of last data */
38 __pdata uint16_t ao_flight_prev_tick; /* time of previous data */
39 __xdata int16_t ao_ground_pres; /* startup pressure */
40 __pdata uint16_t ao_launch_tick; /* time of launch detect */
42 __pdata int16_t ao_ground_accel; /* startup acceleration */
46 * track min/max data over a long interval to detect
49 __pdata uint16_t ao_interval_end;
50 __pdata int16_t ao_interval_min_height;
51 __pdata int16_t ao_interval_max_height;
53 __data uint8_t ao_flight_adc;
54 __pdata int16_t ao_raw_pres;
55 __xdata uint8_t ao_flight_force_idle;
58 __pdata int16_t ao_raw_accel, ao_raw_accel_prev;
59 __pdata int16_t ao_accel_2g;
61 /* Accelerometer calibration
63 * We're sampling the accelerometer through a resistor divider which
64 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
65 * That goes into the cc1111 A/D converter, which is running at 11 bits
66 * of precision with the bits in the MSB of the 16 bit value. Only positive
67 * values are used, so values should range from 0-32752 for 0-3.3V. The
68 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
69 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
70 * for a final computation of:
72 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
74 * Zero g was measured at 16000 (we would expect 16384).
75 * Note that this value is only require to tell if the
76 * rocket is standing upright. Once that is determined,
77 * the value of the accelerometer is averaged for 100 samples
78 * to find the resting accelerometer value, which is used
79 * for all further flight computations
82 #define GRAVITY 9.80665
84 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
89 * Barometer calibration
91 * We directly sample the barometer. The specs say:
93 * Pressure range: 15-115 kPa
94 * Voltage at 115kPa: 2.82
95 * Output scale: 27mV/kPa
97 * If we want to detect launch with the barometer, we need
98 * a large enough bump to not be fooled by noise. At typical
99 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
100 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
101 * As all of our calculations are done in 16 bits, we'll actually see a change
102 * of 16 times this though
104 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
107 /* We also have a clock, which can be used to sanity check things in
108 * case of other failures
111 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
113 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
114 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
115 #define from_fix(x) ((x) >> 16)
117 #include "ao_kalman.h"
119 __pdata int16_t ao_ground_height;
120 __pdata int16_t ao_height;
121 __pdata int16_t ao_speed;
122 __pdata int16_t ao_accel;
123 __pdata int16_t ao_max_height;
125 static __pdata int32_t ao_k_height;
126 static __pdata int32_t ao_k_speed;
127 static __pdata int32_t ao_k_accel;
129 #define AO_K_STEP_100 to_fix16(0.01)
130 #define AO_K_STEP_2_2_100 to_fix16(0.00005)
132 #define AO_K_STEP_10 to_fix16(0.1)
133 #define AO_K_STEP_2_2_10 to_fix16(0.005)
136 * Above this height, the baro sensor doesn't work
138 #define AO_MAX_BARO_HEIGHT 8000
141 * Above this speed, baro measurements are unreliable
143 #define AO_MAX_BARO_SPEED 300
146 ao_kalman_predict(void)
148 #ifdef AO_FLIGHT_TEST
149 if (ao_flight_tick - ao_flight_prev_tick > 5) {
150 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_10 +
151 (int32_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
152 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_10;
157 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_100 +
158 (int32_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
159 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_100;
162 static __pdata int16_t ao_error_h;
163 static __pdata int16_t ao_raw_alt;
164 static __pdata int16_t ao_raw_height;
165 static __pdata int16_t ao_error_h_sq_avg;
168 ao_kalman_err_height(void)
171 ao_error_h = ao_raw_height - (int16_t) (ao_k_height >> 16);
178 ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
179 ao_error_h_sq_avg += (e * e) >> 4;
183 ao_kalman_correct_baro(void)
185 ao_kalman_err_height();
186 #ifdef AO_FLIGHT_TEST
187 if (ao_flight_tick - ao_flight_prev_tick > 5) {
188 ao_k_height += (int32_t) AO_BARO_K0_10 * ao_error_h;
189 ao_k_speed += (int32_t) AO_BARO_K1_10 * ao_error_h;
190 ao_k_accel += (int32_t) AO_BARO_K2_10 * ao_error_h;
194 ao_k_height += (int32_t) AO_BARO_K0_100 * ao_error_h;
195 ao_k_speed += (int32_t) AO_BARO_K1_100 * ao_error_h;
196 ao_k_accel += (int32_t) AO_BARO_K2_100 * ao_error_h;
200 static __pdata int16_t ao_error_a;
201 static __pdata int32_t ao_accel_scale;
204 ao_kalman_err_accel(void)
208 accel = (ao_ground_accel - ao_raw_accel) * ao_accel_scale;
210 /* Can't use ao_accel here as it is the pre-prediction value still */
211 ao_error_a = (accel - ao_k_accel) >> 16;
215 ao_kalman_correct_both(void)
217 ao_kalman_err_height();
218 ao_kalman_err_accel();
222 * Check to see if things are crazy here --
223 * if the computed height is far above the
224 * measured height, we assume that the flight
225 * trajectory is not vertical, and so ignore
226 * the accelerometer for the remainder of the
229 if (ao_error_h_sq_avg > 10)
231 ao_kalman_correct_baro();
236 #ifdef AO_FLIGHT_TEST
237 if (ao_flight_tick - ao_flight_prev_tick > 5) {
239 (int32_t) AO_BOTH_K00_10 * ao_error_h +
240 (int32_t) (AO_BOTH_K01_10 >> 4) * ao_error_a;
242 ((int32_t) AO_BOTH_K10_10 << 4) * ao_error_h +
243 (int32_t) AO_BOTH_K11_10 * ao_error_a;
245 ((int32_t) AO_BOTH_K20_10 << 4) * ao_error_h +
246 (int32_t) AO_BOTH_K21_10 * ao_error_a;
251 (int32_t) AO_BOTH_K00_100 * ao_error_h +
252 (int32_t) AO_BOTH_K01_100 * ao_error_a;
254 (int32_t) AO_BOTH_K10_100 * ao_error_h +
255 (int32_t) AO_BOTH_K11_100 * ao_error_a;
257 (int32_t) AO_BOTH_K20_100 * ao_error_h +
258 (int32_t) AO_BOTH_K21_100 * ao_error_a;
262 ao_kalman_correct_accel(void)
264 ao_kalman_err_accel();
266 #ifdef AO_FLIGHT_TEST
267 if (ao_flight_tick - ao_flight_prev_tick > 5) {
268 ao_k_height +=(int32_t) AO_ACCEL_K0_10 * ao_error_a;
269 ao_k_speed += (int32_t) AO_ACCEL_K1_10 * ao_error_a;
270 ao_k_accel += (int32_t) AO_ACCEL_K2_10 * ao_error_a;
274 ao_k_height += (int32_t) AO_ACCEL_K0_100 * ao_error_a;
275 ao_k_speed += (int32_t) AO_ACCEL_K1_100 * ao_error_a;
276 ao_k_accel += (int32_t) AO_ACCEL_K2_100 * ao_error_a;
278 #endif /* HAS_ACCEL */
280 __xdata int32_t ao_raw_pres_sum;
283 __xdata int32_t ao_raw_accel_sum;
286 /* Landing is detected by getting constant readings from both pressure and accelerometer
287 * for a fairly long time (AO_INTERVAL_TICKS)
289 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
291 #define abs(a) ((a) < 0 ? -(a) : (a))
296 __pdata static uint16_t nsamples = 0;
298 ao_flight_adc = ao_adc_head;
301 ao_raw_accel_prev = 0;
306 ao_wakeup(DATA_TO_XDATA(&ao_flight_adc));
307 ao_sleep(DATA_TO_XDATA(&ao_adc_head));
308 while (ao_flight_adc != ao_adc_head) {
309 __xdata struct ao_adc *ao_adc;
310 ao_flight_prev_tick = ao_flight_tick;
312 /* Capture a sample */
313 ao_adc = &ao_adc_ring[ao_flight_adc];
314 ao_flight_tick = ao_adc->tick;
315 ao_raw_pres = ao_adc->pres;
316 ao_raw_alt = ao_pres_to_altitude(ao_raw_pres);
317 ao_raw_height = ao_raw_alt - ao_ground_height;
319 ao_raw_accel = ao_adc->accel;
322 * Ok, the math here is a bit tricky.
324 * ao_raw_accel: ADC output for acceleration
325 * ao_accel_ref: ADC output for the 5V reference.
326 * ao_cook_accel: Corrected acceleration value
327 * Vcc: 3.3V supply to the CC1111
328 * Vac: 5V supply to the accelerometer
329 * accel: input voltage to accelerometer ADC pin
330 * ref: input voltage to 5V reference ADC pin
333 * Measured acceleration is ratiometric to Vcc:
336 * ------------ = -----
339 * Measured 5v reference is also ratiometric to Vcc:
342 * ------------ = -----
346 * ao_accel_ref = 32767 * (ref / Vcc)
348 * Acceleration is measured ratiometric to the 5V supply,
349 * so what we want is:
351 * ao_cook_accel accel
352 * ------------- = -----
361 * = ------------ * ------------
364 * Multiply through by 32767:
366 * ao_raw_accel * 32767
367 * ao_cook_accel = --------------------
370 * Now, the tricky part. Getting this to compile efficiently
371 * and keeping all of the values in-range.
373 * First off, we need to use a shift of 16 instead of * 32767 as SDCC
374 * does the obvious optimizations for byte-granularity shifts:
376 * ao_cook_accel = (ao_raw_accel << 16) / ao_accel_ref
378 * Next, lets check our input ranges:
380 * 0 <= ao_raw_accel <= 0x7fff (singled ended ADC conversion)
381 * 0x7000 <= ao_accel_ref <= 0x7fff (the 5V ref value is close to 0x7fff)
383 * Plugging in our input ranges, we get an output range of 0 - 0x12490,
384 * which is 17 bits. That won't work. If we take the accel ref and shift
385 * by a bit, we'll change its range:
387 * 0xe000 <= ao_accel_ref<<1 <= 0xfffe
389 * ao_cook_accel = (ao_raw_accel << 16) / (ao_accel_ref << 1)
391 * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
392 * is, however, one bit too large for our signed computations. So, we
393 * take the result and shift that by a bit:
395 * ao_cook_accel = ((ao_raw_accel << 16) / (ao_accel_ref << 1)) >> 1
397 * This finally creates an output range of 0 - 0x4924. As the ADC only
398 * provides 11 bits of data, we haven't actually lost any precision,
399 * just dropped a bit of noise off the low end.
401 ao_raw_accel = (uint16_t) ((((uint32_t) ao_raw_accel << 16) / (ao_accel_ref[ao_flight_adc] << 1))) >> 1;
402 ao_adc->accel = ao_raw_accel;
406 if (ao_flight_state > ao_flight_idle) {
409 if (ao_flight_state <= ao_flight_coast) {
411 if (/*ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED) &&*/
412 ao_raw_alt < AO_MAX_BARO_HEIGHT)
413 ao_kalman_correct_both();
416 ao_kalman_correct_accel();
419 if (ao_raw_alt < AO_MAX_BARO_HEIGHT || ao_flight_state >= ao_flight_drogue)
420 ao_kalman_correct_baro();
421 ao_height = from_fix(ao_k_height);
422 ao_speed = from_fix(ao_k_speed);
423 ao_accel = from_fix(ao_k_accel);
424 if (ao_height > ao_max_height)
425 ao_max_height = ao_height;
427 ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
430 switch (ao_flight_state) {
431 case ao_flight_startup:
435 * Collect 512 samples of acceleration and pressure
436 * data and average them to find the resting values
438 if (nsamples < 512) {
440 ao_raw_accel_sum += ao_raw_accel;
442 ao_raw_pres_sum += ao_raw_pres;
448 ao_ground_accel = ao_raw_accel_sum >> 9;
449 ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
450 ao_accel_scale = to_fix32(GRAVITY * 2 * 16) / ao_accel_2g;
452 ao_ground_pres = ao_raw_pres_sum >> 9;
453 ao_ground_height = ao_pres_to_altitude(ao_ground_pres);
455 /* Check to see what mode we should go to.
456 * - Invalid mode if accel cal appears to be out
457 * - pad mode if we're upright,
458 * - idle mode otherwise
461 if (ao_config.accel_plus_g == 0 ||
462 ao_config.accel_minus_g == 0 ||
463 ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
464 ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
466 /* Detected an accel value outside -1.5g to 1.5g
467 * (or uncalibrated values), so we go into invalid mode
469 ao_flight_state = ao_flight_invalid;
473 if (!ao_flight_force_idle
475 && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
479 /* Set pad mode - we can fly! */
480 ao_flight_state = ao_flight_pad;
482 /* Disable the USB controller in flight mode
488 /* Disable packet mode in pad state */
489 ao_packet_slave_stop();
491 /* Turn on telemetry system */
493 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
495 /* signal successful initialization by turning off the LED */
496 ao_led_off(AO_LED_RED);
499 ao_flight_state = ao_flight_idle;
501 /* signal successful initialization by turning off the LED */
502 ao_led_off(AO_LED_RED);
504 /* wakeup threads due to state change */
505 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
512 * barometer: > 20m vertical motion
514 * accelerometer: > 2g AND velocity > 5m/s
516 * The accelerometer should always detect motion before
517 * the barometer, but we use both to make sure this
518 * transition is detected. If the device
519 * doesn't have an accelerometer, then ignore the
520 * speed and acceleration as they are quite noisy
523 if (ao_height > AO_M_TO_HEIGHT(20)
525 || (ao_accel > AO_MSS_TO_ACCEL(20) &&
526 ao_speed > AO_MS_TO_SPEED(5))
530 ao_flight_state = ao_flight_boost;
531 ao_launch_tick = ao_flight_tick;
533 /* start logging data */
536 /* Increase telemetry rate */
537 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
539 /* disable RDF beacon */
543 /* Record current GPS position by waking up GPS log tasks */
544 ao_wakeup(&ao_gps_data);
545 ao_wakeup(&ao_gps_tracking_data);
548 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
552 case ao_flight_boost:
556 * accelerometer: start to fall at > 1/4 G
558 * time: boost for more than 15 seconds
560 * Detects motor burn out by the switch from acceleration to
561 * deceleration, or by waiting until the maximum burn duration
562 * (15 seconds) has past.
564 if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
565 (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
567 ao_flight_state = ao_flight_fast;
568 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
574 * This is essentially the same as coast,
575 * but the barometer is being ignored as
576 * it may be unreliable.
578 if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) {
579 ao_flight_state = ao_flight_coast;
580 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
584 case ao_flight_coast:
586 /* apogee detect: coast to drogue deploy:
590 * Also make sure the model altitude is tracking
591 * the measured altitude reasonably closely; otherwise
592 * we're probably transsonic.
594 if (ao_speed < 0 && (ao_raw_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100))
596 /* ignite the drogue charge */
597 ao_ignite(ao_igniter_drogue);
599 /* slow down the telemetry system */
600 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
603 * Start recording min/max height
604 * to figure out when the rocket has landed
607 /* initialize interval values */
608 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
610 ao_interval_min_height = ao_interval_max_height = ao_height;
612 /* and enter drogue state */
613 ao_flight_state = ao_flight_drogue;
614 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
618 case ao_flight_drogue:
620 /* drogue to main deploy:
622 * barometer: reach main deploy altitude
624 * Would like to use the accelerometer for this test, but
625 * the orientation of the flight computer is unknown after
626 * drogue deploy, so we ignore it. Could also detect
627 * high descent rate using the pressure sensor to
628 * recognize drogue deploy failure and eject the main
629 * at that point. Perhaps also use the drogue sense lines
630 * to notice continutity?
632 if (ao_height <= ao_config.main_deploy)
634 ao_ignite(ao_igniter_main);
635 ao_flight_state = ao_flight_main;
636 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
639 /* fall through... */
642 /* drogue/main to land:
644 * barometer: altitude stable and within 1000m of the launch altitude
647 if (ao_height < ao_interval_min_height)
648 ao_interval_min_height = ao_height;
649 if (ao_height > ao_interval_max_height)
650 ao_interval_max_height = ao_height;
652 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
653 if (ao_height < AO_M_TO_HEIGHT(1000) &&
654 ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
656 ao_flight_state = ao_flight_landed;
658 /* turn off the ADC capture */
659 ao_timer_set_adc_interval(0);
660 /* Enable RDF beacon */
663 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
665 ao_interval_min_height = ao_interval_max_height = ao_height;
666 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
669 case ao_flight_landed:
675 static __xdata struct ao_task flight_task;
680 ao_flight_state = ao_flight_startup;
681 ao_add_task(&flight_task, ao_flight, "flight");