-
Notifications
You must be signed in to change notification settings - Fork 29
/
example_gravity.c
348 lines (290 loc) · 9.49 KB
/
example_gravity.c
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*!
* \brief Example of the Kalman filter
*
* In this example, the gravity constant (~9.81 m/s^2) will be estimated using only
* measurements of the position. These measurements have a variance of var(s) = 0.5m.
*
* The formulas used are:
* s = s + v*T + g*0.5*T^2
* v = v + g*T
* g = g
*
* The time constant is set to T = 1s.
*
* The initial estimation of the gravity constant is set to 6 m/s^2.
*/
#include <assert.h>
#define KALMAN_TIME_VARYING
#define KALMAN_JOSEPH_FORM
#include "fixkalman.h"
/**
* \brief Enables usage of the functions specialized for systems without control inputs
*
* Define to 0 in order to use the regular functions
*/
#define USE_UNCONTROLLED 1
// create the filter structure
#define KALMAN_NAME gravity
#define KALMAN_NUM_STATES 3
#define KALMAN_NUM_INPUTS 0
#if USE_UNCONTROLLED
kalman16_uc_t kf;
#else
kalman16_t kf;
#endif
// create the measurement structure
#define KALMAN_MEASUREMENT_NAME position
#define KALMAN_NUM_MEASUREMENTS 1
kalman16_observation_t kfm;
#define matrix_set(matrix, row, column, value) \
matrix->data[row][column] = value
#define matrix_set_symmetric(matrix, row, column, value) \
matrix->data[row][column] = value; \
matrix->data[column][row] = value
#ifndef FIXMATRIX_MAX_SIZE
#error FIXMATRIX_MAX_SIZE must be defined and greater or equal to the number of states, inputs and measurements.
#endif
#if (FIXMATRIX_MAX_SIZE < KALMAN_NUM_STATES) || (FIXMATRIX_MAX_SIZE < KALMAN_NUM_INPUTS) || (FIXMATRIX_MAX_SIZE < KALMAN_NUM_MEASUREMENTS)
#error FIXMATRIX_MAX_SIZE must be greater or equal to the number of states, inputs and measurements.
#endif
/*!
* \brief Initializes the gravity Kalman filter
*/
static void kalman_gravity_init()
{
/************************************************************************/
/* initialize the filter structures */
/************************************************************************/
#if USE_UNCONTROLLED
kalman_filter_initialize_uc(&kf, KALMAN_NUM_STATES);
#else
kalman_filter_initialize(&kf, KALMAN_NUM_STATES, KALMAN_NUM_INPUTS);
#endif
kalman_observation_initialize(&kfm, KALMAN_NUM_STATES, KALMAN_NUM_MEASUREMENTS);
/************************************************************************/
/* set initial state */
/************************************************************************/
#if USE_UNCONTROLLED
mf16 *x = kalman_get_state_vector_uc(&kf);
#else
mf16 *x = kalman_get_state_vector(&kf);
#endif
x->data[0][0] = 0; // s_i
x->data[1][0] = 0; // v_i
x->data[2][0] = fix16_from_float(6); // g_i
/************************************************************************/
/* set state transition */
/************************************************************************/
#if USE_UNCONTROLLED
mf16 *A = kalman_get_state_transition_uc(&kf);
#else
mf16 *A = kalman_get_state_transition(&kf);
#endif
// set time constant
const fix16_t T = fix16_one;
const fix16_t Tsquare = fix16_sq(T);
// helper
const fix16_t fix16_half = fix16_from_float(0.5);
// transition of x to s
matrix_set(A, 0, 0, fix16_one); // 1
matrix_set(A, 0, 1, T); // T
matrix_set(A, 0, 2, fix16_mul(fix16_half, Tsquare)); // 0.5 * T^2
// transition of x to v
matrix_set(A, 1, 0, 0); // 0
matrix_set(A, 1, 1, fix16_one); // 1
matrix_set(A, 1, 2, T); // T
// transition of x to g
matrix_set(A, 2, 0, 0); // 0
matrix_set(A, 2, 1, 0); // 0
matrix_set(A, 2, 2, fix16_one); // 1
/************************************************************************/
/* set covariance */
/************************************************************************/
#if USE_UNCONTROLLED
mf16 *P = kalman_get_system_covariance_uc(&kf);
#else
mf16 *P = kalman_get_system_covariance(&kf);
#endif
matrix_set_symmetric(P, 0, 0, fix16_half); // var(s)
matrix_set_symmetric(P, 0, 1, 0); // cov(s,v)
matrix_set_symmetric(P, 0, 2, 0); // cov(s,g)
matrix_set_symmetric(P, 1, 1, fix16_one); // var(v)
matrix_set_symmetric(P, 1, 2, 0); // cov(v,g)
matrix_set_symmetric(P, 2, 2, fix16_one); // var(g)
/************************************************************************/
/* set system process noise */
/************************************************************************/
#if USE_UNCONTROLLED
mf16 *Q = kalman_get_system_process_noise_uc(&kf);
mf16_fill(Q, F16(0.0001));
#endif
/************************************************************************/
/* set measurement transformation */
/************************************************************************/
mf16 *H = kalman_get_observation_transformation(&kfm);
matrix_set(H, 0, 0, fix16_one); // z = 1*s
matrix_set(H, 0, 1, 0); // + 0*v
matrix_set(H, 0, 2, 0); // + 0*g
/************************************************************************/
/* set process noise */
/************************************************************************/
mf16 *R = kalman_get_observation_process_noise(&kfm);
matrix_set(R, 0, 0, fix16_half); // var(s)
}
// define measurements.
//
// MATLAB source
// -------------
// s = s + v*T + g*0.5*T^2;
// v = v + g*T;
#define MEAS_COUNT (15)
static fix16_t real_distance[MEAS_COUNT] = {
F16(4.905),
F16(19.62),
F16(44.145),
F16(78.48),
F16(122.63),
F16(176.58),
F16(240.35),
F16(313.92),
F16(397.31),
F16(490.5),
F16(593.51),
F16(706.32),
F16(828.94),
F16(961.38),
F16(1103.6) };
// define measurement noise with variance 0.5
//
// MATLAB source
// -------------
// noise = 0.5^2*randn(15,1);
static fix16_t measurement_error[MEAS_COUNT] = {
F16(0.13442),
F16(0.45847),
F16(-0.56471),
F16(0.21554),
F16(0.079691),
F16(-0.32692),
F16(-0.1084),
F16(0.085656),
F16(0.8946),
F16(0.69236),
F16(-0.33747),
F16(0.75873),
F16(0.18135),
F16(-0.015764),
F16(0.17869) };
/*!
* \brief Runs the gravity Kalman filter.
*/
#if USE_UNCONTROLLED
void kalman_gravity_demo()
{
// initialize the filter
kalman_gravity_init();
mf16 *x = kalman_get_state_vector_uc(&kf);
mf16 *z = kalman_get_observation_vector(&kfm);
// filter!
uint_fast16_t i;
for (i = 0; i < MEAS_COUNT; ++i)
{
// prediction.
kalman_predict_uc(&kf);
// measure ...
fix16_t measurement = fix16_add(real_distance[i], measurement_error[i]);
matrix_set(z, 0, 0, measurement);
// update
kalman_correct_uc(&kf, &kfm);
}
// fetch estimated g
const fix16_t g_estimated = x->data[2][0];
const float value = fix16_to_float(g_estimated);
assert(value > 9.7 && value < 10);
}
#else
void kalman_gravity_demo()
{
// initialize the filter
kalman_gravity_init();
mf16 *x = kalman_get_state_vector(&kf);
mf16 *z = kalman_get_observation_vector(&kfm);
// filter!
uint_fast16_t i;
for (i = 0; i < MEAS_COUNT; ++i)
{
// prediction.
kalman_predict(&kf);
// measure ...
fix16_t measurement = fix16_add(real_distance[i], measurement_error[i]);
matrix_set(z, 0, 0, measurement);
// update
kalman_correct(&kf, &kfm);
}
// fetch estimated g
const fix16_t g_estimated = x->data[2][0];
const float value = fix16_to_float(g_estimated);
assert(value > 9.7 && value < 10);
}
#endif
/*!
* \brief Runs the gravity Kalman filter with lambda tuning.
*/
#if USE_UNCONTROLLED
void kalman_gravity_demo_lambda()
{
// initialize the filter
kalman_gravity_init();
mf16 *x = kalman_get_state_vector_uc(&kf);
mf16 *z = kalman_get_observation_vector(&kfm);
// forcibly increase uncertainty in every prediction step by ~20% (1/lambda^2)
const fix16_t lambda = F16(0.9);
// filter!
uint_fast16_t i;
for (i = 0; i < MEAS_COUNT; ++i)
{
// prediction.
kalman_predict_tuned_uc(&kf, lambda);
// measure ...
fix16_t measurement = fix16_add(real_distance[i], measurement_error[i]);
matrix_set(z, 0, 0, measurement);
// update
kalman_correct_uc(&kf, &kfm);
}
// fetch estimated g
const fix16_t g_estimated = x->data[2][0];
const float value = fix16_to_float(g_estimated);
assert(value > 9.7 && value < 10);
}
#else
void kalman_gravity_demo_lambda()
{
// initialize the filter
kalman_gravity_init();
mf16 *x = kalman_get_state_vector(&kf);
mf16 *z = kalman_get_observation_vector(&kfm);
// forcibly increase uncertainty in every prediction step by ~20% (1/lambda^2)
const fix16_t lambda = F16(0.9);
// filter!
uint_fast16_t i;
for (i = 0; i < MEAS_COUNT; ++i)
{
// prediction.
kalman_predict_tuned(&kf, lambda);
// measure ...
fix16_t measurement = fix16_add(real_distance[i], measurement_error[i]);
matrix_set(z, 0, 0, measurement);
// update
kalman_correct(&kf, &kfm);
}
// fetch estimated g
const fix16_t g_estimated = x->data[2][0];
const float value = fix16_to_float(g_estimated);
assert(value > 9.7 && value < 10);
}
#endif
void main()
{
kalman_gravity_demo();
kalman_gravity_demo_lambda();
}