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