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