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.
20 /* Main flight thread. */
22 __xdata struct ao_adc ao_flight_data; /* last acquired data */
23 __pdata enum flight_state ao_flight_state; /* current flight state */
24 __pdata uint16_t ao_flight_tick; /* time of last data */
25 __pdata int16_t ao_flight_accel; /* filtered acceleration */
26 __pdata int16_t ao_flight_pres; /* filtered pressure */
27 __pdata int16_t ao_ground_pres; /* startup pressure */
28 __pdata int16_t ao_ground_accel; /* startup acceleration */
29 __pdata int16_t ao_min_pres; /* minimum recorded pressure */
30 __pdata uint16_t ao_launch_time; /* time of launch detect */
31 __pdata int16_t ao_main_pres; /* pressure to eject main */
34 * track min/max data over a long interval to detect
37 __pdata uint16_t ao_interval_end;
38 __pdata int16_t ao_interval_cur_min_accel;
39 __pdata int16_t ao_interval_cur_max_accel;
40 __pdata int16_t ao_interval_cur_min_pres;
41 __pdata int16_t ao_interval_cur_max_pres;
42 __pdata int16_t ao_interval_min_accel;
43 __pdata int16_t ao_interval_max_accel;
44 __pdata int16_t ao_interval_min_pres;
45 __pdata int16_t ao_interval_max_pres;
47 #define AO_INTERVAL_TICKS AO_SEC_TO_TICKS(5)
49 /* Accelerometer calibration
51 * We're sampling the accelerometer through a resistor divider which
52 * consists of 5k and 10k resistors. This multiplies the values by 2/3.
53 * That goes into the cc1111 A/D converter, which is running at 11 bits
54 * of precision with the bits in the MSB of the 16 bit value. Only positive
55 * values are used, so values should range from 0-32752 for 0-3.3V. The
56 * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
57 * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
58 * for a final computation of:
60 * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
62 * Zero g was measured at 16000 (we would expect 16384)
66 #define ACCEL_ZERO_G 16000
67 #define ACCEL_NOSE_UP (ACCEL_ZERO_G - ACCEL_G * 2 /3)
68 #define ACCEL_BOOST (ACCEL_NOSE_UP - ACCEL_G * 2)
71 * Barometer calibration
73 * We directly sample the barometer. The specs say:
75 * Pressure range: 15-115 kPa
76 * Voltage at 115kPa: 2.82
77 * Output scale: 27mV/kPa
79 * If we want to detect launch with the barometer, we need
80 * a large enough bump to not be fooled by noise. At typical
81 * launch elevations (0-2000m), a 200Pa pressure change cooresponds
82 * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
83 * As all of our calculations are done in 16 bits, we'll actually see a change
84 * of 16 times this though
86 * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
90 #define BARO_LAUNCH (BARO_kPa / 5) /* .2kPa */
91 #define BARO_APOGEE (BARO_kPa / 10) /* .1kPa */
92 #define BARO_MAIN (BARO_kPa) /* 1kPa */
93 #define BARO_LAND (BARO_kPa / 20) /* .05kPa */
95 /* We also have a clock, which can be used to sanity check things in
96 * case of other failures
99 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(10)
104 __pdata static uint8_t nsamples = 0;
107 ao_sleep(&ao_adc_ring);
108 ao_adc_get(&ao_flight_data);
109 ao_flight_accel -= ao_flight_accel >> 4;
110 ao_flight_accel += ao_flight_data.accel >> 4;
111 ao_flight_pres -= ao_flight_pres >> 4;
112 ao_flight_pres += ao_flight_data.pres >> 4;
113 ao_flight_tick = ao_time();
115 ao_flight_tick = ao_time();
116 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
117 ao_interval_max_pres = ao_interval_cur_max_pres;
118 ao_interval_min_pres = ao_interval_cur_min_pres;
119 ao_interval_max_accel = ao_interval_cur_max_accel;
120 ao_interval_min_accel = ao_interval_cur_min_accel;
121 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
124 switch (ao_flight_state) {
125 case ao_flight_startup:
126 if (nsamples < 100) {
130 ao_ground_accel = ao_flight_accel;
131 ao_ground_pres = ao_flight_pres;
132 ao_min_pres = ao_flight_pres;
133 ao_main_pres = ao_ground_pres - BARO_MAIN;
135 ao_interval_end = ao_flight_tick;
137 /* Go to launchpad state if the nose is pointing up and the battery is charged */
138 if (ao_flight_accel < ACCEL_NOSE_UP && ao_flight_data.v_batt > 23000) {
139 ao_flight_state = ao_flight_launchpad;
140 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
142 ao_flight_state = ao_flight_idle;
144 /* Turn on the Green LED in idle mode
145 * This also happens to bring the USB up for the TI board
147 ao_led_on(AO_LED_GREEN);
148 ao_timer_set_adc_interval(100);
149 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
151 /* signal successful initialization by turning off the LED */
152 ao_led_off(AO_LED_RED);
154 case ao_flight_launchpad:
155 if (ao_flight_accel < ACCEL_BOOST ||
156 ao_flight_pres + BARO_LAUNCH < ao_ground_pres)
158 ao_flight_state = ao_flight_boost;
160 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
164 case ao_flight_boost:
165 if (ao_flight_accel > ACCEL_ZERO_G ||
166 (int16_t) (ao_flight_data.tick - ao_launch_time) > BOOST_TICKS_MAX)
168 ao_flight_state = ao_flight_coast;
169 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
173 case ao_flight_coast:
174 if (ao_flight_pres < ao_min_pres)
175 ao_min_pres = ao_flight_pres;
176 if (ao_flight_pres - BARO_APOGEE > ao_min_pres) {
177 ao_flight_state = ao_flight_apogee;
178 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
181 case ao_flight_apogee:
182 // ao_ignite(AO_IGNITE_DROGUE);
183 ao_flight_state = ao_flight_drogue;
184 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
186 case ao_flight_drogue:
187 if (ao_flight_pres >= ao_main_pres) {
188 // ao_ignite(AO_IGNITE_MAIN);
189 ao_flight_state = ao_flight_main;
190 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
192 if ((ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND) {
193 ao_flight_state = ao_flight_landed;
194 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
198 if ((ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND) {
199 ao_flight_state = ao_flight_landed;
200 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
203 case ao_flight_landed:
210 static __xdata struct ao_task flight_task;
215 ao_flight_state = ao_flight_startup;
216 ao_interval_min_accel = 0;
217 ao_interval_max_accel = 0x7fff;
218 ao_interval_min_pres = 0;
219 ao_interval_max_pres = 0x7fff;
220 ao_interval_end = AO_INTERVAL_TICKS;
222 ao_add_task(&flight_task, ao_flight, "flight");