]> git.gag.com Git - fw/altos/blob - src/core/ao_flight.c
altos: Shuffle stm-demo SPI test code around a bit
[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                         /* apogee detect: coast to drogue deploy:
227                          *
228                          * speed: < 0
229                          *
230                          * Also make sure the model altitude is tracking
231                          * the measured altitude reasonably closely; otherwise
232                          * we're probably transsonic.
233                          */
234                         if (ao_speed < 0
235 #if !HAS_ACCEL
236                             && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
237 #endif
238                                 )
239                         {
240                                 /* ignite the drogue charge */
241                                 ao_ignite(ao_igniter_drogue);
242
243                                 /* slow down the telemetry system */
244                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
245
246                                 /* Turn the RDF beacon back on */
247                                 ao_rdf_set(1);
248
249                                 /* and enter drogue state */
250                                 ao_flight_state = ao_flight_drogue;
251                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
252                         }
253 #if HAS_ACCEL
254                         else {
255                         check_re_boost:
256                                 ao_coast_avg_accel = ao_coast_avg_accel - (ao_coast_avg_accel >> 6) + (ao_accel >> 6);
257                                 if (ao_coast_avg_accel > AO_MSS_TO_ACCEL(20)) {
258                                         ao_boost_tick = ao_sample_tick;
259                                         ao_flight_state = ao_flight_boost;
260                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
261                                 }
262                         }
263 #endif
264
265                         break;
266                 case ao_flight_drogue:
267
268                         /* drogue to main deploy:
269                          *
270                          * barometer: reach main deploy altitude
271                          *
272                          * Would like to use the accelerometer for this test, but
273                          * the orientation of the flight computer is unknown after
274                          * drogue deploy, so we ignore it. Could also detect
275                          * high descent rate using the pressure sensor to
276                          * recognize drogue deploy failure and eject the main
277                          * at that point. Perhaps also use the drogue sense lines
278                          * to notice continutity?
279                          */
280                         if (ao_height <= ao_config.main_deploy)
281                         {
282                                 ao_ignite(ao_igniter_main);
283
284                                 /*
285                                  * Start recording min/max height
286                                  * to figure out when the rocket has landed
287                                  */
288
289                                 /* initialize interval values */
290                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
291
292                                 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
293
294                                 ao_flight_state = ao_flight_main;
295                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
296                         }
297                         break;
298
299                         /* fall through... */
300                 case ao_flight_main:
301
302                         /* main to land:
303                          *
304                          * barometer: altitude stable
305                          */
306
307                         if (ao_avg_height < ao_interval_min_height)
308                                 ao_interval_min_height = ao_avg_height;
309                         if (ao_avg_height > ao_interval_max_height)
310                                 ao_interval_max_height = ao_avg_height;
311
312                         if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
313                                 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(4))
314                                 {
315                                         ao_flight_state = ao_flight_landed;
316
317                                         /* turn off the ADC capture */
318                                         ao_timer_set_adc_interval(0);
319
320                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
321                                 }
322                                 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
323                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
324                         }
325                         break;
326                 case ao_flight_landed:
327                         break;
328                 }
329         }
330 }
331
332 static __xdata struct ao_task   flight_task;
333
334 void
335 ao_flight_init(void)
336 {
337         ao_flight_state = ao_flight_startup;
338         ao_add_task(&flight_task, ao_flight, "flight");
339 }