altos: Added check for an accel value above 1.5g
[fw/altos] / src / 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 __pdata int16_t ao_accel_2g;
52
53 __xdata uint8_t ao_flight_force_idle;
54
55 /* Accelerometer calibration
56  *
57  * We're sampling the accelerometer through a resistor divider which
58  * consists of 5k and 10k resistors. This multiplies the values by 2/3.
59  * That goes into the cc1111 A/D converter, which is running at 11 bits
60  * of precision with the bits in the MSB of the 16 bit value. Only positive
61  * values are used, so values should range from 0-32752 for 0-3.3V. The
62  * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
63  * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
64  * for a final computation of:
65  *
66  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
67  *
68  * Zero g was measured at 16000 (we would expect 16384).
69  * Note that this value is only require to tell if the
70  * rocket is standing upright. Once that is determined,
71  * the value of the accelerometer is averaged for 100 samples
72  * to find the resting accelerometer value, which is used
73  * for all further flight computations
74  */
75
76 #define GRAVITY 9.80665
77 /* convert m/s to velocity count */
78 #define VEL_MPS_TO_COUNT(mps) (((int32_t) (((mps) / GRAVITY) * (AO_HERTZ/2))) * (int32_t) ao_accel_2g)
79
80 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
81 #define ACCEL_BOOST     ao_accel_2g
82 #define ACCEL_COAST     (ao_accel_2g >> 3)
83 #define ACCEL_INT_LAND  (ao_accel_2g >> 3)
84 #define ACCEL_VEL_MACH  VEL_MPS_TO_COUNT(200)
85 #define ACCEL_VEL_BOOST VEL_MPS_TO_COUNT(5)
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 __pdata int32_t ao_old_vel;
128 __pdata int16_t ao_old_vel_tick;
129 __xdata int32_t ao_raw_accel_sum, ao_raw_pres_sum;
130
131 /* Landing is detected by getting constant readings from both pressure and accelerometer
132  * for a fairly long time (AO_INTERVAL_TICKS)
133  */
134 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(5)
135
136 #define abs(a)  ((a) < 0 ? -(a) : (a))
137
138 void
139 ao_flight(void)
140 {
141         __pdata static uint16_t nsamples = 0;
142
143         ao_flight_adc = ao_adc_head;
144         ao_raw_accel_prev = 0;
145         ao_raw_accel = 0;
146         ao_raw_pres = 0;
147         ao_flight_tick = 0;
148         for (;;) {
149                 ao_sleep(&ao_adc_ring);
150                 while (ao_flight_adc != ao_adc_head) {
151                         __pdata uint8_t ticks;
152                         __pdata int16_t ao_vel_change;
153                         ao_flight_prev_tick = ao_flight_tick;
154
155                         /* Capture a sample */
156                         ao_raw_accel = ao_adc_ring[ao_flight_adc].accel;
157                         ao_raw_pres = ao_adc_ring[ao_flight_adc].pres;
158                         ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
159
160                         ao_flight_accel -= ao_flight_accel >> 4;
161                         ao_flight_accel += ao_raw_accel >> 4;
162                         ao_flight_pres -= ao_flight_pres >> 4;
163                         ao_flight_pres += ao_raw_pres >> 4;
164                         /* Update velocity
165                          *
166                          * The accelerometer is mounted so that
167                          * acceleration yields negative values
168                          * while deceleration yields positive values,
169                          * so subtract instead of add.
170                          */
171                         ticks = ao_flight_tick - ao_flight_prev_tick;
172                         ao_vel_change = ao_ground_accel - (((ao_raw_accel + 1) >> 1) + ((ao_raw_accel_prev + 1) >> 1));
173                         ao_raw_accel_prev = ao_raw_accel;
174
175                         /* one is a common interval */
176                         if (ticks == 1)
177                                 ao_flight_vel += (int32_t) ao_vel_change;
178                         else
179                                 ao_flight_vel += (int32_t) ao_vel_change * (int32_t) ticks;
180
181                         ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
182                 }
183
184                 if (ao_flight_pres < ao_min_pres)
185                         ao_min_pres = ao_flight_pres;
186                 if (ao_flight_vel >= 0) {
187                         if (ao_flight_vel < ao_min_vel)
188                             ao_min_vel = ao_flight_vel;
189                 } else {
190                         if (-ao_flight_vel < ao_min_vel)
191                             ao_min_vel = -ao_flight_vel;
192                 }
193
194                 switch (ao_flight_state) {
195                 case ao_flight_startup:
196
197                         /* startup state:
198                          *
199                          * Collect 512 samples of acceleration and pressure
200                          * data and average them to find the resting values
201                          */
202                         if (nsamples < 512) {
203                                 ao_raw_accel_sum += ao_raw_accel;
204                                 ao_raw_pres_sum += ao_raw_pres;
205                                 ++nsamples;
206                                 continue;
207                         }
208                         ao_ground_accel = ao_raw_accel_sum >> 9;
209                         ao_ground_pres = ao_raw_pres_sum >> 9;
210                         ao_min_pres = ao_ground_pres;
211                         ao_config_get();
212                         ao_main_pres = ao_altitude_to_pres(ao_pres_to_altitude(ao_ground_pres) + ao_config.main_deploy);
213                         ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
214                         ao_flight_vel = 0;
215                         ao_min_vel = 0;
216                         ao_old_vel = ao_flight_vel;
217                         ao_old_vel_tick = ao_flight_tick;
218
219                         /* Go to pad state if the nose is pointing up */
220                         ao_config_get();
221                         if (ao_config.accel_plus_g != 0 &&
222                             ao_config.accel_minus_g != 0 &&
223                             ao_flight_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP &&
224                             ao_flight_accel > ao_config.accel_plus_g - ACCEL_NOSE_UP &&
225                             !ao_flight_force_idle)
226                         {
227                                 /* Disable the USB controller in flight mode
228                                  * to save power
229                                  */
230                                 ao_usb_disable();
231
232                                 /* Turn on telemetry system
233                                  */
234                                 ao_rdf_set(1);
235                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
236
237                                 ao_flight_state = ao_flight_pad;
238                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
239                         } else {
240                                 ao_flight_state = ao_flight_idle;
241
242                                 /* Turn on packet system in idle mode
243                                  */
244                                 ao_packet_slave_start();
245                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
246                         }
247                         /* signal successful initialization by turning off the LED */
248                         ao_led_off(AO_LED_RED);
249                         break;
250                 case ao_flight_pad:
251
252                         /* Trim velocity
253                          *
254                          * Once a second, remove any velocity from
255                          * a second ago
256                          */
257                         if ((int16_t) (ao_flight_tick - ao_old_vel_tick) >= AO_SEC_TO_TICKS(1)) {
258                                 ao_old_vel_tick = ao_flight_tick;
259                                 ao_flight_vel -= ao_old_vel;
260                                 ao_old_vel = ao_flight_vel;
261                         }
262                         /* pad to boost:
263                          *
264                          * accelerometer: > 2g AND velocity > 5m/s
265                          *             OR
266                          * barometer: > 20m vertical motion
267                          *
268                          * The accelerometer should always detect motion before
269                          * the barometer, but we use both to make sure this
270                          * transition is detected
271                          */
272                         if ((ao_flight_accel < ao_ground_accel - ACCEL_BOOST &&
273                              ao_flight_vel > ACCEL_VEL_BOOST) ||
274                             ao_flight_pres < ao_ground_pres - BARO_LAUNCH)
275                         {
276                                 ao_flight_state = ao_flight_boost;
277                                 ao_launch_tick = ao_flight_tick;
278
279                                 /* start logging data */
280                                 ao_log_start();
281
282                                 /* Increase telemetry rate */
283                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
284
285                                 /* disable RDF beacon */
286                                 ao_rdf_set(0);
287
288                                 /* Record current GPS position by waking up GPS log tasks */
289                                 ao_wakeup(&ao_gps_data);
290                                 ao_wakeup(&ao_gps_tracking_data);
291
292                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
293                                 break;
294                         }
295                         break;
296                 case ao_flight_boost:
297
298                         /* boost to fast:
299                          *
300                          * accelerometer: start to fall at > 1/4 G
301                          *              OR
302                          * time: boost for more than 15 seconds
303                          *
304                          * Detects motor burn out by the switch from acceleration to
305                          * deceleration, or by waiting until the maximum burn duration
306                          * (15 seconds) has past.
307                          */
308                         if (ao_flight_accel > ao_ground_accel + ACCEL_COAST ||
309                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
310                         {
311                                 ao_flight_state = ao_flight_fast;
312                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
313                                 break;
314                         }
315                         break;
316                 case ao_flight_fast:
317
318                         /* fast to coast:
319                          *
320                          * accelerometer: integrated velocity < 200 m/s
321                          *               OR
322                          * barometer: fall at least 500m from max altitude
323                          *
324                          * This extra state is required to avoid mis-detecting
325                          * apogee due to mach transitions.
326                          *
327                          * XXX this is essentially a single-detector test
328                          * as the 500m altitude change would likely result
329                          * in a loss of the rocket. More data on precisely
330                          * how big a pressure change the mach transition
331                          * generates would be useful here.
332                          */
333                         if (ao_flight_vel < ACCEL_VEL_MACH ||
334                             ao_flight_pres > ao_min_pres + BARO_COAST)
335                         {
336                                 /* set min velocity to current velocity for
337                                  * apogee detect
338                                  */
339                                 ao_min_vel = abs(ao_flight_vel);
340                                 ao_flight_state = ao_flight_coast;
341                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
342                         }
343                         break;
344                 case ao_flight_coast:
345
346                         /* apogee detect: coast to drogue deploy:
347                          *
348                          * barometer: fall at least 10m
349                          *
350                          * It would be nice to use the accelerometer
351                          * to detect apogee as well, but tests have
352                          * shown that flights far from vertical would
353                          * grossly mis-detect apogee. So, for now,
354                          * we'll trust to a single sensor for this test
355                          */
356                         if (ao_flight_pres > ao_min_pres + BARO_APOGEE)
357                         {
358                                 /* ignite the drogue charge */
359                                 ao_ignite(ao_igniter_drogue);
360
361                                 /* slow down the telemetry system */
362                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
363
364                                 /* slow down the ADC sample rate */
365                                 ao_timer_set_adc_interval(10);
366
367                                 /*
368                                  * Start recording min/max accel and pres for a while
369                                  * to figure out when the rocket has landed
370                                  */
371                                 /* Set the 'last' limits to max range to prevent
372                                  * early resting detection
373                                  */
374                                 ao_interval_min_accel = 0;
375                                 ao_interval_max_accel = 0x7fff;
376                                 ao_interval_min_pres = 0;
377                                 ao_interval_max_pres = 0x7fff;
378
379                                 /* initialize interval values */
380                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
381
382                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
383                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
384
385                                 /* and enter drogue state */
386                                 ao_flight_state = ao_flight_drogue;
387                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
388                         }
389
390                         break;
391                 case ao_flight_drogue:
392
393                         /* drogue to main deploy:
394                          *
395                          * barometer: reach main deploy altitude
396                          *
397                          * Would like to use the accelerometer for this test, but
398                          * the orientation of the flight computer is unknown after
399                          * drogue deploy, so we ignore it. Could also detect
400                          * high descent rate using the pressure sensor to
401                          * recognize drogue deploy failure and eject the main
402                          * at that point. Perhaps also use the drogue sense lines
403                          * to notice continutity?
404                          */
405                         if (ao_flight_pres >= ao_main_pres)
406                         {
407                                 ao_ignite(ao_igniter_main);
408                                 ao_flight_state = ao_flight_main;
409                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
410                         }
411
412                         /* fall through... */
413                 case ao_flight_main:
414
415                         /* drogue/main to land:
416                          *
417                          * accelerometer: value stable
418                          *                           AND
419                          * barometer: altitude stable and within 1000m of the launch altitude
420                          */
421
422                         if (ao_flight_pres < ao_interval_cur_min_pres)
423                                 ao_interval_cur_min_pres = ao_flight_pres;
424                         if (ao_flight_pres > ao_interval_cur_max_pres)
425                                 ao_interval_cur_max_pres = ao_flight_pres;
426                         if (ao_flight_accel < ao_interval_cur_min_accel)
427                                 ao_interval_cur_min_accel = ao_flight_accel;
428                         if (ao_flight_accel > ao_interval_cur_max_accel)
429                                 ao_interval_cur_max_accel = ao_flight_accel;
430
431                         if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
432                                 ao_interval_max_pres = ao_interval_cur_max_pres;
433                                 ao_interval_min_pres = ao_interval_cur_min_pres;
434                                 ao_interval_max_accel = ao_interval_cur_max_accel;
435                                 ao_interval_min_accel = ao_interval_cur_min_accel;
436                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
437                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
438                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
439
440                                 if ((uint16_t) (ao_interval_max_accel - ao_interval_min_accel) < (uint16_t) ACCEL_INT_LAND &&
441                                     ao_flight_pres > ao_ground_pres - BARO_LAND &&
442                                     (uint16_t) (ao_interval_max_pres - ao_interval_min_pres) < (uint16_t) BARO_INT_LAND)
443                                 {
444                                         ao_flight_state = ao_flight_landed;
445
446                                         /* turn off the ADC capture */
447                                         ao_timer_set_adc_interval(0);
448                                         /* Enable RDF beacon */
449                                         ao_rdf_set(1);
450
451                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
452                                 }
453                         }
454                         break;
455                 case ao_flight_landed:
456                         break;
457                 }
458         }
459 }
460
461 static __xdata struct ao_task   flight_task;
462
463 void
464 ao_flight_init(void)
465 {
466         ao_flight_state = ao_flight_startup;
467         ao_add_task(&flight_task, ao_flight, "flight");
468 }