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