altos: Add ability to read new TELEM files to ao_flight_test
[fw/altos] / src / ao_kalman.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #ifndef AO_FLIGHT_TEST
19 #include "ao.h"
20 #endif
21
22 #include "ao_kalman.h"
23
24 static __pdata int32_t          ao_k_height;
25 static __pdata int32_t          ao_k_speed;
26 static __pdata int32_t          ao_k_accel;
27
28 #define AO_K_STEP_100           to_fix16(0.01)
29 #define AO_K_STEP_2_2_100       to_fix16(0.00005)
30
31 #define AO_K_STEP_10            to_fix16(0.1)
32 #define AO_K_STEP_2_2_10        to_fix16(0.005)
33
34 #define AO_K_STEP_1             to_fix16(1)
35 #define AO_K_STEP_2_2_1         to_fix16(0.5)
36
37 __pdata int16_t                 ao_height;
38 __pdata int16_t                 ao_speed;
39 __pdata int16_t                 ao_accel;
40 __pdata int16_t                 ao_max_height;
41
42 __pdata int16_t                 ao_error_h;
43 __pdata int16_t                 ao_error_h_sq_avg;
44
45 #if HAS_ACCEL
46 __pdata int16_t                 ao_error_a;
47 #endif
48
49 static void
50 ao_kalman_predict(void)
51 {
52 #ifdef AO_FLIGHT_TEST
53         if (ao_sample_tick - ao_sample_prev_tick > 50) {
54                 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_1 +
55                                 (int32_t) ao_accel * AO_K_STEP_2_2_1) >> 4;
56                 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_1;
57
58                 return;
59         }
60         if (ao_sample_tick - ao_sample_prev_tick > 5) {
61                 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_10 +
62                                 (int32_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
63                 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_10;
64
65                 return;
66         }
67         if (ao_flight_debug) {
68                 printf ("predict speed %g + (%g * %g) = %g\n",
69                         ao_k_speed / (65536.0 * 16.0), ao_accel / 16.0, AO_K_STEP_100 / 65536.0,
70                         (ao_k_speed + (int32_t) ao_accel * AO_K_STEP_100) / (65536.0 * 16.0));
71         }
72 #endif
73         ao_k_height += ((int32_t) ao_speed * AO_K_STEP_100 +
74                         (int32_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
75         ao_k_speed += (int32_t) ao_accel * AO_K_STEP_100;
76 }
77
78 static void
79 ao_kalman_err_height(void)
80 {
81         int16_t e;
82         int16_t height_distrust;
83 #if HAS_ACCEL
84         int16_t speed_distrust;
85 #endif
86
87         ao_error_h = ao_sample_height - (int16_t) (ao_k_height >> 16);
88
89         e = ao_error_h;
90         if (e < 0)
91                 e = -e;
92         if (e > 127)
93                 e = 127;
94 #if HAS_ACCEL
95         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 2;
96         ao_error_h_sq_avg += (e * e) >> 2;
97 #else
98         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
99         ao_error_h_sq_avg += (e * e) >> 4;
100 #endif
101
102         if (ao_flight_state >= ao_flight_drogue)
103                 return;
104         height_distrust = ao_sample_alt - AO_MAX_BARO_HEIGHT;
105 #if HAS_ACCEL
106         /* speed is stored * 16, but we need to ramp between 200 and 328, so
107          * we want to multiply by 2. The result is a shift by 3.
108          */
109         speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
110         if (speed_distrust <= 0)
111                 speed_distrust = 0;
112         else if (speed_distrust > height_distrust)
113                 height_distrust = speed_distrust;
114 #endif
115         if (height_distrust > 0) {
116 #ifdef AO_FLIGHT_TEST
117                 int     old_ao_error_h = ao_error_h;
118 #endif
119                 if (height_distrust > 0x100)
120                         height_distrust = 0x100;
121                 ao_error_h = (int16_t) (((int32_t) ao_error_h * (0x100 - height_distrust)) >> 8);
122 #ifdef AO_FLIGHT_TEST
123                 if (ao_flight_debug) {
124                         printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
125                                (double) (ao_sample_alt - AO_MAX_BARO_HEIGHT),
126                                (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
127                                height_distrust / 256.0,
128                                old_ao_error_h, ao_error_h);
129                 }
130 #endif
131         }
132 }
133
134 static void
135 ao_kalman_correct_baro(void)
136 {
137         ao_kalman_err_height();
138 #ifdef AO_FLIGHT_TEST
139         if (ao_sample_tick - ao_sample_prev_tick > 50) {
140                 ao_k_height += (int32_t) AO_BARO_K0_1 * ao_error_h;
141                 ao_k_speed  += (int32_t) AO_BARO_K1_1 * ao_error_h;
142                 ao_k_accel  += (int32_t) AO_BARO_K2_1 * ao_error_h;
143                 return;
144         }
145         if (ao_sample_tick - ao_sample_prev_tick > 5) {
146                 ao_k_height += (int32_t) AO_BARO_K0_10 * ao_error_h;
147                 ao_k_speed  += (int32_t) AO_BARO_K1_10 * ao_error_h;
148                 ao_k_accel  += (int32_t) AO_BARO_K2_10 * ao_error_h;
149                 return;
150         }
151 #endif
152         ao_k_height += (int32_t) AO_BARO_K0_100 * ao_error_h;
153         ao_k_speed  += (int32_t) AO_BARO_K1_100 * ao_error_h;
154         ao_k_accel  += (int32_t) AO_BARO_K2_100 * ao_error_h;
155 }
156
157 #if HAS_ACCEL
158
159 static void
160 ao_kalman_err_accel(void)
161 {
162         int32_t accel;
163
164         accel = (ao_ground_accel - ao_sample_accel) * ao_accel_scale;
165
166         /* Can't use ao_accel here as it is the pre-prediction value still */
167         ao_error_a = (accel - ao_k_accel) >> 16;
168 }
169
170 static void
171 ao_kalman_correct_both(void)
172 {
173         ao_kalman_err_height();
174         ao_kalman_err_accel();
175
176 #ifdef AO_FLIGHT_TEST
177         if (ao_sample_tick - ao_sample_prev_tick > 50) {
178                 if (ao_flight_debug) {
179                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
180                                 ao_k_speed / (65536.0 * 16.0),
181                                 (double) ao_error_h, AO_BOTH_K10_1 / 65536.0,
182                                 (double) ao_error_a, AO_BOTH_K11_1 / 65536.0,
183                                 (ao_k_speed +
184                                  (int32_t) AO_BOTH_K10_1 * ao_error_h +
185                                  (int32_t) AO_BOTH_K11_1 * ao_error_a) / (65536.0 * 16.0));
186                 }
187                 ao_k_height +=
188                         (int32_t) AO_BOTH_K00_1 * ao_error_h +
189                         (int32_t) AO_BOTH_K01_1 * ao_error_a;
190                 ao_k_speed +=
191                         (int32_t) AO_BOTH_K10_1 * ao_error_h +
192                         (int32_t) AO_BOTH_K11_1 * ao_error_a;
193                 ao_k_accel +=
194                         (int32_t) AO_BOTH_K20_1 * ao_error_h +
195                         (int32_t) AO_BOTH_K21_1 * ao_error_a;
196                 return;
197         }
198         if (ao_sample_tick - ao_sample_prev_tick > 5) {
199                 if (ao_flight_debug) {
200                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
201                                 ao_k_speed / (65536.0 * 16.0),
202                                 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
203                                 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
204                                 (ao_k_speed +
205                                  (int32_t) AO_BOTH_K10_10 * ao_error_h +
206                                  (int32_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
207                 }
208                 ao_k_height +=
209                         (int32_t) AO_BOTH_K00_10 * ao_error_h +
210                         (int32_t) AO_BOTH_K01_10 * ao_error_a;
211                 ao_k_speed +=
212                         (int32_t) AO_BOTH_K10_10 * ao_error_h +
213                         (int32_t) AO_BOTH_K11_10 * ao_error_a;
214                 ao_k_accel +=
215                         (int32_t) AO_BOTH_K20_10 * ao_error_h +
216                         (int32_t) AO_BOTH_K21_10 * ao_error_a;
217                 return;
218         }
219         if (ao_flight_debug) {
220                 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
221                         ao_k_speed / (65536.0 * 16.0),
222                         (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
223                         (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
224                         (ao_k_speed +
225                          (int32_t) AO_BOTH_K10_100 * ao_error_h +
226                          (int32_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
227         }
228 #endif
229         ao_k_height +=
230                 (int32_t) AO_BOTH_K00_100 * ao_error_h +
231                 (int32_t) AO_BOTH_K01_100 * ao_error_a;
232         ao_k_speed +=
233                 (int32_t) AO_BOTH_K10_100 * ao_error_h +
234                 (int32_t) AO_BOTH_K11_100 * ao_error_a;
235         ao_k_accel +=
236                 (int32_t) AO_BOTH_K20_100 * ao_error_h +
237                 (int32_t) AO_BOTH_K21_100 * ao_error_a;
238 }
239
240 #ifdef FORCE_ACCEL
241 static void
242 ao_kalman_correct_accel(void)
243 {
244         ao_kalman_err_accel();
245
246         if (ao_sample_tick - ao_sample_prev_tick > 5) {
247                 ao_k_height +=(int32_t) AO_ACCEL_K0_10 * ao_error_a;
248                 ao_k_speed  += (int32_t) AO_ACCEL_K1_10 * ao_error_a;
249                 ao_k_accel  += (int32_t) AO_ACCEL_K2_10 * ao_error_a;
250                 return;
251         }
252         ao_k_height += (int32_t) AO_ACCEL_K0_100 * ao_error_a;
253         ao_k_speed  += (int32_t) AO_ACCEL_K1_100 * ao_error_a;
254         ao_k_accel  += (int32_t) AO_ACCEL_K2_100 * ao_error_a;
255 }
256 #endif
257 #endif /* HAS_ACCEL */
258
259 void
260 ao_kalman(void)
261 {
262         ao_kalman_predict();
263 #if HAS_ACCEL
264         if (ao_flight_state <= ao_flight_coast) {
265 #ifdef FORCE_ACCEL
266                 ao_kalman_correct_accel();
267 #else
268                 ao_kalman_correct_both();
269 #endif
270         } else
271 #endif
272                 ao_kalman_correct_baro();
273         ao_height = from_fix(ao_k_height);
274         ao_speed = from_fix(ao_k_speed);
275         ao_accel = from_fix(ao_k_accel);
276         if (ao_height > ao_max_height)
277                 ao_max_height = ao_height;
278 #ifdef AO_FLIGHT_TEST
279         ao_sample_prev_tick = ao_sample_tick;
280 #endif
281 }