altos: Switch ao_rssi.c __xdata to __pdata
[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         if (ao_flight_state >= ao_flight_drogue)
93                 return;
94         height_distrust = ao_sample_alt - AO_MAX_BARO_HEIGHT;
95 #if HAS_ACCEL
96         /* speed is stored * 16, but we need to ramp between 200 and 328, so
97          * we want to multiply by 2. The result is a shift by 3.
98          */
99         speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
100         if (speed_distrust <= 0)
101                 speed_distrust = 0;
102         else if (speed_distrust > height_distrust)
103                 height_distrust = speed_distrust;
104 #endif
105         if (height_distrust > 0) {
106 #ifdef AO_FLIGHT_TEST
107                 int     old_ao_error_h = ao_error_h;
108 #endif
109                 if (height_distrust > 0x100)
110                         height_distrust = 0x100;
111                 ao_error_h = (int16_t) (((int32_t) ao_error_h * (0x100 - height_distrust)) >> 8);
112 #ifdef AO_FLIGHT_TEST
113                 if (ao_flight_debug) {
114                         printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
115                                (double) (ao_sample_alt - AO_MAX_BARO_HEIGHT),
116                                (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
117                                height_distrust / 256.0,
118                                old_ao_error_h, ao_error_h);
119                 }
120 #endif
121         }
122 }
123
124 static void
125 ao_kalman_correct_baro(void)
126 {
127         ao_kalman_err_height();
128 #ifdef AO_FLIGHT_TEST
129         if (ao_sample_tick - ao_sample_prev_tick > 5) {
130                 ao_k_height += (int32_t) AO_BARO_K0_10 * ao_error_h;
131                 ao_k_speed  += (int32_t) AO_BARO_K1_10 * ao_error_h;
132                 ao_k_accel  += (int32_t) AO_BARO_K2_10 * ao_error_h;
133                 return;
134         }
135 #endif
136         ao_k_height += (int32_t) AO_BARO_K0_100 * ao_error_h;
137         ao_k_speed  += (int32_t) AO_BARO_K1_100 * ao_error_h;
138         ao_k_accel  += (int32_t) AO_BARO_K2_100 * ao_error_h;
139 }
140
141 #if HAS_ACCEL
142
143 static void
144 ao_kalman_err_accel(void)
145 {
146         int32_t accel;
147
148         accel = (ao_ground_accel - ao_sample_accel) * ao_accel_scale;
149
150         /* Can't use ao_accel here as it is the pre-prediction value still */
151         ao_error_a = (accel - ao_k_accel) >> 16;
152 }
153
154 static void
155 ao_kalman_correct_both(void)
156 {
157         ao_kalman_err_height();
158         ao_kalman_err_accel();
159
160 #ifdef AO_FLIGHT_TEST
161         if (ao_sample_tick - ao_sample_prev_tick > 5) {
162                 if (ao_flight_debug) {
163                         printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
164                                 ao_k_speed / (65536.0 * 16.0),
165                                 (double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
166                                 (double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
167                                 (ao_k_speed +
168                                  (int32_t) AO_BOTH_K10_10 * ao_error_h +
169                                  (int32_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
170                 }
171                 ao_k_height +=
172                         (int32_t) AO_BOTH_K00_10 * ao_error_h +
173                         (int32_t) AO_BOTH_K01_10 * ao_error_a;
174                 ao_k_speed +=
175                         (int32_t) AO_BOTH_K10_10 * ao_error_h +
176                         (int32_t) AO_BOTH_K11_10 * ao_error_a;
177                 ao_k_accel +=
178                         (int32_t) AO_BOTH_K20_10 * ao_error_h +
179                         (int32_t) AO_BOTH_K21_10 * ao_error_a;
180                 return;
181         }
182         if (ao_flight_debug) {
183                 printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
184                         ao_k_speed / (65536.0 * 16.0),
185                         (double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
186                         (double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
187                         (ao_k_speed +
188                          (int32_t) AO_BOTH_K10_100 * ao_error_h +
189                          (int32_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
190         }
191 #endif
192         ao_k_height +=
193                 (int32_t) AO_BOTH_K00_100 * ao_error_h +
194                 (int32_t) AO_BOTH_K01_100 * ao_error_a;
195         ao_k_speed +=
196                 (int32_t) AO_BOTH_K10_100 * ao_error_h +
197                 (int32_t) AO_BOTH_K11_100 * ao_error_a;
198         ao_k_accel +=
199                 (int32_t) AO_BOTH_K20_100 * ao_error_h +
200                 (int32_t) AO_BOTH_K21_100 * ao_error_a;
201 }
202
203 #ifdef FORCE_ACCEL
204 static void
205 ao_kalman_correct_accel(void)
206 {
207         ao_kalman_err_accel();
208
209         if (ao_sample_tick - ao_sample_prev_tick > 5) {
210                 ao_k_height +=(int32_t) AO_ACCEL_K0_10 * ao_error_a;
211                 ao_k_speed  += (int32_t) AO_ACCEL_K1_10 * ao_error_a;
212                 ao_k_accel  += (int32_t) AO_ACCEL_K2_10 * ao_error_a;
213                 return;
214         }
215         ao_k_height += (int32_t) AO_ACCEL_K0_100 * ao_error_a;
216         ao_k_speed  += (int32_t) AO_ACCEL_K1_100 * ao_error_a;
217         ao_k_accel  += (int32_t) AO_ACCEL_K2_100 * ao_error_a;
218 }
219 #endif
220 #endif /* HAS_ACCEL */
221
222 void
223 ao_kalman(void)
224 {
225         ao_kalman_predict();
226 #if HAS_ACCEL
227         if (ao_flight_state <= ao_flight_coast) {
228 #ifdef FORCE_ACCEL
229                 ao_kalman_correct_accel();
230 #else
231                 ao_kalman_correct_both();
232 #endif
233         } else
234 #endif
235                 ao_kalman_correct_baro();
236         ao_height = from_fix(ao_k_height);
237         ao_speed = from_fix(ao_k_speed);
238         ao_accel = from_fix(ao_k_accel);
239         if (ao_height > ao_max_height)
240                 ao_max_height = ao_height;
241 #ifdef AO_FLIGHT_TEST
242         ao_sample_prev_tick = ao_sample_tick;
243 #endif
244 }