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