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