e4cc6d4b9f73676fbc93b750ececfad91b2ed332
[fw/altos] / src / kernel / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef AO_FLIGHT_TEST
20 #include "ao.h"
21 #include "ao_flight.h"
22 #endif
23
24 #include "ao_sample.h"
25 #include "ao_kalman.h"
26
27 static __pdata ao_k_t           ao_k_height;
28 static __pdata ao_k_t           ao_k_speed;
29 static __pdata ao_k_t           ao_k_accel;
30
31 #define AO_K_STEP_100           to_fix_v(0.01)
32 #define AO_K_STEP_2_2_100       to_fix_v(0.00005)
33
34 #define AO_K_STEP_10            to_fix_v(0.1)
35 #define AO_K_STEP_2_2_10        to_fix_v(0.005)
36
37 #define AO_K_STEP_1             to_fix_v(1)
38 #define AO_K_STEP_2_2_1         to_fix_v(0.5)
39
40 __pdata ao_v_t                  ao_height;
41 __pdata ao_v_t                  ao_speed;
42 __pdata ao_v_t                  ao_accel;
43 __xdata ao_v_t                  ao_max_height;
44 static __pdata ao_k_t           ao_avg_height_scaled;
45 __xdata ao_v_t                  ao_avg_height;
46
47 __pdata ao_v_t                  ao_error_h;
48 #if !HAS_ACCEL || AO_FLIGHT_TEST
49 #define AO_ERROR_H_SQ_AVG       1
50 #endif
51
52 #if AO_ERROR_H_SQ_AVG
53 __pdata ao_v_t                  ao_error_h_sq_avg;
54 #endif
55
56 #if HAS_ACCEL
57 __pdata ao_v_t                  ao_error_a;
58 #endif
59
60 static void
61 ao_kalman_predict(void)
62 {
63 #ifdef AO_FLIGHT_TEST
64         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
65                 ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_1 +
66                                 (ao_k_t) ao_accel * AO_K_STEP_2_2_1) >> 4;
67                 ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_1;
68
69                 return;
70         }
71         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
72                 ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_10 +
73                                 (ao_k_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
74                 ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_10;
75
76                 return;
77         }
78         if (ao_flight_debug) {
79                 printf ("predict speed %g + (%g * %g) = %g\n",
80                         ao_k_speed / (65536.0 * 16.0), ao_accel / 16.0, AO_K_STEP_100 / 65536.0,
81                         (ao_k_speed + (ao_k_t) ao_accel * AO_K_STEP_100) / (65536.0 * 16.0));
82         }
83 #endif
84         ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_100 +
85                         (ao_k_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
86         ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_100;
87 }
88
89 static void
90 ao_kalman_err_height(void)
91 {
92 #if AO_ERROR_H_SQ_AVG
93         ao_v_t  e;
94 #endif
95         ao_v_t height_distrust;
96 #if HAS_ACCEL
97         ao_v_t  speed_distrust;
98 #endif
99
100         ao_error_h = ao_sample_height - (ao_v_t) (ao_k_height >> 16);
101
102 #if AO_ERROR_H_SQ_AVG
103         e = ao_error_h;
104         if (e < 0)
105                 e = -e;
106         if (e > 127)
107                 e = 127;
108         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
109         ao_error_h_sq_avg += (e * e) >> 4;
110 #endif
111
112         if (ao_flight_state >= ao_flight_drogue)
113                 return;
114         height_distrust = ao_sample_alt - AO_MAX_BARO_HEIGHT;
115 #if HAS_ACCEL
116         /* speed is stored * 16, but we need to ramp between 248 and 328, so
117          * we want to multiply by 2. The result is a shift by 3.
118          */
119         speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
120         if (speed_distrust > AO_MAX_SPEED_DISTRUST)
121                 speed_distrust = AO_MAX_SPEED_DISTRUST;
122         if (speed_distrust > height_distrust)
123                 height_distrust = speed_distrust;
124 #endif
125         if (height_distrust > 0) {
126 #ifdef AO_FLIGHT_TEST
127                 int     old_ao_error_h = ao_error_h;
128 #endif
129                 if (height_distrust > 0x100)
130                         height_distrust = 0x100;
131                 ao_error_h = (ao_v_t) (((ao_k_t) ao_error_h * (0x100 - height_distrust)) >> 8);
132 #ifdef AO_FLIGHT_TEST
133                 if (ao_flight_debug) {
134                         printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
135                                (double) (ao_sample_alt - AO_MAX_BARO_HEIGHT),
136                                (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
137                                height_distrust / 256.0,
138                                old_ao_error_h, ao_error_h);
139                 }
140 #endif
141         }
142 }
143
144 static void
145 ao_kalman_correct_baro(void)
146 {
147         ao_kalman_err_height();
148 #ifdef AO_FLIGHT_TEST
149         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
150                 ao_k_height += (ao_k_t) AO_BARO_K0_1 * ao_error_h;
151                 ao_k_speed  += (ao_k_t) AO_BARO_K1_1 * ao_error_h;
152                 ao_k_accel  += (ao_k_t) AO_BARO_K2_1 * ao_error_h;
153                 return;
154         }
155         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
156                 ao_k_height += (ao_k_t) AO_BARO_K0_10 * ao_error_h;
157                 ao_k_speed  += (ao_k_t) AO_BARO_K1_10 * ao_error_h;
158                 ao_k_accel  += (ao_k_t) AO_BARO_K2_10 * ao_error_h;
159                 return;
160         }
161 #endif
162         ao_k_height += (ao_k_t) AO_BARO_K0_100 * ao_error_h;
163         ao_k_speed  += (ao_k_t) AO_BARO_K1_100 * ao_error_h;
164         ao_k_accel  += (ao_k_t) AO_BARO_K2_100 * ao_error_h;
165 }
166
167 #if HAS_ACCEL
168
169 static void
170 ao_kalman_err_accel(void)
171 {
172         ao_k_t  accel;
173
174         accel = (ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale;
175
176         /* Can't use ao_accel here as it is the pre-prediction value still */
177         ao_error_a = (accel - ao_k_accel) >> 16;
178 }
179
180 #ifndef FORCE_ACCEL
181 static void
182 ao_kalman_correct_both(void)
183 {
184         ao_kalman_err_height();
185         ao_kalman_err_accel();
186
187 #ifdef AO_FLIGHT_TEST
188         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
189                 if (ao_flight_debug) {
190                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
191                                 ao_k_speed / (65536.0 * 16.0),
192                                 (double) ao_error_h, AO_BOTH_K10_1 / 65536.0,
193                                 (double) ao_error_a, AO_BOTH_K11_1 / 65536.0,
194                                 (ao_k_speed +
195                                  (ao_k_t) AO_BOTH_K10_1 * ao_error_h +
196                                  (ao_k_t) AO_BOTH_K11_1 * ao_error_a) / (65536.0 * 16.0));
197                 }
198                 ao_k_height +=
199                         (ao_k_t) AO_BOTH_K00_1 * ao_error_h +
200                         (ao_k_t) AO_BOTH_K01_1 * ao_error_a;
201                 ao_k_speed +=
202                         (ao_k_t) AO_BOTH_K10_1 * ao_error_h +
203                         (ao_k_t) AO_BOTH_K11_1 * ao_error_a;
204                 ao_k_accel +=
205                         (ao_k_t) AO_BOTH_K20_1 * ao_error_h +
206                         (ao_k_t) AO_BOTH_K21_1 * ao_error_a;
207                 return;
208         }
209         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
210                 if (ao_flight_debug) {
211                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
212                                 ao_k_speed / (65536.0 * 16.0),
213                                 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
214                                 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
215                                 (ao_k_speed +
216                                  (ao_k_t) AO_BOTH_K10_10 * ao_error_h +
217                                  (ao_k_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
218                 }
219                 ao_k_height +=
220                         (ao_k_t) AO_BOTH_K00_10 * ao_error_h +
221                         (ao_k_t) AO_BOTH_K01_10 * ao_error_a;
222                 ao_k_speed +=
223                         (ao_k_t) AO_BOTH_K10_10 * ao_error_h +
224                         (ao_k_t) AO_BOTH_K11_10 * ao_error_a;
225                 ao_k_accel +=
226                         (ao_k_t) AO_BOTH_K20_10 * ao_error_h +
227                         (ao_k_t) AO_BOTH_K21_10 * ao_error_a;
228                 return;
229         }
230         if (ao_flight_debug) {
231                 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
232                         ao_k_speed / (65536.0 * 16.0),
233                         (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
234                         (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
235                         (ao_k_speed +
236                          (ao_k_t) AO_BOTH_K10_100 * ao_error_h +
237                          (ao_k_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
238         }
239 #endif
240         ao_k_height +=
241                 (ao_k_t) AO_BOTH_K00_100 * ao_error_h +
242                 (ao_k_t) AO_BOTH_K01_100 * ao_error_a;
243         ao_k_speed +=
244                 (ao_k_t) AO_BOTH_K10_100 * ao_error_h +
245                 (ao_k_t) AO_BOTH_K11_100 * ao_error_a;
246         ao_k_accel +=
247                 (ao_k_t) AO_BOTH_K20_100 * ao_error_h +
248                 (ao_k_t) AO_BOTH_K21_100 * ao_error_a;
249 }
250
251 #else
252
253 static void
254 ao_kalman_correct_accel(void)
255 {
256         ao_kalman_err_accel();
257
258         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
259                 ao_k_height +=(ao_k_t) AO_ACCEL_K0_10 * ao_error_a;
260                 ao_k_speed  += (ao_k_t) AO_ACCEL_K1_10 * ao_error_a;
261                 ao_k_accel  += (ao_k_t) AO_ACCEL_K2_10 * ao_error_a;
262                 return;
263         }
264         ao_k_height += (ao_k_t) AO_ACCEL_K0_100 * ao_error_a;
265         ao_k_speed  += (ao_k_t) AO_ACCEL_K1_100 * ao_error_a;
266         ao_k_accel  += (ao_k_t) AO_ACCEL_K2_100 * ao_error_a;
267 }
268
269 #endif /* else FORCE_ACCEL */
270 #endif /* HAS_ACCEL */
271
272 void
273 ao_kalman(void)
274 {
275         ao_kalman_predict();
276 #if HAS_ACCEL
277         if (ao_flight_state <= ao_flight_coast) {
278 #ifdef FORCE_ACCEL
279                 ao_kalman_correct_accel();
280 #else
281                 ao_kalman_correct_both();
282 #endif
283         } else
284 #endif
285                 ao_kalman_correct_baro();
286         ao_height = from_fix(ao_k_height);
287         ao_speed = from_fix(ao_k_speed);
288         ao_accel = from_fix(ao_k_accel);
289         if (ao_height > ao_max_height)
290                 ao_max_height = ao_height;
291         ao_avg_height_scaled = ao_avg_height_scaled - ao_avg_height + ao_sample_height;
292 #ifdef AO_FLIGHT_TEST
293         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50)
294                 ao_avg_height = (ao_avg_height_scaled + 1) >> 1;
295         else if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5)
296                 ao_avg_height = (ao_avg_height_scaled + 7) >> 4;
297         else 
298 #endif
299                 ao_avg_height = (ao_avg_height_scaled + 63) >> 7;
300 }