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