fix typo in comment
[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 /* Accelerometer calibration
54  *
55  * We're sampling the accelerometer through a resistor divider which
56  * consists of 5k and 10k resistors. This multiplies the values by 2/3.
57  * That goes into the cc1111 A/D converter, which is running at 11 bits
58  * of precision with the bits in the MSB of the 16 bit value. Only positive
59  * values are used, so values should range from 0-32752 for 0-3.3V. The
60  * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
61  * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
62  * for a final computation of:
63  *
64  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
65  *
66  * Zero g was measured at 16000 (we would expect 16384).
67  * Note that this value is only require to tell if the
68  * rocket is standing upright. Once that is determined,
69  * the value of the accelerometer is averaged for 100 samples
70  * to find the resting accelerometer value, which is used
71  * for all further flight computations
72  */
73
74 #define GRAVITY 9.80665
75 /* convert m/s to velocity count */
76 #define VEL_MPS_TO_COUNT(mps) (((int32_t) (((mps) / GRAVITY) * (AO_HERTZ/2))) * (int32_t) ao_accel_2g)
77
78 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
79 #define ACCEL_BOOST     ao_accel_2g
80 #define ACCEL_COAST     (ao_accel_2g >> 3)
81 #define ACCEL_INT_LAND  (ao_accel_2g >> 3)
82 #define ACCEL_VEL_MACH  VEL_MPS_TO_COUNT(200)
83 #define ACCEL_VEL_BOOST VEL_MPS_TO_COUNT(5)
84
85 int32_t accel_vel_mach;
86 int32_t accel_vel_boost;
87
88 /*
89  * Barometer calibration
90  *
91  * We directly sample the barometer. The specs say:
92  *
93  * Pressure range: 15-115 kPa
94  * Voltage at 115kPa: 2.82
95  * Output scale: 27mV/kPa
96  *
97  * If we want to detect launch with the barometer, we need
98  * a large enough bump to not be fooled by noise. At typical
99  * launch elevations (0-2000m), a 200Pa pressure change cooresponds
100  * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
101  * As all of our calculations are done in 16 bits, we'll actually see a change
102  * of 16 times this though
103  *
104  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
105  */
106
107 #define BARO_kPa        268
108 #define BARO_LAUNCH     (BARO_kPa / 5)  /* .2kPa, or about 20m */
109 #define BARO_APOGEE     (BARO_kPa / 10) /* .1kPa, or about 10m */
110 #define BARO_COAST      (BARO_kPa * 5)  /* 5kpa, or about 500m */
111 #define BARO_MAIN       (BARO_kPa)      /* 1kPa, or about 100m */
112 #define BARO_INT_LAND   (BARO_kPa / 20) /* .05kPa, or about 5m */
113 #define BARO_LAND       (BARO_kPa * 10) /* 10kPa or about 1000m */
114
115 /* We also have a clock, which can be used to sanity check things in
116  * case of other failures
117  */
118
119 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
120
121 /* This value is scaled in a weird way. It's a running total of accelerometer
122  * readings minus the ground accelerometer reading. That means it measures
123  * velocity, and quite accurately too. As it gets updated 100 times a second,
124  * it's scaled by 100
125  */
126 __pdata int32_t ao_flight_vel;
127 __pdata int32_t ao_min_vel;
128 __pdata int32_t ao_old_vel;
129 __pdata int16_t ao_old_vel_tick;
130 __xdata int32_t ao_raw_accel_sum, ao_raw_pres_sum;
131
132 /* Landing is detected by getting constant readings from both pressure and accelerometer
133  * for a fairly long time (AO_INTERVAL_TICKS)
134  */
135 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(5)
136
137 #define abs(a)  ((a) < 0 ? -(a) : (a))
138
139 void
140 ao_flight(void)
141 {
142         __pdata static uint16_t nsamples = 0;
143
144         ao_flight_adc = ao_adc_head;
145         ao_raw_accel_prev = 0;
146         ao_raw_accel = 0;
147         ao_raw_pres = 0;
148         ao_flight_tick = 0;
149         for (;;) {
150                 ao_sleep(&ao_adc_ring);
151                 while (ao_flight_adc != ao_adc_head) {
152                         __pdata uint8_t ticks;
153                         __pdata int16_t ao_vel_change;
154                         ao_flight_prev_tick = ao_flight_tick;
155
156                         /* Capture a sample */
157                         ao_raw_accel = ao_adc_ring[ao_flight_adc].accel;
158                         ao_raw_pres = ao_adc_ring[ao_flight_adc].pres;
159                         ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
160
161                         ao_flight_accel -= ao_flight_accel >> 4;
162                         ao_flight_accel += ao_raw_accel >> 4;
163                         ao_flight_pres -= ao_flight_pres >> 4;
164                         ao_flight_pres += ao_raw_pres >> 4;
165                         /* Update velocity
166                          *
167                          * The accelerometer is mounted so that
168                          * acceleration yields negative values
169                          * while deceleration yields positive values,
170                          * so subtract instead of add.
171                          */
172                         ticks = ao_flight_tick - ao_flight_prev_tick;
173                         ao_vel_change = ao_ground_accel - (((ao_raw_accel + 1) >> 1) + ((ao_raw_accel_prev + 1) >> 1));
174                         ao_raw_accel_prev = ao_raw_accel;
175
176                         /* one is a common interval */
177                         if (ticks == 1)
178                                 ao_flight_vel += (int32_t) ao_vel_change;
179                         else
180                                 ao_flight_vel += (int32_t) ao_vel_change * (int32_t) ticks;
181
182                         ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
183                 }
184
185                 if (ao_flight_pres < ao_min_pres)
186                         ao_min_pres = ao_flight_pres;
187                 if (ao_flight_vel >= 0) {
188                         if (ao_flight_vel < ao_min_vel)
189                             ao_min_vel = ao_flight_vel;
190                 } else {
191                         if (-ao_flight_vel < ao_min_vel)
192                             ao_min_vel = -ao_flight_vel;
193                 }
194
195                 switch (ao_flight_state) {
196                 case ao_flight_startup:
197
198                         /* startup state:
199                          *
200                          * Collect 1000 samples of acceleration and pressure
201                          * data and average them to find the resting values
202                          */
203                         if (nsamples < 1000) {
204                                 ao_raw_accel_sum += ao_raw_accel;
205                                 ao_raw_pres_sum += ao_raw_pres;
206                                 ++nsamples;
207                                 continue;
208                         }
209                         ao_ground_accel = (ao_raw_accel_sum / nsamples);
210                         ao_ground_pres = (ao_raw_pres_sum / nsamples);
211                         ao_min_pres = ao_ground_pres;
212                         ao_config_get();
213                         ao_main_pres = ao_altitude_to_pres(ao_pres_to_altitude(ao_ground_pres) + ao_config.main_deploy);
214                         ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
215                         accel_vel_mach = ACCEL_VEL_MACH;
216                         accel_vel_boost = ACCEL_VEL_BOOST;
217                         ao_flight_vel = 0;
218                         ao_min_vel = 0;
219                         ao_old_vel = ao_flight_vel;
220                         ao_old_vel_tick = ao_flight_tick;
221
222                         /* Go to pad state if the nose is pointing up */
223                         ao_config_get();
224                         if (ao_config.accel_plus_g != 0 && ao_config.accel_minus_g != 0 &&
225                             ao_flight_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP)
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                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
289                                 break;
290                         }
291                         break;
292                 case ao_flight_boost:
293
294                         /* boost to fast:
295                          *
296                          * accelerometer: start to fall at > 1/4 G
297                          *              OR
298                          * time: boost for more than 15 seconds
299                          *
300                          * Detects motor burn out by the switch from acceleration to
301                          * deceleration, or by waiting until the maximum burn duration
302                          * (15 seconds) has past.
303                          */
304                         if (ao_flight_accel > ao_ground_accel + ACCEL_COAST ||
305                             (int16_t) (ao_flight_tick - ao_launch_tick) > BOOST_TICKS_MAX)
306                         {
307                                 ao_flight_state = ao_flight_fast;
308                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
309                                 break;
310                         }
311                         break;
312                 case ao_flight_fast:
313
314                         /* fast to coast:
315                          *
316                          * accelerometer: integrated velocity < 200 m/s
317                          *               OR
318                          * barometer: fall at least 500m from max altitude
319                          *
320                          * This extra state is required to avoid mis-detecting
321                          * apogee due to mach transitions.
322                          *
323                          * XXX this is essentially a single-detector test
324                          * as the 500m altitude change would likely result
325                          * in a loss of the rocket. More data on precisely
326                          * how big a pressure change the mach transition
327                          * generates would be useful here.
328                          */
329                         if (ao_flight_vel < ACCEL_VEL_MACH ||
330                             ao_flight_pres > ao_min_pres + BARO_COAST)
331                         {
332                                 /* set min velocity to current velocity for
333                                  * apogee detect
334                                  */
335                                 ao_min_vel = abs(ao_flight_vel);
336                                 ao_flight_state = ao_flight_coast;
337                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
338                         }
339                         break;
340                 case ao_flight_coast:
341
342                         /* apogee detect: coast to drogue deploy:
343                          *
344                          * barometer: fall at least 10m
345                          *
346                          * It would be nice to use the accelerometer
347                          * to detect apogee as well, but tests have
348                          * shown that flights far from vertical would
349                          * grossly mis-detect apogee. So, for now,
350                          * we'll trust to a single sensor for this test
351                          */
352                         if (ao_flight_pres > ao_min_pres + BARO_APOGEE)
353                         {
354                                 /* ignite the drogue charge */
355                                 ao_ignite(ao_igniter_drogue);
356
357                                 /* slow down the telemetry system */
358                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
359
360                                 /* slow down the ADC sample rate */
361                                 ao_timer_set_adc_interval(10);
362
363                                 /*
364                                  * Start recording min/max accel and pres for a while
365                                  * to figure out when the rocket has landed
366                                  */
367                                 /* Set the 'last' limits to max range to prevent
368                                  * early resting detection
369                                  */
370                                 ao_interval_min_accel = 0;
371                                 ao_interval_max_accel = 0x7fff;
372                                 ao_interval_min_pres = 0;
373                                 ao_interval_max_pres = 0x7fff;
374
375                                 /* initialize interval values */
376                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
377
378                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
379                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
380
381                                 /* and enter drogue state */
382                                 ao_flight_state = ao_flight_drogue;
383                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
384                         }
385
386                         break;
387                 case ao_flight_drogue:
388
389                         /* drogue to main deploy:
390                          *
391                          * barometer: reach main deploy altitude
392                          *
393                          * Would like to use the accelerometer for this test, but
394                          * the orientation of the flight computer is unknown after
395                          * drogue deploy, so we ignore it. Could also detect
396                          * high descent rate using the pressure sensor to
397                          * recognize drogue deploy failure and eject the main
398                          * at that point. Perhaps also use the drogue sense lines
399                          * to notice continutity?
400                          */
401                         if (ao_flight_pres >= ao_main_pres)
402                         {
403                                 ao_ignite(ao_igniter_main);
404                                 ao_flight_state = ao_flight_main;
405                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
406                         }
407
408                         /* fall through... */
409                 case ao_flight_main:
410
411                         /* drogue/main to land:
412                          *
413                          * accelerometer: value stable
414                          *                           AND
415                          * barometer: altitude stable and within 1000m of the launch altitude
416                          */
417
418                         if (ao_flight_pres < ao_interval_cur_min_pres)
419                                 ao_interval_cur_min_pres = ao_flight_pres;
420                         if (ao_flight_pres > ao_interval_cur_max_pres)
421                                 ao_interval_cur_max_pres = ao_flight_pres;
422                         if (ao_flight_accel < ao_interval_cur_min_accel)
423                                 ao_interval_cur_min_accel = ao_flight_accel;
424                         if (ao_flight_accel > ao_interval_cur_max_accel)
425                                 ao_interval_cur_max_accel = ao_flight_accel;
426
427                         if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
428                                 ao_interval_max_pres = ao_interval_cur_max_pres;
429                                 ao_interval_min_pres = ao_interval_cur_min_pres;
430                                 ao_interval_max_accel = ao_interval_cur_max_accel;
431                                 ao_interval_min_accel = ao_interval_cur_min_accel;
432                                 ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
433                                 ao_interval_cur_min_pres = ao_interval_cur_max_pres = ao_flight_pres;
434                                 ao_interval_cur_min_accel = ao_interval_cur_max_accel = ao_flight_accel;
435
436                                 if ((uint16_t) (ao_interval_max_accel - ao_interval_min_accel) < (uint16_t) ACCEL_INT_LAND &&
437                                     ao_flight_pres > ao_ground_pres - BARO_LAND &&
438                                     (uint16_t) (ao_interval_max_pres - ao_interval_min_pres) < (uint16_t) BARO_INT_LAND)
439                                 {
440                                         ao_flight_state = ao_flight_landed;
441
442                                         /* turn off the ADC capture */
443                                         ao_timer_set_adc_interval(0);
444                                         /* Enable RDF beacon */
445                                         ao_rdf_set(1);
446
447                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
448                                 }
449                         }
450                         break;
451                 case ao_flight_landed:
452                         break;
453                 }
454         }
455 }
456
457 static __xdata struct ao_task   flight_task;
458
459 void
460 ao_flight_init(void)
461 {
462         ao_flight_state = ao_flight_startup;
463         ao_interval_min_accel = 0;
464         ao_interval_max_accel = 0x7fff;
465         ao_interval_min_pres = 0;
466         ao_interval_max_pres = 0x7fff;
467         ao_interval_end = AO_INTERVAL_TICKS;
468
469         ao_add_task(&flight_task, ao_flight, "flight");
470 }