2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #ifndef AO_FLIGHT_TEST
25 #include <ao_quaternion.h>
29 * Current sensor values
33 #define PRES_TYPE int32_t
34 #define ALT_TYPE int32_t
35 #define ACCEL_TYPE int16_t
38 uint16_t ao_sample_tick; /* time of last data */
40 pres_t ao_sample_pres;
42 alt_t ao_sample_height;
45 accel_t ao_sample_accel;
48 accel_t ao_sample_accel_along;
49 accel_t ao_sample_accel_across;
50 accel_t ao_sample_accel_through;
53 gyro_t ao_sample_roll;
54 gyro_t ao_sample_pitch;
56 angle_t ao_sample_orient;
57 angle_t ao_sample_orients[AO_NUM_ORIENT];
58 uint8_t ao_sample_orient_pos;
61 uint8_t ao_sample_data;
64 * Sensor calibration values
68 pres_t ao_ground_pres; /* startup pressure */
69 alt_t ao_ground_height; /* MSL of ao_ground_pres */
73 accel_t ao_ground_accel; /* startup acceleration */
74 accel_t ao_accel_2g; /* factory accel calibration */
75 int32_t ao_accel_scale; /* sensor to m/s² conversion */
79 accel_t ao_ground_accel_along;
80 accel_t ao_ground_accel_across;
81 accel_t ao_ground_accel_through;
85 int32_t ao_ground_pitch;
86 int32_t ao_ground_yaw;
87 int32_t ao_ground_roll;
90 static uint8_t ao_preflight; /* in preflight mode */
92 static uint16_t nsamples;
94 int32_t ao_sample_pres_sum;
97 int32_t ao_sample_accel_sum;
100 int32_t ao_sample_accel_along_sum;
101 int32_t ao_sample_accel_across_sum;
102 int32_t ao_sample_accel_through_sum;
105 int32_t ao_sample_pitch_sum;
106 int32_t ao_sample_yaw_sum;
107 int32_t ao_sample_roll_sum;
108 static struct ao_quaternion ao_rotation;
112 extern uint8_t ao_orient_test;
116 ao_sample_preflight_add(void)
119 ao_sample_accel_sum += ao_sample_accel;
122 ao_sample_pres_sum += ao_sample_pres;
125 ao_sample_accel_along_sum += ao_sample_accel_along;
126 ao_sample_accel_across_sum += ao_sample_accel_across;
127 ao_sample_accel_through_sum += ao_sample_accel_through;
130 ao_sample_pitch_sum += ao_sample_pitch;
131 ao_sample_yaw_sum += ao_sample_yaw;
132 ao_sample_roll_sum += ao_sample_roll;
139 ao_sample_set_all_orients(void)
142 for (i = 0; i < AO_NUM_ORIENT; i++)
143 ao_sample_orients[i] = ao_sample_orient;
144 ao_sample_orient_pos = 0;
148 ao_sample_set_one_orient(void)
150 ao_sample_orients[ao_sample_orient_pos] = ao_sample_orient;
151 ao_sample_orient_pos = (ao_sample_orient_pos + 1) % AO_NUM_ORIENT;
155 ao_sample_compute_orient(void)
157 /* Compute pitch angle from vertical by taking the pad
158 * orientation vector and rotating it by the current total
159 * rotation value. That will be a unit vector pointing along
160 * the airframe axis. The Z value will be the cosine of the
161 * change in the angle from vertical since boost.
163 * rot = ao_rotation * vertical * ao_rotation°
164 * rot = ao_rotation * (0,0,0,1) * ao_rotation°
165 * = ((a.z, a.y, -a.x, a.r) * (a.r, -a.x, -a.y, -a.z)) .z
167 * = (-a.z * -a.z) + (a.y * -a.y) - (-a.x * -a.x) + (a.r * a.r)
168 * = a.z² - a.y² - a.x² + a.r²
170 * rot = ao_rotation * (0, 0, 0, -1) * ao_rotation°
171 * = ((-a.z, -a.y, a.x, -a.r) * (a.r, -a.x, -a.y, -a.z)) .z
173 * = (a.z * -a.z) + (-a.y * -a.y) - (a.x * -a.x) + (-a.r * a.r)
174 * = -a.z² + a.y² + a.x² - a.r²
178 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;
180 ao_sample_orient = acosf(rotz) * (float) (180.0/M_PI);
182 #endif /* HAS_GYRO */
185 ao_sample_preflight_set(void)
188 ao_ground_accel = ao_sample_accel_sum >> 9;
189 ao_sample_accel_sum = 0;
192 ao_ground_pres = ao_sample_pres_sum >> 9;
193 ao_ground_height = pres_to_altitude(ao_ground_pres);
194 ao_sample_pres_sum = 0;
197 ao_ground_accel_along = ao_sample_accel_along_sum >> 9;
198 ao_ground_accel_across = ao_sample_accel_across_sum >> 9;
199 ao_ground_accel_through = ao_sample_accel_through_sum >> 9;
203 ao_ground_pitch = ao_sample_pitch_sum;
204 ao_ground_yaw = ao_sample_yaw_sum;
205 ao_ground_roll = ao_sample_roll_sum;
206 ao_sample_accel_along_sum = 0;
207 ao_sample_accel_across_sum = 0;
208 ao_sample_accel_through_sum = 0;
209 ao_sample_pitch_sum = 0;
210 ao_sample_yaw_sum = 0;
211 ao_sample_roll_sum = 0;
212 ao_sample_set_all_orients();
214 struct ao_quaternion orient;
216 /* Take the pad IMU acceleration values and compute our current direction
219 ao_quaternion_init_vector(&orient,
220 (ao_ground_accel_across - ao_config.accel_zero_across),
221 (ao_ground_accel_through - ao_config.accel_zero_through),
222 (ao_ground_accel_along - ao_config.accel_zero_along));
224 ao_quaternion_normalize(&orient,
229 struct ao_quaternion up = { .r = 0, .x = 0, .y = 0, .z = 1 };
231 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
234 /* Compute rotation to get from up to our current orientation, set
235 * that as the current rotation vector
237 ao_quaternion_vectors_to_rotation(&ao_rotation, &up, &orient);
240 printf("\n\treset across %d through %d along %d\n",
241 (ao_ground_accel_across - ao_config.accel_zero_across),
242 (ao_ground_accel_through - ao_config.accel_zero_through),
243 (ao_ground_accel_along - ao_config.accel_zero_along));
246 ao_sample_compute_orient();
247 ao_sample_set_all_orients();
254 #define TIME_DIV 200.0f
257 ao_sample_rotate(void)
259 #ifdef AO_FLIGHT_TEST
260 float dt = (int16_t) (ao_sample_tick - ao_sample_prev_tick) / TIME_DIV;
262 static const float dt = 1/TIME_DIV;
264 float x = ao_convert_gyro((float) ((ao_sample_pitch << 9) - ao_ground_pitch) / 512.0f) * dt;
265 float y = ao_convert_gyro((float) ((ao_sample_yaw << 9) - ao_ground_yaw) / 512.0f) * dt;
266 float z = ao_convert_gyro((float) ((ao_sample_roll << 9) - ao_ground_roll) / 512.0f) * dt;
267 struct ao_quaternion rot;
269 ao_quaternion_init_half_euler(&rot, x, y, z);
270 ao_quaternion_multiply(&ao_rotation, &rot, &ao_rotation);
272 /* And normalize to make sure it remains a unit vector */
273 ao_quaternion_normalize(&ao_rotation, &ao_rotation);
276 if (ao_orient_test) {
277 printf ("rot %d %d %d orient %d \r",
284 ao_sample_compute_orient();
285 ao_sample_set_one_orient();
290 ao_sample_preflight(void)
294 * Collect 512 samples of acceleration and pressure
295 * data and average them to find the resting values
297 if (nsamples < 512) {
298 ao_sample_preflight_add();
301 ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
302 ao_accel_scale = to_fix_32(GRAVITY * 2 * 16) / ao_accel_2g;
304 ao_sample_preflight_set();
305 ao_preflight = false;
310 * While in pad mode, constantly update the ground state by
311 * re-averaging the data. This tracks changes in orientation, which
312 * might be caused by adjustments to the rocket on the pad and
313 * pressure, which might be caused by changes in the weather.
317 ao_sample_preflight_update(void)
320 ao_sample_preflight_add();
321 else if (nsamples < 1024)
324 ao_sample_preflight_set();
329 static int32_t p_filt;
330 static int32_t y_filt;
332 static gyro_t inline ao_gyro(void) {
333 gyro_t p = ao_sample_pitch - ao_ground_pitch;
334 gyro_t y = ao_sample_yaw - ao_ground_yaw;
336 p_filt = p_filt - (p_filt >> 6) + p;
337 y_filt = y_filt - (y_filt >> 6) + y;
341 return ao_sqrt(p*p + y*y);
349 ao_wakeup(&ao_sample_data);
350 ao_sleep((void *) &ao_data_head);
351 while (ao_sample_data != ao_data_head) {
352 struct ao_data *ao_data;
354 /* Capture a sample */
355 ao_data = (struct ao_data *) &ao_data_ring[ao_sample_data];
356 ao_sample_tick = ao_data->tick;
359 ao_data_pres_cook(ao_data);
360 ao_sample_pres = ao_data_pres(ao_data);
361 ao_sample_alt = pres_to_altitude(ao_sample_pres);
362 ao_sample_height = ao_sample_alt - ao_ground_height;
366 ao_sample_accel = ao_data_accel(ao_data);
369 ao_sample_accel_along = ao_data_along(ao_data);
370 ao_sample_accel_across = ao_data_across(ao_data);
371 ao_sample_accel_through = ao_data_through(ao_data);
374 ao_sample_pitch = ao_data_pitch(ao_data);
375 ao_sample_yaw = ao_data_yaw(ao_data);
376 ao_sample_roll = ao_data_roll(ao_data);
380 ao_sample_preflight();
382 if (ao_flight_state < ao_flight_boost)
383 ao_sample_preflight_update();
389 #ifdef AO_FLIGHT_TEST
390 ao_sample_prev_tick = ao_sample_tick;
392 ao_sample_data = ao_data_ring_next(ao_sample_data);
394 return !ao_preflight;
403 ao_sample_pres_sum = 0;
407 ao_sample_accel_sum = 0;
411 ao_sample_accel_along_sum = 0;
412 ao_sample_accel_across_sum = 0;
413 ao_sample_accel_through_sum = 0;
414 ao_sample_accel_along = 0;
415 ao_sample_accel_across = 0;
416 ao_sample_accel_through = 0;
419 ao_sample_pitch_sum = 0;
420 ao_sample_yaw_sum = 0;
421 ao_sample_roll_sum = 0;
425 ao_sample_orient = 0;
426 ao_sample_set_all_orients();
428 ao_sample_data = ao_data_head;