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"
26 static __pdata ao_k_t ao_k_height;
27 static __pdata ao_k_t ao_k_speed;
28 static __pdata ao_k_t ao_k_accel;
30 #define AO_K_STEP_100 to_fix_v(0.01)
31 #define AO_K_STEP_2_2_100 to_fix_v(0.00005)
33 #define AO_K_STEP_10 to_fix_v(0.1)
34 #define AO_K_STEP_2_2_10 to_fix_v(0.005)
36 #define AO_K_STEP_1 to_fix_v(1)
37 #define AO_K_STEP_2_2_1 to_fix_v(0.5)
39 __pdata ao_v_t ao_height;
40 __pdata ao_v_t ao_speed;
41 __pdata ao_v_t ao_accel;
42 __xdata ao_v_t ao_max_height;
43 static __pdata ao_k_t ao_avg_height_scaled;
44 __xdata ao_v_t ao_avg_height;
46 __pdata ao_v_t ao_error_h;
47 __pdata ao_v_t ao_error_h_sq_avg;
50 __pdata ao_v_t ao_error_a;
54 ao_kalman_predict(void)
57 if (ao_sample_tick - ao_sample_prev_tick > 50) {
58 ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_1 +
59 (ao_k_t) ao_accel * AO_K_STEP_2_2_1) >> 4;
60 ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_1;
64 if (ao_sample_tick - ao_sample_prev_tick > 5) {
65 ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_10 +
66 (ao_k_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
67 ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_10;
71 if (ao_flight_debug) {
72 printf ("predict speed %g + (%g * %g) = %g\n",
73 ao_k_speed / (65536.0 * 16.0), ao_accel / 16.0, AO_K_STEP_100 / 65536.0,
74 (ao_k_speed + (ao_k_t) ao_accel * AO_K_STEP_100) / (65536.0 * 16.0));
77 ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_100 +
78 (ao_k_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
79 ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_100;
83 ao_kalman_err_height(void)
86 ao_v_t height_distrust;
88 ao_v_t speed_distrust;
91 ao_error_h = ao_sample_height - (ao_v_t) (ao_k_height >> 16);
99 ao_error_h_sq_avg -= ao_error_h_sq_avg >> 2;
100 ao_error_h_sq_avg += (e * e) >> 2;
102 ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
103 ao_error_h_sq_avg += (e * e) >> 4;
106 if (ao_flight_state >= ao_flight_drogue)
108 height_distrust = ao_sample_alt - AO_MAX_BARO_HEIGHT;
110 /* speed is stored * 16, but we need to ramp between 200 and 328, so
111 * we want to multiply by 2. The result is a shift by 3.
113 speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
114 if (speed_distrust <= 0)
116 else if (speed_distrust > height_distrust)
117 height_distrust = speed_distrust;
119 if (height_distrust > 0) {
120 #ifdef AO_FLIGHT_TEST
121 int old_ao_error_h = ao_error_h;
123 if (height_distrust > 0x100)
124 height_distrust = 0x100;
125 ao_error_h = (ao_v_t) (((ao_k_t) ao_error_h * (0x100 - height_distrust)) >> 8);
126 #ifdef AO_FLIGHT_TEST
127 if (ao_flight_debug) {
128 printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
129 (double) (ao_sample_alt - AO_MAX_BARO_HEIGHT),
130 (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
131 height_distrust / 256.0,
132 old_ao_error_h, ao_error_h);
139 ao_kalman_correct_baro(void)
141 ao_kalman_err_height();
142 #ifdef AO_FLIGHT_TEST
143 if (ao_sample_tick - ao_sample_prev_tick > 50) {
144 ao_k_height += (ao_k_t) AO_BARO_K0_1 * ao_error_h;
145 ao_k_speed += (ao_k_t) AO_BARO_K1_1 * ao_error_h;
146 ao_k_accel += (ao_k_t) AO_BARO_K2_1 * ao_error_h;
149 if (ao_sample_tick - ao_sample_prev_tick > 5) {
150 ao_k_height += (ao_k_t) AO_BARO_K0_10 * ao_error_h;
151 ao_k_speed += (ao_k_t) AO_BARO_K1_10 * ao_error_h;
152 ao_k_accel += (ao_k_t) AO_BARO_K2_10 * ao_error_h;
156 ao_k_height += (ao_k_t) AO_BARO_K0_100 * ao_error_h;
157 ao_k_speed += (ao_k_t) AO_BARO_K1_100 * ao_error_h;
158 ao_k_accel += (ao_k_t) AO_BARO_K2_100 * ao_error_h;
164 ao_kalman_err_accel(void)
168 accel = (ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale;
170 /* Can't use ao_accel here as it is the pre-prediction value still */
171 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 (ao_k_t) AO_BOTH_K10_1 * ao_error_h +
190 (ao_k_t) AO_BOTH_K11_1 * ao_error_a) / (65536.0 * 16.0));
193 (ao_k_t) AO_BOTH_K00_1 * ao_error_h +
194 (ao_k_t) AO_BOTH_K01_1 * ao_error_a;
196 (ao_k_t) AO_BOTH_K10_1 * ao_error_h +
197 (ao_k_t) AO_BOTH_K11_1 * ao_error_a;
199 (ao_k_t) AO_BOTH_K20_1 * ao_error_h +
200 (ao_k_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 (ao_k_t) AO_BOTH_K10_10 * ao_error_h +
211 (ao_k_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
214 (ao_k_t) AO_BOTH_K00_10 * ao_error_h +
215 (ao_k_t) AO_BOTH_K01_10 * ao_error_a;
217 (ao_k_t) AO_BOTH_K10_10 * ao_error_h +
218 (ao_k_t) AO_BOTH_K11_10 * ao_error_a;
220 (ao_k_t) AO_BOTH_K20_10 * ao_error_h +
221 (ao_k_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 (ao_k_t) AO_BOTH_K10_100 * ao_error_h +
231 (ao_k_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
235 (ao_k_t) AO_BOTH_K00_100 * ao_error_h +
236 (ao_k_t) AO_BOTH_K01_100 * ao_error_a;
238 (ao_k_t) AO_BOTH_K10_100 * ao_error_h +
239 (ao_k_t) AO_BOTH_K11_100 * ao_error_a;
241 (ao_k_t) AO_BOTH_K20_100 * ao_error_h +
242 (ao_k_t) AO_BOTH_K21_100 * ao_error_a;
248 ao_kalman_correct_accel(void)
250 ao_kalman_err_accel();
252 if (ao_sample_tick - ao_sample_prev_tick > 5) {
253 ao_k_height +=(ao_k_t) AO_ACCEL_K0_10 * ao_error_a;
254 ao_k_speed += (ao_k_t) AO_ACCEL_K1_10 * ao_error_a;
255 ao_k_accel += (ao_k_t) AO_ACCEL_K2_10 * ao_error_a;
258 ao_k_height += (ao_k_t) AO_ACCEL_K0_100 * ao_error_a;
259 ao_k_speed += (ao_k_t) AO_ACCEL_K1_100 * ao_error_a;
260 ao_k_accel += (ao_k_t) AO_ACCEL_K2_100 * ao_error_a;
263 #endif /* else FORCE_ACCEL */
264 #endif /* HAS_ACCEL */
271 if (ao_flight_state <= ao_flight_coast) {
273 ao_kalman_correct_accel();
275 ao_kalman_correct_both();
279 ao_kalman_correct_baro();
280 ao_height = from_fix(ao_k_height);
281 ao_speed = from_fix(ao_k_speed);
282 ao_accel = from_fix(ao_k_accel);
283 if (ao_height > ao_max_height)
284 ao_max_height = ao_height;
285 ao_avg_height_scaled = ao_avg_height_scaled - ao_avg_height + ao_sample_height;
286 #ifdef AO_FLIGHT_TEST
287 if (ao_sample_tick - ao_sample_prev_tick > 50)
288 ao_avg_height = (ao_avg_height_scaled + 1) >> 1;
289 else if (ao_sample_tick - ao_sample_prev_tick > 5)
290 ao_avg_height = (ao_avg_height_scaled + 7) >> 4;
293 ao_avg_height = (ao_avg_height_scaled + 63) >> 7;