KalmanFilter.cpp
Go to the documentation of this file.
1 // *****************************************************************
2 // Filename: KalmanFilter.cpp
3 // Copyright: Kai Welke, Chair Prof. Dillmann (IAIM),
4 // Institute for Computer Science and Engineering (CSE),
5 // University of Karlsruhe. All rights reserved.
6 // Author: Kai Welke
7 // Date: 27.05.2009
8 // *****************************************************************
9 
10 // *****************************************************************
11 // includes
12 // *****************************************************************
13 #include "KalmanFilter.h"
14 
15 #include <iostream>
16 
17 // *****************************************************************
18 // setup
19 // *****************************************************************
20 void
21 KalmanFilter::setMotionModel(const Eigen::MatrixXd& motion_A,
22  const Eigen::MatrixXd& motion_B,
23  const Gaussian& motion_noise)
24 {
25  m_A = motion_A;
26  m_B = motion_B;
27 
28  m_R = motion_noise;
29 
30  m_A_t = m_A.transpose();
31 }
32 
33 void
34 KalmanFilter::setMeasurementModel(const Eigen::MatrixXd& measurement_C,
35  const Gaussian& measurement_noise)
36 {
37  m_C = measurement_C;
38 
39  m_Q = measurement_noise;
40 
41  m_C_t = m_C.transpose();
42 }
43 
44 // *****************************************************************
45 // prediction / update
46 // *****************************************************************
49  const Eigen::VectorXd& actionU,
50  const Eigen::VectorXd& perceptZ)
51 {
52  // prediction
53  Gaussian bel_predicted = predict(believe, actionU);
54 
55  // update
56  Gaussian result = update(bel_predicted, perceptZ);
57 
58  return result;
59 }
60 
62 KalmanFilter::predict(const Gaussian& believe, const Eigen::VectorXd& actionU)
63 {
64  // predict step bel_predicted = p(xt|xt-1,ut)
65  Gaussian predicted(believe.getDimensions());
66 
67  predicted.setMean(m_A * believe.getMean() + m_B * actionU);
68  predicted.setCovariance(m_A * believe.getCovariance() * m_A_t + m_R.getCovariance());
69 
70  return predicted;
71 }
72 
74 KalmanFilter::update(const Gaussian& predicted_believe, const Eigen::VectorXd& perceptZ)
75 {
76  // calculate Kalman gain
77  //@@@ TODO FIX: if temp ~= 0 temp.inverse() is not possible => temp.inverse() contains nan.
78  //std::cout << "***********m_Q.getCovariance():" << m_Q.getCovariance() << std::endl;
79  Eigen::MatrixXd temp = m_C * predicted_believe.getCovariance() * m_C_t + m_Q.getCovariance();
80  //std::cout << "***********temp:" << temp << std::endl;
81  //std::cout << "***********temp.inverse():" << temp.inverse() << std::endl;
82  //std::cout << "***********predicted_believe.getCovariance():" << predicted_believe.getCovariance() << std::endl;
83  //std::cout << "***********m_C_t:" << m_C_t << std::endl;
84  Eigen::MatrixXd gain = predicted_believe.getCovariance() * m_C_t * temp.inverse();
85  //std::cout << "***********gain:" << gain << std::endl;
86 
87  // update step bel = lambda * p(zt|xt) * bel_prior
88  Gaussian result(predicted_believe.getDimensions());
89  result.setMean(predicted_believe.getMean() +
90  gain * (perceptZ - m_C * predicted_believe.getMean()));
91  // result.setCovariance((Eigen::MatrixXd::Identity(gain.rows(),gain.cols()) - gain * m_C) * predicted_believe.getCovariance());
92 
93  result.setCovariance(predicted_believe.getCovariance() - gain * temp * gain.transpose());
94 
95  return result;
96 }
Gaussian::getCovariance
const covariance_type & getCovariance() const
Definition: Gaussian.h:82
KalmanFilter::setMotionModel
void setMotionModel(const Eigen::MatrixXd &motion_A, const Eigen::MatrixXd &motion_B, const Gaussian &motion_noise)
Definition: KalmanFilter.cpp:21
KalmanFilter::setMeasurementModel
void setMeasurementModel(const Eigen::MatrixXd &measurement_C, const Gaussian &measurement_noise)
Definition: KalmanFilter.cpp:34
KalmanFilter::m_C
Eigen::MatrixXd m_C
Definition: KalmanFilter.h:64
KalmanFilter::filter
Gaussian filter(const Gaussian &believe, const Eigen::VectorXd &actionU, const Eigen::VectorXd &perceptZ)
Definition: KalmanFilter.cpp:48
Gaussian::getDimensions
int getDimensions() const
Definition: Gaussian.h:94
KalmanFilter::m_B
Eigen::MatrixXd m_B
Definition: KalmanFilter.h:63
Gaussian::setMean
void setMean(const value_type &mean)
Definition: Gaussian.cpp:255
KalmanFilter::m_C_t
Eigen::MatrixXd m_C_t
Definition: KalmanFilter.h:66
Gaussian::setCovariance
void setCovariance(const covariance_type &cov)
Definition: Gaussian.cpp:272
KalmanFilter::predict
Gaussian predict(const Gaussian &believe, const Eigen::VectorXd &actionU)
Definition: KalmanFilter.cpp:62
KalmanFilter.h
KalmanFilter::m_A
Eigen::MatrixXd m_A
Definition: KalmanFilter.h:62
Gaussian::getMean
const value_type & getMean() const
Definition: Gaussian.h:88
Gaussian
Definition: Gaussian.h:50
KalmanFilter::m_Q
Gaussian m_Q
Definition: KalmanFilter.h:69
KalmanFilter::m_R
Gaussian m_R
Definition: KalmanFilter.h:70
KalmanFilter::update
Gaussian update(const Gaussian &predicted_believe, const Eigen::VectorXd &perceptZ)
Definition: KalmanFilter.cpp:74
KalmanFilter::m_A_t
Eigen::MatrixXd m_A_t
Definition: KalmanFilter.h:65