87f1bf66f6147e372edad8a94513802aa11c1774
[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 ((int16_t) (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 ((int16_t) (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         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
100         ao_error_h_sq_avg += (e * e) >> 4;
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 = (ao_v_t) (((ao_k_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 ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
140                 ao_k_height += (ao_k_t) AO_BARO_K0_1 * ao_error_h;
141                 ao_k_speed  += (ao_k_t) AO_BARO_K1_1 * ao_error_h;
142                 ao_k_accel  += (ao_k_t) AO_BARO_K2_1 * ao_error_h;
143                 return;
144         }
145         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
146                 ao_k_height += (ao_k_t) AO_BARO_K0_10 * ao_error_h;
147                 ao_k_speed  += (ao_k_t) AO_BARO_K1_10 * ao_error_h;
148                 ao_k_accel  += (ao_k_t) AO_BARO_K2_10 * ao_error_h;
149                 return;
150         }
151 #endif
152         ao_k_height += (ao_k_t) AO_BARO_K0_100 * ao_error_h;
153         ao_k_speed  += (ao_k_t) AO_BARO_K1_100 * ao_error_h;
154         ao_k_accel  += (ao_k_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         ao_k_t  accel;
163
164         accel = (ao_config.accel_plus_g - 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 #ifndef FORCE_ACCEL
171 static void
172 ao_kalman_correct_both(void)
173 {
174         ao_kalman_err_height();
175         ao_kalman_err_accel();
176
177 #ifdef AO_FLIGHT_TEST
178         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
179                 if (ao_flight_debug) {
180                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
181                                 ao_k_speed / (65536.0 * 16.0),
182                                 (double) ao_error_h, AO_BOTH_K10_1 / 65536.0,
183                                 (double) ao_error_a, AO_BOTH_K11_1 / 65536.0,
184                                 (ao_k_speed +
185                                  (ao_k_t) AO_BOTH_K10_1 * ao_error_h +
186                                  (ao_k_t) AO_BOTH_K11_1 * ao_error_a) / (65536.0 * 16.0));
187                 }
188                 ao_k_height +=
189                         (ao_k_t) AO_BOTH_K00_1 * ao_error_h +
190                         (ao_k_t) AO_BOTH_K01_1 * ao_error_a;
191                 ao_k_speed +=
192                         (ao_k_t) AO_BOTH_K10_1 * ao_error_h +
193                         (ao_k_t) AO_BOTH_K11_1 * ao_error_a;
194                 ao_k_accel +=
195                         (ao_k_t) AO_BOTH_K20_1 * ao_error_h +
196                         (ao_k_t) AO_BOTH_K21_1 * ao_error_a;
197                 return;
198         }
199         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
200                 if (ao_flight_debug) {
201                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
202                                 ao_k_speed / (65536.0 * 16.0),
203                                 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
204                                 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
205                                 (ao_k_speed +
206                                  (ao_k_t) AO_BOTH_K10_10 * ao_error_h +
207                                  (ao_k_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
208                 }
209                 ao_k_height +=
210                         (ao_k_t) AO_BOTH_K00_10 * ao_error_h +
211                         (ao_k_t) AO_BOTH_K01_10 * ao_error_a;
212                 ao_k_speed +=
213                         (ao_k_t) AO_BOTH_K10_10 * ao_error_h +
214                         (ao_k_t) AO_BOTH_K11_10 * ao_error_a;
215                 ao_k_accel +=
216                         (ao_k_t) AO_BOTH_K20_10 * ao_error_h +
217                         (ao_k_t) AO_BOTH_K21_10 * ao_error_a;
218                 return;
219         }
220         if (ao_flight_debug) {
221                 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
222                         ao_k_speed / (65536.0 * 16.0),
223                         (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
224                         (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
225                         (ao_k_speed +
226                          (ao_k_t) AO_BOTH_K10_100 * ao_error_h +
227                          (ao_k_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
228         }
229 #endif
230         ao_k_height +=
231                 (ao_k_t) AO_BOTH_K00_100 * ao_error_h +
232                 (ao_k_t) AO_BOTH_K01_100 * ao_error_a;
233         ao_k_speed +=
234                 (ao_k_t) AO_BOTH_K10_100 * ao_error_h +
235                 (ao_k_t) AO_BOTH_K11_100 * ao_error_a;
236         ao_k_accel +=
237                 (ao_k_t) AO_BOTH_K20_100 * ao_error_h +
238                 (ao_k_t) AO_BOTH_K21_100 * ao_error_a;
239 }
240
241 #else
242
243 static void
244 ao_kalman_correct_accel(void)
245 {
246         ao_kalman_err_accel();
247
248         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
249                 ao_k_height +=(ao_k_t) AO_ACCEL_K0_10 * ao_error_a;
250                 ao_k_speed  += (ao_k_t) AO_ACCEL_K1_10 * ao_error_a;
251                 ao_k_accel  += (ao_k_t) AO_ACCEL_K2_10 * ao_error_a;
252                 return;
253         }
254         ao_k_height += (ao_k_t) AO_ACCEL_K0_100 * ao_error_a;
255         ao_k_speed  += (ao_k_t) AO_ACCEL_K1_100 * ao_error_a;
256         ao_k_accel  += (ao_k_t) AO_ACCEL_K2_100 * ao_error_a;
257 }
258
259 #endif /* else FORCE_ACCEL */
260 #endif /* HAS_ACCEL */
261
262 void
263 ao_kalman(void)
264 {
265         ao_kalman_predict();
266 #if HAS_ACCEL
267         if (ao_flight_state <= ao_flight_coast) {
268 #ifdef FORCE_ACCEL
269                 ao_kalman_correct_accel();
270 #else
271                 ao_kalman_correct_both();
272 #endif
273         } else
274 #endif
275                 ao_kalman_correct_baro();
276         ao_height = from_fix(ao_k_height);
277         ao_speed = from_fix(ao_k_speed);
278         ao_accel = from_fix(ao_k_accel);
279         if (ao_height > ao_max_height)
280                 ao_max_height = ao_height;
281         ao_avg_height_scaled = ao_avg_height_scaled - ao_avg_height + ao_sample_height;
282 #ifdef AO_FLIGHT_TEST
283         if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50)
284                 ao_avg_height = (ao_avg_height_scaled + 1) >> 1;
285         else if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5)
286                 ao_avg_height = (ao_avg_height_scaled + 7) >> 4;
287         else 
288 #endif
289                 ao_avg_height = (ao_avg_height_scaled + 63) >> 7;
290 }