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 __data enum flight_state ao_flight_state; /* current flight state */
24 __data uint16_t ao_flight_tick; /* time of last data */
25 __data int16_t ao_flight_accel; /* filtered acceleration */
26 __data int16_t ao_flight_pres; /* filtered pressure */
27 __data int16_t ao_ground_pres; /* startup pressure */
28 __data int16_t ao_ground_accel; /* startup acceleration */
29 __data int16_t ao_min_pres; /* minimum recorded pressure */
30 __data uint16_t ao_launch_time; /* time of launch detect */
31 __data int16_t ao_main_pres; /* pressure to eject main */
34 * track min/max data over a long interval to detect
37 __data uint16_t ao_interval_end;
38 __data int16_t ao_interval_cur_min_accel;
39 __data int16_t ao_interval_cur_max_accel;
40 __data int16_t ao_interval_cur_min_pres;
41 __data int16_t ao_interval_cur_max_pres;
42 __data int16_t ao_interval_min_accel;
43 __data int16_t ao_interval_max_accel;
44 __data int16_t ao_interval_min_pres;
45 __data 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 __data 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 if (ao_flight_accel < ACCEL_NOSE_UP) {
138 ao_flight_state = ao_flight_launchpad;
141 ao_flight_state = ao_flight_idle;
144 /* signal successful initialization by turning off the LED */
145 ao_led_off(AO_LED_RED);
147 case ao_flight_launchpad:
148 if (ao_flight_accel < ACCEL_BOOST ||
149 ao_flight_pres + BARO_LAUNCH < ao_ground_pres)
151 ao_flight_state = ao_flight_boost;
157 case ao_flight_boost:
158 if (ao_flight_accel > ACCEL_ZERO_G ||
159 (int16_t) (ao_flight_data.tick - ao_launch_time) > BOOST_TICKS_MAX)
161 ao_flight_state = ao_flight_coast;
166 case ao_flight_coast:
167 if (ao_flight_pres < ao_min_pres)
168 ao_min_pres = ao_flight_pres;
169 if (ao_flight_pres - BARO_APOGEE > ao_min_pres) {
170 ao_flight_state = ao_flight_apogee;
174 case ao_flight_apogee:
175 // ao_ignite(AO_IGNITE_DROGUE);
176 ao_flight_state = ao_flight_drogue;
179 case ao_flight_drogue:
180 if (ao_flight_pres >= ao_main_pres) {
181 // ao_ignite(AO_IGNITE_MAIN);
182 ao_flight_state = ao_flight_main;
185 if ((ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND) {
186 ao_flight_state = ao_flight_landed;
191 if ((ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND) {
192 ao_flight_state = ao_flight_landed;
196 case ao_flight_landed:
203 static __xdata struct ao_task flight_task;
208 ao_flight_state = ao_flight_startup;
209 ao_interval_min_accel = 0;
210 ao_interval_max_accel = 0x7fff;
211 ao_interval_min_pres = 0;
212 ao_interval_max_pres = 0x7fff;
213 ao_interval_end = AO_INTERVAL_TICKS;
215 ao_add_task(&flight_task, ao_flight);