-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkalman.cpp
72 lines (63 loc) · 1.85 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
#include <iostream>
#include "kalman.hpp"
KalmanFilter::KalmanFilter(
double dt,
const Eigen::MatrixXd& F,
const Eigen::MatrixXd& B,
const Eigen::MatrixXd& Q,
const Eigen::MatrixXd& H,
const Eigen::MatrixXd& R,
const Eigen::MatrixXd& P)
: F(F), B(B), Q(Q), H(H), R(R), P0(P),
m(H.rows()), n(F.rows()), dt(dt), initialized(false),
x_hat(n), x_hat_new(n), I(n, n)
{
I.setIdentity();
}
KalmanFilter::KalmanFilter() {}
void KalmanFilter::init() {
// initialize to zero
x_hat.setZero();
P = P0;
t0 = 0;
t = t0;
initialized = true;
}
void KalmanFilter::init(double t0, const Eigen::VectorXd& x0) {
x_hat = x0;
this->t0 = t0;
t = t0;
initialized = true;
}
void KalmanFilter::predict(const Eigen::VectorXd& u) {
// prediction step based on system model and control input: x_hat_new = F*x_hat + B*u
x_hat_new = F * x_hat + B * u;
P = F * P * F.transpose() + Q;
// do not increase time yet!
}
void KalmanFilter::predict() {
// prediction step based on system dynamic x_hat_new = F*x_hat
x_hat_new = F * x_hat;
P = F * P * F.transpose() + Q;
// do not increase time yet!
}
void KalmanFilter::update(const Eigen::VectorXd& y) {
// update step of kalman filter -> measurement residual y is defined as y = z - H*x_hat_new
// 1. determine optimal Kalman gain -> optimally weighthing innovation and prediction covariance
K = P * H.transpose() * (H * P * H.transpose() + R).inverse();
// 2. update a posteriori state estimate
x_hat_new = x_hat_new + K * y;
// 3. update a posteriori estimate covariance matrix
P = (I - K * H) * P;
// update variables
x_hat = x_hat_new;
t += dt;
}
void KalmanFilter::filter_step(const Eigen::VectorXd& y){
predict();
update(y);
}
void KalmanFilter::filter_step(const Eigen::VectorXd& y, const Eigen::VectorXd& u){
predict(u);
update(y);
}