-
Notifications
You must be signed in to change notification settings - Fork 15
/
KalmanFilter.cpp
71 lines (64 loc) · 1.8 KB
/
KalmanFilter.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
/***************************************************************************
* *
* Copyright (C) 2017 by University of Illinois *
* *
* http://illinois.edu *
* *
***************************************************************************/
/**
* @file KalmanFilter.cpp
*
* A linear KalmanFilter aims to recover latent velocity variables based on
* Vicon position measurement.
*
* @author Bo Liu <[email protected]>
*
*/
#include "KalmanFilter.h"
using Eigen::MatrixXd;
using Eigen::VectorXd;
KalmanFilter::KalmanFilter(QObject *parent) : QObject(parent)
{
// nothing to do
}
void KalmanFilter::update_dt(double dt)
{
F_(0,3) = dt;
F_(1,4) = dt;
F_(2, 5) = dt;
}
void KalmanFilter::init(VectorXd &x_in, MatrixXd &P_in, MatrixXd &F_in, MatrixXd &Q_in,
MatrixXd &H_in, MatrixXd &R_in)
{
x_ = x_in;
P_ = P_in;
F_ = F_in;
H_ = H_in;
R_ = R_in;
Q_ = Q_in;
}
void KalmanFilter::predict() {
/**
* predict the state
*/
x_ = F_ * x_;
MatrixXd Ft = F_.transpose();
P_ = F_ * P_ * Ft + Q_;
}
void KalmanFilter::update(const VectorXd &z) {
/**
* update the state by using Kalman Filter equations
*/
VectorXd z_pred = H_ * x_;
VectorXd y = z - z_pred;
MatrixXd Ht = H_.transpose();
MatrixXd S = H_ * P_ * Ht + R_;
MatrixXd Si = S.inverse();
MatrixXd PHt = P_ * Ht;
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_;
}