Steal C code from ao-view
[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 int32_t accel_vel_mach;
88 int32_t accel_vel_boost;
89
90 /*
91  * Barometer calibration
92  *
93  * We directly sample the barometer. The specs say:
94  *
95  * Pressure range: 15-115 kPa
96  * Voltage at 115kPa: 2.82
97  * Output scale: 27mV/kPa
98  *
99  * If we want to detect launch with the barometer, we need
100  * a large enough bump to not be fooled by noise. At typical
101  * launch elevations (0-2000m), a 200Pa pressure change cooresponds
102  * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
103  * As all of our calculations are done in 16 bits, we'll actually see a change
104  * of 16 times this though
105  *
106  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
107  */
108
109 #define BARO_kPa        268
110 #define BARO_LAUNCH     (BARO_kPa / 5)  /* .2kPa, or about 20m */
111 #define BARO_APOGEE     (BARO_kPa / 10) /* .1kPa, or about 10m */
112 #define BARO_COAST      (BARO_kPa * 5)  /* 5kpa, or about 500m */
113 #define BARO_MAIN       (BARO_kPa)      /* 1kPa, or about 100m */
114 #define BARO_INT_LAND   (BARO_kPa / 20) /* .05kPa, or about 5m */
115 #define BARO_LAND       (BARO_kPa * 10) /* 10kPa or about 1000m */
116
117 /* We also have a clock, which can be used to sanity check things in
118  * case of other failures
119  */
120
121 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
122
123 /* This value is scaled in a weird way. It's a running total of accelerometer
124  * readings minus the ground accelerometer reading. That means it measures
125  * velocity, and quite accurately too. As it gets updated 100 times a second,
126  * it's scaled by 100
127  */
128 __pdata int32_t ao_flight_vel;
129 __pdata int32_t ao_min_vel;
130 __pdata int32_t ao_old_vel;
131 __pdata int16_t ao_old_vel_tick;
132 __xdata int32_t ao_raw_accel_sum, ao_raw_pres_sum;
133
134 /* Landing is detected by getting constant readings from both pressure and accelerometer
135  * for a fairly long time (AO_INTERVAL_TICKS)
136  */
137 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(5)
138
139 #define abs(a)  ((a) < 0 ? -(a) : (a))
140
141 void
142 ao_flight(void)
143 {
144         __pdata static uint16_t nsamples = 0;
145
146         ao_flight_adc = ao_adc_head;
147         ao_raw_accel_prev = 0;
148         ao_raw_accel = 0;
149         ao_raw_pres = 0;
150         ao_flight_tick = 0;
151         for (;;) {
152                 ao_sleep(&ao_adc_ring);
153                 while (ao_flight_adc != ao_adc_head) {
154                         __pdata uint8_t ticks;
155                         __pdata int16_t ao_vel_change;
156                         ao_flight_prev_tick = ao_flight_tick;
157
158                         /* Capture a sample */
159                         ao_raw_accel = ao_adc_ring[ao_flight_adc].accel;
160                         ao_raw_pres = ao_adc_ring[ao_flight_adc].pres;
161                         ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
162
163                         ao_flight_accel -= ao_flight_accel >> 4;
164                         ao_flight_accel += ao_raw_accel >> 4;
165                         ao_flight_pres -= ao_flight_pres >> 4;
166                         ao_flight_pres += ao_raw_pres >> 4;
167                         /* Update velocity
168                          *
169                          * The accelerometer is mounted so that
170                          * acceleration yields negative values
171                          * while deceleration yields positive values,
172                          * so subtract instead of add.
173                          */
174                         ticks = ao_flight_tick - ao_flight_prev_tick;
175                         ao_vel_change = ao_ground_accel - (((ao_raw_accel + 1) >> 1) + ((ao_raw_accel_prev + 1) >> 1));
176                         ao_raw_accel_prev = ao_raw_accel;
177
178                         /* one is a common interval */
179                         if (ticks == 1)
180                                 ao_flight_vel += (int32_t) ao_vel_change;
181                         else
182                                 ao_flight_vel += (int32_t) ao_vel_change * (int32_t) ticks;
183
184                         ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
185                 }
186
187                 if (ao_flight_pres < ao_min_pres)
188                         ao_min_pres = ao_flight_pres;
189                 if (ao_flight_vel >= 0) {
190                         if (ao_flight_vel < ao_min_vel)
191                             ao_min_vel = ao_flight_vel;
192                 } else {
193                         if (-ao_flight_vel < ao_min_vel)
194                             ao_min_vel = -ao_flight_vel;
195                 }
196
197                 switch (ao_flight_state) {
198                 case ao_flight_startup:
199
200                         /* startup state:
201                          *
202                          * Collect 1000 samples of acceleration and pressure
203                          * data and average them to find the resting values
204                          */
205                         if (nsamples < 1000) {
206                                 ao_raw_accel_sum += ao_raw_accel;
207                                 ao_raw_pres_sum += ao_raw_pres;
208                                 ++nsamples;
209                                 continue;
210                         }
211                         ao_ground_accel = (ao_raw_accel_sum / nsamples);
212                         ao_ground_pres = (ao_raw_pres_sum / nsamples);
213                         ao_min_pres = ao_ground_pres;
214                         ao_config_get();
215                         ao_main_pres = ao_altitude_to_pres(ao_pres_to_altitude(ao_ground_pres) + ao_config.main_deploy);
216                         ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
217                         accel_vel_mach = ACCEL_VEL_MACH;
218                         accel_vel_boost = ACCEL_VEL_BOOST;
219                         ao_flight_vel = 0;
220                         ao_min_vel = 0;
221                         ao_old_vel = ao_flight_vel;
222                         ao_old_vel_tick = ao_flight_tick;
223
224                         /* Go to pad state if the nose is pointing up */
225                         ao_config_get();
226                         if (ao_config.accel_plus_g != 0 &&
227                             ao_config.accel_minus_g != 0 &&
228                             ao_flight_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP &&
229                             !ao_flight_force_idle)
230                         {
231                                 /* Disable the USB controller in flight mode
232                                  * to save power
233                                  */
234                                 ao_usb_disable();
235
236                                 /* Turn on telemetry system
237                                  */
238                                 ao_rdf_set(1);
239                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
240
241                                 ao_flight_state = ao_flight_pad;
242                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
243                         } else {
244                                 ao_flight_state = ao_flight_idle;
245
246                                 /* Turn on packet system in idle mode
247                                  */
248                                 ao_packet_slave_start();
249                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
250                         }
251                         /* signal successful initialization by turning off the LED */
252                         ao_led_off(AO_LED_RED);
253                         break;
254                 case ao_flight_pad:
255
256                         /* Trim velocity
257                          *
258                          * Once a second, remove any velocity from
259                          * a second ago
260                          */
261                         if ((int16_t) (ao_flight_tick - ao_old_vel_tick) >= AO_SEC_TO_TICKS(1)) {
262                                 ao_old_vel_tick = ao_flight_tick;
263                                 ao_flight_vel -= ao_old_vel;
264                                 ao_old_vel = ao_flight_vel;
265                         }
266                         /* pad to boost:
267                          *
268                          * accelerometer: > 2g AND velocity > 5m/s
269                          *             OR
270                          * barometer: > 20m vertical motion
271                          *
272                          * The accelerometer should always detect motion before
273                          * the barometer, but we use both to make sure this
274                          * transition is detected
275                          */
276                         if ((ao_flight_accel < ao_ground_accel - ACCEL_BOOST &&
277                              ao_flight_vel > ACCEL_VEL_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                                 /* Increase telemetry rate */
287                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
288
289                                 /* disable RDF beacon */
290                                 ao_rdf_set(0);
291
292                                 /* Record current GPS position by waking up GPS log tasks */
293                                 ao_wakeup(&ao_gps_data);
294                                 ao_wakeup(&ao_gps_tracking_data);
295
296                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
297                                 break;
298                         }
299                         break;
300                 case ao_flight_boost:
301
302                         /* boost to fast:
303                          *
304                          * accelerometer: start to fall at > 1/4 G
305                          *              OR
306                          * time: boost for more than 15 seconds
307                          *
308                          * Detects motor burn out by the switch from acceleration to
309                          * deceleration, or by waiting until the maximum burn duration
310                          * (15 seconds) has past.
311                          */
312                         if (ao_flight_accel > ao_ground_accel + ACCEL_COAST ||
313                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
314                         {
315                                 ao_flight_state = ao_flight_fast;
316                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
317                                 break;
318                         }
319                         break;
320                 case ao_flight_fast:
321
322                         /* fast to coast:
323                          *
324                          * accelerometer: integrated velocity < 200 m/s
325                          *               OR
326                          * barometer: fall at least 500m from max altitude
327                          *
328                          * This extra state is required to avoid mis-detecting
329                          * apogee due to mach transitions.
330                          *
331                          * XXX this is essentially a single-detector test
332                          * as the 500m altitude change would likely result
333                          * in a loss of the rocket. More data on precisely
334                          * how big a pressure change the mach transition
335                          * generates would be useful here.
336                          */
337                         if (ao_flight_vel < ACCEL_VEL_MACH ||
338                             ao_flight_pres > ao_min_pres + BARO_COAST)
339                         {
340                                 /* set min velocity to current velocity for
341                                  * apogee detect
342                                  */
343                                 ao_min_vel = abs(ao_flight_vel);
344                                 ao_flight_state = ao_flight_coast;
345                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
346                         }
347                         break;
348                 case ao_flight_coast:
349
350                         /* apogee detect: coast to drogue deploy:
351                          *
352                          * barometer: fall at least 10m
353                          *
354                          * It would be nice to use the accelerometer
355                          * to detect apogee as well, but tests have
356                          * shown that flights far from vertical would
357                          * grossly mis-detect apogee. So, for now,
358                          * we'll trust to a single sensor for this test
359                          */
360                         if (ao_flight_pres > ao_min_pres + BARO_APOGEE)
361                         {
362                                 /* ignite the drogue charge */
363                                 ao_ignite(ao_igniter_drogue);
364
365                                 /* slow down the telemetry system */
366                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
367
368                                 /* slow down the ADC sample rate */
369                                 ao_timer_set_adc_interval(10);
370
371                                 /*
372                                  * Start recording min/max accel and pres for a while
373                                  * to figure out when the rocket has landed
374                                  */
375                                 /* Set the 'last' limits to max range to prevent
376                                  * early resting detection
377                                  */
378                                 ao_interval_min_accel = 0;
379                                 ao_interval_max_accel = 0x7fff;
380                                 ao_interval_min_pres = 0;
381                                 ao_interval_max_pres = 0x7fff;
382
383                                 /* initialize interval values */
384                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
385
386                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
387                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
388
389                                 /* and enter drogue state */
390                                 ao_flight_state = ao_flight_drogue;
391                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
392                         }
393
394                         break;
395                 case ao_flight_drogue:
396
397                         /* drogue to main deploy:
398                          *
399                          * barometer: reach main deploy altitude
400                          *
401                          * Would like to use the accelerometer for this test, but
402                          * the orientation of the flight computer is unknown after
403                          * drogue deploy, so we ignore it. Could also detect
404                          * high descent rate using the pressure sensor to
405                          * recognize drogue deploy failure and eject the main
406                          * at that point. Perhaps also use the drogue sense lines
407                          * to notice continutity?
408                          */
409                         if (ao_flight_pres >= ao_main_pres)
410                         {
411                                 ao_ignite(ao_igniter_main);
412                                 ao_flight_state = ao_flight_main;
413                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
414                         }
415
416                         /* fall through... */
417                 case ao_flight_main:
418
419                         /* drogue/main to land:
420                          *
421                          * accelerometer: value stable
422                          *                           AND
423                          * barometer: altitude stable and within 1000m of the launch altitude
424                          */
425
426                         if (ao_flight_pres < ao_interval_cur_min_pres)
427                                 ao_interval_cur_min_pres = ao_flight_pres;
428                         if (ao_flight_pres > ao_interval_cur_max_pres)
429                                 ao_interval_cur_max_pres = ao_flight_pres;
430                         if (ao_flight_accel < ao_interval_cur_min_accel)
431                                 ao_interval_cur_min_accel = ao_flight_accel;
432                         if (ao_flight_accel > ao_interval_cur_max_accel)
433                                 ao_interval_cur_max_accel = ao_flight_accel;
434
435                         if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
436                                 ao_interval_max_pres = ao_interval_cur_max_pres;
437                                 ao_interval_min_pres = ao_interval_cur_min_pres;
438                                 ao_interval_max_accel = ao_interval_cur_max_accel;
439                                 ao_interval_min_accel = ao_interval_cur_min_accel;
440                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
441                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
442                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
443
444                                 if ((uint16_t) (ao_interval_max_accel - ao_interval_min_accel) < (uint16_t) ACCEL_INT_LAND &&
445                                     ao_flight_pres > ao_ground_pres - BARO_LAND &&
446                                     (uint16_t) (ao_interval_max_pres - ao_interval_min_pres) < (uint16_t) BARO_INT_LAND)
447                                 {
448                                         ao_flight_state = ao_flight_landed;
449
450                                         /* turn off the ADC capture */
451                                         ao_timer_set_adc_interval(0);
452                                         /* Enable RDF beacon */
453                                         ao_rdf_set(1);
454
455                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
456                                 }
457                         }
458                         break;
459                 case ao_flight_landed:
460                         break;
461                 }
462         }
463 }
464
465 static __xdata struct ao_task   flight_task;
466
467 void
468 ao_flight_init(void)
469 {
470         ao_flight_state = ao_flight_startup;
471         ao_interval_min_accel = 0;
472         ao_interval_max_accel = 0x7fff;
473         ao_interval_min_pres = 0;
474         ao_interval_max_pres = 0x7fff;
475         ao_interval_end = AO_INTERVAL_TICKS;
476
477         ao_add_task(&flight_task, ao_flight, "flight");
478 }