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
35 #error Please define USE_KALMAN
38 /* Main flight thread. */
40 __pdata enum ao_flight_state ao_flight_state; /* current flight state */
41 __pdata uint16_t ao_flight_tick; /* time of last data */
42 __pdata uint16_t ao_flight_prev_tick; /* time of previous data */
43 __pdata int16_t ao_flight_pres; /* filtered pressure */
44 __pdata int16_t ao_ground_pres; /* startup pressure */
45 __pdata int16_t ao_min_pres; /* minimum recorded pressure */
46 __pdata uint16_t ao_launch_tick; /* time of launch detect */
47 __pdata int16_t ao_main_pres; /* pressure to eject main */
49 __pdata int16_t ao_flight_accel; /* filtered acceleration */
50 __pdata int16_t ao_ground_accel; /* startup acceleration */
54 * track min/max data over a long interval to detect
57 __pdata uint16_t ao_interval_end;
58 __pdata int16_t ao_interval_cur_min_pres;
59 __pdata int16_t ao_interval_cur_max_pres;
60 __pdata int16_t ao_interval_min_pres;
61 __pdata int16_t ao_interval_max_pres;
63 __pdata int16_t ao_interval_cur_min_accel;
64 __pdata int16_t ao_interval_cur_max_accel;
65 __pdata int16_t ao_interval_min_accel;
66 __pdata int16_t ao_interval_max_accel;
69 __data uint8_t ao_flight_adc;
70 __pdata int16_t ao_raw_pres;
71 __xdata uint8_t ao_flight_force_idle;
74 __pdata int16_t ao_raw_accel, ao_raw_accel_prev;
75 __pdata int16_t ao_accel_2g;
77 /* Accelerometer calibration
79 * We're sampling the accelerometer through a resistor divider which
80 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
81 * That goes into the cc1111 A/D converter, which is running at 11 bits
82 * of precision with the bits in the MSB of the 16 bit value. Only positive
83 * values are used, so values should range from 0-32752 for 0-3.3V. The
84 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
85 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
86 * for a final computation of:
88 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
90 * Zero g was measured at 16000 (we would expect 16384).
91 * Note that this value is only require to tell if the
92 * rocket is standing upright. Once that is determined,
93 * the value of the accelerometer is averaged for 100 samples
94 * to find the resting accelerometer value, which is used
95 * for all further flight computations
98 #define GRAVITY 9.80665
99 /* convert m/s to velocity count */
100 #define VEL_MPS_TO_COUNT(mps) (((int32_t) (((mps) / GRAVITY) * (AO_HERTZ/2))) * (int32_t) ao_accel_2g)
102 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
103 #define ACCEL_BOOST ao_accel_2g
104 #define ACCEL_COAST (ao_accel_2g >> 3)
105 #define ACCEL_INT_LAND (ao_accel_2g >> 3)
106 #define ACCEL_VEL_MACH VEL_MPS_TO_COUNT(200)
107 #define ACCEL_VEL_BOOST VEL_MPS_TO_COUNT(5)
112 * Barometer calibration
114 * We directly sample the barometer. The specs say:
116 * Pressure range: 15-115 kPa
117 * Voltage at 115kPa: 2.82
118 * Output scale: 27mV/kPa
120 * If we want to detect launch with the barometer, we need
121 * a large enough bump to not be fooled by noise. At typical
122 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
123 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
124 * As all of our calculations are done in 16 bits, we'll actually see a change
125 * of 16 times this though
127 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
131 #define BARO_LAUNCH (BARO_kPa / 5) /* .2kPa, or about 20m */
132 #define BARO_APOGEE (BARO_kPa / 10) /* .1kPa, or about 10m */
133 #define BARO_COAST (BARO_kPa * 5) /* 5kpa, or about 500m */
134 #define BARO_MAIN (BARO_kPa) /* 1kPa, or about 100m */
135 #define BARO_INT_LAND (BARO_kPa / 20) /* .05kPa, or about 5m */
136 #define BARO_LAND (BARO_kPa * 10) /* 10kPa or about 1000m */
138 /* We also have a clock, which can be used to sanity check things in
139 * case of other failures
142 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
145 /* This value is scaled in a weird way. It's a running total of accelerometer
146 * readings minus the ground accelerometer reading. That means it measures
147 * velocity, and quite accurately too. As it gets updated 100 times a second,
150 __pdata int32_t ao_flight_vel;
151 __pdata int32_t ao_min_vel;
152 __pdata int32_t ao_old_vel;
153 __pdata int16_t ao_old_vel_tick;
154 __xdata int32_t ao_raw_accel_sum;
158 __pdata int16_t ao_ground_height;
159 __pdata int32_t ao_k_max_height;
160 __pdata int32_t ao_k_height;
161 __pdata int32_t ao_k_speed;
162 __pdata int32_t ao_k_accel;
164 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
165 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
167 #define from_fix(x) ((x) >> 16)
169 #define AO_K0_100 to_fix16(0.05680323)
170 #define AO_K1_100 to_fix16(0.16608182)
171 #define AO_K2_100 to_fix16(0.24279580)
173 #define AO_K_STEP_100 to_fix16(0.01)
174 #define AO_K_STEP_2_2_100 to_fix16(0.00005)
176 #define AO_K0_10 to_fix16(0.23772023)
177 #define AO_K1_10 to_fix16(0.32214149)
178 #define AO_K2_10 to_fix16(0.21827159)
180 #define AO_K_STEP_10 to_fix16(0.1)
181 #define AO_K_STEP_2_2_10 to_fix16(0.005)
186 int16_t err = ((ao_pres_to_altitude(ao_raw_pres) - ao_ground_height))
187 - (int16_t) (ao_k_height >> 16);
189 #ifdef AO_FLIGHT_TEST
190 if (ao_flight_tick - ao_flight_prev_tick > 5) {
191 ao_k_height += ((ao_k_speed >> 16) * AO_K_STEP_10 +
192 (ao_k_accel >> 16) * AO_K_STEP_2_2_10);
193 ao_k_speed += (ao_k_accel >> 16) * AO_K_STEP_10;
196 ao_k_height += (int32_t) AO_K0_10 * err;
197 ao_k_speed += (int32_t) AO_K1_10 * err;
198 ao_k_accel += (int32_t) AO_K2_10 * err;
202 ao_k_height += ((ao_k_speed >> 16) * AO_K_STEP_100 +
203 (ao_k_accel >> 16) * AO_K_STEP_2_2_100);
204 ao_k_speed += (ao_k_accel >> 16) * AO_K_STEP_100;
207 ao_k_height += (int32_t) AO_K0_100 * err;
208 ao_k_speed += (int32_t) AO_K1_100 * err;
209 ao_k_accel += (int32_t) AO_K2_100 * err;
213 __xdata int32_t ao_raw_pres_sum;
215 /* Landing is detected by getting constant readings from both pressure and accelerometer
216 * for a fairly long time (AO_INTERVAL_TICKS)
218 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
220 #define abs(a) ((a) < 0 ? -(a) : (a))
225 __pdata static uint16_t nsamples = 0;
227 ao_flight_adc = ao_adc_head;
230 ao_raw_accel_prev = 0;
235 ao_wakeup(DATA_TO_XDATA(&ao_flight_adc));
236 ao_sleep(DATA_TO_XDATA(&ao_adc_head));
237 while (ao_flight_adc != ao_adc_head) {
239 __pdata uint8_t ticks;
240 __pdata int16_t ao_vel_change;
242 __xdata struct ao_adc *ao_adc;
243 ao_flight_prev_tick = ao_flight_tick;
245 /* Capture a sample */
246 ao_adc = &ao_adc_ring[ao_flight_adc];
247 ao_flight_tick = ao_adc->tick;
248 ao_raw_pres = ao_adc->pres;
249 ao_flight_pres -= ao_flight_pres >> 4;
250 ao_flight_pres += ao_raw_pres >> 4;
253 ao_raw_accel = ao_adc->accel;
256 * Ok, the math here is a bit tricky.
258 * ao_raw_accel: ADC output for acceleration
259 * ao_accel_ref: ADC output for the 5V reference.
260 * ao_cook_accel: Corrected acceleration value
261 * Vcc: 3.3V supply to the CC1111
262 * Vac: 5V supply to the accelerometer
263 * accel: input voltage to accelerometer ADC pin
264 * ref: input voltage to 5V reference ADC pin
267 * Measured acceleration is ratiometric to Vcc:
270 * ------------ = -----
273 * Measured 5v reference is also ratiometric to Vcc:
276 * ------------ = -----
280 * ao_accel_ref = 32767 * (ref / Vcc)
282 * Acceleration is measured ratiometric to the 5V supply,
283 * so what we want is:
285 * ao_cook_accel accel
286 * ------------- = -----
295 * = ------------ * ------------
298 * Multiply through by 32767:
300 * ao_raw_accel * 32767
301 * ao_cook_accel = --------------------
304 * Now, the tricky part. Getting this to compile efficiently
305 * and keeping all of the values in-range.
307 * First off, we need to use a shift of 16 instead of * 32767 as SDCC
308 * does the obvious optimizations for byte-granularity shifts:
310 * ao_cook_accel = (ao_raw_accel << 16) / ao_accel_ref
312 * Next, lets check our input ranges:
314 * 0 <= ao_raw_accel <= 0x7fff (singled ended ADC conversion)
315 * 0x7000 <= ao_accel_ref <= 0x7fff (the 5V ref value is close to 0x7fff)
317 * Plugging in our input ranges, we get an output range of 0 - 0x12490,
318 * which is 17 bits. That won't work. If we take the accel ref and shift
319 * by a bit, we'll change its range:
321 * 0xe000 <= ao_accel_ref<<1 <= 0xfffe
323 * ao_cook_accel = (ao_raw_accel << 16) / (ao_accel_ref << 1)
325 * Now the output range is 0 - 0x9248, which nicely fits in 16 bits. It
326 * is, however, one bit too large for our signed computations. So, we
327 * take the result and shift that by a bit:
329 * ao_cook_accel = ((ao_raw_accel << 16) / (ao_accel_ref << 1)) >> 1
331 * This finally creates an output range of 0 - 0x4924. As the ADC only
332 * provides 11 bits of data, we haven't actually lost any precision,
333 * just dropped a bit of noise off the low end.
335 ao_raw_accel = (uint16_t) ((((uint32_t) ao_raw_accel << 16) / (ao_accel_ref[ao_flight_adc] << 1))) >> 1;
336 ao_adc->accel = ao_raw_accel;
339 ao_flight_accel -= ao_flight_accel >> 4;
340 ao_flight_accel += ao_raw_accel >> 4;
343 * The accelerometer is mounted so that
344 * acceleration yields negative values
345 * while deceleration yields positive values,
346 * so subtract instead of add.
348 ticks = ao_flight_tick - ao_flight_prev_tick;
349 ao_vel_change = ao_ground_accel - (((ao_raw_accel + 1) >> 1) + ((ao_raw_accel_prev + 1) >> 1));
350 ao_raw_accel_prev = ao_raw_accel;
352 /* one is a common interval */
354 ao_flight_vel += (int32_t) ao_vel_change;
356 ao_flight_vel += (int32_t) ao_vel_change * (int32_t) ticks;
360 if (ao_flight_state > ao_flight_idle)
363 ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
366 if (ao_flight_pres < ao_min_pres)
367 ao_min_pres = ao_flight_pres;
369 if (ao_flight_vel >= 0) {
370 if (ao_flight_vel < ao_min_vel)
371 ao_min_vel = ao_flight_vel;
373 if (-ao_flight_vel < ao_min_vel)
374 ao_min_vel = -ao_flight_vel;
378 switch (ao_flight_state) {
379 case ao_flight_startup:
383 * Collect 512 samples of acceleration and pressure
384 * data and average them to find the resting values
386 if (nsamples < 512) {
388 ao_raw_accel_sum += ao_raw_accel;
390 ao_raw_pres_sum += ao_raw_pres;
395 ao_ground_accel = ao_raw_accel_sum >> 9;
397 ao_ground_pres = ao_raw_pres_sum >> 9;
398 ao_min_pres = ao_ground_pres;
401 ao_ground_height = ao_pres_to_altitude(ao_ground_pres);
403 ao_main_pres = ao_altitude_to_pres(ao_pres_to_altitude(ao_ground_pres) + ao_config.main_deploy);
405 ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
408 ao_old_vel = ao_flight_vel;
409 ao_old_vel_tick = ao_flight_tick;
412 /* Check to see what mode we should go to.
413 * - Invalid mode if accel cal appears to be out
414 * - pad mode if we're upright,
415 * - idle mode otherwise
419 if (ao_config.accel_plus_g == 0 ||
420 ao_config.accel_minus_g == 0 ||
421 ao_flight_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
422 ao_flight_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
424 /* Detected an accel value outside -1.5g to 1.5g
425 * (or uncalibrated values), so we go into invalid mode
427 ao_flight_state = ao_flight_invalid;
431 if (!ao_flight_force_idle
433 && ao_flight_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
437 /* Set pad mode - we can fly! */
438 ao_flight_state = ao_flight_pad;
440 /* Disable the USB controller in flight mode
446 /* Disable packet mode in pad state */
447 ao_packet_slave_stop();
449 /* Turn on telemetry system */
451 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
453 /* signal successful initialization by turning off the LED */
454 ao_led_off(AO_LED_RED);
457 ao_flight_state = ao_flight_idle;
459 /* signal successful initialization by turning off the LED */
460 ao_led_off(AO_LED_RED);
462 /* wakeup threads due to state change */
463 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
471 * Once a second, remove any velocity from
474 if ((int16_t) (ao_flight_tick - ao_old_vel_tick) >= AO_SEC_TO_TICKS(1)) {
475 ao_old_vel_tick = ao_flight_tick;
476 ao_flight_vel -= ao_old_vel;
477 ao_old_vel = ao_flight_vel;
482 * accelerometer: > 2g AND velocity > 5m/s
484 * barometer: > 20m vertical motion
486 * The accelerometer should always detect motion before
487 * the barometer, but we use both to make sure this
488 * transition is detected
493 * With an accelerometer, either to detect launch
495 if ((ao_k_accel > to_fix32(20) &&
496 ao_k_speed > to_fix32(5)) ||
497 ao_k_height > to_fix32(20))
500 * Without an accelerometer, the barometer is far too
501 * noisy to rely on speed or acceleration data
503 if (ao_k_height > to_fix32(20))
508 (ao_flight_accel < ao_ground_accel - ACCEL_BOOST &&
509 ao_flight_vel > ACCEL_VEL_BOOST) ||
511 ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
514 #if HAS_ACCEL || USE_KALMAN
515 ao_flight_state = ao_flight_boost;
517 ao_flight_state = ao_flight_coast;
519 ao_launch_tick = ao_flight_tick;
521 /* start logging data */
524 /* Increase telemetry rate */
525 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
527 /* disable RDF beacon */
531 /* Record current GPS position by waking up GPS log tasks */
532 ao_wakeup(&ao_gps_data);
533 ao_wakeup(&ao_gps_tracking_data);
536 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
540 #if HAS_ACCEL || USE_KALMAN
541 case ao_flight_boost:
545 * accelerometer: start to fall at > 1/4 G
547 * time: boost for more than 15 seconds
549 * Detects motor burn out by the switch from acceleration to
550 * deceleration, or by waiting until the maximum burn duration
551 * (15 seconds) has past.
554 if ((ao_k_accel < to_fix32(-10) && ao_k_height > to_fix32(100)) ||
555 (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
557 if (ao_flight_accel > ao_ground_accel + ACCEL_COAST ||
558 (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
561 ao_flight_state = ao_flight_fast;
562 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
570 * accelerometer: integrated velocity < 200 m/s
572 * barometer: fall at least 500m from max altitude
574 * This extra state is required to avoid mis-detecting
575 * apogee due to mach transitions.
577 * XXX this is essentially a single-detector test
578 * as the 500m altitude change would likely result
579 * in a loss of the rocket. More data on precisely
580 * how big a pressure change the mach transition
581 * generates would be useful here.
584 if (ao_k_speed < to_fix32(200) ||
585 ao_k_height < ao_k_max_height - to_fix32(500))
587 if (ao_flight_vel < ACCEL_VEL_MACH ||
588 ao_flight_pres > ao_min_pres + BARO_COAST)
592 /* set min velocity to current velocity for
595 ao_min_vel = abs(ao_flight_vel);
597 ao_flight_state = ao_flight_coast;
598 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
601 #endif /* HAS_ACCEL */
602 case ao_flight_coast:
605 /* apogee detect: coast to drogue deploy:
611 /* apogee detect: coast to drogue deploy:
613 * barometer: fall at least 10m
615 * It would be nice to use the accelerometer
616 * to detect apogee as well, but tests have
617 * shown that flights far from vertical would
618 * grossly mis-detect apogee. So, for now,
619 * we'll trust to a single sensor for this test
621 if (ao_flight_pres > ao_min_pres + BARO_APOGEE)
624 /* ignite the drogue charge */
625 ao_ignite(ao_igniter_drogue);
627 /* slow down the telemetry system */
628 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
630 /* slow down the ADC sample rate */
631 ao_timer_set_adc_interval(10);
634 * Start recording min/max accel and pres for a while
635 * to figure out when the rocket has landed
637 /* Set the 'last' limits to max range to prevent
638 * early resting detection
641 ao_interval_min_accel = 0;
642 ao_interval_max_accel = 0x7fff;
644 ao_interval_min_pres = 0;
645 ao_interval_max_pres = 0x7fff;
647 /* initialize interval values */
648 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
650 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
652 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
655 /* and enter drogue state */
656 ao_flight_state = ao_flight_drogue;
657 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
661 case ao_flight_drogue:
663 /* drogue to main deploy:
665 * barometer: reach main deploy altitude
667 * Would like to use the accelerometer for this test, but
668 * the orientation of the flight computer is unknown after
669 * drogue deploy, so we ignore it. Could also detect
670 * high descent rate using the pressure sensor to
671 * recognize drogue deploy failure and eject the main
672 * at that point. Perhaps also use the drogue sense lines
673 * to notice continutity?
676 if (from_fix(ao_k_height) < ao_config.main_deploy)
678 if (ao_flight_pres >= ao_main_pres)
681 ao_ignite(ao_igniter_main);
682 ao_flight_state = ao_flight_main;
683 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
686 /* fall through... */
689 /* drogue/main to land:
691 * accelerometer: value stable
693 * barometer: altitude stable and within 1000m of the launch altitude
696 if (ao_flight_pres < ao_interval_cur_min_pres)
697 ao_interval_cur_min_pres = ao_flight_pres;
698 if (ao_flight_pres > ao_interval_cur_max_pres)
699 ao_interval_cur_max_pres = ao_flight_pres;
701 if (ao_flight_accel < ao_interval_cur_min_accel)
702 ao_interval_cur_min_accel = ao_flight_accel;
703 if (ao_flight_accel > ao_interval_cur_max_accel)
704 ao_interval_cur_max_accel = ao_flight_accel;
707 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
708 ao_interval_max_pres = ao_interval_cur_max_pres;
709 ao_interval_min_pres = ao_interval_cur_min_pres;
710 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
712 ao_interval_max_accel = ao_interval_cur_max_accel;
713 ao_interval_min_accel = ao_interval_cur_min_accel;
714 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
716 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
720 (uint16_t) (ao_interval_max_accel - ao_interval_min_accel) < (uint16_t) ACCEL_INT_LAND &&
722 ao_flight_pres > ao_ground_pres - BARO_LAND &&
723 (uint16_t) (ao_interval_max_pres - ao_interval_min_pres) < (uint16_t) BARO_INT_LAND)
725 ao_flight_state = ao_flight_landed;
727 /* turn off the ADC capture */
728 ao_timer_set_adc_interval(0);
729 /* Enable RDF beacon */
732 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
736 case ao_flight_landed:
742 static __xdata struct ao_task flight_task;
747 ao_flight_state = ao_flight_startup;
748 ao_add_task(&flight_task, ao_flight, "flight");