-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalman_filter.cc
61 lines (49 loc) · 1.21 KB
/
kalman_filter.cc
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
#include <math.h>
#include "kalman_filter.hh"
#include <iostream>
using Eigen::MatrixXd;
using Eigen::VectorXd;
const float PI2 = 2 * M_PI;
KalmanFilter::KalmanFilter() {}
KalmanFilter::~KalmanFilter() {}
void KalmanFilter::Init(VectorXd &x_in, MatrixXd &P_in, MatrixXd &F_in,
MatrixXd &H_in, MatrixXd &R_in, MatrixXd &Q_in) {
x_ = x_in;
P_ = P_in;
F_ = F_in;
H_ = H_in;
R_ = R_in;
Q_ = Q_in;
}
void KalmanFilter::Predict() {
/**
TODO:
* predict the state
*/
x_ = F_ * x_;
MatrixXd Ft = F_.transpose();
P_ = F_ * P_ * Ft + Q_;
}
void KalmanFilter::Update(const VectorXd &z) {
/**
TODO:
* update the state by using Kalman Filter equations
*/
//std::cout << "H_: " << H_.size() << " x_: " << x_.size() << std::endl;
VectorXd z_pred = H_ * x_;
VectorXd y = z - z_pred;
MatrixXd Ht = H_.transpose();
MatrixXd PHt = P_ * Ht;
MatrixXd S = H_ * PHt + R_;
MatrixXd Si = S.inverse();
MatrixXd K = PHt * Si;
//new estimate
x_ = x_ + (K * y);
long x_size = x_.size();
MatrixXd I = MatrixXd::Identity(x_size, x_size);
P_ = (I - K * H_) * P_;
}
// template<typename T>
// void KalmanFilter::Update(const T& meas){
// Eigen::VectorXd z_pred;
// }