forked from matthew-t-watson/Picopter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kalman.cpp
72 lines (59 loc) · 1.44 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* File: Kalman.cpp
* Author: matt
*
* Created on 26 November 2012, 10:04
*/
#include "Kalman.h"
KalmanClass::KalmanClass()
{
Q_angle_ = 0.0;
Q_bias_ = 0.00001;
R_angle_ = 200;
P_[0][0] = 0;
P_[0][1] = 0;
P_[1][0] = 0;
P_[1][1] = 0;
bias_ = 0;
}
KalmanClass::KalmanClass(const KalmanClass& orig)
{
}
KalmanClass::~KalmanClass()
{
}
void KalmanClass::predict(double* u, double* x, float* dt)
{
//Predicted state estimate
//x = F.x + B.u
*x += (*u - bias_) * *dt;
//Predicted estimate covariance
//P = F.P.F' + Q
P_[0][0] += -(P_[0][1] + P_[1][0]) * *dt + Q_angle_; // Can ommit P_[1][1]*dt*dt due to very small influence
P_[0][1] += -P_[1][1] * *dt; //Q[0][1] can be ommited as diagonals are always 0
P_[1][0] += -P_[1][1] * *dt; //Q[1][0] can be ommited as diagonals are always 0
P_[1][1] += Q_bias_;
}
void KalmanClass::update(double* z, double* x)
{
//Innovation
//y = z - H.x
y_ = *z - *x;
//Innovation covariance
//S = H.P.H' + R
S_ = P_[0][0] + R_angle_;
//Optimal Kalman gain
//K = P.H'.S^-1
K_[0] = P_[0][0] / S_;
K_[1] = P_[1][0] / S_;
//Updated state estimate
//x = x + K.y
*x += K_[0] * y_;
bias_ += K_[1] * y_;
//Updated estimate covariance
//P = (I - K.H).P
P_[0][0] -= K_[0] * P_[0][0];
P_[0][1] -= K_[0] * P_[0][1];
P_[1][0] -= K_[1] * P_[0][0];
P_[1][1] -= K_[1] * P_[0][1];
}