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