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