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; version 2 of the License.
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.
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.
18 #ifndef AO_FLIGHT_TEST
24 #include <ao_quaternion.h>
28 * Current sensor values
32 #define PRES_TYPE int32_t
33 #define ALT_TYPE int32_t
34 #define ACCEL_TYPE int16_t
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;
42 __pdata accel_t ao_sample_accel;
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;
54 __data uint8_t ao_sample_data;
57 * Sensor calibration values
60 __pdata pres_t ao_ground_pres; /* startup pressure */
61 __pdata alt_t ao_ground_height; /* MSL of ao_ground_pres */
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 */
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;
78 static __pdata uint8_t ao_preflight; /* in preflight mode */
80 static __pdata uint16_t nsamples;
81 __pdata int32_t ao_sample_pres_sum;
83 __pdata int32_t ao_sample_accel_sum;
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;
96 ao_sample_preflight_add(void)
99 ao_sample_accel_sum += ao_sample_accel;
101 ao_sample_pres_sum += ao_sample_pres;
103 ao_sample_accel_along_sum += ao_sample_accel_along;
104 ao_sample_accel_across_sum += ao_sample_accel_across;
105 ao_sample_accel_through_sum += ao_sample_accel_through;
106 ao_sample_pitch_sum += ao_sample_pitch;
107 ao_sample_yaw_sum += ao_sample_yaw;
108 ao_sample_roll_sum += ao_sample_roll;
114 ao_sample_preflight_set(void)
117 ao_ground_accel = ao_sample_accel_sum >> 9;
118 ao_sample_accel_sum = 0;
120 ao_ground_pres = ao_sample_pres_sum >> 9;
121 ao_ground_height = pres_to_altitude(ao_ground_pres);
122 ao_sample_pres_sum = 0;
124 ao_ground_accel_along = ao_sample_accel_along_sum >> 9;
125 ao_ground_accel_across = ao_sample_accel_across_sum >> 9;
126 ao_ground_accel_through = ao_sample_accel_through_sum >> 9;
127 ao_ground_pitch = ao_sample_pitch_sum;
128 ao_ground_yaw = ao_sample_yaw_sum;
129 ao_ground_roll = ao_sample_roll_sum;
130 ao_sample_accel_along_sum = 0;
131 ao_sample_accel_across_sum = 0;
132 ao_sample_accel_through_sum = 0;
133 ao_sample_pitch_sum = 0;
134 ao_sample_yaw_sum = 0;
135 ao_sample_roll_sum = 0;
136 ao_sample_orient = 0;
138 struct ao_quaternion orient;
140 /* Take the pad IMU acceleration values and compute our current direction
143 ao_quaternion_init_vector(&orient,
144 (ao_ground_accel_across - ao_config.accel_zero_across),
145 (ao_ground_accel_through - ao_config.accel_zero_through),
146 (ao_ground_accel_along - ao_config.accel_zero_along));
148 ao_quaternion_normalize(&orient,
153 struct ao_quaternion up = { .r = 0, .x = 0, .y = 0, .z = 1 };
155 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
158 /* Compute rotation to get from up to our current orientation, set
159 * that as the current rotation vector
161 ao_quaternion_vectors_to_rotation(&ao_rotation, &up, &orient);
168 #define TIME_DIV 200.0f
171 ao_sample_rotate(void)
173 #ifdef AO_FLIGHT_TEST
174 float dt = (ao_sample_tick - ao_sample_prev_tick) / TIME_DIV;
176 static const float dt = 1/TIME_DIV;
178 float x = ao_mpu6000_gyro((float) ((ao_sample_pitch << 9) - ao_ground_pitch) / 512.0f) * dt;
179 float y = ao_mpu6000_gyro((float) ((ao_sample_yaw << 9) - ao_ground_yaw) / 512.0f) * dt;
180 float z = ao_mpu6000_gyro((float) ((ao_sample_roll << 9) - ao_ground_roll) / 512.0f) * dt;
181 struct ao_quaternion rot;
183 ao_quaternion_init_half_euler(&rot, x, y, z);
184 ao_quaternion_multiply(&ao_rotation, &rot, &ao_rotation);
186 /* And normalize to make sure it remains a unit vector */
187 ao_quaternion_normalize(&ao_rotation, &ao_rotation);
189 /* Compute pitch angle from vertical by taking the pad
190 * orientation vector and rotating it by the current total
191 * rotation value. That will be a unit vector pointing along
192 * the airframe axis. The Z value will be the cosine of the
193 * change in the angle from vertical since boost.
195 * rot = ao_rotation * vertical * ao_rotation°
196 * rot = ao_rotation * (0,0,0,1) * ao_rotation°
197 * = ((a.z, a.y, -a.x, a.r) * (a.r, -a.x, -a.y, -a.z)) .z
199 * = (-a.z * -a.z) + (a.y * -a.y) - (-a.x * -a.x) + (a.r * a.r)
200 * = a.z² - a.y² - a.x² + a.r²
202 * rot = ao_rotation * (0, 0, 0, -1) * ao_rotation°
203 * = ((-a.z, -a.y, a.x, -a.r) * (a.r, -a.x, -a.y, -a.z)) .z
205 * = (a.z * -a.z) + (-a.y * -a.y) - (a.x * -a.x) + (-a.r * a.r)
206 * = -a.z² + a.y² + a.x² - a.r²
210 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;
212 ao_sample_orient = acosf(rotz) * (float) (180.0/M_PI);
217 ao_sample_preflight(void)
221 * Collect 512 samples of acceleration and pressure
222 * data and average them to find the resting values
224 if (nsamples < 512) {
225 ao_sample_preflight_add();
228 ao_accel_2g = ao_config.accel_minus_g - ao_config.accel_plus_g;
229 ao_accel_scale = to_fix32(GRAVITY * 2 * 16) / ao_accel_2g;
231 ao_sample_preflight_set();
232 ao_preflight = FALSE;
237 * While in pad mode, constantly update the ground state by
238 * re-averaging the data. This tracks changes in orientation, which
239 * might be caused by adjustments to the rocket on the pad and
240 * pressure, which might be caused by changes in the weather.
244 ao_sample_preflight_update(void)
247 ao_sample_preflight_add();
248 else if (nsamples < 1024)
251 ao_sample_preflight_set();
256 static int32_t p_filt;
257 static int32_t y_filt;
259 static gyro_t inline ao_gyro(void) {
260 gyro_t p = ao_sample_pitch - ao_ground_pitch;
261 gyro_t y = ao_sample_yaw - ao_ground_yaw;
263 p_filt = p_filt - (p_filt >> 6) + p;
264 y_filt = y_filt - (y_filt >> 6) + y;
268 return ao_sqrt(p*p + y*y);
276 ao_wakeup(DATA_TO_XDATA(&ao_sample_data));
277 ao_sleep((void *) DATA_TO_XDATA(&ao_data_head));
278 while (ao_sample_data != ao_data_head) {
279 __xdata struct ao_data *ao_data;
281 /* Capture a sample */
282 ao_data = (struct ao_data *) &ao_data_ring[ao_sample_data];
283 ao_sample_tick = ao_data->tick;
286 ao_data_pres_cook(ao_data);
287 ao_sample_pres = ao_data_pres(ao_data);
288 ao_sample_alt = pres_to_altitude(ao_sample_pres);
289 ao_sample_height = ao_sample_alt - ao_ground_height;
293 ao_sample_accel = ao_data_accel_cook(ao_data);
294 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
295 ao_sample_accel = ao_data_accel_invert(ao_sample_accel);
296 ao_data_set_accel(ao_data, ao_sample_accel);
299 ao_sample_accel_along = ao_data_along(ao_data);
300 ao_sample_accel_across = ao_data_across(ao_data);
301 ao_sample_accel_through = ao_data_through(ao_data);
302 ao_sample_pitch = ao_data_pitch(ao_data);
303 ao_sample_yaw = ao_data_yaw(ao_data);
304 ao_sample_roll = ao_data_roll(ao_data);
308 ao_sample_preflight();
310 if (ao_flight_state < ao_flight_boost)
311 ao_sample_preflight_update();
317 #ifdef AO_FLIGHT_TEST
318 ao_sample_prev_tick = ao_sample_tick;
320 ao_sample_data = ao_data_ring_next(ao_sample_data);
322 return !ao_preflight;
330 ao_sample_pres_sum = 0;
333 ao_sample_accel_sum = 0;
337 ao_sample_accel_along_sum = 0;
338 ao_sample_accel_across_sum = 0;
339 ao_sample_accel_through_sum = 0;
340 ao_sample_accel_along = 0;
341 ao_sample_accel_across = 0;
342 ao_sample_accel_through = 0;
343 ao_sample_pitch_sum = 0;
344 ao_sample_yaw_sum = 0;
345 ao_sample_roll_sum = 0;
349 ao_sample_orient = 0;
351 ao_sample_data = ao_data_head;