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
22 /* Main flight thread. */
24 __pdata enum ao_flight_state ao_flight_state; /* current flight state */
25 __pdata uint16_t ao_flight_tick; /* time of last data */
26 __pdata uint16_t ao_flight_prev_tick; /* time of previous data */
27 __pdata int16_t ao_flight_accel; /* filtered acceleration */
28 __pdata int16_t ao_flight_pres; /* filtered pressure */
29 __pdata int16_t ao_ground_pres; /* startup pressure */
30 __pdata int16_t ao_ground_accel; /* startup acceleration */
31 __pdata int16_t ao_min_pres; /* minimum recorded pressure */
32 __pdata uint16_t ao_launch_tick; /* time of launch detect */
33 __pdata int16_t ao_main_pres; /* pressure to eject main */
36 * track min/max data over a long interval to detect
39 __pdata uint16_t ao_interval_end;
40 __pdata int16_t ao_interval_cur_min_accel;
41 __pdata int16_t ao_interval_cur_max_accel;
42 __pdata int16_t ao_interval_cur_min_pres;
43 __pdata int16_t ao_interval_cur_max_pres;
44 __pdata int16_t ao_interval_min_accel;
45 __pdata int16_t ao_interval_max_accel;
46 __pdata int16_t ao_interval_min_pres;
47 __pdata int16_t ao_interval_max_pres;
49 __data uint8_t ao_flight_adc;
50 __pdata int16_t ao_raw_accel, ao_raw_accel_prev, ao_raw_pres;
52 /* Accelerometer calibration
54 * We're sampling the accelerometer through a resistor divider which
55 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
56 * That goes into the cc1111 A/D converter, which is running at 11 bits
57 * of precision with the bits in the MSB of the 16 bit value. Only positive
58 * values are used, so values should range from 0-32752 for 0-3.3V. The
59 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
60 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
61 * for a final computation of:
63 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
65 * Zero g was measured at 16000 (we would expect 16384).
66 * Note that this value is only require to tell if the
67 * rocket is standing upright. Once that is determined,
68 * the value of the accelerometer is averaged for 100 samples
69 * to find the resting accelerometer value, which is used
70 * for all further flight computations
73 #define GRAVITY 9.80665
74 /* convert m/s to velocity count */
75 #define VEL_MPS_TO_COUNT(mps) ((int32_t) (((mps) / GRAVITY) * ACCEL_G * 100))
78 #define ACCEL_ZERO_G 16000
79 #define ACCEL_NOSE_UP (ACCEL_G * 2 /3)
80 #define ACCEL_BOOST ACCEL_G * 2
81 #define ACCEL_INT_LAND (ACCEL_G / 10)
82 #define ACCEL_VEL_LAND VEL_MPS_TO_COUNT(10)
83 #define ACCEL_VEL_MACH VEL_MPS_TO_COUNT(200)
84 #define ACCEL_VEL_APOGEE VEL_MPS_TO_COUNT(2)
85 #define ACCEL_VEL_MAIN VEL_MPS_TO_COUNT(100)
86 #define ACCEL_VEL_BOOST VEL_MPS_TO_COUNT(5)
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
108 #define BARO_LAUNCH (BARO_kPa / 5) /* .2kPa, or about 20m */
109 #define BARO_APOGEE (BARO_kPa / 10) /* .1kPa, or about 10m */
110 #define BARO_COAST (BARO_kPa * 5) /* 5kpa, or about 500m */
111 #define BARO_MAIN (BARO_kPa) /* 1kPa, or about 100m */
112 #define BARO_INT_LAND (BARO_kPa / 20) /* .05kPa, or about 5m */
113 #define BARO_LAND (BARO_kPa * 10) /* 10kPa or about 1000m */
115 /* We also have a clock, which can be used to sanity check things in
116 * case of other failures
119 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
121 /* This value is scaled in a weird way. It's a running total of accelerometer
122 * readings minus the ground accelerometer reading. That means it measures
123 * velocity, and quite accurately too. As it gets updated 100 times a second,
126 __pdata int32_t ao_flight_vel;
127 __pdata int32_t ao_min_vel;
128 __xdata int32_t ao_raw_accel_sum, ao_raw_pres_sum;
130 /* Landing is detected by getting constant readings from both pressure and accelerometer
131 * for a fairly long time (AO_INTERVAL_TICKS)
133 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(20)
135 #define abs(a) ((a) < 0 ? -(a) : (a))
140 __pdata static uint16_t nsamples = 0;
142 ao_flight_adc = ao_adc_head;
143 ao_raw_accel_prev = 0;
148 ao_sleep(&ao_adc_ring);
149 while (ao_flight_adc != ao_adc_head) {
150 __pdata uint8_t ticks;
151 __pdata int16_t ao_vel_change;
152 ao_flight_prev_tick = ao_flight_tick;
154 /* Capture a sample */
155 ao_raw_accel = ao_adc_ring[ao_flight_adc].accel;
156 ao_raw_pres = ao_adc_ring[ao_flight_adc].pres;
157 ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
161 * The accelerometer is mounted so that
162 * acceleration yields negative values
163 * while deceleration yields positive values,
164 * so subtract instead of add.
166 ticks = ao_flight_tick - ao_flight_prev_tick;
167 ao_vel_change = (((ao_raw_accel + ao_raw_accel_prev) >> 1) - ao_ground_accel);
168 ao_raw_accel_prev = ao_raw_accel;
170 /* one is a common interval */
172 ao_flight_vel -= (int32_t) ao_vel_change;
174 ao_flight_vel -= (int32_t) ao_vel_change * (int32_t) ticks;
176 ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
178 ao_flight_accel -= ao_flight_accel >> 4;
179 ao_flight_accel += ao_raw_accel >> 4;
180 ao_flight_pres -= ao_flight_pres >> 4;
181 ao_flight_pres += ao_raw_pres >> 4;
183 if (ao_flight_pres < ao_min_pres)
184 ao_min_pres = ao_flight_pres;
185 if (ao_flight_vel >= 0) {
186 if (ao_flight_vel < ao_min_vel)
187 ao_min_vel = ao_flight_vel;
189 if (-ao_flight_vel < ao_min_vel)
190 ao_min_vel = -ao_flight_vel;
193 switch (ao_flight_state) {
194 case ao_flight_startup:
198 * Collect 1000 samples of acceleration and pressure
199 * data and average them to find the resting values
201 if (nsamples < 1000) {
202 ao_raw_accel_sum += ao_raw_accel;
203 ao_raw_pres_sum += ao_raw_pres;
207 ao_ground_accel = (ao_raw_accel_sum / nsamples);
208 ao_ground_pres = (ao_raw_pres_sum / nsamples);
209 ao_min_pres = ao_ground_pres;
211 ao_main_pres = ao_altitude_to_pres(ao_pres_to_altitude(ao_ground_pres) + ao_config.main_deploy);
215 /* Go to launchpad state if the nose is pointing up */
217 if (ao_flight_accel < ao_config.accel_zero_g - ACCEL_NOSE_UP) {
219 /* Disable the USB controller in flight mode
224 /* Turn on telemetry system
227 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
229 ao_flight_state = ao_flight_launchpad;
230 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
232 ao_flight_state = ao_flight_idle;
234 /* Turn on the Green LED in idle mode
236 ao_led_on(AO_LED_GREEN);
237 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
239 /* signal successful initialization by turning off the LED */
240 ao_led_off(AO_LED_RED);
242 case ao_flight_launchpad:
246 * accelerometer: > 2g AND velocity > 5m/s
248 * barometer: > 20m vertical motion
250 * The accelerometer should always detect motion before
251 * the barometer, but we use both to make sure this
252 * transition is detected
254 if ((ao_flight_accel < ao_ground_accel - ACCEL_BOOST &&
255 ao_flight_vel > ACCEL_VEL_BOOST) ||
256 ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
258 ao_flight_state = ao_flight_boost;
259 ao_launch_tick = ao_flight_tick;
261 /* start logging data */
264 /* Increase telemetry rate */
265 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
267 /* disable RDF beacon */
270 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
274 case ao_flight_boost:
278 * accelerometer: start to fall at > 1/4 G
280 * time: boost for more than 15 seconds
282 * Detects motor burn out by the switch from acceleration to
283 * deceleration, or by waiting until the maximum burn duration
284 * (15 seconds) has past.
286 if (ao_flight_accel > ao_ground_accel + (ACCEL_G >> 2) ||
287 (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
289 ao_flight_state = ao_flight_coast;
290 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
294 case ao_flight_coast:
296 /* coast to apogee detect:
298 * accelerometer: integrated velocity < 200 m/s
300 * barometer: fall at least 500m from max altitude
302 * This extra state is required to avoid mis-detecting
303 * apogee due to mach transitions.
305 * XXX this is essentially a single-detector test
306 * as the 500m altitude change would likely result
307 * in a loss of the rocket. More data on precisely
308 * how big a pressure change the mach transition
309 * generates would be useful here.
311 if (ao_flight_vel < ACCEL_VEL_MACH ||
312 ao_flight_pres > ao_min_pres + BARO_COAST)
314 /* set min velocity to current velocity for
317 ao_min_vel = abs(ao_flight_vel);
318 ao_flight_state = ao_flight_apogee;
319 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
322 case ao_flight_apogee:
324 /* apogee detect to drogue deploy:
326 * accelerometer: abs(velocity) > min_velocity + 2m/s
328 * barometer: fall at least 10m
330 * If the barometer saturates because the flight
331 * goes over its measuring range (about 53k'),
332 * requiring a 10m fall will avoid prematurely
333 * detecting apogee; the accelerometer will take
334 * over in that case and the integrated velocity
335 * measurement should suffice to find apogee
337 if (abs(ao_flight_vel) > ao_min_vel + ACCEL_VEL_APOGEE ||
338 ao_flight_pres > ao_min_pres + BARO_APOGEE)
340 /* ignite the drogue charge */
341 ao_ignite(ao_igniter_drogue);
343 /* slow down the telemetry system */
344 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
346 /* slow down the ADC sample rate */
347 ao_timer_set_adc_interval(10);
349 /* Enable RDF beacon */
353 * Start recording min/max accel and pres for a while
354 * to figure out when the rocket has landed
356 /* Set the 'last' limits to max range to prevent
357 * early resting detection
359 ao_interval_min_accel = 0;
360 ao_interval_max_accel = 0x7fff;
361 ao_interval_min_pres = 0;
362 ao_interval_max_pres = 0x7fff;
364 /* initialize interval values */
365 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
367 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
368 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
370 /* and enter drogue state */
371 ao_flight_state = ao_flight_drogue;
372 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
376 case ao_flight_drogue:
378 /* drogue to main deploy:
380 * accelerometer: abs(velocity) > 100m/s (in case the drogue failed)
382 * barometer: reach main deploy altitude
384 if (ao_flight_vel < -ACCEL_VEL_MAIN ||
385 ao_flight_vel > ACCEL_VEL_MAIN ||
386 ao_flight_pres >= ao_main_pres)
388 ao_ignite(ao_igniter_main);
389 ao_flight_state = ao_flight_main;
390 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
393 /* fall through... */
396 /* drogue/main to land:
398 * accelerometer: value stable and velocity less than 10m/s
400 * barometer: altitude stable and within 1000m of the launch altitude
403 if (ao_flight_pres < ao_interval_cur_min_pres)
404 ao_interval_cur_min_pres = ao_flight_pres;
405 if (ao_flight_pres > ao_interval_cur_max_pres)
406 ao_interval_cur_max_pres = ao_flight_pres;
407 if (ao_flight_accel < ao_interval_cur_min_accel)
408 ao_interval_cur_min_accel = ao_flight_accel;
409 if (ao_flight_accel > ao_interval_cur_max_accel)
410 ao_interval_cur_max_accel = ao_flight_accel;
412 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
413 ao_interval_max_pres = ao_interval_cur_max_pres;
414 ao_interval_min_pres = ao_interval_cur_min_pres;
415 ao_interval_max_accel = ao_interval_cur_max_accel;
416 ao_interval_min_accel = ao_interval_cur_min_accel;
417 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
418 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
419 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
422 if ((abs(ao_flight_vel) < ACCEL_VEL_LAND &&
423 (uint16_t) (ao_interval_max_accel - ao_interval_min_accel) < (uint16_t) ACCEL_INT_LAND) ||
424 (ao_flight_pres > ao_ground_pres - BARO_LAND &&
425 (uint16_t) (ao_interval_max_pres - ao_interval_min_pres) < (uint16_t) BARO_INT_LAND))
427 ao_flight_state = ao_flight_landed;
429 /* turn off the ADC capture */
430 ao_timer_set_adc_interval(0);
432 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
435 case ao_flight_landed:
441 #define AO_ACCEL_COUNT_TO_MSS(count) ((count) / 27)
442 #define AO_VEL_COUNT_TO_MS(count) ((int16_t) ((count) / 2700))
445 ao_flight_status(void)
447 printf("STATE: %7s accel: %d speed: %d altitude: %d main: %d\n",
448 ao_state_names[ao_flight_state],
449 AO_ACCEL_COUNT_TO_MSS(ACCEL_ZERO_G - ao_flight_accel),
450 AO_VEL_COUNT_TO_MS(ao_flight_vel),
451 ao_pres_to_altitude(ao_flight_pres),
452 ao_pres_to_altitude(ao_main_pres));
455 static __xdata struct ao_task flight_task;
457 __code struct ao_cmds ao_flight_cmds[] = {
458 { 'f', ao_flight_status, "f Display current flight state" },
459 { 0, ao_flight_status, NULL }
465 ao_flight_state = ao_flight_startup;
466 ao_interval_min_accel = 0;
467 ao_interval_max_accel = 0x7fff;
468 ao_interval_min_pres = 0;
469 ao_interval_max_pres = 0x7fff;
470 ao_interval_end = AO_INTERVAL_TICKS;
472 ao_add_task(&flight_task, ao_flight, "flight");
473 ao_cmd_register(&ao_flight_cmds[0]);