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