cfecc5af5d770509d1341a63ae958d4e92550891
[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 #ifdef ASCENT_PIN
172                                 ASCENT_PIN = 0;
173 #endif
174
175                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
176                                 break;
177                         }
178                         break;
179                 case ao_flight_boost:
180
181                         /* boost to fast:
182                          *
183                          * accelerometer: start to fall at > 1/4 G
184                          *              OR
185                          * time: boost for more than 15 seconds
186                          *
187                          * Detects motor burn out by the switch from acceleration to
188                          * deceleration, or by waiting until the maximum burn duration
189                          * (15 seconds) has past.
190                          */
191                         if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
192                             (int16_t) (ao_sample_tick - ao_launch_tick) > BOOST_TICKS_MAX)
193                         {
194 #if HAS_ACCEL
195                                 ao_flight_state = ao_flight_fast;
196 #else
197                                 ao_flight_state = ao_flight_coast;
198 #endif
199                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
200                                 break;
201                         }
202                         break;
203 #if HAS_ACCEL
204                 case ao_flight_fast:
205                         /*
206                          * This is essentially the same as coast,
207                          * but the barometer is being ignored as
208                          * it may be unreliable.
209                          */
210                         if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
211                         {
212                                 ao_flight_state = ao_flight_coast;
213                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
214                                 break;
215                         }
216                         break;
217 #endif
218                 case ao_flight_coast:
219
220                         /* apogee detect: coast to drogue deploy:
221                          *
222                          * speed: < 0
223                          *
224                          * Also make sure the model altitude is tracking
225                          * the measured altitude reasonably closely; otherwise
226                          * we're probably transsonic.
227                          */
228                         if (ao_speed < 0
229 #if !HAS_ACCEL
230                             && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
231 #endif
232                                 )
233                         {
234                                 /* ignite the drogue charge */
235                                 ao_ignite(ao_igniter_drogue);
236
237                                 /* slow down the telemetry system */
238                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
239
240                                 /* Turn the RDF beacon back on */
241                                 ao_rdf_set(1);
242
243 #ifdef ASCENT_PIN
244                                 ASCENT_PIN = 1;
245 #endif
246                                 /*
247                                  * Start recording min/max height
248                                  * to figure out when the rocket has landed
249                                  */
250
251                                 /* initialize interval values */
252                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
253
254                                 ao_interval_min_height = ao_interval_max_height = ao_height;
255
256                                 /* and enter drogue state */
257                                 ao_flight_state = ao_flight_drogue;
258                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
259                         }
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                                 ao_flight_state = ao_flight_main;
280                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
281                         }
282
283                         /* fall through... */
284                 case ao_flight_main:
285
286                         /* drogue/main to land:
287                          *
288                          * barometer: altitude stable and within 1000m of the launch altitude
289                          */
290
291                         if (ao_height < ao_interval_min_height)
292                                 ao_interval_min_height = ao_height;
293                         if (ao_height > ao_interval_max_height)
294                                 ao_interval_max_height = ao_height;
295
296                         if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
297                                 if (ao_height < AO_M_TO_HEIGHT(1000) &&
298                                     ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
299                                 {
300                                         ao_flight_state = ao_flight_landed;
301
302                                         /* turn off the ADC capture */
303                                         ao_timer_set_adc_interval(0);
304
305                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
306                                 }
307                                 ao_interval_min_height = ao_interval_max_height = ao_height;
308                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
309                         }
310                         break;
311                 case ao_flight_landed:
312                         break;
313                 }
314         }
315 }
316
317 static __xdata struct ao_task   flight_task;
318
319 void
320 ao_flight_init(void)
321 {
322 #ifdef ASCENT_SIGNAL
323         ASCENT_SIGNAL = 1;
324         ASCENT_SIGNAL_DIR |= (1 << ASCENT_SIGNAL_PIN);
325         ASCENT_SIGNAL_SEL &= ~(1 << ASCENT_SIGNAL_PIN);
326 #endif
327         ao_flight_state = ao_flight_startup;
328         ao_add_task(&flight_task, ao_flight, "flight");
329 }