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