altos: product defines are always in ao_product.h
[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 __pdata 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                         }
174                         break;
175                 case ao_flight_boost:
176
177                         /* boost to fast:
178                          *
179                          * accelerometer: start to fall at > 1/4 G
180                          *              OR
181                          * time: boost for more than 15 seconds
182                          *
183                          * Detects motor burn out by the switch from acceleration to
184                          * deceleration, or by waiting until the maximum burn duration
185                          * (15 seconds) has past.
186                          */
187                         if ((ao_accel < AO_MSS_TO_ACCEL(-2.5) && ao_height > AO_M_TO_HEIGHT(100)) ||
188                             (int16_t) (ao_sample_tick - ao_launch_tick) > BOOST_TICKS_MAX)
189                         {
190 #if HAS_ACCEL
191                                 ao_flight_state = ao_flight_fast;
192 #else
193                                 ao_flight_state = ao_flight_coast;
194 #endif
195                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
196                         }
197                         break;
198 #if HAS_ACCEL
199                 case ao_flight_fast:
200                         /*
201                          * This is essentially the same as coast,
202                          * but the barometer is being ignored as
203                          * it may be unreliable.
204                          */
205                         if (ao_speed < AO_MS_TO_SPEED(AO_MAX_BARO_SPEED))
206                         {
207                                 ao_flight_state = ao_flight_coast;
208                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
209                         }
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                                 /*
238                                  * Start recording min/max height
239                                  * to figure out when the rocket has landed
240                                  */
241
242                                 /* initialize interval values */
243                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
244
245                                 ao_interval_min_height = ao_interval_max_height = ao_height;
246
247                                 /* and enter drogue state */
248                                 ao_flight_state = ao_flight_drogue;
249                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
250                         }
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                                 ao_flight_state = ao_flight_main;
271                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
272                         }
273
274                         /* fall through... */
275                 case ao_flight_main:
276
277                         /* drogue/main to land:
278                          *
279                          * barometer: altitude stable and within 1000m of the launch altitude
280                          */
281
282                         if (ao_height < ao_interval_min_height)
283                                 ao_interval_min_height = ao_height;
284                         if (ao_height > ao_interval_max_height)
285                                 ao_interval_max_height = ao_height;
286
287                         if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
288                                 if (ao_height < AO_M_TO_HEIGHT(1000) &&
289                                     ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
290                                 {
291                                         ao_flight_state = ao_flight_landed;
292
293                                         /* turn off the ADC capture */
294                                         ao_timer_set_adc_interval(0);
295
296                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
297                                 }
298                                 ao_interval_min_height = ao_interval_max_height = ao_height;
299                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
300                         }
301                         break;
302                 case ao_flight_landed:
303                         break;
304                 }
305         }
306 }
307
308 static __xdata struct ao_task   flight_task;
309
310 void
311 ao_flight_init(void)
312 {
313         ao_flight_state = ao_flight_startup;
314         ao_add_task(&flight_task, ao_flight, "flight");
315 }