altos: Switch telemini from v0.1 to v1.0
[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 __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(5)
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_launch_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_launch_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                         }
209                         break;
210 #endif
211                 case ao_flight_coast:
212
213                         /* apogee detect: coast to drogue deploy:
214                          *
215                          * speed: < 0
216                          *
217                          * Also make sure the model altitude is tracking
218                          * the measured altitude reasonably closely; otherwise
219                          * we're probably transsonic.
220                          */
221                         if (ao_speed < 0
222 #if !HAS_ACCEL
223                             && (ao_sample_alt >= AO_MAX_BARO_HEIGHT || ao_error_h_sq_avg < 100)
224 #endif
225                                 )
226                         {
227                                 /* ignite the drogue charge */
228                                 ao_ignite(ao_igniter_drogue);
229
230                                 /* slow down the telemetry system */
231                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_RECOVER);
232
233                                 /* Turn the RDF beacon back on */
234                                 ao_rdf_set(1);
235
236                                 /* and enter drogue state */
237                                 ao_flight_state = ao_flight_drogue;
238                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
239                         }
240
241                         break;
242                 case ao_flight_drogue:
243
244                         /* drogue to main deploy:
245                          *
246                          * barometer: reach main deploy altitude
247                          *
248                          * Would like to use the accelerometer for this test, but
249                          * the orientation of the flight computer is unknown after
250                          * drogue deploy, so we ignore it. Could also detect
251                          * high descent rate using the pressure sensor to
252                          * recognize drogue deploy failure and eject the main
253                          * at that point. Perhaps also use the drogue sense lines
254                          * to notice continutity?
255                          */
256                         if (ao_height <= ao_config.main_deploy)
257                         {
258                                 ao_ignite(ao_igniter_main);
259
260                                 /*
261                                  * Start recording min/max height
262                                  * to figure out when the rocket has landed
263                                  */
264
265                                 /* initialize interval values */
266                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
267
268                                 ao_interval_min_height = ao_interval_max_height = ao_avg_height;
269
270                                 ao_flight_state = ao_flight_main;
271                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
272                         }
273                         break;
274
275                         /* fall through... */
276                 case ao_flight_main:
277
278                         /* main to land:
279                          *
280                          * barometer: altitude stable
281                          */
282
283                         if (ao_avg_height < ao_interval_min_height)
284                                 ao_interval_min_height = ao_avg_height;
285                         if (ao_avg_height > ao_interval_max_height)
286                                 ao_interval_max_height = ao_avg_height;
287
288                         if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
289                                 if (ao_interval_max_height - ao_interval_min_height <= AO_M_TO_HEIGHT(2))
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_avg_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 }