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