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
22 #include "ao_kalman.h"
24 static __pdata int32_t ao_k_height;
25 static __pdata int32_t ao_k_speed;
26 static __pdata int32_t ao_k_accel;
28 #define AO_K_STEP_100 to_fix16(0.01)
29 #define AO_K_STEP_2_2_100 to_fix16(0.00005)
31 #define AO_K_STEP_10 to_fix16(0.1)
32 #define AO_K_STEP_2_2_10 to_fix16(0.005)
34 __pdata int16_t ao_height;
35 __pdata int16_t ao_speed;
36 __pdata int16_t ao_accel;
37 __pdata int16_t ao_max_height;
39 __pdata int16_t ao_error_h;
40 __pdata int16_t ao_error_h_sq_avg;
43 __pdata int16_t ao_error_a;
47 ao_kalman_predict(void)
50 if (ao_sample_tick - ao_sample_prev_tick > 5) {
51 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_10 +
52 (int32_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
53 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_10;
57 if (ao_flight_debug) {
58 printf ("predict speed %g + (%g * %g) = %g\n",
59 ao_k_speed / (65536.0 * 16.0), ao_accel / 16.0, AO_K_STEP_100 / 65536.0,
60 (ao_k_speed + (int32_t) ao_accel * AO_K_STEP_100) / (65536.0 * 16.0));
63 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_100 +
64 (int32_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
65 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_100;
69 ao_kalman_err_height(void)
72 int16_t height_distrust;
74 int16_t speed_distrust;
77 ao_error_h = ao_sample_height - (int16_t) (ao_k_height >> 16);
85 ao_error_h_sq_avg -= ao_error_h_sq_avg >> 2;
86 ao_error_h_sq_avg += (e * e) >> 2;
88 ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
89 ao_error_h_sq_avg += (e * e) >> 4;
92 if (ao_flight_state >= ao_flight_drogue)
94 height_distrust = ao_sample_alt - AO_MAX_BARO_HEIGHT;
96 /* speed is stored * 16, but we need to ramp between 200 and 328, so
97 * we want to multiply by 2. The result is a shift by 3.
99 speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
100 if (speed_distrust <= 0)
102 else if (speed_distrust > height_distrust)
103 height_distrust = speed_distrust;
105 if (height_distrust > 0) {
106 #ifdef AO_FLIGHT_TEST
107 int old_ao_error_h = ao_error_h;
109 if (height_distrust > 0x100)
110 height_distrust = 0x100;
111 ao_error_h = (int16_t) (((int32_t) ao_error_h * (0x100 - height_distrust)) >> 8);
112 #ifdef AO_FLIGHT_TEST
113 if (ao_flight_debug) {
114 printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
115 (double) (ao_sample_alt - AO_MAX_BARO_HEIGHT),
116 (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
117 height_distrust / 256.0,
118 old_ao_error_h, ao_error_h);
125 ao_kalman_correct_baro(void)
127 ao_kalman_err_height();
128 #ifdef AO_FLIGHT_TEST
129 if (ao_sample_tick - ao_sample_prev_tick > 5) {
130 ao_k_height += (int32_t) AO_BARO_K0_10 * ao_error_h;
131 ao_k_speed += (int32_t) AO_BARO_K1_10 * ao_error_h;
132 ao_k_accel += (int32_t) AO_BARO_K2_10 * ao_error_h;
136 ao_k_height += (int32_t) AO_BARO_K0_100 * ao_error_h;
137 ao_k_speed += (int32_t) AO_BARO_K1_100 * ao_error_h;
138 ao_k_accel += (int32_t) AO_BARO_K2_100 * ao_error_h;
144 ao_kalman_err_accel(void)
148 accel = (ao_ground_accel - ao_sample_accel) * ao_accel_scale;
150 /* Can't use ao_accel here as it is the pre-prediction value still */
151 ao_error_a = (accel - ao_k_accel) >> 16;
155 ao_kalman_correct_both(void)
157 ao_kalman_err_height();
158 ao_kalman_err_accel();
160 #ifdef AO_FLIGHT_TEST
161 if (ao_sample_tick - ao_sample_prev_tick > 5) {
162 if (ao_flight_debug) {
163 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
164 ao_k_speed / (65536.0 * 16.0),
165 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
166 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
168 (int32_t) AO_BOTH_K10_10 * ao_error_h +
169 (int32_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
172 (int32_t) AO_BOTH_K00_10 * ao_error_h +
173 (int32_t) AO_BOTH_K01_10 * ao_error_a;
175 (int32_t) AO_BOTH_K10_10 * ao_error_h +
176 (int32_t) AO_BOTH_K11_10 * ao_error_a;
178 (int32_t) AO_BOTH_K20_10 * ao_error_h +
179 (int32_t) AO_BOTH_K21_10 * ao_error_a;
182 if (ao_flight_debug) {
183 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
184 ao_k_speed / (65536.0 * 16.0),
185 (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
186 (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
188 (int32_t) AO_BOTH_K10_100 * ao_error_h +
189 (int32_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
193 (int32_t) AO_BOTH_K00_100 * ao_error_h +
194 (int32_t) AO_BOTH_K01_100 * ao_error_a;
196 (int32_t) AO_BOTH_K10_100 * ao_error_h +
197 (int32_t) AO_BOTH_K11_100 * ao_error_a;
199 (int32_t) AO_BOTH_K20_100 * ao_error_h +
200 (int32_t) AO_BOTH_K21_100 * ao_error_a;
205 ao_kalman_correct_accel(void)
207 ao_kalman_err_accel();
209 if (ao_sample_tick - ao_sample_prev_tick > 5) {
210 ao_k_height +=(int32_t) AO_ACCEL_K0_10 * ao_error_a;
211 ao_k_speed += (int32_t) AO_ACCEL_K1_10 * ao_error_a;
212 ao_k_accel += (int32_t) AO_ACCEL_K2_10 * ao_error_a;
215 ao_k_height += (int32_t) AO_ACCEL_K0_100 * ao_error_a;
216 ao_k_speed += (int32_t) AO_ACCEL_K1_100 * ao_error_a;
217 ao_k_accel += (int32_t) AO_ACCEL_K2_100 * ao_error_a;
220 #endif /* HAS_ACCEL */
227 if (ao_flight_state <= ao_flight_coast) {
229 ao_kalman_correct_accel();
231 ao_kalman_correct_both();
235 ao_kalman_correct_baro();
236 ao_height = from_fix(ao_k_height);
237 ao_speed = from_fix(ao_k_speed);
238 ao_accel = from_fix(ao_k_accel);
239 if (ao_height > ao_max_height)
240 ao_max_height = ao_height;
241 #ifdef AO_FLIGHT_TEST
242 ao_sample_prev_tick = ao_sample_tick;