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