altos: Keep 9 more bits of average pad IMU gyro data
[fw/altos] / src / core / ao_sample.c
1 /*
2  * Copyright © 2011 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 #include <ao_data.h>
21 #endif
22
23 #if HAS_GYRO
24 #include <ao_quaternion.h>
25 #endif
26
27 /*
28  * Current sensor values
29  */
30
31 #ifndef PRES_TYPE
32 #define PRES_TYPE int32_t
33 #define ALT_TYPE int32_t
34 #define ACCEL_TYPE int16_t
35 #endif
36
37 __pdata uint16_t        ao_sample_tick;         /* time of last data */
38 __pdata pres_t          ao_sample_pres;
39 __pdata alt_t           ao_sample_alt;
40 __pdata alt_t           ao_sample_height;
41 #if HAS_ACCEL
42 __pdata accel_t         ao_sample_accel;
43 #endif
44 #if HAS_GYRO
45 __pdata accel_t         ao_sample_accel_along;
46 __pdata accel_t         ao_sample_accel_across;
47 __pdata accel_t         ao_sample_accel_through;
48 __pdata gyro_t          ao_sample_roll;
49 __pdata gyro_t          ao_sample_pitch;
50 __pdata gyro_t          ao_sample_yaw;
51 __pdata angle_t         ao_sample_orient;
52 #endif
53
54 __data uint8_t          ao_sample_data;
55
56 /*
57  * Sensor calibration values
58  */
59
60 __pdata pres_t          ao_ground_pres;         /* startup pressure */
61 __pdata alt_t           ao_ground_height;       /* MSL of ao_ground_pres */
62
63 #if HAS_ACCEL
64 __pdata accel_t         ao_ground_accel;        /* startup acceleration */
65 __pdata accel_t         ao_accel_2g;            /* factory accel calibration */
66 __pdata int32_t         ao_accel_scale;         /* sensor to m/s² conversion */
67 #endif
68
69 #if HAS_GYRO
70 __pdata accel_t         ao_ground_accel_along;
71 __pdata accel_t         ao_ground_accel_across;
72 __pdata accel_t         ao_ground_accel_through;
73 __pdata int32_t         ao_ground_pitch;
74 __pdata int32_t         ao_ground_yaw;
75 __pdata int32_t         ao_ground_roll;
76 #endif
77
78 static __pdata uint8_t  ao_preflight;           /* in preflight mode */
79
80 static __pdata uint16_t nsamples;
81 __pdata int32_t ao_sample_pres_sum;
82 #if HAS_ACCEL
83 __pdata int32_t ao_sample_accel_sum;
84 #endif
85 #if HAS_GYRO
86 __pdata int32_t ao_sample_accel_along_sum;
87 __pdata int32_t ao_sample_accel_across_sum;
88 __pdata int32_t ao_sample_accel_through_sum;
89 __pdata int32_t ao_sample_pitch_sum;
90 __pdata int32_t ao_sample_yaw_sum;
91 __pdata int32_t ao_sample_roll_sum;
92 static struct ao_quaternion ao_rotation;
93 static struct ao_quaternion ao_pad_orientation;
94 #endif
95
96 static void
97 ao_sample_preflight_add(void)
98 {
99 #if HAS_ACCEL
100         ao_sample_accel_sum += ao_sample_accel;
101 #endif
102         ao_sample_pres_sum += ao_sample_pres;
103 #if HAS_GYRO
104         ao_sample_accel_along_sum += ao_sample_accel_along;
105         ao_sample_accel_across_sum += ao_sample_accel_across;
106         ao_sample_accel_through_sum += ao_sample_accel_through;
107         ao_sample_pitch_sum += ao_sample_pitch;
108         ao_sample_yaw_sum += ao_sample_yaw;
109         ao_sample_roll_sum += ao_sample_roll;
110 #endif
111         ++nsamples;
112 }
113
114 static void
115 ao_sample_preflight_set(void)
116 {
117 #if HAS_ACCEL
118         ao_ground_accel = ao_sample_accel_sum >> 9;
119         ao_sample_accel_sum = 0;
120 #endif
121         ao_ground_pres = ao_sample_pres_sum >> 9;
122         ao_ground_height = pres_to_altitude(ao_ground_pres);
123         ao_sample_pres_sum = 0;
124 #if HAS_GYRO
125         ao_ground_accel_along = ao_sample_accel_along_sum >> 9;
126         ao_ground_accel_across = ao_sample_accel_across_sum >> 9;
127         ao_ground_accel_through = ao_sample_accel_through_sum >> 9;
128         ao_ground_pitch = ao_sample_pitch_sum;
129         ao_ground_yaw = ao_sample_yaw_sum;
130         ao_ground_roll = ao_sample_roll_sum;
131         ao_sample_accel_along_sum = 0;
132         ao_sample_accel_across_sum = 0;
133         ao_sample_accel_through_sum = 0;
134         ao_sample_pitch_sum = 0;
135         ao_sample_yaw_sum = 0;
136         ao_sample_roll_sum = 0;
137         ao_sample_orient = 0;
138
139         /* No rotation yet */
140         ao_quaternion_init_zero_rotation(&ao_rotation);
141
142         /* Take the pad IMU acceleration values and compute our current direction
143          */
144         ao_quaternion_init_vector(&ao_pad_orientation,
145                                   ao_ground_accel_across - ao_config.accel_zero_across,
146                                   ao_ground_accel_through - ao_config.accel_zero_through,
147                                   -ao_ground_accel_along - ao_config.accel_zero_along);
148
149         ao_quaternion_normalize(&ao_pad_orientation,
150                                 &ao_pad_orientation);
151                                   
152 #endif  
153         nsamples = 0;
154 }
155
156 #if HAS_GYRO
157 static void
158 ao_sample_rotate(void)
159 {
160 #ifdef AO_FLIGHT_TEST
161         float   dt = (ao_sample_tick - ao_sample_prev_tick) / 100.0;
162 #else
163         static const float dt = 1/100.0;
164 #endif
165         float   x = ao_mpu6000_gyro((float) ((ao_sample_pitch << 9) - ao_ground_pitch) / 512.0f) * dt;
166         float   y = ao_mpu6000_gyro((float) ((ao_sample_yaw << 9) - ao_ground_yaw) / 512.0f) * dt;
167         float   z = ao_mpu6000_gyro((float) ((ao_sample_roll << 9) - ao_ground_roll) / 512.0f) * dt;
168         struct ao_quaternion    rot;
169         struct ao_quaternion    point;
170
171         /* The amount of rotation is just the length of the vector. Now,
172          * here's the trick -- assume that the rotation amount is small. In this case,
173          * sin(x) ≃ x, so we can just make this the sin.
174          */
175
176         n_2 = x*x + y*y + z*z;
177         n = sqrtf(n_2);
178         s = n / 2;
179         if (s > 1)
180                 s = 1;
181         c = sqrtf(1 - s*s);
182
183         /* Make unit vector */
184         if (n > 0) {
185                 x /= n;
186                 y /= n;
187                 z /= n;
188         }
189
190         /* Now compute the unified rotation quaternion */
191
192         ao_quaternion_init_rotation(&rot,
193                                     x, y, z,
194                                     s, c);
195
196         /* Integrate with the previous rotation amount */
197         ao_quaternion_multiply(&ao_rotation, &ao_rotation, &rot);
198
199         /* And normalize to make sure it remains a unit vector */
200         ao_quaternion_normalize(&ao_rotation, &ao_rotation);
201
202         /* Compute pitch angle from vertical by taking the pad
203          * orientation vector and rotating it by the current total
204          * rotation value. That will be a unit vector pointing along
205          * the airframe axis. The Z value will be the cosine of the
206          * change in the angle from vertical since boost
207          */
208
209         ao_quaternion_rotate(&point, &ao_pad_orientation, &ao_rotation);
210
211         ao_sample_orient = acosf(rotz) * (float) (180.0/M_PI);
212 }
213 #endif
214
215 static void
216 ao_sample_preflight(void)
217 {
218         /* startup state:
219          *
220          * Collect 512 samples of acceleration and pressure
221          * data and average them to find the resting values
222          */
223         if (nsamples < 512) {
224                 ao_sample_preflight_add();
225         } else {
226 #if HAS_ACCEL
227                 ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
228                 ao_accel_scale = to_fix32(GRAVITY * 2 * 16) / ao_accel_2g;
229 #endif
230                 ao_sample_preflight_set();
231                 ao_preflight = FALSE;
232         }
233 }
234
235 /*
236  * While in pad mode, constantly update the ground state by
237  * re-averaging the data.  This tracks changes in orientation, which
238  * might be caused by adjustments to the rocket on the pad and
239  * pressure, which might be caused by changes in the weather.
240  */
241
242 static void
243 ao_sample_preflight_update(void)
244 {
245         if (nsamples < 512)
246                 ao_sample_preflight_add();
247         else if (nsamples < 1024)
248                 ++nsamples;
249         else
250                 ao_sample_preflight_set();
251 }
252
253 #if 0
254 #if HAS_GYRO
255 static int32_t  p_filt;
256 static int32_t  y_filt;
257
258 static gyro_t inline ao_gyro(void) {
259         gyro_t  p = ao_sample_pitch - ao_ground_pitch;
260         gyro_t  y = ao_sample_yaw - ao_ground_yaw;
261
262         p_filt = p_filt - (p_filt >> 6) + p;
263         y_filt = y_filt - (y_filt >> 6) + y;
264
265         p = p_filt >> 6;
266         y = y_filt >> 6;
267         return ao_sqrt(p*p + y*y);
268 }
269 #endif
270 #endif
271
272 uint8_t
273 ao_sample(void)
274 {
275         ao_wakeup(DATA_TO_XDATA(&ao_sample_data));
276         ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
277         while (ao_sample_data != ao_data_head) {
278                 __xdata struct ao_data *ao_data;
279
280                 /* Capture a sample */
281                 ao_data = (struct ao_data *) &ao_data_ring[ao_sample_data];
282                 ao_sample_tick = ao_data->tick;
283
284 #if HAS_BARO
285                 ao_data_pres_cook(ao_data);
286                 ao_sample_pres = ao_data_pres(ao_data);
287                 ao_sample_alt = pres_to_altitude(ao_sample_pres);
288                 ao_sample_height = ao_sample_alt - ao_ground_height;
289 #endif
290
291 #if HAS_ACCEL
292                 ao_sample_accel = ao_data_accel_cook(ao_data);
293                 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
294                         ao_sample_accel = ao_data_accel_invert(ao_sample_accel);
295                 ao_data_set_accel(ao_data, ao_sample_accel);
296 #endif
297 #if HAS_GYRO
298                 ao_sample_accel_along = ao_data_along(ao_data);
299                 ao_sample_accel_across = ao_data_across(ao_data);
300                 ao_sample_accel_through = ao_data_through(ao_data);
301                 ao_sample_pitch = ao_data_pitch(ao_data);
302                 ao_sample_yaw = ao_data_yaw(ao_data);
303                 ao_sample_roll = ao_data_roll(ao_data);
304 #endif
305
306                 if (ao_preflight)
307                         ao_sample_preflight();
308                 else {
309                         if (ao_flight_state < ao_flight_boost)
310                                 ao_sample_preflight_update();
311                         ao_kalman();
312 #if HAS_GYRO
313                         ao_sample_rotate();
314 #endif
315                 }
316 #ifdef AO_FLIGHT_TEST
317                 ao_sample_prev_tick = ao_sample_tick;
318 #endif
319                 ao_sample_data = ao_data_ring_next(ao_sample_data);
320         }
321         return !ao_preflight;
322 }
323
324 void
325 ao_sample_init(void)
326 {
327         ao_config_get();
328         nsamples = 0;
329         ao_sample_pres_sum = 0;
330         ao_sample_pres = 0;
331 #if HAS_ACCEL
332         ao_sample_accel_sum = 0;
333         ao_sample_accel = 0;
334 #endif
335 #if HAS_GYRO
336         ao_sample_accel_along_sum = 0;
337         ao_sample_accel_across_sum = 0;
338         ao_sample_accel_through_sum = 0;
339         ao_sample_accel_along = 0;
340         ao_sample_accel_across = 0;
341         ao_sample_accel_through = 0;
342         ao_sample_pitch_sum = 0;
343         ao_sample_yaw_sum = 0;
344         ao_sample_roll_sum = 0;
345         ao_sample_pitch = 0;
346         ao_sample_yaw = 0;
347         ao_sample_roll = 0;
348         ao_sample_orient = 0;
349 #endif
350         ao_sample_data = ao_data_head;
351         ao_preflight = TRUE;
352 }