1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/*
* Copyright © 2011 Keith Packard <keithp@keithp.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#ifndef AO_FLIGHT_TEST
#include "ao.h"
#include "ao_flight.h"
#endif
#include "ao_sample.h"
#include "ao_kalman.h"
static ao_k_t ao_k_height;
static ao_k_t ao_k_speed;
static ao_k_t ao_k_accel;
#define AO_K_STEP_100 to_fix_v(0.01)
#define AO_K_STEP_2_2_100 to_fix_v(0.00005)
#define AO_K_STEP_10 to_fix_v(0.1)
#define AO_K_STEP_2_2_10 to_fix_v(0.005)
#define AO_K_STEP_1 to_fix_v(1)
#define AO_K_STEP_2_2_1 to_fix_v(0.5)
ao_v_t ao_height;
ao_v_t ao_speed;
ao_v_t ao_accel;
ao_v_t ao_max_height;
static ao_k_t ao_avg_height_scaled;
ao_v_t ao_avg_height;
ao_v_t ao_error_h;
#if !HAS_ACCEL || AO_FLIGHT_TEST
#define AO_ERROR_H_SQ_AVG 1
#endif
#if AO_ERROR_H_SQ_AVG
ao_v_t ao_error_h_sq_avg;
#endif
#if HAS_ACCEL
ao_v_t ao_error_a;
#endif
static void
ao_kalman_predict(void)
{
#ifdef AO_FLIGHT_TEST
if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_1 +
(ao_k_t) ao_accel * AO_K_STEP_2_2_1) >> 4;
ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_1;
return;
}
if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_10 +
(ao_k_t) ao_accel * AO_K_STEP_2_2_10) >> 4;
ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_10;
return;
}
if (ao_flight_debug) {
printf ("predict speed %g + (%g * %g) = %g\n",
ao_k_speed / (65536.0 * 16.0), ao_accel / 16.0, AO_K_STEP_100 / 65536.0,
(ao_k_speed + (ao_k_t) ao_accel * AO_K_STEP_100) / (65536.0 * 16.0));
}
#endif
ao_k_height += ((ao_k_t) ao_speed * AO_K_STEP_100 +
(ao_k_t) ao_accel * AO_K_STEP_2_2_100) >> 4;
ao_k_speed += (ao_k_t) ao_accel * AO_K_STEP_100;
}
static void
ao_kalman_err_height(void)
{
#if AO_ERROR_H_SQ_AVG
ao_v_t e;
#endif
ao_v_t height_distrust;
#if HAS_ACCEL
ao_v_t speed_distrust;
#endif
ao_error_h = ao_sample_height - (ao_v_t) (ao_k_height >> 16);
#if AO_ERROR_H_SQ_AVG
e = ao_error_h;
if (e < 0)
e = -e;
if (e > 127)
e = 127;
ao_error_h_sq_avg -= ao_error_h_sq_avg >> 4;
ao_error_h_sq_avg += (e * e) >> 4;
#endif
if (ao_flight_state >= ao_flight_drogue)
return;
height_distrust = ao_sample_alt - AO_MAX_BARO_HEIGHT;
#if HAS_ACCEL
/* speed is stored * 16, but we need to ramp between 248 and 328, so
* we want to multiply by 2. The result is a shift by 3.
*/
speed_distrust = (ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) >> (4 - 1);
if (speed_distrust > AO_MAX_SPEED_DISTRUST)
speed_distrust = AO_MAX_SPEED_DISTRUST;
if (speed_distrust > height_distrust)
height_distrust = speed_distrust;
#endif
if (height_distrust > 0) {
#ifdef AO_FLIGHT_TEST
int old_ao_error_h = ao_error_h;
#endif
if (height_distrust > 0x100)
height_distrust = 0x100;
ao_error_h = (ao_v_t) (((ao_k_t) ao_error_h * (0x100 - height_distrust)) >> 8);
#ifdef AO_FLIGHT_TEST
if (ao_flight_debug) {
printf("over height %g over speed %g distrust: %g height: error %d -> %d\n",
(double) (ao_sample_alt - AO_MAX_BARO_HEIGHT),
(ao_speed - AO_MS_TO_SPEED(AO_MAX_BARO_SPEED)) / 16.0,
height_distrust / 256.0,
old_ao_error_h, ao_error_h);
}
#endif
}
}
static void
ao_kalman_correct_baro(void)
{
ao_kalman_err_height();
#ifdef AO_FLIGHT_TEST
if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
ao_k_height += (ao_k_t) AO_BARO_K0_1 * ao_error_h;
ao_k_speed += (ao_k_t) AO_BARO_K1_1 * ao_error_h;
ao_k_accel += (ao_k_t) AO_BARO_K2_1 * ao_error_h;
return;
}
if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
ao_k_height += (ao_k_t) AO_BARO_K0_10 * ao_error_h;
ao_k_speed += (ao_k_t) AO_BARO_K1_10 * ao_error_h;
ao_k_accel += (ao_k_t) AO_BARO_K2_10 * ao_error_h;
return;
}
#endif
ao_k_height += (ao_k_t) AO_BARO_K0_100 * ao_error_h;
ao_k_speed += (ao_k_t) AO_BARO_K1_100 * ao_error_h;
ao_k_accel += (ao_k_t) AO_BARO_K2_100 * ao_error_h;
}
#if HAS_ACCEL
static void
ao_kalman_err_accel(void)
{
ao_k_t accel;
accel = (ao_config.accel_plus_g - ao_sample_accel) * ao_accel_scale;
/* Can't use ao_accel here as it is the pre-prediction value still */
ao_error_a = (accel - ao_k_accel) >> 16;
}
#ifndef FORCE_ACCEL
static void
ao_kalman_correct_both(void)
{
ao_kalman_err_height();
ao_kalman_err_accel();
#ifdef AO_FLIGHT_TEST
if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50) {
if (ao_flight_debug) {
printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
ao_k_speed / (65536.0 * 16.0),
(double) ao_error_h, AO_BOTH_K10_1 / 65536.0,
(double) ao_error_a, AO_BOTH_K11_1 / 65536.0,
(ao_k_speed +
(ao_k_t) AO_BOTH_K10_1 * ao_error_h +
(ao_k_t) AO_BOTH_K11_1 * ao_error_a) / (65536.0 * 16.0));
}
ao_k_height +=
(ao_k_t) AO_BOTH_K00_1 * ao_error_h +
(ao_k_t) AO_BOTH_K01_1 * ao_error_a;
ao_k_speed +=
(ao_k_t) AO_BOTH_K10_1 * ao_error_h +
(ao_k_t) AO_BOTH_K11_1 * ao_error_a;
ao_k_accel +=
(ao_k_t) AO_BOTH_K20_1 * ao_error_h +
(ao_k_t) AO_BOTH_K21_1 * ao_error_a;
return;
}
if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
if (ao_flight_debug) {
printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
ao_k_speed / (65536.0 * 16.0),
(double) ao_error_h, AO_BOTH_K10_10 / 65536.0,
(double) ao_error_a, AO_BOTH_K11_10 / 65536.0,
(ao_k_speed +
(ao_k_t) AO_BOTH_K10_10 * ao_error_h +
(ao_k_t) AO_BOTH_K11_10 * ao_error_a) / (65536.0 * 16.0));
}
ao_k_height +=
(ao_k_t) AO_BOTH_K00_10 * ao_error_h +
(ao_k_t) AO_BOTH_K01_10 * ao_error_a;
ao_k_speed +=
(ao_k_t) AO_BOTH_K10_10 * ao_error_h +
(ao_k_t) AO_BOTH_K11_10 * ao_error_a;
ao_k_accel +=
(ao_k_t) AO_BOTH_K20_10 * ao_error_h +
(ao_k_t) AO_BOTH_K21_10 * ao_error_a;
return;
}
if (ao_flight_debug) {
printf ("correct speed %g + (%g * %g) + (%g * %g) = %g\n",
ao_k_speed / (65536.0 * 16.0),
(double) ao_error_h, AO_BOTH_K10_100 / 65536.0,
(double) ao_error_a, AO_BOTH_K11_100 / 65536.0,
(ao_k_speed +
(ao_k_t) AO_BOTH_K10_100 * ao_error_h +
(ao_k_t) AO_BOTH_K11_100 * ao_error_a) / (65536.0 * 16.0));
}
#endif
ao_k_height +=
(ao_k_t) AO_BOTH_K00_100 * ao_error_h +
(ao_k_t) AO_BOTH_K01_100 * ao_error_a;
ao_k_speed +=
(ao_k_t) AO_BOTH_K10_100 * ao_error_h +
(ao_k_t) AO_BOTH_K11_100 * ao_error_a;
ao_k_accel +=
(ao_k_t) AO_BOTH_K20_100 * ao_error_h +
(ao_k_t) AO_BOTH_K21_100 * ao_error_a;
}
#else
static void
ao_kalman_correct_accel(void)
{
ao_kalman_err_accel();
if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5) {
ao_k_height +=(ao_k_t) AO_ACCEL_K0_10 * ao_error_a;
ao_k_speed += (ao_k_t) AO_ACCEL_K1_10 * ao_error_a;
ao_k_accel += (ao_k_t) AO_ACCEL_K2_10 * ao_error_a;
return;
}
ao_k_height += (ao_k_t) AO_ACCEL_K0_100 * ao_error_a;
ao_k_speed += (ao_k_t) AO_ACCEL_K1_100 * ao_error_a;
ao_k_accel += (ao_k_t) AO_ACCEL_K2_100 * ao_error_a;
}
#endif /* else FORCE_ACCEL */
#endif /* HAS_ACCEL */
void
ao_kalman(void)
{
ao_kalman_predict();
#if HAS_ACCEL
if (ao_flight_state <= ao_flight_coast) {
#ifdef FORCE_ACCEL
ao_kalman_correct_accel();
#else
ao_kalman_correct_both();
#endif
} else
#endif
ao_kalman_correct_baro();
ao_height = from_fix(ao_k_height);
ao_speed = from_fix(ao_k_speed);
ao_accel = from_fix(ao_k_accel);
if (ao_height > ao_max_height)
ao_max_height = ao_height;
ao_avg_height_scaled = ao_avg_height_scaled - ao_avg_height + ao_sample_height;
#ifdef AO_FLIGHT_TEST
if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 50)
ao_avg_height = (ao_avg_height_scaled + 1) >> 1;
else if ((int16_t) (ao_sample_tick - ao_sample_prev_tick) > 5)
ao_avg_height = (ao_avg_height_scaled + 7) >> 4;
else
#endif
ao_avg_height = (ao_avg_height_scaled + 63) >> 7;
}
|