altos: De-bias height/speed data while on pad
[fw/altos] / src / kernel / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef AO_FLIGHT_TEST
20 #include "ao.h"
21 #include <ao_data.h>
22 #endif
23
24 #if HAS_GYRO
25 #include <ao_quaternion.h>
26 #endif
27
28 /*
29  * Current sensor values
30  */
31
32 #ifndef PRES_TYPE
33 #define PRES_TYPE int32_t
34 #define ALT_TYPE int32_t
35 #define ACCEL_TYPE int16_t
36 #endif
37
38 uint16_t        ao_sample_tick;         /* time of last data */
39 #if HAS_BARO
40 pres_t          ao_sample_pres;
41 alt_t           ao_sample_alt;
42 alt_t           ao_sample_height;
43 #endif
44 #if HAS_ACCEL
45 accel_t         ao_sample_accel;
46 #endif
47 #if HAS_IMU
48 accel_t         ao_sample_accel_along;
49 accel_t         ao_sample_accel_across;
50 accel_t         ao_sample_accel_through;
51 #endif
52 #if HAS_GYRO
53 gyro_t          ao_sample_roll;
54 gyro_t          ao_sample_pitch;
55 gyro_t          ao_sample_yaw;
56 angle_t         ao_sample_orient;
57 angle_t         ao_sample_orients[AO_NUM_ORIENT];
58 uint8_t         ao_sample_orient_pos;
59 #endif
60
61 uint8_t         ao_sample_data;
62
63 /*
64  * Sensor calibration values
65  */
66
67 #if HAS_BARO
68 pres_t          ao_ground_pres;         /* startup pressure */
69 alt_t           ao_ground_height;       /* MSL of ao_ground_pres */
70 #endif
71
72 #if HAS_ACCEL
73 accel_t         ao_ground_accel;        /* startup acceleration */
74 accel_t         ao_accel_2g;            /* factory accel calibration */
75 int32_t         ao_accel_scale;         /* sensor to m/s² conversion */
76 #endif
77
78 #if HAS_IMU
79 accel_t         ao_ground_accel_along;
80 accel_t         ao_ground_accel_across;
81 accel_t         ao_ground_accel_through;
82 #endif
83
84 #if HAS_GYRO
85 int32_t         ao_ground_pitch;
86 int32_t         ao_ground_yaw;
87 int32_t         ao_ground_roll;
88 #endif
89
90 static uint8_t  ao_preflight;           /* in preflight mode */
91
92 static uint16_t nsamples;
93 #if HAS_BARO
94 int32_t ao_sample_pres_sum;
95 #endif
96 #if HAS_ACCEL
97 int32_t ao_sample_accel_sum;
98 #endif
99 #if HAS_IMU
100 int32_t ao_sample_accel_along_sum;
101 int32_t ao_sample_accel_across_sum;
102 int32_t ao_sample_accel_through_sum;
103 #endif
104 #if HAS_GYRO
105 int32_t ao_sample_pitch_sum;
106 int32_t ao_sample_yaw_sum;
107 int32_t ao_sample_roll_sum;
108 static struct ao_quaternion ao_rotation;
109 #endif
110
111 #if HAS_FLIGHT_DEBUG
112 extern uint8_t ao_orient_test;
113 #endif
114
115 static void
116 ao_sample_preflight_add(void)
117 {
118 #if HAS_ACCEL
119         ao_sample_accel_sum += ao_sample_accel;
120 #endif
121 #if HAS_BARO
122         ao_sample_pres_sum += ao_sample_pres;
123 #endif
124 #if HAS_IMU
125         ao_sample_accel_along_sum += ao_sample_accel_along;
126         ao_sample_accel_across_sum += ao_sample_accel_across;
127         ao_sample_accel_through_sum += ao_sample_accel_through;
128 #endif
129 #if HAS_GYRO
130         ao_sample_pitch_sum += ao_sample_pitch;
131         ao_sample_yaw_sum += ao_sample_yaw;
132         ao_sample_roll_sum += ao_sample_roll;
133 #endif
134         ++nsamples;
135 }
136
137 #if HAS_GYRO
138 static void
139 ao_sample_set_all_orients(void)
140 {
141         int i;
142         for (i = 0; i < AO_NUM_ORIENT; i++)
143                 ao_sample_orients[i] = ao_sample_orient;
144         ao_sample_orient_pos = 0;
145 }
146
147 static void
148 ao_sample_set_one_orient(void)
149 {
150         ao_sample_orients[ao_sample_orient_pos] = ao_sample_orient;
151         ao_sample_orient_pos = (ao_sample_orient_pos + 1) % AO_NUM_ORIENT;
152 }
153
154 static void
155 ao_sample_compute_orient(void)
156 {
157         /* Compute pitch angle from vertical by taking the pad
158          * orientation vector and rotating it by the current total
159          * rotation value. That will be a unit vector pointing along
160          * the airframe axis. The Z value will be the cosine of the
161          * change in the angle from vertical since boost.
162          *
163          * rot = ao_rotation * vertical * ao_rotation°
164          * rot = ao_rotation * (0,0,0,1) * ao_rotation°
165          *     = ((a.z, a.y, -a.x, a.r) * (a.r, -a.x, -a.y, -a.z)) .z
166          *
167          *     = (-a.z * -a.z) + (a.y * -a.y) - (-a.x * -a.x) + (a.r * a.r)
168          *     = a.z² - a.y² - a.x² + a.r²
169          *
170          * rot = ao_rotation * (0, 0, 0, -1) * ao_rotation°
171          *     = ((-a.z, -a.y, a.x, -a.r) * (a.r, -a.x, -a.y, -a.z)) .z
172          *
173          *     = (a.z * -a.z) + (-a.y * -a.y) - (a.x * -a.x) + (-a.r * a.r)
174          *     = -a.z² + a.y² + a.x² - a.r²
175          */
176
177         float rotz;
178         rotz = ao_rotation.z * ao_rotation.z - ao_rotation.y * ao_rotation.y - ao_rotation.x * ao_rotation.x + ao_rotation.r * ao_rotation.r;
179
180         ao_sample_orient = acosf(rotz) * (float) (180.0/M_PI);
181 }
182 #endif /* HAS_GYRO */
183
184 static void
185 ao_sample_preflight_set(void)
186 {
187 #if HAS_ACCEL
188         ao_ground_accel = ao_sample_accel_sum >> 9;
189         ao_sample_accel_sum = 0;
190 #endif
191 #if HAS_BARO
192         ao_ground_pres = ao_sample_pres_sum >> 9;
193         ao_ground_height = pres_to_altitude(ao_ground_pres);
194         ao_sample_pres_sum = 0;
195 #endif
196 #if HAS_IMU
197         ao_ground_accel_along = ao_sample_accel_along_sum >> 9;
198         ao_ground_accel_across = ao_sample_accel_across_sum >> 9;
199         ao_ground_accel_through = ao_sample_accel_through_sum >> 9;
200
201 #endif
202 #if HAS_GYRO
203         ao_ground_pitch = ao_sample_pitch_sum;
204         ao_ground_yaw = ao_sample_yaw_sum;
205         ao_ground_roll = ao_sample_roll_sum;
206         ao_sample_accel_along_sum = 0;
207         ao_sample_accel_across_sum = 0;
208         ao_sample_accel_through_sum = 0;
209         ao_sample_pitch_sum = 0;
210         ao_sample_yaw_sum = 0;
211         ao_sample_roll_sum = 0;
212         ao_sample_set_all_orients();
213
214         struct ao_quaternion    orient;
215
216         /* Take the pad IMU acceleration values and compute our current direction
217          */
218
219         ao_quaternion_init_vector(&orient,
220                                   (ao_ground_accel_across - ao_config.accel_zero_across),
221                                   (ao_ground_accel_through - ao_config.accel_zero_through),
222                                   (ao_ground_accel_along - ao_config.accel_zero_along));
223
224         ao_quaternion_normalize(&orient,
225                                 &orient);
226
227         /* Here's up */
228
229         struct ao_quaternion    up = { .r = 0, .x = 0, .y = 0, .z = 1 };
230
231         if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
232                 up.z = -1;
233
234         /* Compute rotation to get from up to our current orientation, set
235          * that as the current rotation vector
236          */
237         ao_quaternion_vectors_to_rotation(&ao_rotation, &up, &orient);
238 #if HAS_FLIGHT_DEBUG
239         if (ao_orient_test)
240                 printf("\n\treset across %d through %d along %d\n",
241                        (ao_ground_accel_across - ao_config.accel_zero_across),
242                        (ao_ground_accel_through - ao_config.accel_zero_through),
243                        (ao_ground_accel_along - ao_config.accel_zero_along));
244 #endif  
245
246         ao_sample_compute_orient();
247         ao_sample_set_all_orients();
248 #endif
249         nsamples = 0;
250 }
251
252 #if HAS_GYRO
253
254 #define TIME_DIV        200.0f
255
256 static void
257 ao_sample_rotate(void)
258 {
259 #ifdef AO_FLIGHT_TEST
260         float   dt = (int16_t) (ao_sample_tick - ao_sample_prev_tick) / TIME_DIV;
261 #else
262         static const float dt = 1/TIME_DIV;
263 #endif
264         float   x = ao_convert_gyro((float) ((ao_sample_pitch << 9) - ao_ground_pitch) / 512.0f) * dt;
265         float   y = ao_convert_gyro((float) ((ao_sample_yaw << 9) - ao_ground_yaw) / 512.0f) * dt;
266         float   z = ao_convert_gyro((float) ((ao_sample_roll << 9) - ao_ground_roll) / 512.0f) * dt;
267         struct ao_quaternion    rot;
268
269         ao_quaternion_init_half_euler(&rot, x, y, z);
270         ao_quaternion_multiply(&ao_rotation, &rot, &ao_rotation);
271
272         /* And normalize to make sure it remains a unit vector */
273         ao_quaternion_normalize(&ao_rotation, &ao_rotation);
274
275 #if HAS_FLIGHT_DEBUG
276         if (ao_orient_test) {
277                 printf ("rot %d %d %d orient %d     \r",
278                         (int) (x * 1000),
279                         (int) (y * 1000),
280                         (int) (z * 1000),
281                         ao_sample_orient);
282         }
283 #endif
284         ao_sample_compute_orient();
285         ao_sample_set_one_orient();
286 }
287 #endif
288
289 static void
290 ao_sample_preflight(void)
291 {
292         /* startup state:
293          *
294          * Collect 512 samples of acceleration and pressure
295          * data and average them to find the resting values
296          */
297         if (nsamples < 512) {
298                 ao_sample_preflight_add();
299         } else {
300 #if HAS_ACCEL
301                 ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
302                 ao_accel_scale = to_fix_32(GRAVITY * 2 * 16) / ao_accel_2g;
303 #endif
304                 ao_sample_preflight_set();
305                 ao_preflight = false;
306         }
307 }
308
309 /*
310  * While in pad mode, constantly update the ground state by
311  * re-averaging the data.  This tracks changes in orientation, which
312  * might be caused by adjustments to the rocket on the pad and
313  * pressure, which might be caused by changes in the weather.
314  */
315
316 static void
317 ao_sample_preflight_update(void)
318 {
319         if (nsamples < 512)
320                 ao_sample_preflight_add();
321         else if (nsamples < 1024)
322                 ++nsamples;
323         else
324                 ao_sample_preflight_set();
325 #if !HAS_BARO
326         if ((nsamples & 0x3f) == 0)
327                 ao_kalman_reset_accumulate();
328 #endif
329 }
330
331 #if 0
332 #if HAS_GYRO
333 static int32_t  p_filt;
334 static int32_t  y_filt;
335
336 static gyro_t inline ao_gyro(void) {
337         gyro_t  p = ao_sample_pitch - ao_ground_pitch;
338         gyro_t  y = ao_sample_yaw - ao_ground_yaw;
339
340         p_filt = p_filt - (p_filt >> 6) + p;
341         y_filt = y_filt - (y_filt >> 6) + y;
342
343         p = p_filt >> 6;
344         y = y_filt >> 6;
345         return ao_sqrt(p*p + y*y);
346 }
347 #endif
348 #endif
349
350 uint8_t
351 ao_sample(void)
352 {
353         ao_wakeup(&ao_sample_data);
354         ao_sleep((void *) &ao_data_head);
355         while (ao_sample_data != ao_data_head) {
356                 struct ao_data *ao_data;
357
358                 /* Capture a sample */
359                 ao_data = (struct ao_data *) &ao_data_ring[ao_sample_data];
360                 ao_sample_tick = ao_data->tick;
361
362 #if HAS_BARO
363                 ao_data_pres_cook(ao_data);
364                 ao_sample_pres = ao_data_pres(ao_data);
365                 ao_sample_alt = pres_to_altitude(ao_sample_pres);
366                 ao_sample_height = ao_sample_alt - ao_ground_height;
367 #endif
368
369 #if HAS_ACCEL
370                 ao_sample_accel = ao_data_accel(ao_data);
371 #endif
372 #if HAS_IMU
373                 ao_sample_accel_along = ao_data_along(ao_data);
374                 ao_sample_accel_across = ao_data_across(ao_data);
375                 ao_sample_accel_through = ao_data_through(ao_data);
376 #endif
377 #if HAS_GYRO
378                 ao_sample_pitch = ao_data_pitch(ao_data);
379                 ao_sample_yaw = ao_data_yaw(ao_data);
380                 ao_sample_roll = ao_data_roll(ao_data);
381 #endif
382
383                 if (ao_preflight)
384                         ao_sample_preflight();
385                 else {
386                         if (ao_flight_state < ao_flight_boost)
387                                 ao_sample_preflight_update();
388                         ao_kalman();
389 #if HAS_GYRO
390                         ao_sample_rotate();
391 #endif
392                 }
393 #ifdef AO_FLIGHT_TEST
394                 ao_sample_prev_tick = ao_sample_tick;
395 #endif
396                 ao_sample_data = ao_data_ring_next(ao_sample_data);
397         }
398         return !ao_preflight;
399 }
400
401 void
402 ao_sample_init(void)
403 {
404         ao_config_get();
405         nsamples = 0;
406 #if HAS_BARO
407         ao_sample_pres_sum = 0;
408         ao_sample_pres = 0;
409 #endif
410 #if HAS_ACCEL
411         ao_sample_accel_sum = 0;
412         ao_sample_accel = 0;
413 #endif
414 #if HAS_IMU
415         ao_sample_accel_along_sum = 0;
416         ao_sample_accel_across_sum = 0;
417         ao_sample_accel_through_sum = 0;
418         ao_sample_accel_along = 0;
419         ao_sample_accel_across = 0;
420         ao_sample_accel_through = 0;
421 #endif
422 #if HAS_GYRO
423         ao_sample_pitch_sum = 0;
424         ao_sample_yaw_sum = 0;
425         ao_sample_roll_sum = 0;
426         ao_sample_pitch = 0;
427         ao_sample_yaw = 0;
428         ao_sample_roll = 0;
429         ao_sample_orient = 0;
430         ao_sample_set_all_orients();
431 #endif
432         ao_sample_data = ao_data_head;
433         ao_preflight = true;
434 }