altos: Split up flight code into separate flight/sample/kalman bits
[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 __pdata int16_t                 ao_height;
35 __pdata int16_t                 ao_speed;
36 __pdata int16_t                 ao_accel;
37 __pdata int16_t                 ao_max_height;
38
39 __pdata int16_t                 ao_error_h;
40 __pdata int16_t                 ao_error_h_sq_avg;
41
42 #if HAS_ACCEL
43 __pdata int16_t                 ao_error_a;
44 #endif
45
46 static void
47 ao_kalman_predict(void)
48 {
49 #ifdef AO_FLIGHT_TEST
50         if (ao_sample_tick - ao_sample_prev_tick > 5) {
51                 ao_k_height += ((int32_t) ao_speed * AO_K_STEP_10 +
52                                 (int32_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
53                 ao_k_speed += (int32_t) ao_accel * AO_K_STEP_10;
54
55                 return;
56         }
57         if (ao_flight_debug) {
58                 printf ("predict speed %g + (%g * %g) = %g\n",
59                         ao_k_speed / (65536.0 * 16.0), ao_accel / 16.0, AO_K_STEP_100 / 65536.0,
60                         (ao_k_speed + (int32_t) ao_accel * AO_K_STEP_100) / (65536.0 * 16.0));
61         }
62 #endif
63         ao_k_height += ((int32_t) ao_speed * AO_K_STEP_100 +
64                         (int32_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
65         ao_k_speed += (int32_t) ao_accel * AO_K_STEP_100;
66 }
67
68 static void
69 ao_kalman_err_height(void)
70 {
71         int16_t e;
72         int16_t height_distrust;
73 #if HAS_ACCEL
74         int16_t speed_distrust;
75 #endif
76
77         ao_error_h = ao_sample_height - (int16_t) (ao_k_height >> 16);
78
79         e = ao_error_h;
80         if (e < 0)
81                 e = -e;
82         if (e > 127)
83                 e = 127;
84 #if HAS_ACCEL
85         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 2;
86         ao_error_h_sq_avg += (e * e) >> 2;
87 #else
88         ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
89         ao_error_h_sq_avg += (e * e) >> 4;
90 #endif
91
92         height_distrust = ao_sample_height - AO_MAX_BARO_HEIGHT;
93 #if HAS_ACCEL
94         /* speed is stored * 16, but we need to ramp between 200 and 328, so
95          * we want to multiply by 2. The result is a shift by 3.
96          */
97         speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
98         if (speed_distrust <= 0)
99                 speed_distrust = 0;
100         else if (speed_distrust > height_distrust)
101                 height_distrust = speed_distrust;
102 #endif
103         if (height_distrust <= 0)
104                 height_distrust = 0;
105
106         if (height_distrust) {
107 #ifdef AO_FLIGHT_TEST
108                 int     old_ao_error_h = ao_error_h;
109 #endif
110                 if (height_distrust > 0x100)
111                         height_distrust = 0x100;
112                 ao_error_h = (int16_t) (((int32_t) ao_error_h * (0x100 - height_distrust)) >> 8);
113 #ifdef AO_FLIGHT_TEST
114                 if (ao_flight_debug) {
115                         printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
116                                (double) (ao_sample_height - AO_MAX_BARO_HEIGHT),
117                                (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
118                                height_distrust / 256.0,
119                                old_ao_error_h, ao_error_h);
120                 }
121 #endif
122         }
123 }
124
125 static void
126 ao_kalman_correct_baro(void)
127 {
128         ao_kalman_err_height();
129 #ifdef AO_FLIGHT_TEST
130         if (ao_sample_tick - ao_sample_prev_tick > 5) {
131                 ao_k_height += (int32_t) AO_BARO_K0_10 * ao_error_h;
132                 ao_k_speed  += (int32_t) AO_BARO_K1_10 * ao_error_h;
133                 ao_k_accel  += (int32_t) AO_BARO_K2_10 * ao_error_h;
134                 return;
135         }
136 #endif
137         ao_k_height += (int32_t) AO_BARO_K0_100 * ao_error_h;
138         ao_k_speed  += (int32_t) AO_BARO_K1_100 * ao_error_h;
139         ao_k_accel  += (int32_t) AO_BARO_K2_100 * ao_error_h;
140 }
141
142 #if HAS_ACCEL
143
144 static void
145 ao_kalman_err_accel(void)
146 {
147         int32_t accel;
148
149         accel = (ao_ground_accel - ao_sample_accel) * ao_accel_scale;
150
151         /* Can't use ao_accel here as it is the pre-prediction value still */
152         ao_error_a = (accel - ao_k_accel) >> 16;
153 }
154
155 static void
156 ao_kalman_correct_both(void)
157 {
158         ao_kalman_err_height();
159         ao_kalman_err_accel();
160
161 #ifdef AO_FLIGHT_TEST
162         if (ao_sample_tick - ao_sample_prev_tick > 5) {
163                 if (ao_flight_debug) {
164                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
165                                 ao_k_speed / (65536.0 * 16.0),
166                                 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
167                                 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
168                                 (ao_k_speed +
169                                  (int32_t) AO_BOTH_K10_10 * ao_error_h +
170                                  (int32_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
171                 }
172                 ao_k_height +=
173                         (int32_t) AO_BOTH_K00_10 * ao_error_h +
174                         (int32_t) AO_BOTH_K01_10 * ao_error_a;
175                 ao_k_speed +=
176                         (int32_t) AO_BOTH_K10_10 * ao_error_h +
177                         (int32_t) AO_BOTH_K11_10 * ao_error_a;
178                 ao_k_accel +=
179                         (int32_t) AO_BOTH_K20_10 * ao_error_h +
180                         (int32_t) AO_BOTH_K21_10 * ao_error_a;
181                 return;
182         }
183         if (ao_flight_debug) {
184                 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
185                         ao_k_speed / (65536.0 * 16.0),
186                         (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
187                         (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
188                         (ao_k_speed +
189                          (int32_t) AO_BOTH_K10_100 * ao_error_h +
190                          (int32_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
191         }
192 #endif
193         ao_k_height +=
194                 (int32_t) AO_BOTH_K00_100 * ao_error_h +
195                 (int32_t) AO_BOTH_K01_100 * ao_error_a;
196         ao_k_speed +=
197                 (int32_t) AO_BOTH_K10_100 * ao_error_h +
198                 (int32_t) AO_BOTH_K11_100 * ao_error_a;
199         ao_k_accel +=
200                 (int32_t) AO_BOTH_K20_100 * ao_error_h +
201                 (int32_t) AO_BOTH_K21_100 * ao_error_a;
202 }
203
204 #ifdef FORCE_ACCEL
205 static void
206 ao_kalman_correct_accel(void)
207 {
208         ao_kalman_err_accel();
209
210         if (ao_sample_tick - ao_sample_prev_tick > 5) {
211                 ao_k_height +=(int32_t) AO_ACCEL_K0_10 * ao_error_a;
212                 ao_k_speed  += (int32_t) AO_ACCEL_K1_10 * ao_error_a;
213                 ao_k_accel  += (int32_t) AO_ACCEL_K2_10 * ao_error_a;
214                 return;
215         }
216         ao_k_height += (int32_t) AO_ACCEL_K0_100 * ao_error_a;
217         ao_k_speed  += (int32_t) AO_ACCEL_K1_100 * ao_error_a;
218         ao_k_accel  += (int32_t) AO_ACCEL_K2_100 * ao_error_a;
219 }
220 #endif
221 #endif /* HAS_ACCEL */
222
223 void
224 ao_kalman(void)
225 {
226         ao_kalman_predict();
227 #if HAS_ACCEL
228         if (ao_flight_state <= ao_flight_coast) {
229 #ifdef FORCE_ACCEL
230                 ao_kalman_correct_accel();
231 #else
232                 ao_kalman_correct_both();
233 #endif
234         } else
235 #endif
236                 ao_kalman_correct_baro();
237         ao_height = from_fix(ao_k_height);
238         ao_speed = from_fix(ao_k_speed);
239         ao_accel = from_fix(ao_k_accel);
240         if (ao_height > ao_max_height)
241                 ao_max_height = ao_height;
242 #ifdef AO_FLIGHT_TEST
243         ao_sample_prev_tick = ao_sample_tick;
244 #endif
245 }