Bump NUM_CMDS to 10
[fw/altos] / ao_flight.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #ifndef AO_FLIGHT_TEST
19 #include "ao.h"
20 #endif
21
22 /* Main flight thread. */
23
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 */
33
34 /*
35  * track min/max data over a long interval to detect
36  * resting
37  */
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;
47
48 __data uint8_t ao_flight_adc;
49 __pdata int16_t ao_raw_accel, ao_raw_accel_prev, ao_raw_pres;
50
51 /* Accelerometer calibration
52  *
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:
61  *
62  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
63  *
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
70  */
71
72 #define ACCEL_G         265
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)
77
78 /*
79  * Barometer calibration
80  *
81  * We directly sample the barometer. The specs say:
82  *
83  * Pressure range: 15-115 kPa
84  * Voltage at 115kPa: 2.82
85  * Output scale: 27mV/kPa
86  *
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
93  *
94  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
95  */
96
97 #define BARO_kPa        268
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 */
103
104 /* We also have a clock, which can be used to sanity check things in
105  * case of other failures
106  */
107
108 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
109
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,
113  * it's scaled by 100
114  */
115 __data int32_t  ao_flight_vel;
116 __xdata int32_t ao_raw_accel_sum, ao_raw_pres_sum;
117
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))
121
122 /* Landing is detected by getting constant readings from both pressure and accelerometer
123  * for a fairly long time (AO_INTERVAL_TICKS)
124  */
125 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(10)
126
127 void
128 ao_flight(void)
129 {
130         __pdata static uint8_t  nsamples = 0;
131
132         ao_flight_adc = ao_adc_head;
133         ao_raw_accel_prev = 0;
134         ao_raw_accel = 0;
135         ao_raw_pres = 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;
140         for (;;) {
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);
150                 }
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;
155
156                 if (ao_flight_pres < ao_min_pres)
157                         ao_min_pres = ao_flight_pres;
158
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;
167
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;
176                 }
177
178                 switch (ao_flight_state) {
179                 case ao_flight_startup:
180
181                         /* startup state:
182                          *
183                          * Collect 100 samples of acceleration and pressure
184                          * data and average them to find the resting values
185                          */
186                         if (nsamples < 100) {
187                                 ao_raw_accel_sum += ao_raw_accel;
188                                 ao_raw_pres_sum += ao_raw_pres;
189                                 ++nsamples;
190                                 continue;
191                         }
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;
196                         ao_flight_vel = 0;
197
198                         ao_interval_end = ao_flight_tick;
199
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));
204                         } else {
205                                 ao_flight_state = ao_flight_idle;
206
207                                 /* Turn on the Green LED in idle mode
208                                  */
209                                 ao_led_on(AO_LED_GREEN);
210                                 ao_timer_set_adc_interval(100);
211                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
212                         }
213                         /* signal successful initialization by turning off the LED */
214                         ao_led_off(AO_LED_RED);
215                         break;
216                 case ao_flight_launchpad:
217
218                         /* pad to boost:
219                          *
220                          * accelerometer: > 2g
221                          *             OR
222                          * barometer: > 20m vertical motion
223                          *
224                          * The accelerometer should always detect motion before
225                          * the barometer, but we use both to make sure this
226                          * transition is detected
227                          */
228                         if (ao_flight_accel < ao_ground_accel - ACCEL_BOOST ||
229                             ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
230                         {
231                                 ao_flight_state = ao_flight_boost;
232                                 ao_launch_tick = ao_flight_tick;
233                                 ao_log_start();
234                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
235                                 break;
236                         }
237                         break;
238                 case ao_flight_boost:
239
240                         /* boost to coast:
241                          *
242                          * accelerometer: start to fall at > 1/4 G
243                          *              OR
244                          * time: boost for more than 15 seconds
245                          *
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.
249                          */
250                         if (ao_flight_accel > ao_ground_accel + (ACCEL_G >> 2) ||
251                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
252                         {
253                                 ao_flight_state = ao_flight_coast;
254                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
255                                 break;
256                         }
257                         /* fall through ... */
258                 case ao_flight_coast:
259
260                         /* boost/coast to apogee detect:
261                          *
262                          * accelerometer: integrated velocity < 200 m/s
263                          *               OR
264                          * barometer: fall at least 500m from max altitude
265                          *
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
269                          * apogee detect.
270                          */
271                         if (ao_flight_vel < VEL_MPS_TO_COUNT(200) ||
272                             ao_flight_pres > ao_min_pres + BARO_COAST)
273                         {
274                                 ao_flight_state = ao_flight_apogee;
275                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
276                         }
277                         break;
278                 case ao_flight_apogee:
279
280                         /* apogee to drogue deploy:
281                          *
282                          * accelerometer: integrated velocity < 10m/s
283                          *               OR
284                          * barometer: fall at least 10m
285                          *
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
292                          */
293                         if (ao_flight_vel < VEL_MPS_TO_COUNT(-10) ||
294                             ao_flight_pres - BARO_APOGEE > ao_min_pres)
295                         {
296                                 ao_ignite(ao_igniter_drogue);
297                                 ao_flight_state = ao_flight_drogue;
298                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
299                         }
300                         break;
301                 case ao_flight_drogue:
302
303                         /* drogue to main deploy:
304                          *
305                          * accelerometer: abs(velocity) > 50m/s
306                          *               OR
307                          * barometer: reach main deploy altitude
308                          */
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)
312                         {
313                                 ao_ignite(ao_igniter_main);
314                                 ao_flight_state = ao_flight_main;
315                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
316                         }
317                         /* fall through... */
318                 case ao_flight_main:
319
320                         /* drogue/main to land:
321                          *
322                          * accelerometer: value stable
323                          *           AND
324                          * barometer: altitude stable
325                          */
326                         if ((ao_interval_max_accel - ao_interval_min_accel) < ACCEL_LAND &&
327                              (ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND)
328                         {
329                                 ao_flight_state = ao_flight_landed;
330                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
331                         }
332                         break;
333                 case ao_flight_landed:
334                         ao_log_stop();
335                         break;
336                 }
337         }
338 }
339
340 static __xdata struct ao_task   flight_task;
341
342 void
343 ao_flight_init(void)
344 {
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;
351
352         ao_add_task(&flight_task, ao_flight, "flight");
353 }
354