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