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, typename T2>
70 void clear(const Eigen::MatrixBase<T1>& x, const Eigen::MatrixBase<T2>& P0)
71 {
72 x_ = x;
73 inited = true;
74 K_ = DMat<T>::Zero(n_, m_);
75 P_ = P0;
76 P_new_ = P0;
77 }
78
79 template <typename T1>
80 void clear(const Eigen::MatrixBase<T1>& x)
81 {
82 x_ = x;
83 inited = true;
84 K_ = DMat<T>::Zero(n_, m_);
85 P_.setIdentity();
86 P_new_.setIdentity();
87 }
88
89 template <typename T1>
90 void update(const Eigen::MatrixBase<T1>& z)
91 {
92 update(z, R_);
93 };
94
95 template <typename T1, typename T2>
96 void update(const Eigen::MatrixBase<T1>& z, const Eigen::MatrixBase<T2>& R)
97 {
98 if (!inited)
99 return; // TODO: add assert
100 // update R_
101 R_ = R;
102 // update
103 K_ = P_new_ * H_.transpose() * ((H_ * P_new_ * H_.transpose() + R_).inverse());
104 x_ = x_ + K_ * (z - H_ * x_);
105 P_ = (I_ - K_ * H_) * P_new_;
106 };
107
108 template <typename T1>
109 void predict(const Eigen::MatrixBase<T1>& u)
110 {
111 predict(u, Q_);
112 }
113
114 template <typename T1, typename T2>
115 void predict(const Eigen::MatrixBase<T1>& u, const Eigen::MatrixBase<T2>& Q)
116 {
117 if (!inited)
118 return; // TODO: add assert
119 // update Q_
120 Q_ = Q;
121 // predict
122 x_ = A_ * x_ + B_ * u;
123 P_new_ = A_ * P_ * A_.transpose() + Q_;
124 }
125
127 {
128 return x_;
129 }
130
131private:
132 DMat<T> A_, B_, H_, I_;
133 DMat<T> Q_, R_, P_, P_new_, K_;
134 DVec<T> x_;
135 const int m_, n_; // dimension
136 bool inited;
137};
Definition kalman_filter.h:45
void update(const Eigen::MatrixBase< T1 > &z, const Eigen::MatrixBase< T2 > &R)
Definition kalman_filter.h:96
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
void clear(const Eigen::MatrixBase< T1 > &x, const Eigen::MatrixBase< T2 > &P0)
Definition kalman_filter.h:70
~KalmanFilter()=default
void update(const Eigen::MatrixBase< T1 > &z)
Definition kalman_filter.h:90
void predict(const Eigen::MatrixBase< T1 > &u)
Definition kalman_filter.h:109
DVec< T > getState()
Definition kalman_filter.h:126
void predict(const Eigen::MatrixBase< T1 > &u, const Eigen::MatrixBase< T2 > &Q)
Definition kalman_filter.h:115
void clear(const Eigen::MatrixBase< T1 > &x)
Definition kalman_filter.h:80
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