altos: Add arbitrary pyro channel support
[fw/altos] / src / core / 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 #include <ao_log.h>
21 #endif
22
23 #ifndef HAS_ACCEL
24 #error Please define HAS_ACCEL
25 #endif
26
27 #ifndef HAS_GPS
28 #error Please define HAS_GPS
29 #endif
30
31 #ifndef HAS_USB
32 #error Please define HAS_USB
33 #endif
34
35 #ifndef HAS_TELEMETRY
36 #define HAS_TELEMETRY   HAS_RADIO
37 #endif
38
39 /* Main flight thread. */
40
41 __pdata enum ao_flight_state    ao_flight_state;        /* current flight state */
42 __pdata uint16_t                ao_boost_tick;          /* time of launch detect */
43 #if AO_PYRO_NUM
44 __pdata uint16_t                ao_motor_number;        /* number of motors burned so far */
45 #endif
46
47 /*
48  * track min/max data over a long interval to detect
49  * resting
50  */
51 static __data uint16_t          ao_interval_end;
52 static __data int16_t           ao_interval_min_height;
53 static __data int16_t           ao_interval_max_height;
54 #if HAS_ACCEL
55 static __data int16_t           ao_coast_avg_accel;
56 #endif
57
58 __pdata uint8_t                 ao_flight_force_idle;
59
60 /* We also have a clock, which can be used to sanity check things in
61  * case of other failures
62  */
63
64 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
65
66 /* Landing is detected by getting constant readings from both pressure and accelerometer
67  * for a fairly long time (AO_INTERVAL_TICKS)
68  */
69 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(10)
70
71 #define abs(a)  ((a) < 0 ? -(a) : (a))
72
73 void
74 ao_flight(void)
75 {
76         ao_sample_init();
77         ao_flight_state = ao_flight_startup;
78         for (;;) {
79
80                 /*
81                  * Process ADC samples, just looping
82                  * until the sensors are calibrated.
83                  */
84                 if (!ao_sample())
85                         continue;
86
87                 switch (ao_flight_state) {
88                 case ao_flight_startup:
89
90                         /* Check to see what mode we should go to.
91                          *  - Invalid mode if accel cal appears to be out
92                          *  - pad mode if we're upright,
93                          *  - idle mode otherwise
94                          */
95 #if HAS_ACCEL
96                         if (ao_config.accel_plus_g == 0 ||
97                             ao_config.accel_minus_g == 0 ||
98                             ao_ground_accel < ao_config.accel_plus_g - ACCEL_NOSE_UP ||
99                             ao_ground_accel > ao_config.accel_minus_g + ACCEL_NOSE_UP)
100                         {
101                                 /* Detected an accel value outside -1.5g to 1.5g
102                                  * (or uncalibrated values), so we go into invalid mode
103                                  */
104                                 ao_flight_state = ao_flight_invalid;
105
106 #if HAS_RADIO && PACKET_HAS_SLAVE
107                                 /* Turn on packet system in invalid mode on TeleMetrum */
108                                 ao_packet_slave_start();
109 #endif
110                         } else
111 #endif
112                                 if (!ao_flight_force_idle
113 #if HAS_ACCEL
114                                     && ao_ground_accel < ao_config.accel_plus_g + ACCEL_NOSE_UP
115 #endif
116                                         )
117                         {
118                                 /* Set pad mode - we can fly! */
119                                 ao_flight_state = ao_flight_pad;
120 #if HAS_USB && HAS_RADIO && !HAS_FLIGHT_DEBUG
121                                 /* Disable the USB controller in flight mode
122                                  * to save power
123                                  */
124                                 ao_usb_disable();
125 #endif
126
127 #if !HAS_ACCEL
128                                 /* Disable packet mode in pad state on TeleMini */
129                                 ao_packet_slave_stop();
130 #endif
131
132 #if HAS_TELEMETRY
133                                 /* Turn on telemetry system */
134                                 ao_rdf_set(1);
135                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
136 #endif
137                                 /* signal successful initialization by turning off the LED */
138                                 ao_led_off(AO_LED_RED);
139                         } else {
140                                 /* Set idle mode */
141                                 ao_flight_state = ao_flight_idle;
142  
143 #if HAS_ACCEL && HAS_RADIO && PACKET_HAS_SLAVE
144                                 /* Turn on packet system in idle mode on TeleMetrum */
145                                 ao_packet_slave_start();
146 #endif
147
148                                 /* signal successful initialization by turning off the LED */
149                                 ao_led_off(AO_LED_RED);
150                         }
151                         /* wakeup threads due to state change */
152                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
153
154                         break;
155                 case ao_flight_pad:
156
157                         /* pad to boost:
158                          *
159                          * barometer: > 20m vertical motion
160                          *             OR
161                          * accelerometer: > 2g AND velocity > 5m/s
162                          *
163                          * The accelerometer should always detect motion before
164                          * the barometer, but we use both to make sure this
165                          * transition is detected. If the device
166                          * doesn't have an accelerometer, then ignore the
167                          * speed and acceleration as they are quite noisy
168                          * on the pad.
169                          */
170                         if (ao_height > AO_M_TO_HEIGHT(20)
171 #if HAS_ACCEL
172                             || (ao_accel > AO_MSS_TO_ACCEL(20) &&
173                                 ao_speed > AO_MS_TO_SPEED(5))
174 #endif
175                                 )
176                         {
177                                 ao_flight_state = ao_flight_boost;
178                                 ao_boost_tick = ao_sample_tick;
179
180                                 /* start logging data */
181                                 ao_log_start();
182
183 #if HAS_TELEMETRY
184                                 /* Increase telemetry rate */
185                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_FLIGHT);
186
187                                 /* disable RDF beacon */
188                                 ao_rdf_set(0);
189 #endif
190
191 #if HAS_GPS
192                                 /* Record current GPS position by waking up GPS log tasks */
193                                 ao_wakeup(&ao_gps_data);
194                                 ao_wakeup(&ao_gps_tracking_data);
195 #endif
196
197                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
198                         }
199                         break;
200                 case ao_flight_boost:
201
202                         /* boost to fast:
203                          *
204                          * accelerometer: start to fall at > 1/4 G
205                          *              OR
206                          * time: boost for more than 15 seconds
207                          *
208                          * Detects motor burn out by the switch from acceleration to
209                          * deceleration, or by waiting until the maximum burn duration
210                          * (15 seconds) has past.
211                          */
212                         if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
213                             (int16_t) (ao_sample_tick - ao_boost_tick) > BOOST_TICKS_MAX)
214                         {
215 #if HAS_ACCEL
216                                 ao_flight_state = ao_flight_fast;
217                                 ao_coast_avg_accel = ao_accel;
218 #else
219                                 ao_flight_state = ao_flight_coast;
220 #endif
221 #if AO_PYRO_NUM
222                                 ++ao_motor_number;
223 #endif
224                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
225                         }
226                         break;
227 #if HAS_ACCEL
228                 case ao_flight_fast:
229                         /*
230                          * This is essentially the same as coast,
231                          * but the barometer is being ignored as
232                          * it may be unreliable.
233                          */
234                         if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
235                         {
236                                 ao_flight_state = ao_flight_coast;
237                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
238                         } else
239                                 goto check_re_boost;
240                         break;
241 #endif
242                 case ao_flight_coast:
243
244                         /*
245                          * By customer request - allow the user
246                          * to lock out apogee detection for a specified
247                          * number of seconds.
248                          */
249                         if (ao_config.apogee_lockout) {
250                                 if ((ao_sample_tick - ao_boost_tick) <
251                                     AO_SEC_TO_TICKS(ao_config.apogee_lockout))
252                                         break;
253                         }
254
255                         /* apogee detect: coast to drogue deploy:
256                          *
257                          * speed: < 0
258                          *
259                          * Also make sure the model altitude is tracking
260                          * the measured altitude reasonably closely; otherwise
261                          * we're probably transsonic.
262                          */
263                         if (ao_speed < 0
264 #if !HAS_ACCEL
265                             && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
266 #endif
267                                 )
268                         {
269 #if HAS_IGNITE
270                                 /* ignite the drogue charge */
271                                 ao_ignite(ao_igniter_drogue);
272 #endif
273
274 #if HAS_TELEMETRY
275                                 /* slow down the telemetry system */
276                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
277
278                                 /* Turn the RDF beacon back on */
279                                 ao_rdf_set(1);
280 #endif
281
282                                 /* and enter drogue state */
283                                 ao_flight_state = ao_flight_drogue;
284                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
285                         }
286 #if HAS_ACCEL
287                         else {
288                         check_re_boost:
289                                 ao_coast_avg_accel = ao_coast_avg_accel - (ao_coast_avg_accel >> 6) + (ao_accel >> 6);
290                                 if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
291                                         ao_boost_tick = ao_sample_tick;
292                                         ao_flight_state = ao_flight_boost;
293                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
294                                 }
295                         }
296 #endif
297
298                         break;
299                 case ao_flight_drogue:
300
301                         /* drogue to main deploy:
302                          *
303                          * barometer: reach main deploy altitude
304                          *
305                          * Would like to use the accelerometer for this test, but
306                          * the orientation of the flight computer is unknown after
307                          * drogue deploy, so we ignore it. Could also detect
308                          * high descent rate using the pressure sensor to
309                          * recognize drogue deploy failure and eject the main
310                          * at that point. Perhaps also use the drogue sense lines
311                          * to notice continutity?
312                          */
313                         if (ao_height <= ao_config.main_deploy)
314                         {
315 #if HAS_IGNITE
316                                 ao_ignite(ao_igniter_main);
317 #endif
318
319                                 /*
320                                  * Start recording min/max height
321                                  * to figure out when the rocket has landed
322                                  */
323
324                                 /* initialize interval values */
325                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
326
327                                 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
328
329                                 ao_flight_state = ao_flight_main;
330                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
331                         }
332                         break;
333
334                         /* fall through... */
335                 case ao_flight_main:
336
337                         /* main to land:
338                          *
339                          * barometer: altitude stable
340                          */
341
342                         if (ao_avg_height < ao_interval_min_height)
343                                 ao_interval_min_height = ao_avg_height;
344                         if (ao_avg_height > ao_interval_max_height)
345                                 ao_interval_max_height = ao_avg_height;
346
347                         if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
348                                 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
349                                 {
350                                         ao_flight_state = ao_flight_landed;
351
352                                         /* turn off the ADC capture */
353                                         ao_timer_set_adc_interval(0);
354
355                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
356                                 }
357                                 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
358                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
359                         }
360                         break;
361                 default:
362                         break;
363                 }
364         }
365 }
366
367 #if HAS_FLIGHT_DEBUG
368 static inline int int_part(int16_t i)   { return i >> 4; }
369 static inline int frac_part(int16_t i)  { return ((i & 0xf) * 100 + 8) / 16; }
370
371 static void
372 ao_flight_dump(void)
373 {
374 #if HAS_ACCEL
375         int16_t accel;
376
377         accel = ((ao_ground_accel - ao_sample_accel) * ao_accel_scale) >> 16;
378 #endif
379
380         printf ("sample:\n");
381         printf ("  tick        %d\n", ao_sample_tick);
382         printf ("  raw pres    %d\n", ao_sample_pres);
383 #if HAS_ACCEL
384         printf ("  raw accel   %d\n", ao_sample_accel);
385 #endif
386         printf ("  ground pres %d\n", ao_ground_pres);
387         printf ("  ground alt  %d\n", ao_ground_height);
388 #if HAS_ACCEL
389         printf ("  raw accel   %d\n", ao_sample_accel);
390         printf ("  groundaccel %d\n", ao_ground_accel);
391         printf ("  accel_2g    %d\n", ao_accel_2g);
392 #endif
393
394         printf ("  alt         %d\n", ao_sample_alt);
395         printf ("  height      %d\n", ao_sample_height);
396 #if HAS_ACCEL
397         printf ("  accel       %d.%02d\n", int_part(accel), frac_part(accel));
398 #endif
399
400
401         printf ("kalman:\n");
402         printf ("  height      %d\n", ao_height);
403         printf ("  speed       %d.%02d\n", int_part(ao_speed), frac_part(ao_speed));
404         printf ("  accel       %d.%02d\n", int_part(ao_accel), frac_part(ao_accel));
405         printf ("  max_height  %d\n", ao_max_height);
406         printf ("  avg_height  %d\n", ao_avg_height);
407         printf ("  error_h     %d\n", ao_error_h);
408         printf ("  error_avg   %d\n", ao_error_h_sq_avg);
409 }
410
411 __code struct ao_cmds ao_flight_cmds[] = {
412         { ao_flight_dump,       "F\0Dump flight status" },
413         { 0, NULL },
414 };
415 #endif
416
417 static __xdata struct ao_task   flight_task;
418
419 void
420 ao_flight_init(void)
421 {
422         ao_flight_state = ao_flight_startup;
423 #if HAS_FLIGHT_DEBUG
424         ao_cmd_register(&ao_flight_cmds[0]);
425 #endif
426         ao_add_task(&flight_task, ao_flight, "flight");
427 }