5998f291c42881e7c361af105b03659c0be8deb9
[fw/altos] / 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 /* Main flight thread. */
23
24 __pdata enum ao_flight_state    ao_flight_state;        /* current flight state */
25 __pdata uint16_t                ao_flight_tick;         /* time of last data */
26 __pdata int16_t                 ao_flight_accel;        /* filtered acceleration */
27 __pdata int16_t                 ao_flight_pres;         /* filtered pressure */
28 __pdata int16_t                 ao_ground_pres;         /* startup pressure */
29 __pdata int16_t                 ao_ground_accel;        /* startup acceleration */
30 __pdata int16_t                 ao_min_pres;            /* minimum recorded pressure */
31 __pdata uint16_t                ao_launch_time;         /* time of launch detect */
32 __pdata int16_t                 ao_main_pres;           /* pressure to eject main */
33
34 /*
35  * track min/max data over a long interval to detect
36  * resting
37  */
38 __pdata uint16_t                ao_interval_end;
39 __pdata int16_t                 ao_interval_cur_min_accel;
40 __pdata int16_t                 ao_interval_cur_max_accel;
41 __pdata int16_t                 ao_interval_cur_min_pres;
42 __pdata int16_t                 ao_interval_cur_max_pres;
43 __pdata int16_t                 ao_interval_min_accel;
44 __pdata int16_t                 ao_interval_max_accel;
45 __pdata int16_t                 ao_interval_min_pres;
46 __pdata int16_t                 ao_interval_max_pres;
47
48 __data uint8_t ao_flight_adc;
49 __xdata int16_t ao_accel, ao_prev_accel, ao_pres;
50
51 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(5)
52
53 /* Accelerometer calibration
54  *
55  * We're sampling the accelerometer through a resistor divider which
56  * consists of 5k and 10k resistors. This multiplies the values by 2/3.
57  * That goes into the cc1111 A/D converter, which is running at 11 bits
58  * of precision with the bits in the MSB of the 16 bit value. Only positive
59  * values are used, so values should range from 0-32752 for 0-3.3V. The
60  * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
61  * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
62  * for a final computation of:
63  *
64  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
65  *
66  * Zero g was measured at 16000 (we would expect 16384)
67  */
68
69 #define ACCEL_G         265
70 #define ACCEL_ZERO_G    16000
71 #define ACCEL_NOSE_UP   (ACCEL_ZERO_G - ACCEL_G * 2 /3)
72 #define ACCEL_BOOST     (ACCEL_NOSE_UP - ACCEL_G * 2)
73 #define ACCEL_LAND      (ACCEL_G / 10)
74
75 /*
76  * Barometer calibration
77  *
78  * We directly sample the barometer. The specs say:
79  *
80  * Pressure range: 15-115 kPa
81  * Voltage at 115kPa: 2.82
82  * Output scale: 27mV/kPa
83  * 
84  * If we want to detect launch with the barometer, we need
85  * a large enough bump to not be fooled by noise. At typical
86  * launch elevations (0-2000m), a 200Pa pressure change cooresponds
87  * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
88  * As all of our calculations are done in 16 bits, we'll actually see a change
89  * of 16 times this though
90  *
91  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
92  */
93
94 #define BARO_kPa        268
95 #define BARO_LAUNCH     (BARO_kPa / 5)  /* .2kPa */
96 #define BARO_APOGEE     (BARO_kPa / 10) /* .1kPa */
97 #define BARO_MAIN       (BARO_kPa)      /* 1kPa */
98 #define BARO_LAND       (BARO_kPa / 20) /* .05kPa */
99
100 /* We also have a clock, which can be used to sanity check things in
101  * case of other failures
102  */
103
104 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(15)
105
106 /* This value is scaled in a weird way. It's a running total of accelerometer
107  * readings minus the ground accelerometer reading. That means it measures
108  * velocity, and quite accurately too. As it gets updated 100 times a second,
109  * it's scaled by 100
110  */
111 __data int32_t  ao_flight_vel;
112
113 /* convert m/s to velocity count */
114 #define VEL_MPS_TO_COUNT(mps) ((int32_t) ((int32_t) (mps) * (int32_t) 100 / (int32_t) ACCEL_G))
115
116 void
117 ao_flight(void)
118 {
119         __pdata static uint8_t  nsamples = 0;
120         
121         ao_flight_adc = ao_adc_head;
122         ao_prev_accel = 0;
123         ao_accel = 0;
124         ao_pres = 0;
125         for (;;) {
126                 ao_sleep(&ao_adc_ring);
127                 while (ao_flight_adc != ao_adc_head) {
128                         ao_accel = ao_adc_ring[ao_flight_adc].accel;
129                         ao_pres = ao_adc_ring[ao_flight_adc].pres;
130                         ao_flight_tick = ao_adc_ring[ao_flight_adc].tick;
131                         ao_flight_vel += (int32_t) (((ao_accel + ao_prev_accel) >> 4) - (ao_ground_accel << 1));
132                         ao_prev_accel = ao_accel;
133                         ao_flight_adc = ao_adc_ring_next(ao_flight_adc);
134                 }
135                 ao_flight_accel -= ao_flight_accel >> 4;
136                 ao_flight_accel += ao_accel >> 4;
137                 ao_flight_pres -= ao_flight_pres >> 4;
138                 ao_flight_pres += ao_pres >> 4;
139                 
140                 if (ao_flight_pres < ao_min_pres)
141                         ao_min_pres = ao_flight_pres;
142
143                 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
144                         ao_interval_max_pres = ao_interval_cur_max_pres;
145                         ao_interval_min_pres = ao_interval_cur_min_pres;
146                         ao_interval_max_accel = ao_interval_cur_max_accel;
147                         ao_interval_min_accel = ao_interval_cur_min_accel;
148                         ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
149                 }
150                                
151                 switch (ao_flight_state) {
152                 case ao_flight_startup:
153                         if (nsamples < 100) {
154                                 ++nsamples;
155                                 continue;
156                         }
157                         ao_ground_accel = ao_flight_accel;
158                         ao_ground_pres = ao_flight_pres;
159                         ao_min_pres = ao_flight_pres;
160                         ao_main_pres = ao_ground_pres - BARO_MAIN;
161                         ao_flight_vel = 0;
162                         
163                         ao_interval_end = ao_flight_tick;
164                         
165                         /* Go to launchpad state if the nose is pointing up */
166                         if (ao_flight_accel < ACCEL_NOSE_UP) {
167                                 ao_flight_state = ao_flight_launchpad;
168                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
169                         } else {
170                                 ao_flight_state = ao_flight_idle;
171                                 
172                                 /* Turn on the Green LED in idle mode
173                                  * This also happens to bring the USB up for the TI board
174                                  */
175                                 ao_led_on(AO_LED_GREEN);
176                                 ao_timer_set_adc_interval(100);
177                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
178                         }
179                         /* signal successful initialization by turning off the LED */
180                         ao_led_off(AO_LED_RED);
181                         break;
182                 case ao_flight_launchpad:
183
184                         /* pad to boost:
185                          *
186                          * accelerometer: > 2g
187                          * barometer: > 20m vertical motion
188                          */
189                         if (ao_flight_accel < ACCEL_BOOST || 
190                             ao_flight_pres + BARO_LAUNCH < ao_ground_pres)
191                         {
192                                 ao_flight_state = ao_flight_boost;
193                                 ao_log_start();
194                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
195                                 break;
196                         }
197                         break;
198                 case ao_flight_boost:
199
200                         /* boost to coast:
201                          *
202                          * accelerometer: start to fall at > 1/4 G
203                          * time: boost for more than 15 seconds
204                          */
205                         if (ao_flight_accel > ao_ground_accel + (ACCEL_G >> 2) ||
206                             (int16_t) (ao_flight_tick - ao_launch_time) > BOOST_TICKS_MAX)
207                         {
208                                 ao_flight_state = ao_flight_coast;
209                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
210                                 break;
211                         }
212                         break;
213                 case ao_flight_coast:
214                         
215                         /* coast to apogee detect:
216                          * 
217                          * accelerometer: integrated velocity < 200 m/s
218                          * barometer: fall at least 500m from max altitude
219                          */
220                         if (ao_flight_vel < VEL_MPS_TO_COUNT(200) ||
221                             ao_flight_pres - (5 * BARO_kPa) > ao_min_pres)
222                         {
223                                 ao_flight_state = ao_flight_apogee;
224                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
225                         }
226                         break;
227                 case ao_flight_apogee:
228
229                         /* apogee to drogue deploy:
230                          *
231                          * accelerometer: integrated velocity < 10m/s
232                          * barometer: fall at least 10m
233                          */
234                         if (ao_flight_vel < VEL_MPS_TO_COUNT(-10) ||
235                             ao_flight_pres - BARO_APOGEE > ao_min_pres)
236                         {
237                                 ao_ignite(ao_igniter_drogue);
238                                 ao_flight_state = ao_flight_drogue;
239                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
240                         }
241                         break; 
242                 case ao_flight_drogue:
243                         
244                         /* drogue to main deploy:
245                          *
246                          * accelerometer: abs(velocity) > 50m/s
247                          * barometer: reach main deploy altitude
248                          */
249                         if (ao_flight_vel < VEL_MPS_TO_COUNT(-50) ||
250                             ao_flight_vel > VEL_MPS_TO_COUNT(50) ||
251                             ao_flight_pres >= ao_main_pres)
252                         {
253                                 ao_ignite(ao_igniter_main);
254                                 ao_flight_state = ao_flight_main;
255                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
256                         }
257                         /* fall through... */
258                 case ao_flight_main:
259
260                         /* drogue/main to land:
261                          *
262                          * accelerometer: value stable
263                          * barometer: altitude stable
264                          */
265                         if ((ao_interval_max_accel - ao_interval_min_accel) < ACCEL_LAND ||
266                              (ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND)
267                         {
268                                 ao_flight_state = ao_flight_landed;
269                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
270                         }
271                         break;
272                 case ao_flight_landed:
273                         ao_log_stop();
274                         break;
275                 }
276         }
277 }
278
279 static __xdata struct ao_task   flight_task;
280
281 void
282 ao_flight_init(void)
283 {
284         ao_flight_state = ao_flight_startup;
285         ao_interval_min_accel = 0;
286         ao_interval_max_accel = 0x7fff;
287         ao_interval_min_pres = 0;
288         ao_interval_max_pres = 0x7fff;
289         ao_interval_end = AO_INTERVAL_TICKS;
290
291         ao_add_task(&flight_task, ao_flight, "flight");
292 }
293