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