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
73 #define ACCEL_ZERO_G 16000
74 #define ACCEL_NOSE_UP (ACCEL_ZERO_G - ACCEL_G * 2 /3)
75 #define ACCEL_BOOST ACCEL_G * 2
76 #define ACCEL_LAND (ACCEL_G / 10)
79 * Barometer calibration
81 * We directly sample the barometer. The specs say:
83 * Pressure range: 15-115 kPa
84 * Voltage at 115kPa: 2.82
85 * Output scale: 27mV/kPa
87 * If we want to detect launch with the barometer, we need
88 * a large enough bump to not be fooled by noise. At typical
89 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
90 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
91 * As all of our calculations are done in 16 bits, we'll actually see a change
92 * of 16 times this though
94 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
98 #define BARO_LAUNCH (BARO_kPa / 5) /* .2kPa, or about 20m */
99 #define BARO_APOGEE (BARO_kPa / 10) /* .1kPa, or about 10m */
100 #define BARO_COAST (BARO_kPa * 5) /* 5kpa, or about 500m */
101 #define BARO_MAIN (BARO_kPa) /* 1kPa, or about 100m */
102 #define BARO_LAND (BARO_kPa / 20) /* .05kPa, or about 5m */
104 /* We also have a clock, which can be used to sanity check things in
105 * case of other failures
108 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
110 /* This value is scaled in a weird way. It's a running total of accelerometer
111 * readings minus the ground accelerometer reading. That means it measures
112 * velocity, and quite accurately too. As it gets updated 100 times a second,
115 __data int32_t ao_flight_vel;
116 __xdata int32_t ao_raw_accel_sum, ao_raw_pres_sum;
118 #define GRAVITY 9.80665
119 /* convert m/s to velocity count */
120 #define VEL_MPS_TO_COUNT(mps) ((int32_t) (((mps) / GRAVITY) * ACCEL_G * 100))
122 /* Landing is detected by getting constant readings from both pressure and accelerometer
123 * for a fairly long time (AO_INTERVAL_TICKS)
125 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(10)
130 __pdata static uint8_t nsamples = 0;
132 ao_flight_adc = ao_adc_head;
133 ao_raw_accel_prev = 0;
136 ao_interval_cur_min_pres = 0x7fff;
137 ao_interval_cur_max_pres = -0x7fff;
138 ao_interval_cur_min_accel = 0x7fff;
139 ao_interval_cur_max_accel = -0x7fff;
141 ao_sleep(&ao_adc_ring);
142 while (ao_flight_adc != ao_adc_head) {
143 ao_raw_accel = ao_adc_ring[ao_flight_adc].accel;
144 ao_raw_pres = ao_adc_ring[ao_flight_adc].pres;
145 ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
146 /* all of our accelerations are negative, so subtract instead of add to get speed */
147 ao_flight_vel -= (int32_t) (((ao_raw_accel + ao_raw_accel_prev) >> 1) - ao_ground_accel);
148 ao_raw_accel_prev = ao_raw_accel;
149 ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
151 ao_flight_accel -= ao_flight_accel >> 4;
152 ao_flight_accel += ao_raw_accel >> 4;
153 ao_flight_pres -= ao_flight_pres >> 4;
154 ao_flight_pres += ao_raw_pres >> 4;
156 if (ao_flight_pres < ao_min_pres)
157 ao_min_pres = ao_flight_pres;
159 if (ao_flight_pres < ao_interval_cur_min_pres)
160 ao_interval_cur_min_pres = ao_flight_pres;
161 if (ao_flight_pres > ao_interval_cur_max_pres)
162 ao_interval_cur_max_pres = ao_flight_pres;
163 if (ao_flight_accel < ao_interval_cur_min_accel)
164 ao_interval_cur_min_accel = ao_flight_accel;
165 if (ao_flight_accel > ao_interval_cur_max_accel)
166 ao_interval_cur_max_accel = ao_flight_accel;
168 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
169 ao_interval_max_pres = ao_interval_cur_max_pres;
170 ao_interval_min_pres = ao_interval_cur_min_pres;
171 ao_interval_max_accel = ao_interval_cur_max_accel;
172 ao_interval_min_accel = ao_interval_cur_min_accel;
173 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
174 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
175 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
178 switch (ao_flight_state) {
179 case ao_flight_startup:
183 * Collect 100 samples of acceleration and pressure
184 * data and average them to find the resting values
186 if (nsamples < 100) {
187 ao_raw_accel_sum += ao_raw_accel;
188 ao_raw_pres_sum += ao_raw_pres;
192 ao_ground_accel = (ao_raw_accel_sum / nsamples);
193 ao_ground_pres = (ao_raw_pres_sum / nsamples);
194 ao_min_pres = ao_ground_pres;
195 ao_main_pres = ao_ground_pres - BARO_MAIN;
198 ao_interval_end = ao_flight_tick;
200 /* Go to launchpad state if the nose is pointing up */
201 if (ao_flight_accel < ACCEL_NOSE_UP) {
202 ao_flight_state = ao_flight_launchpad;
203 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
205 ao_flight_state = ao_flight_idle;
207 /* Turn on the Green LED in idle mode
209 ao_led_on(AO_LED_GREEN);
210 ao_timer_set_adc_interval(100);
211 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
213 /* signal successful initialization by turning off the LED */
214 ao_led_off(AO_LED_RED);
216 case ao_flight_launchpad:
220 * accelerometer: > 2g
222 * barometer: > 20m vertical motion
224 * The accelerometer should always detect motion before
225 * the barometer, but we use both to make sure this
226 * transition is detected
228 if (ao_flight_accel < ao_ground_accel - ACCEL_BOOST ||
229 ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
231 ao_flight_state = ao_flight_boost;
232 ao_launch_tick = ao_flight_tick;
234 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
238 case ao_flight_boost:
242 * accelerometer: start to fall at > 1/4 G
244 * time: boost for more than 15 seconds
246 * Detects motor burn out by the switch from acceleration to
247 * deceleration, or by waiting until the maximum burn duration
248 * (15 seconds) has past.
250 if (ao_flight_accel > ao_ground_accel + (ACCEL_G >> 2) ||
251 (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
253 ao_flight_state = ao_flight_coast;
254 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
257 /* fall through ... */
258 case ao_flight_coast:
260 /* boost/coast to apogee detect:
262 * accelerometer: integrated velocity < 200 m/s
264 * barometer: fall at least 500m from max altitude
266 * This extra state is required to avoid mis-detecting
267 * apogee due to mach transitions. For slow flights (<200m/s)
268 * we expect to transition right through this stage to
271 if (ao_flight_vel < VEL_MPS_TO_COUNT(200) ||
272 ao_flight_pres > ao_min_pres + BARO_COAST)
274 ao_flight_state = ao_flight_apogee;
275 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
278 case ao_flight_apogee:
280 /* apogee to drogue deploy:
282 * accelerometer: integrated velocity < 10m/s
284 * barometer: fall at least 10m
286 * If the barometer saturates because the flight
287 * goes over its measuring range (about 53k'),
288 * requiring a 10m fall will avoid prematurely
289 * detecting apogee; the accelerometer will take
290 * over in that case and the integrated velocity
291 * measurement should suffice to find apogee
293 if (ao_flight_vel < VEL_MPS_TO_COUNT(-10) ||
294 ao_flight_pres - BARO_APOGEE > ao_min_pres)
296 ao_ignite(ao_igniter_drogue);
297 ao_flight_state = ao_flight_drogue;
298 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
301 case ao_flight_drogue:
303 /* drogue to main deploy:
305 * accelerometer: abs(velocity) > 50m/s
307 * barometer: reach main deploy altitude
309 if (ao_flight_vel < VEL_MPS_TO_COUNT(-50) ||
310 ao_flight_vel > VEL_MPS_TO_COUNT(50) ||
311 ao_flight_pres >= ao_main_pres)
313 ao_ignite(ao_igniter_main);
314 ao_flight_state = ao_flight_main;
315 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
317 /* fall through... */
320 /* drogue/main to land:
322 * accelerometer: value stable
324 * barometer: altitude stable
326 if ((ao_interval_max_accel - ao_interval_min_accel) < ACCEL_LAND &&
327 (ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND)
329 ao_flight_state = ao_flight_landed;
330 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
333 case ao_flight_landed:
340 static __xdata struct ao_task flight_task;
345 ao_flight_state = ao_flight_startup;
346 ao_interval_min_accel = 0;
347 ao_interval_max_accel = 0x7fff;
348 ao_interval_min_pres = 0;
349 ao_interval_max_pres = 0x7fff;
350 ao_interval_end = AO_INTERVAL_TICKS;
352 ao_add_task(&flight_task, ao_flight, "flight");