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