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_time; /* 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 __xdata int16_t ao_accel, ao_prev_accel, ao_pres;
51 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
53 /* Accelerometer calibration
55 * We're sampling the accelerometer through a resistor divider which
56 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
57 * That goes into the cc1111 A/D converter, which is running at 11 bits
58 * of precision with the bits in the MSB of the 16 bit value. Only positive
59 * values are used, so values should range from 0-32752 for 0-3.3V. The
60 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
61 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
62 * for a final computation of:
64 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
66 * Zero g was measured at 16000 (we would expect 16384)
70 #define ACCEL_ZERO_G 16000
71 #define ACCEL_NOSE_UP (ACCEL_ZERO_G - ACCEL_G * 2 /3)
72 #define ACCEL_BOOST (ACCEL_NOSE_UP - ACCEL_G * 2)
73 #define ACCEL_LAND (ACCEL_G / 10)
76 * Barometer calibration
78 * We directly sample the barometer. The specs say:
80 * Pressure range: 15-115 kPa
81 * Voltage at 115kPa: 2.82
82 * Output scale: 27mV/kPa
84 * If we want to detect launch with the barometer, we need
85 * a large enough bump to not be fooled by noise. At typical
86 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
87 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
88 * As all of our calculations are done in 16 bits, we'll actually see a change
89 * of 16 times this though
91 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
95 #define BARO_LAUNCH (BARO_kPa / 5) /* .2kPa */
96 #define BARO_APOGEE (BARO_kPa / 10) /* .1kPa */
97 #define BARO_MAIN (BARO_kPa) /* 1kPa */
98 #define BARO_LAND (BARO_kPa / 20) /* .05kPa */
100 /* We also have a clock, which can be used to sanity check things in
101 * case of other failures
104 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
106 /* This value is scaled in a weird way. It's a running total of accelerometer
107 * readings minus the ground accelerometer reading. That means it measures
108 * velocity, and quite accurately too. As it gets updated 100 times a second,
111 __data int32_t ao_flight_vel;
113 /* convert m/s to velocity count */
114 #define VEL_MPS_TO_COUNT(mps) ((int32_t) ((int32_t) (mps) * (int32_t) 100 / (int32_t) ACCEL_G))
119 __pdata static uint8_t nsamples = 0;
121 ao_flight_adc = ao_adc_head;
126 ao_sleep(&ao_adc_ring);
127 while (ao_flight_adc != ao_adc_head) {
128 ao_accel = ao_adc_ring[ao_flight_adc].accel;
129 ao_pres = ao_adc_ring[ao_flight_adc].pres;
130 ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
131 ao_flight_vel += (int32_t) (((ao_accel + ao_prev_accel) >> 4) - (ao_ground_accel << 1));
132 ao_prev_accel = ao_accel;
133 ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
135 ao_flight_accel -= ao_flight_accel >> 4;
136 ao_flight_accel += ao_accel >> 4;
137 ao_flight_pres -= ao_flight_pres >> 4;
138 ao_flight_pres += ao_pres >> 4;
140 if (ao_flight_pres < ao_min_pres)
141 ao_min_pres = ao_flight_pres;
143 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
144 ao_interval_max_pres = ao_interval_cur_max_pres;
145 ao_interval_min_pres = ao_interval_cur_min_pres;
146 ao_interval_max_accel = ao_interval_cur_max_accel;
147 ao_interval_min_accel = ao_interval_cur_min_accel;
148 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
151 switch (ao_flight_state) {
152 case ao_flight_startup:
153 if (nsamples < 100) {
157 ao_ground_accel = ao_flight_accel;
158 ao_ground_pres = ao_flight_pres;
159 ao_min_pres = ao_flight_pres;
160 ao_main_pres = ao_ground_pres - BARO_MAIN;
163 ao_interval_end = ao_flight_tick;
165 /* Go to launchpad state if the nose is pointing up */
166 if (ao_flight_accel < ACCEL_NOSE_UP) {
167 ao_flight_state = ao_flight_launchpad;
168 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
170 ao_flight_state = ao_flight_idle;
172 /* Turn on the Green LED in idle mode
173 * This also happens to bring the USB up for the TI board
175 ao_led_on(AO_LED_GREEN);
176 ao_timer_set_adc_interval(100);
177 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
179 /* signal successful initialization by turning off the LED */
180 ao_led_off(AO_LED_RED);
182 case ao_flight_launchpad:
186 * accelerometer: > 2g
187 * barometer: > 20m vertical motion
189 if (ao_flight_accel < ACCEL_BOOST ||
190 ao_flight_pres + BARO_LAUNCH < ao_ground_pres)
192 ao_flight_state = ao_flight_boost;
194 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
198 case ao_flight_boost:
202 * accelerometer: start to fall at > 1/4 G
203 * time: boost for more than 15 seconds
205 if (ao_flight_accel > ao_ground_accel + (ACCEL_G >> 2) ||
206 (int16_t) (ao_flight_tick - ao_launch_time) > BOOST_TICKS_MAX)
208 ao_flight_state = ao_flight_coast;
209 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
213 case ao_flight_coast:
215 /* coast to apogee detect:
217 * accelerometer: integrated velocity < 200 m/s
218 * barometer: fall at least 500m from max altitude
220 if (ao_flight_vel < VEL_MPS_TO_COUNT(200) ||
221 ao_flight_pres - (5 * BARO_kPa) > ao_min_pres)
223 ao_flight_state = ao_flight_apogee;
224 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
227 case ao_flight_apogee:
229 /* apogee to drogue deploy:
231 * accelerometer: integrated velocity < 10m/s
232 * barometer: fall at least 10m
234 if (ao_flight_vel < VEL_MPS_TO_COUNT(-10) ||
235 ao_flight_pres - BARO_APOGEE > ao_min_pres)
237 ao_ignite(ao_igniter_drogue);
238 ao_flight_state = ao_flight_drogue;
239 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
242 case ao_flight_drogue:
244 /* drogue to main deploy:
246 * accelerometer: abs(velocity) > 50m/s
247 * barometer: reach main deploy altitude
249 if (ao_flight_vel < VEL_MPS_TO_COUNT(-50) ||
250 ao_flight_vel > VEL_MPS_TO_COUNT(50) ||
251 ao_flight_pres >= ao_main_pres)
253 ao_ignite(ao_igniter_main);
254 ao_flight_state = ao_flight_main;
255 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
257 /* fall through... */
260 /* drogue/main to land:
262 * accelerometer: value stable
263 * barometer: altitude stable
265 if ((ao_interval_max_accel - ao_interval_min_accel) < ACCEL_LAND ||
266 (ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND)
268 ao_flight_state = ao_flight_landed;
269 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
272 case ao_flight_landed:
279 static __xdata struct ao_task flight_task;
284 ao_flight_state = ao_flight_startup;
285 ao_interval_min_accel = 0;
286 ao_interval_max_accel = 0x7fff;
287 ao_interval_min_pres = 0;
288 ao_interval_max_pres = 0x7fff;
289 ao_interval_end = AO_INTERVAL_TICKS;
291 ao_add_task(&flight_task, ao_flight, "flight");