1f56dab694bea189ade842888f02e8452b239902
[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 uint16_t                ao_flight_prev_tick;    /* time of previous data */
27 __pdata int16_t                 ao_flight_accel;        /* filtered acceleration */
28 __pdata int16_t                 ao_flight_pres;         /* filtered pressure */
29 __pdata int16_t                 ao_ground_pres;         /* startup pressure */
30 __pdata int16_t                 ao_ground_accel;        /* startup acceleration */
31 __pdata int16_t                 ao_min_pres;            /* minimum recorded pressure */
32 __pdata uint16_t                ao_launch_tick;         /* time of launch detect */
33 __pdata int16_t                 ao_main_pres;           /* pressure to eject main */
34
35 /*
36  * track min/max data over a long interval to detect
37  * resting
38  */
39 __pdata uint16_t                ao_interval_end;
40 __pdata int16_t                 ao_interval_cur_min_accel;
41 __pdata int16_t                 ao_interval_cur_max_accel;
42 __pdata int16_t                 ao_interval_cur_min_pres;
43 __pdata int16_t                 ao_interval_cur_max_pres;
44 __pdata int16_t                 ao_interval_min_accel;
45 __pdata int16_t                 ao_interval_max_accel;
46 __pdata int16_t                 ao_interval_min_pres;
47 __pdata int16_t                 ao_interval_max_pres;
48
49 __data uint8_t ao_flight_adc;
50 __pdata int16_t ao_raw_accel, ao_raw_accel_prev, ao_raw_pres;
51
52 /* Accelerometer calibration
53  *
54  * We're sampling the accelerometer through a resistor divider which
55  * consists of 5k and 10k resistors. This multiplies the values by 2/3.
56  * That goes into the cc1111 A/D converter, which is running at 11 bits
57  * of precision with the bits in the MSB of the 16 bit value. Only positive
58  * values are used, so values should range from 0-32752 for 0-3.3V. The
59  * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
60  * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
61  * for a final computation of:
62  *
63  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
64  *
65  * Zero g was measured at 16000 (we would expect 16384).
66  * Note that this value is only require to tell if the
67  * rocket is standing upright. Once that is determined,
68  * the value of the accelerometer is averaged for 100 samples
69  * to find the resting accelerometer value, which is used
70  * for all further flight computations
71  */
72
73 #define GRAVITY 9.80665
74 /* convert m/s to velocity count */
75 #define VEL_MPS_TO_COUNT(mps) ((int32_t) (((mps) / GRAVITY) * ACCEL_G * 100))
76
77 #define ACCEL_G         265
78 #define ACCEL_ZERO_G    16000
79 #define ACCEL_NOSE_UP   (ACCEL_ZERO_G - ACCEL_G * 2 /3)
80 #define ACCEL_BOOST     ACCEL_G * 2
81 #define ACCEL_INT_LAND  (ACCEL_G / 10)
82 #define ACCEL_VEL_LAND  VEL_MPS_TO_COUNT(10)
83 #define ACCEL_VEL_MACH  VEL_MPS_TO_COUNT(200)
84 #define ACCEL_VEL_APOGEE        VEL_MPS_TO_COUNT(2)
85 #define ACCEL_VEL_MAIN  VEL_MPS_TO_COUNT(100)
86
87 /*
88  * Barometer calibration
89  *
90  * We directly sample the barometer. The specs say:
91  *
92  * Pressure range: 15-115 kPa
93  * Voltage at 115kPa: 2.82
94  * Output scale: 27mV/kPa
95  *
96  * If we want to detect launch with the barometer, we need
97  * a large enough bump to not be fooled by noise. At typical
98  * launch elevations (0-2000m), a 200Pa pressure change cooresponds
99  * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
100  * As all of our calculations are done in 16 bits, we'll actually see a change
101  * of 16 times this though
102  *
103  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
104  */
105
106 #define BARO_kPa        268
107 #define BARO_LAUNCH     (BARO_kPa / 5)  /* .2kPa, or about 20m */
108 #define BARO_APOGEE     (BARO_kPa / 10) /* .1kPa, or about 10m */
109 #define BARO_COAST      (BARO_kPa * 5)  /* 5kpa, or about 500m */
110 #define BARO_MAIN       (BARO_kPa)      /* 1kPa, or about 100m */
111 #define BARO_INT_LAND   (BARO_kPa / 20) /* .05kPa, or about 5m */
112 #define BARO_LAND       (BARO_kPa * 10) /* 10kPa or about 1000m */
113
114 /* We also have a clock, which can be used to sanity check things in
115  * case of other failures
116  */
117
118 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
119
120 /* This value is scaled in a weird way. It's a running total of accelerometer
121  * readings minus the ground accelerometer reading. That means it measures
122  * velocity, and quite accurately too. As it gets updated 100 times a second,
123  * it's scaled by 100
124  */
125 __pdata int32_t ao_flight_vel;
126 __pdata int32_t ao_min_vel;
127 __xdata int32_t ao_raw_accel_sum, ao_raw_pres_sum;
128
129 /* Landing is detected by getting constant readings from both pressure and accelerometer
130  * for a fairly long time (AO_INTERVAL_TICKS)
131  */
132 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(20)
133
134 #define abs(a)  ((a) < 0 ? -(a) : (a))
135
136 void
137 ao_flight(void)
138 {
139         __pdata static uint8_t  nsamples = 0;
140
141         ao_flight_adc = ao_adc_head;
142         ao_raw_accel_prev = 0;
143         ao_raw_accel = 0;
144         ao_raw_pres = 0;
145         ao_interval_cur_min_pres = 0x7fff;
146         ao_interval_cur_max_pres = -0x7fff;
147         ao_interval_cur_min_accel = 0x7fff;
148         ao_interval_cur_max_accel = -0x7fff;
149         ao_flight_tick = 0;
150         for (;;) {
151                 ao_sleep(&ao_adc_ring);
152                 while (ao_flight_adc != ao_adc_head) {
153                         __pdata uint8_t ticks;
154                         __pdata int16_t ao_vel_change;
155                         ao_flight_prev_tick = ao_flight_tick;
156
157                         /* Capture a sample */
158                         ao_raw_accel = ao_adc_ring[ao_flight_adc].accel;
159                         ao_raw_pres = ao_adc_ring[ao_flight_adc].pres;
160                         ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
161
162                         /* Update velocity
163                          *
164                          * The accelerometer is mounted so that
165                          * acceleration yields negative values
166                          * while deceleration yields positive values,
167                          * so subtract instead of add.
168                          */
169                         ticks = ao_flight_tick - ao_flight_prev_tick;
170                         ao_vel_change = (((ao_raw_accel + ao_raw_accel_prev) >> 1) - ao_ground_accel);
171                         ao_raw_accel_prev = ao_raw_accel;
172
173                         /* one is a common interval */
174                         if (ticks == 1)
175                                 ao_flight_vel -= (int32_t) ao_vel_change;
176                         else
177                                 ao_flight_vel -= (int32_t) ao_vel_change * (int32_t) ticks;
178
179                         ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
180                 }
181                 ao_flight_accel -= ao_flight_accel >> 4;
182                 ao_flight_accel += ao_raw_accel >> 4;
183                 ao_flight_pres -= ao_flight_pres >> 4;
184                 ao_flight_pres += ao_raw_pres >> 4;
185
186                 if (ao_flight_pres < ao_min_pres)
187                         ao_min_pres = ao_flight_pres;
188                 if (ao_flight_vel >= 0) {
189                         if (ao_flight_vel < ao_min_vel)
190                             ao_min_vel = ao_flight_vel;
191                 } else {
192                         if (-ao_flight_vel < ao_min_vel)
193                             ao_min_vel = -ao_flight_vel;
194                 }
195
196                 if (ao_flight_pres < ao_interval_cur_min_pres)
197                         ao_interval_cur_min_pres = ao_flight_pres;
198                 if (ao_flight_pres > ao_interval_cur_max_pres)
199                         ao_interval_cur_max_pres = ao_flight_pres;
200                 if (ao_flight_accel < ao_interval_cur_min_accel)
201                         ao_interval_cur_min_accel = ao_flight_accel;
202                 if (ao_flight_accel > ao_interval_cur_max_accel)
203                         ao_interval_cur_max_accel = ao_flight_accel;
204
205                 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
206                         ao_interval_max_pres = ao_interval_cur_max_pres;
207                         ao_interval_min_pres = ao_interval_cur_min_pres;
208                         ao_interval_max_accel = ao_interval_cur_max_accel;
209                         ao_interval_min_accel = ao_interval_cur_min_accel;
210                         ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
211                         ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
212                         ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
213                 }
214
215                 switch (ao_flight_state) {
216                 case ao_flight_startup:
217
218                         /* startup state:
219                          *
220                          * Collect 100 samples of acceleration and pressure
221                          * data and average them to find the resting values
222                          */
223                         if (nsamples < 100) {
224                                 ao_raw_accel_sum += ao_raw_accel;
225                                 ao_raw_pres_sum += ao_raw_pres;
226                                 ++nsamples;
227                                 continue;
228                         }
229                         ao_ground_accel = (ao_raw_accel_sum / nsamples);
230                         ao_ground_pres = (ao_raw_pres_sum / nsamples);
231                         ao_min_pres = ao_ground_pres;
232                         ao_main_pres = ao_ground_pres - BARO_MAIN;
233                         ao_flight_vel = 0;
234                         ao_min_vel = 0;
235
236                         ao_interval_end = ao_flight_tick;
237
238                         /* Go to launchpad state if the nose is pointing up */
239                         if (ao_flight_accel < ACCEL_NOSE_UP) {
240
241                                 /* Disable the USB controller in flight mode
242                                  * to save power
243                                  */
244                                 ao_usb_disable();
245
246                                 /* Turn on telemetry system
247                                  */
248                                 ao_rdf_set(1);
249                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
250
251                                 ao_flight_state = ao_flight_launchpad;
252                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
253                         } else {
254                                 ao_flight_state = ao_flight_idle;
255
256                                 /* Turn on the Green LED in idle mode
257                                  */
258                                 ao_led_on(AO_LED_GREEN);
259                                 ao_timer_set_adc_interval(100);
260                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
261                         }
262                         /* signal successful initialization by turning off the LED */
263                         ao_led_off(AO_LED_RED);
264                         break;
265                 case ao_flight_launchpad:
266
267                         /* pad to boost:
268                          *
269                          * accelerometer: > 2g
270                          *             OR
271                          * barometer: > 20m vertical motion
272                          *
273                          * The accelerometer should always detect motion before
274                          * the barometer, but we use both to make sure this
275                          * transition is detected
276                          */
277                         if (ao_flight_accel < ao_ground_accel - ACCEL_BOOST ||
278                             ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
279                         {
280                                 ao_flight_state = ao_flight_boost;
281                                 ao_launch_tick = ao_flight_tick;
282
283                                 /* start logging data */
284                                 ao_log_start();
285
286                                 /* disable RDF beacon */
287                                 ao_rdf_set(0);
288
289                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
290                                 break;
291                         }
292                         break;
293                 case ao_flight_boost:
294
295                         /* boost to coast:
296                          *
297                          * accelerometer: start to fall at > 1/4 G
298                          *              OR
299                          * time: boost for more than 15 seconds
300                          *
301                          * Detects motor burn out by the switch from acceleration to
302                          * deceleration, or by waiting until the maximum burn duration
303                          * (15 seconds) has past.
304                          */
305                         if (ao_flight_accel > ao_ground_accel + (ACCEL_G >> 2) ||
306                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
307                         {
308                                 ao_flight_state = ao_flight_coast;
309                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
310                                 break;
311                         }
312                         break;
313                 case ao_flight_coast:
314
315                         /* coast to apogee detect:
316                          *
317                          * accelerometer: integrated velocity < 200 m/s
318                          *               OR
319                          * barometer: fall at least 500m from max altitude
320                          *
321                          * This extra state is required to avoid mis-detecting
322                          * apogee due to mach transitions.
323                          *
324                          * XXX this is essentially a single-detector test
325                          * as the 500m altitude change would likely result
326                          * in a loss of the rocket. More data on precisely
327                          * how big a pressure change the mach transition
328                          * generates would be useful here.
329                          */
330                         if (ao_flight_vel < ACCEL_VEL_MACH ||
331                             ao_flight_pres > ao_min_pres + BARO_COAST)
332                         {
333                                 /* set min velocity to current velocity for
334                                  * apogee detect
335                                  */
336                                 ao_min_vel = ao_flight_vel;
337                                 ao_flight_state = ao_flight_apogee;
338                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
339                         }
340                         break;
341                 case ao_flight_apogee:
342
343                         /* apogee detect to drogue deploy:
344                          *
345                          * accelerometer: abs(velocity) > min_velocity + 2m/s
346                          *               OR
347                          * barometer: fall at least 10m
348                          *
349                          * If the barometer saturates because the flight
350                          * goes over its measuring range (about 53k'),
351                          * requiring a 10m fall will avoid prematurely
352                          * detecting apogee; the accelerometer will take
353                          * over in that case and the integrated velocity
354                          * measurement should suffice to find apogee
355                          */
356                         if (abs(ao_flight_vel) > ao_min_vel + ACCEL_VEL_APOGEE ||
357                             ao_flight_pres > ao_min_pres + BARO_APOGEE)
358                         {
359                                 /* ignite the drogue charge */
360                                 ao_ignite(ao_igniter_drogue);
361
362                                 /* slow down the telemetry system */
363                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
364
365                                 /* Enable RDF beacon */
366                                 ao_rdf_set(1);
367
368                                 ao_flight_state = ao_flight_drogue;
369                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
370                         }
371                         break;
372                 case ao_flight_drogue:
373
374                         /* drogue to main deploy:
375                          *
376                          * accelerometer: abs(velocity) > 100m/s (in case the drogue failed)
377                          *               OR
378                          * barometer: reach main deploy altitude
379                          */
380                         if (ao_flight_vel < -ACCEL_VEL_MAIN ||
381                             ao_flight_vel > ACCEL_VEL_MAIN ||
382                             ao_flight_pres >= ao_main_pres)
383                         {
384                                 ao_ignite(ao_igniter_main);
385                                 ao_flight_state = ao_flight_main;
386                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
387                         }
388                         /* fall through... */
389                 case ao_flight_main:
390
391                         /* drogue/main to land:
392                          *
393                          * accelerometer: value stable and velocity less than 10m/s
394                          *                           OR
395                          * barometer: altitude stable and within 1000m of the launch altitude
396                          */
397                         if ((abs(ao_flight_vel) < ACCEL_VEL_LAND &&
398                              (ao_interval_max_accel - ao_interval_min_accel) < ACCEL_INT_LAND) ||
399                             (ao_flight_pres > ao_ground_pres - BARO_LAND &&
400                              (ao_interval_max_pres - ao_interval_min_pres) < BARO_INT_LAND))
401                         {
402                                 ao_flight_state = ao_flight_landed;
403
404                                 /* turn off the ADC capture */
405                                 ao_timer_set_adc_interval(0);
406
407                                 /* stop logging data */
408                                 ao_log_stop();
409
410                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
411                         }
412                         break;
413                 case ao_flight_landed:
414                         break;
415                 }
416         }
417 }
418
419 #define AO_ACCEL_COUNT_TO_MSS(count)    ((count) / 27)
420 #define AO_VEL_COUNT_TO_MS(count)       ((int16_t) ((count) / 2700))
421
422 static void
423 ao_flight_status(void)
424 {
425         printf("STATE: %7s accel: %d speed: %d altitude: %d\n",
426                ao_state_names[ao_flight_state],
427                AO_ACCEL_COUNT_TO_MSS(ACCEL_ZERO_G - ao_flight_accel),
428                AO_VEL_COUNT_TO_MS(ao_flight_vel),
429                ao_pres_to_altitude(ao_flight_pres));
430 }
431
432 static __xdata struct ao_task   flight_task;
433
434 __code struct ao_cmds ao_flight_cmds[] = {
435         { 'f', ao_flight_status,        "f                                  Display current flight state" },
436         { 0, ao_flight_status, NULL }
437 };
438
439 void
440 ao_flight_init(void)
441 {
442         ao_flight_state = ao_flight_startup;
443         ao_interval_min_accel = 0;
444         ao_interval_max_accel = 0x7fff;
445         ao_interval_min_pres = 0;
446         ao_interval_max_pres = 0x7fff;
447         ao_interval_end = AO_INTERVAL_TICKS;
448
449         ao_add_task(&flight_task, ao_flight, "flight");
450         ao_cmd_register(&ao_flight_cmds[0]);
451 }