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