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