-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kalman.cpp
52 lines (38 loc) · 881 Bytes
/
Kalman.cpp
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
/*
Copyright(C) 2019- White Noise Team
One dimensional Kalman Filter implementation for
use with BNO055 IMU to filter noisy gyro and
accelerometer measurements.
This software is distributed under the MIT License
This file includes the source of the Kalman Filter API
*/
#include "Kalman.h"
Kalman::Kalman(float r, float q, float a, float b, float c) {
R = r;
Q = q;
A = a;
B = b;
C = c;
init = false;
}
float Kalman::filter(float z, float u) {
if (!init) {
x = 1 / C * z;
cov = Q / (C * C);
init = true;
}
else {
float pred = predict(u);
float p_cov = uncertainty();
float K = p_cov * C / (C * C * p_cov + Q);
x = pred + K * (z - C * pred);
cov = p_cov - K * C * p_cov;
}
return x;
}
float Kalman::predict(float u) {
return A * x + B * u;
}
float Kalman::uncertainty() {
return A * A * cov + R;
}