rm_control
Loading...
Searching...
No Matches
kalman_filter.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BSD 3-Clause License
3 *
4 * Copyright (c) 2021, Qiayuan Liao
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * * Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33
34//
35// Created by qiayuan on 4/3/20.
36//
37
38#pragma once
39
40#include <iostream>
42
43template <typename T>
45{
46public:
47 template <typename TA, typename TB, typename TH, typename TQ, typename TR>
48 KalmanFilter(const Eigen::MatrixBase<TA>& A, const Eigen::MatrixBase<TB>& B, const Eigen::MatrixBase<TH>& H,
49 const Eigen::MatrixBase<TQ>& Q, const Eigen::MatrixBase<TR>& R)
50 : A_(A), B_(B), H_(H), Q_(Q), R_(R), m_(TH::RowsAtCompileTime), n_(TA::RowsAtCompileTime), inited(false)
51 {
52 // Check dimension
53 // ref:http://www.swarthmore.edu/NatSci/echeeve1/Ref/Kalman/MatrixKalman.html
54 static_assert(TA::RowsAtCompileTime == TA::ColsAtCompileTime, "A should be square matrix");
55 static_assert(TA::RowsAtCompileTime == TH::ColsAtCompileTime, "H columns should be equal to A columns");
56 static_assert(TB::RowsAtCompileTime == TA::ColsAtCompileTime, "B rows should be equal to A columns ");
57 static_assert(TQ::RowsAtCompileTime == TA::ColsAtCompileTime && TQ::ColsAtCompileTime == TA::ColsAtCompileTime,
58 "The rows and columns of Q should be equal to the columns of A");
59 static_assert(TR::RowsAtCompileTime == TH::RowsAtCompileTime && TR::ColsAtCompileTime == TH::RowsAtCompileTime,
60 "The rows and columns of Q should be equal to the rows of H");
61 x_.resize(n_);
62 P_.resize(n_, n_);
63 I_.resize(n_, n_);
64 I_.setIdentity();
65 }
66
67 ~KalmanFilter() = default;
68
69 template <typename T1>
70 void clear(const Eigen::MatrixBase<T1>& x)
71 {
72 x_ = x;
73 inited = true;
74 K_ = DMat<T>::Zero(n_, m_);
75 P_ = DMat<T>::Zero(n_, m_);
76 P_new_ = DMat<T>::Zero(n_, n_);
77 }
78
79 template <typename T1>
80 void update(const Eigen::MatrixBase<T1>& z)
81 {
82 update(z, R_);
83 };
84
85 template <typename T1, typename T2>
86 void update(const Eigen::MatrixBase<T1>& z, const Eigen::MatrixBase<T2>& R)
87 {
88 if (!inited)
89 return; // TODO: add assert
90 // update R_
91 R_ = R;
92 // update
93 K_ = P_new_ * H_.transpose() * ((H_ * P_new_ * H_.transpose() + R_).inverse());
94 x_ = x_ + K_ * (z - H_ * x_);
95 P_ = (I_ - K_ * H_) * P_new_;
96 };
97
98 template <typename T1>
99 void predict(const Eigen::MatrixBase<T1>& u)
100 {
101 predict(u, Q_);
102 }
103
104 template <typename T1, typename T2>
105 void predict(const Eigen::MatrixBase<T1>& u, const Eigen::MatrixBase<T2>& Q)
106 {
107 if (!inited)
108 return; // TODO: add assert
109 // update Q_
110 Q_ = Q;
111 // predict
112 x_ = A_ * x_ + B_ * u;
113 P_new_ = A_ * P_ * A_.transpose() + Q_;
114 }
115
117 {
118 return x_;
119 }
120
121private:
122 DMat<T> A_, B_, H_, I_;
123 DMat<T> Q_, R_, P_, P_new_, K_;
124 DVec<T> x_;
125 const int m_, n_; // dimension
126 bool inited;
127};
Definition kalman_filter.h:45
void update(const Eigen::MatrixBase< T1 > &z, const Eigen::MatrixBase< T2 > &R)
Definition kalman_filter.h:86
KalmanFilter(const Eigen::MatrixBase< TA > &A, const Eigen::MatrixBase< TB > &B, const Eigen::MatrixBase< TH > &H, const Eigen::MatrixBase< TQ > &Q, const Eigen::MatrixBase< TR > &R)
Definition kalman_filter.h:48
~KalmanFilter()=default
void update(const Eigen::MatrixBase< T1 > &z)
Definition kalman_filter.h:80
void predict(const Eigen::MatrixBase< T1 > &u)
Definition kalman_filter.h:99
DVec< T > getState()
Definition kalman_filter.h:116
void predict(const Eigen::MatrixBase< T1 > &u, const Eigen::MatrixBase< T2 > &Q)
Definition kalman_filter.h:105
void clear(const Eigen::MatrixBase< T1 > &x)
Definition kalman_filter.h:70
typename Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > DMat
Definition eigen_types.h:146
typename Eigen::Matrix< T, Eigen::Dynamic, 1 > DVec
Definition eigen_types.h:142