dac110aab9f0c9953b9db25561a8821a9d0f5b46
[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 #include "ao.h"
19
20 /* Main flight thread. */
21
22 __xdata struct ao_adc           ao_flight_data;         /* last acquired data */
23 __pdata enum flight_state       ao_flight_state;        /* current flight state */
24 __pdata uint16_t                ao_flight_tick;         /* time of last data */
25 __pdata int16_t                 ao_flight_accel;        /* filtered acceleration */
26 __pdata int16_t                 ao_flight_pres;         /* filtered pressure */
27 __pdata int16_t                 ao_ground_pres;         /* startup pressure */
28 __pdata int16_t                 ao_ground_accel;        /* startup acceleration */
29 __pdata int16_t                 ao_min_pres;            /* minimum recorded pressure */
30 __pdata uint16_t                ao_launch_time;         /* time of launch detect */
31 __pdata int16_t                 ao_main_pres;           /* pressure to eject main */
32
33 /*
34  * track min/max data over a long interval to detect
35  * resting
36  */
37 __pdata uint16_t                ao_interval_end;
38 __pdata int16_t                 ao_interval_cur_min_accel;
39 __pdata int16_t                 ao_interval_cur_max_accel;
40 __pdata int16_t                 ao_interval_cur_min_pres;
41 __pdata int16_t                 ao_interval_cur_max_pres;
42 __pdata int16_t                 ao_interval_min_accel;
43 __pdata int16_t                 ao_interval_max_accel;
44 __pdata int16_t                 ao_interval_min_pres;
45 __pdata int16_t                 ao_interval_max_pres;
46
47 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(5)
48
49 /* Accelerometer calibration
50  *
51  * We're sampling the accelerometer through a resistor divider which
52  * consists of 5k and 10k resistors. This multiplies the values by 2/3.
53  * That goes into the cc1111 A/D converter, which is running at 11 bits
54  * of precision with the bits in the MSB of the 16 bit value. Only positive
55  * values are used, so values should range from 0-32752 for 0-3.3V. The
56  * specs say we should see 40mV/g (uncalibrated), multiply by 2/3 for what
57  * the A/D converter sees (26.67 mV/g). We should see 32752/3300 counts/mV,
58  * for a final computation of:
59  *
60  * 26.67 mV/g * 32767/3300 counts/mV = 264.8 counts/g
61  *
62  * Zero g was measured at 16000 (we would expect 16384)
63  */
64
65 #define ACCEL_G         265
66 #define ACCEL_ZERO_G    16000
67 #define ACCEL_NOSE_UP   (ACCEL_ZERO_G - ACCEL_G * 2 /3)
68 #define ACCEL_BOOST     (ACCEL_NOSE_UP - ACCEL_G * 2)
69
70 /*
71  * Barometer calibration
72  *
73  * We directly sample the barometer. The specs say:
74  *
75  * Pressure range: 15-115 kPa
76  * Voltage at 115kPa: 2.82
77  * Output scale: 27mV/kPa
78  * 
79  * If we want to detect launch with the barometer, we need
80  * a large enough bump to not be fooled by noise. At typical
81  * launch elevations (0-2000m), a 200Pa pressure change cooresponds
82  * to about a 20m elevation change. This is 5.4mV, or about 3LSB.
83  * As all of our calculations are done in 16 bits, we'll actually see a change
84  * of 16 times this though
85  *
86  * 27 mV/kPa * 32767 / 3300 counts/mV = 268.1 counts/kPa
87  */
88
89 #define BARO_kPa        268
90 #define BARO_LAUNCH     (BARO_kPa / 5)  /* .2kPa */
91 #define BARO_APOGEE     (BARO_kPa / 10) /* .1kPa */
92 #define BARO_MAIN       (BARO_kPa)      /* 1kPa */
93 #define BARO_LAND       (BARO_kPa / 20) /* .05kPa */
94
95 /* We also have a clock, which can be used to sanity check things in
96  * case of other failures
97  */
98
99 #define BOOST_TICKS_MAX AO_SEC_TO_TICKS(10)
100
101 void
102 ao_flight(void)
103 {
104         __pdata static uint8_t  nsamples = 0;
105         
106         for (;;) {
107                 ao_sleep(&ao_adc_ring);
108                 ao_adc_get(&ao_flight_data);
109                 ao_flight_accel -= ao_flight_accel >> 4;
110                 ao_flight_accel += ao_flight_data.accel >> 4;
111                 ao_flight_pres -= ao_flight_pres >> 4;
112                 ao_flight_pres += ao_flight_data.pres >> 4;
113                 ao_flight_tick = ao_time();
114                 
115                 ao_flight_tick = ao_time();
116                 if ((int16_t) (ao_flight_tick - ao_interval_end) >= 0) {
117                         ao_interval_max_pres = ao_interval_cur_max_pres;
118                         ao_interval_min_pres = ao_interval_cur_min_pres;
119                         ao_interval_max_accel = ao_interval_cur_max_accel;
120                         ao_interval_min_accel = ao_interval_cur_min_accel;
121                         ao_interval_end = ao_flight_tick + AO_INTERVAL_TICKS;
122                 }
123                                
124                 switch (ao_flight_state) {
125                 case ao_flight_startup:
126                         if (nsamples < 100) {
127                                 ++nsamples;
128                                 continue;
129                         }
130                         ao_ground_accel = ao_flight_accel;
131                         ao_ground_pres = ao_flight_pres;
132                         ao_min_pres = ao_flight_pres;
133                         ao_main_pres = ao_ground_pres - BARO_MAIN;
134                         
135                         ao_interval_end = ao_flight_tick;
136                         
137                         if (ao_flight_accel < ACCEL_NOSE_UP) {
138                                 ao_flight_state = ao_flight_launchpad;
139                                 ao_report_notify();
140                         } else {
141                                 ao_flight_state = ao_flight_idle;
142                                 ao_report_notify();
143                         }
144                         /* signal successful initialization by turning off the LED */
145                         ao_led_off(AO_LED_RED);
146                         break;
147                 case ao_flight_launchpad:
148                         if (ao_flight_accel < ACCEL_BOOST || 
149                             ao_flight_pres + BARO_LAUNCH < ao_ground_pres)
150                         {
151                                 ao_flight_state = ao_flight_boost;
152                                 ao_log_start();
153                                 ao_report_notify();
154                                 break;
155                         }
156                         break;
157                 case ao_flight_boost:
158                         if (ao_flight_accel > ACCEL_ZERO_G ||
159                             (int16_t) (ao_flight_data.tick - ao_launch_time) > BOOST_TICKS_MAX)
160                         {
161                                 ao_flight_state = ao_flight_coast;
162                                 ao_report_notify();
163                                 break;
164                         }
165                         break;
166                 case ao_flight_coast:
167                         if (ao_flight_pres < ao_min_pres)
168                                 ao_min_pres = ao_flight_pres;
169                         if (ao_flight_pres - BARO_APOGEE > ao_min_pres) {
170                                 ao_flight_state = ao_flight_apogee;
171                                 ao_report_notify();
172                         }
173                         break;
174                 case ao_flight_apogee:
175 //                      ao_ignite(AO_IGNITE_DROGUE);
176                         ao_flight_state = ao_flight_drogue;
177                         ao_report_notify();
178                         break; 
179                 case ao_flight_drogue:
180                         if (ao_flight_pres >= ao_main_pres) {
181 //                              ao_ignite(AO_IGNITE_MAIN);
182                                 ao_flight_state = ao_flight_main;
183                                 ao_report_notify();
184                         }
185                         if ((ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND) {
186                                 ao_flight_state = ao_flight_landed;
187                                 ao_report_notify();
188                         }
189                         break;
190                 case ao_flight_main:
191                         if ((ao_interval_max_pres - ao_interval_min_pres) < BARO_LAND) {
192                                 ao_flight_state = ao_flight_landed;
193                                 ao_report_notify();
194                         }
195                         break;
196                 case ao_flight_landed:
197                         ao_log_stop();
198                         break;
199                 }
200         }
201 }
202
203 static __xdata struct ao_task   flight_task;
204
205 void
206 ao_flight_init(void)
207 {
208         ao_flight_state = ao_flight_startup;
209         ao_interval_min_accel = 0;
210         ao_interval_max_accel = 0x7fff;
211         ao_interval_min_pres = 0;
212         ao_interval_max_pres = 0x7fff;
213         ao_interval_end = AO_INTERVAL_TICKS;
214
215         ao_add_task(&flight_task, ao_flight, "flight");
216 }
217