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