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
20 #include "ao_flight.h"
23 #include "ao_sample.h"
24 #include "ao_kalman.h"
27 static __pdata int32_t ao_k_height;
28 static __pdata int32_t ao_k_speed;
29 static __pdata int32_t ao_k_accel;
31 #define AO_K_STEP_100 to_fix16(0.01)
32 #define AO_K_STEP_2_2_100 to_fix16(0.00005)
34 #define AO_K_STEP_10 to_fix16(0.1)
35 #define AO_K_STEP_2_2_10 to_fix16(0.005)
37 #define AO_K_STEP_1 to_fix16(1)
38 #define AO_K_STEP_2_2_1 to_fix16(0.5)
40 __pdata int16_t ao_height;
41 __pdata int16_t ao_speed;
42 __pdata int16_t ao_accel;
43 __pdata int16_t ao_max_height;
44 static __pdata int32_t ao_avg_height_scaled;
45 __pdata int16_t ao_avg_height;
47 __pdata int16_t ao_error_h;
48 __pdata int16_t ao_error_h_sq_avg;
51 __pdata int16_t ao_error_a;
55 ao_kalman_predict(void)
58 if (ao_sample_tick - ao_sample_prev_tick > 50) {
59 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_1 +
60 (int32_t) ao_accel * AO_K_STEP_2_2_1) >> 4;
61 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_1;
65 if (ao_sample_tick - ao_sample_prev_tick > 5) {
66 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_10 +
67 (int32_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
68 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_10;
72 if (ao_flight_debug) {
73 printf ("predict speed %g + (%g * %g) = %g\n",
74 ao_k_speed / (65536.0 * 16.0), ao_accel / 16.0, AO_K_STEP_100 / 65536.0,
75 (ao_k_speed + (int32_t) ao_accel * AO_K_STEP_100) / (65536.0 * 16.0));
78 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_100 +
79 (int32_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
80 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_100;
84 ao_kalman_err_height(void)
87 int16_t height_distrust;
89 int16_t speed_distrust;
92 ao_error_h = ao_sample_height - (int16_t) (ao_k_height >> 16);
100 ao_error_h_sq_avg -= ao_error_h_sq_avg >> 2;
101 ao_error_h_sq_avg += (e * e) >> 2;
103 ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
104 ao_error_h_sq_avg += (e * e) >> 4;
107 if (ao_flight_state >= ao_flight_drogue)
109 height_distrust = ao_sample_alt - AO_MAX_BARO_HEIGHT;
111 /* speed is stored * 16, but we need to ramp between 200 and 328, so
112 * we want to multiply by 2. The result is a shift by 3.
114 speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
115 if (speed_distrust <= 0)
117 else if (speed_distrust > height_distrust)
118 height_distrust = speed_distrust;
120 if (height_distrust > 0) {
121 #ifdef AO_FLIGHT_TEST
122 int old_ao_error_h = ao_error_h;
124 if (height_distrust > 0x100)
125 height_distrust = 0x100;
126 ao_error_h = (int16_t) (((int32_t) ao_error_h * (0x100 - height_distrust)) >> 8);
127 #ifdef AO_FLIGHT_TEST
128 if (ao_flight_debug) {
129 printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
130 (double) (ao_sample_alt - AO_MAX_BARO_HEIGHT),
131 (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
132 height_distrust / 256.0,
133 old_ao_error_h, ao_error_h);
140 ao_kalman_correct_baro(void)
142 ao_kalman_err_height();
143 #ifdef AO_FLIGHT_TEST
144 if (ao_sample_tick - ao_sample_prev_tick > 50) {
145 ao_k_height += (int32_t) AO_BARO_K0_1 * ao_error_h;
146 ao_k_speed += (int32_t) AO_BARO_K1_1 * ao_error_h;
147 ao_k_accel += (int32_t) AO_BARO_K2_1 * ao_error_h;
150 if (ao_sample_tick - ao_sample_prev_tick > 5) {
151 ao_k_height += (int32_t) AO_BARO_K0_10 * ao_error_h;
152 ao_k_speed += (int32_t) AO_BARO_K1_10 * ao_error_h;
153 ao_k_accel += (int32_t) AO_BARO_K2_10 * ao_error_h;
157 ao_k_height += (int32_t) AO_BARO_K0_100 * ao_error_h;
158 ao_k_speed += (int32_t) AO_BARO_K1_100 * ao_error_h;
159 ao_k_accel += (int32_t) AO_BARO_K2_100 * ao_error_h;
165 ao_kalman_err_accel(void)
169 accel = (ao_ground_accel - ao_sample_accel) * ao_accel_scale;
171 /* Can't use ao_accel here as it is the pre-prediction value still */
172 ao_error_a = (accel - ao_k_accel) >> 16;
176 ao_kalman_correct_both(void)
178 ao_kalman_err_height();
179 ao_kalman_err_accel();
181 #ifdef AO_FLIGHT_TEST
182 if (ao_sample_tick - ao_sample_prev_tick > 50) {
183 if (ao_flight_debug) {
184 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
185 ao_k_speed / (65536.0 * 16.0),
186 (double) ao_error_h, AO_BOTH_K10_1 / 65536.0,
187 (double) ao_error_a, AO_BOTH_K11_1 / 65536.0,
189 (int32_t) AO_BOTH_K10_1 * ao_error_h +
190 (int32_t) AO_BOTH_K11_1 * ao_error_a) / (65536.0 * 16.0));
193 (int32_t) AO_BOTH_K00_1 * ao_error_h +
194 (int32_t) AO_BOTH_K01_1 * ao_error_a;
196 (int32_t) AO_BOTH_K10_1 * ao_error_h +
197 (int32_t) AO_BOTH_K11_1 * ao_error_a;
199 (int32_t) AO_BOTH_K20_1 * ao_error_h +
200 (int32_t) AO_BOTH_K21_1 * ao_error_a;
203 if (ao_sample_tick - ao_sample_prev_tick > 5) {
204 if (ao_flight_debug) {
205 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
206 ao_k_speed / (65536.0 * 16.0),
207 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
208 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
210 (int32_t) AO_BOTH_K10_10 * ao_error_h +
211 (int32_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
214 (int32_t) AO_BOTH_K00_10 * ao_error_h +
215 (int32_t) AO_BOTH_K01_10 * ao_error_a;
217 (int32_t) AO_BOTH_K10_10 * ao_error_h +
218 (int32_t) AO_BOTH_K11_10 * ao_error_a;
220 (int32_t) AO_BOTH_K20_10 * ao_error_h +
221 (int32_t) AO_BOTH_K21_10 * ao_error_a;
224 if (ao_flight_debug) {
225 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
226 ao_k_speed / (65536.0 * 16.0),
227 (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
228 (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
230 (int32_t) AO_BOTH_K10_100 * ao_error_h +
231 (int32_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
235 (int32_t) AO_BOTH_K00_100 * ao_error_h +
236 (int32_t) AO_BOTH_K01_100 * ao_error_a;
238 (int32_t) AO_BOTH_K10_100 * ao_error_h +
239 (int32_t) AO_BOTH_K11_100 * ao_error_a;
241 (int32_t) AO_BOTH_K20_100 * ao_error_h +
242 (int32_t) AO_BOTH_K21_100 * ao_error_a;
247 ao_kalman_correct_accel(void)
249 ao_kalman_err_accel();
251 if (ao_sample_tick - ao_sample_prev_tick > 5) {
252 ao_k_height +=(int32_t) AO_ACCEL_K0_10 * ao_error_a;
253 ao_k_speed += (int32_t) AO_ACCEL_K1_10 * ao_error_a;
254 ao_k_accel += (int32_t) AO_ACCEL_K2_10 * ao_error_a;
257 ao_k_height += (int32_t) AO_ACCEL_K0_100 * ao_error_a;
258 ao_k_speed += (int32_t) AO_ACCEL_K1_100 * ao_error_a;
259 ao_k_accel += (int32_t) AO_ACCEL_K2_100 * ao_error_a;
262 #endif /* HAS_ACCEL */
269 if (ao_flight_state <= ao_flight_coast) {
271 ao_kalman_correct_accel();
273 ao_kalman_correct_both();
277 ao_kalman_correct_baro();
278 ao_height = from_fix(ao_k_height);
279 ao_speed = from_fix(ao_k_speed);
280 ao_accel = from_fix(ao_k_accel);
281 if (ao_height > ao_max_height)
282 ao_max_height = ao_height;
283 ao_avg_height_scaled = ao_avg_height_scaled - ao_avg_height + ao_sample_height;
284 #ifdef AO_FLIGHT_TEST
285 if (ao_sample_tick - ao_sample_prev_tick > 50)
286 ao_avg_height = (ao_avg_height_scaled + 1) >> 1;
287 else if (ao_sample_tick - ao_sample_prev_tick > 5)
288 ao_avg_height = (ao_avg_height_scaled + 7) >> 4;
291 ao_avg_height = (ao_avg_height_scaled + 63) >> 7;
292 #ifdef AO_FLIGHT_TEST
293 ao_sample_prev_tick = ao_sample_tick;