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 int16_t ao_flight_accel; /* filtered acceleration */
27 __pdata int16_t ao_flight_pres; /* filtered pressure */
28 __pdata int16_t ao_ground_pres; /* startup pressure */
29 __pdata int16_t ao_ground_accel; /* startup acceleration */
30 __pdata int16_t ao_min_pres; /* minimum recorded pressure */
31 __pdata uint16_t ao_launch_tick; /* time of launch detect */
32 __pdata int16_t ao_main_pres; /* pressure to eject main */
35 * track min/max data over a long interval to detect
38 __pdata uint16_t ao_interval_end;
39 __pdata int16_t ao_interval_cur_min_accel;
40 __pdata int16_t ao_interval_cur_max_accel;
41 __pdata int16_t ao_interval_cur_min_pres;
42 __pdata int16_t ao_interval_cur_max_pres;
43 __pdata int16_t ao_interval_min_accel;
44 __pdata int16_t ao_interval_max_accel;
45 __pdata int16_t ao_interval_min_pres;
46 __pdata int16_t ao_interval_max_pres;
48 __data uint8_t ao_flight_adc;
49 __pdata int16_t ao_raw_accel, ao_raw_accel_prev, ao_raw_pres;
51 /* Accelerometer calibration
53 * We're sampling the accelerometer through a resistor divider which
54 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
55 * That goes into the cc1111 A/D converter, which is running at 11 bits
56 * of precision with the bits in the MSB of the 16 bit value. Only positive
57 * values are used, so values should range from 0-32752 for 0-3.3V. The
58 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
59 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
60 * for a final computation of:
62 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
64 * Zero g was measured at 16000 (we would expect 16384).
65 * Note that this value is only require to tell if the
66 * rocket is standing upright. Once that is determined,
67 * the value of the accelerometer is averaged for 100 samples
68 * to find the resting accelerometer value, which is used
69 * for all further flight computations
72 #define GRAVITY 9.80665
73 /* convert m/s to velocity count */
74 #define VEL_MPS_TO_COUNT(mps) ((int32_t) (((mps) / GRAVITY) * ACCEL_G * 100))
77 #define ACCEL_ZERO_G 16000
78 #define ACCEL_NOSE_UP (ACCEL_ZERO_G - ACCEL_G * 2 /3)
79 #define ACCEL_BOOST ACCEL_G * 2
80 #define ACCEL_INT_LAND (ACCEL_G / 10)
81 #define ACCEL_VEL_LAND VEL_MPS_TO_COUNT(10)
84 * Barometer calibration
86 * We directly sample the barometer. The specs say:
88 * Pressure range: 15-115 kPa
89 * Voltage at 115kPa: 2.82
90 * Output scale: 27mV/kPa
92 * If we want to detect launch with the barometer, we need
93 * a large enough bump to not be fooled by noise. At typical
94 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
95 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
96 * As all of our calculations are done in 16 bits, we'll actually see a change
97 * of 16 times this though
99 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
103 #define BARO_LAUNCH (BARO_kPa / 5) /* .2kPa, or about 20m */
104 #define BARO_APOGEE (BARO_kPa / 10) /* .1kPa, or about 10m */
105 #define BARO_COAST (BARO_kPa * 5) /* 5kpa, or about 500m */
106 #define BARO_MAIN (BARO_kPa) /* 1kPa, or about 100m */
107 #define BARO_INT_LAND (BARO_kPa / 20) /* .05kPa, or about 5m */
108 #define BARO_LAND (BARO_kPa * 5) /* 5kPa or about 1000m */
110 /* We also have a clock, which can be used to sanity check things in
111 * case of other failures
114 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
116 /* This value is scaled in a weird way. It's a running total of accelerometer
117 * readings minus the ground accelerometer reading. That means it measures
118 * velocity, and quite accurately too. As it gets updated 100 times a second,
121 __pdata int32_t ao_flight_vel;
122 __pdata int32_t ao_max_vel;
123 __xdata int32_t ao_raw_accel_sum, ao_raw_pres_sum;
125 /* Landing is detected by getting constant readings from both pressure and accelerometer
126 * for a fairly long time (AO_INTERVAL_TICKS)
128 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(20)
133 __pdata static uint8_t nsamples = 0;
135 ao_flight_adc = ao_adc_head;
136 ao_raw_accel_prev = 0;
139 ao_interval_cur_min_pres = 0x7fff;
140 ao_interval_cur_max_pres = -0x7fff;
141 ao_interval_cur_min_accel = 0x7fff;
142 ao_interval_cur_max_accel = -0x7fff;
144 ao_sleep(&ao_adc_ring);
145 while (ao_flight_adc != ao_adc_head) {
146 ao_raw_accel = ao_adc_ring[ao_flight_adc].accel;
147 ao_raw_pres = ao_adc_ring[ao_flight_adc].pres;
148 ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
149 /* all of our accelerations are negative, so subtract instead of add to get speed */
150 ao_flight_vel -= (int32_t) (((ao_raw_accel + ao_raw_accel_prev) >> 1) - ao_ground_accel);
151 ao_raw_accel_prev = ao_raw_accel;
152 ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
154 ao_flight_accel -= ao_flight_accel >> 4;
155 ao_flight_accel += ao_raw_accel >> 4;
156 ao_flight_pres -= ao_flight_pres >> 4;
157 ao_flight_pres += ao_raw_pres >> 4;
159 if (ao_flight_pres < ao_min_pres)
160 ao_min_pres = ao_flight_pres;
161 if (ao_flight_vel > ao_max_vel)
162 ao_max_vel = ao_flight_vel;
164 if (ao_flight_pres < ao_interval_cur_min_pres)
165 ao_interval_cur_min_pres = ao_flight_pres;
166 if (ao_flight_pres > ao_interval_cur_max_pres)
167 ao_interval_cur_max_pres = ao_flight_pres;
168 if (ao_flight_accel < ao_interval_cur_min_accel)
169 ao_interval_cur_min_accel = ao_flight_accel;
170 if (ao_flight_accel > ao_interval_cur_max_accel)
171 ao_interval_cur_max_accel = ao_flight_accel;
173 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
174 ao_interval_max_pres = ao_interval_cur_max_pres;
175 ao_interval_min_pres = ao_interval_cur_min_pres;
176 ao_interval_max_accel = ao_interval_cur_max_accel;
177 ao_interval_min_accel = ao_interval_cur_min_accel;
178 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
179 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
180 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
183 switch (ao_flight_state) {
184 case ao_flight_startup:
188 * Collect 100 samples of acceleration and pressure
189 * data and average them to find the resting values
191 if (nsamples < 100) {
192 ao_raw_accel_sum += ao_raw_accel;
193 ao_raw_pres_sum += ao_raw_pres;
197 ao_ground_accel = (ao_raw_accel_sum / nsamples);
198 ao_ground_pres = (ao_raw_pres_sum / nsamples);
199 ao_min_pres = ao_ground_pres;
200 ao_main_pres = ao_ground_pres - BARO_MAIN;
204 ao_interval_end = ao_flight_tick;
206 /* Go to launchpad state if the nose is pointing up */
207 if (ao_flight_accel < ACCEL_NOSE_UP) {
208 ao_flight_state = ao_flight_launchpad;
209 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
211 ao_flight_state = ao_flight_idle;
213 /* Turn on the Green LED in idle mode
215 ao_led_on(AO_LED_GREEN);
216 ao_timer_set_adc_interval(100);
217 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
219 /* signal successful initialization by turning off the LED */
220 ao_led_off(AO_LED_RED);
222 case ao_flight_launchpad:
226 * accelerometer: > 2g
228 * barometer: > 20m vertical motion
230 * The accelerometer should always detect motion before
231 * the barometer, but we use both to make sure this
232 * transition is detected
234 if (ao_flight_accel < ao_ground_accel - ACCEL_BOOST ||
235 ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
237 ao_flight_state = ao_flight_boost;
238 ao_launch_tick = ao_flight_tick;
240 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
244 case ao_flight_boost:
248 * accelerometer: start to fall at > 1/4 G
250 * time: boost for more than 15 seconds
252 * Detects motor burn out by the switch from acceleration to
253 * deceleration, or by waiting until the maximum burn duration
254 * (15 seconds) has past.
256 if (ao_flight_accel > ao_ground_accel + (ACCEL_G >> 2) ||
257 (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
259 ao_flight_state = ao_flight_coast;
260 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
263 /* fall through ... */
264 case ao_flight_coast:
266 /* boost/coast to apogee detect:
268 * accelerometer: integrated velocity < 200 m/s AND < max_vel - 50m/s
270 * barometer: fall at least 500m from max altitude
272 * This extra state is required to avoid mis-detecting
273 * apogee due to mach transitions. For slow flights (<200m/s)
274 * we expect to transition right through this stage to
277 if ((ao_flight_vel < VEL_MPS_TO_COUNT(200) &&
278 ao_flight_vel < ao_max_vel - VEL_MPS_TO_COUNT(50)) ||
279 ao_flight_pres > ao_min_pres + BARO_COAST)
281 ao_flight_state = ao_flight_apogee;
282 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
285 case ao_flight_apogee:
287 /* apogee to drogue deploy:
289 * accelerometer: integrated velocity < 10m/s
291 * barometer: fall at least 10m
293 * If the barometer saturates because the flight
294 * goes over its measuring range (about 53k'),
295 * requiring a 10m fall will avoid prematurely
296 * detecting apogee; the accelerometer will take
297 * over in that case and the integrated velocity
298 * measurement should suffice to find apogee
300 if (ao_flight_vel < VEL_MPS_TO_COUNT(-10) ||
301 ao_flight_pres - BARO_APOGEE > ao_min_pres)
303 ao_ignite(ao_igniter_drogue);
304 ao_flight_state = ao_flight_drogue;
305 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
308 case ao_flight_drogue:
310 /* drogue to main deploy:
312 * accelerometer: abs(velocity) > 50m/s
314 * barometer: reach main deploy altitude
316 if (ao_flight_vel < VEL_MPS_TO_COUNT(-50) ||
317 ao_flight_vel > VEL_MPS_TO_COUNT(50) ||
318 ao_flight_pres >= ao_main_pres)
320 ao_ignite(ao_igniter_main);
321 ao_flight_state = ao_flight_main;
322 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
324 /* fall through... */
327 /* drogue/main to land:
329 * accelerometer: value stable and velocity less than 10m/s
331 * barometer: altitude stable and within 500m of the launch altitude
333 if ((ao_flight_vel < ACCEL_VEL_LAND &&
334 (ao_interval_max_accel - ao_interval_min_accel) < ACCEL_INT_LAND) ||
335 (ao_flight_pres > ao_ground_pres - BARO_LAND &&
336 (ao_interval_max_pres - ao_interval_min_pres) < BARO_INT_LAND))
338 ao_flight_state = ao_flight_landed;
339 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
342 case ao_flight_landed:
349 #define AO_ACCEL_COUNT_TO_MSS(count) ((count) / 27)
350 #define AO_VEL_COUNT_TO_MS(count) ((int16_t) ((count) / 2700))
353 ao_flight_status(void)
355 printf("STATE: %7s accel: %d speed: %d altitude: %d\n",
356 ao_state_names[ao_flight_state],
357 AO_ACCEL_COUNT_TO_MSS(ACCEL_ZERO_G - ao_flight_accel),
358 AO_VEL_COUNT_TO_MS(ao_flight_vel),
359 ao_pres_to_altitude(ao_flight_pres));
362 static __xdata struct ao_task flight_task;
364 __code struct ao_cmds ao_flight_cmds[] = {
365 { 'f', ao_flight_status, "f Display current flight state" },
366 { 0, ao_flight_status, NULL }
372 ao_flight_state = ao_flight_startup;
373 ao_interval_min_accel = 0;
374 ao_interval_max_accel = 0x7fff;
375 ao_interval_min_pres = 0;
376 ao_interval_max_pres = 0x7fff;
377 ao_interval_end = AO_INTERVAL_TICKS;
379 ao_add_task(&flight_task, ao_flight, "flight");
380 ao_cmd_register(&ao_flight_cmds[0]);