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