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