0.2.0
Loading...
Searching...
No Matches
KalmanFilter.hpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
13
14#pragma once
15
16#include <Eigen/Core>
17#include <Eigen/Dense>
18
20
21namespace NAV
22{
25{
26 public:
30 KalmanFilter(int n, int m)
31 {
32 // xฬ‚ State vector
33 x = Eigen::MatrixXd::Zero(n, 1);
34
35 // ๐ Error covariance matrix
36 P = Eigen::MatrixXd::Zero(n, n);
37
38 // ๐šฝ State transition matrix
39 Phi = Eigen::MatrixXd::Zero(n, n);
40
41 // ๐ System/Process noise covariance matrix
42 Q = Eigen::MatrixXd::Zero(n, n);
43
44 // ๐ณ Measurement vector
45 z = Eigen::MatrixXd::Zero(m, 1);
46
47 // ๐‡ Measurement sensitivity Matrix
48 H = Eigen::MatrixXd::Zero(m, n);
49
50 // ๐‘ = ๐ธ{๐ฐโ‚˜๐ฐโ‚˜แต€} Measurement noise covariance matrix
51 R = Eigen::MatrixXd::Zero(m, m);
52
53 // ๐—ฆ Measurement prediction covariance matrix
54 S = Eigen::MatrixXd::Zero(m, m);
55
56 // ๐Š Kalman gain matrix
57 K = Eigen::MatrixXd::Zero(n, m);
58
59 // ๐‘ฐ Identity Matrix
60 I = Eigen::MatrixXd::Identity(n, n);
61 }
62
64 KalmanFilter() = delete;
65
67 void setZero()
68 {
69 // xฬ‚ State vector
70 x.setZero();
71
72 // ๐ Error covariance matrix
73 P.setZero();
74
75 // ๐šฝ State transition matrix
76 Phi.setZero();
77
78 // ๐ System/Process noise covariance matrix
79 Q.setZero();
80
81 // ๐ณ Measurement vector
82 z.setZero();
83
84 // ๐‡ Measurement sensitivity Matrix
85 H.setZero();
86
87 // ๐‘ = ๐ธ{๐ฐโ‚˜๐ฐโ‚˜แต€} Measurement noise covariance matrix
88 R.setZero();
89
90 // ๐—ฆ Measurement prediction covariance matrix
91 S.setZero();
92
93 // ๐Š Kalman gain matrix
94 K.setZero();
95 }
96
100 void predict()
101 {
102 // Math: \mathbf{\hat{x}}_k^- = \mathbf{\Phi}_{k-1}\mathbf{\hat{x}}_{k-1}^+ \qquad \text{P. Groves}\,(3.14)
103 x = Phi * x;
104
105 // Math: \mathbf{P}_k^- = \mathbf{\Phi}_{k-1} P_{k-1}^+ \mathbf{\Phi}_{k-1}^T + \mathbf{Q}_{k-1} \qquad \text{P. Groves}\,(3.15)
106 P = Phi * P * Phi.transpose() + Q;
107 }
108
113 void correct()
114 {
115 S = H * P * H.transpose() + R;
116
117 // Math: \mathbf{K}_k = \mathbf{P}_k^- \mathbf{H}_k^T (\mathbf{H}_k \mathbf{P}_k^- \mathbf{H}_k^T + R_k)^{-1} \qquad \text{P. Groves}\,(3.21)
118 K = P * H.transpose() * S.inverse();
119
120 // Math: \begin{align*} \mathbf{\hat{x}}_k^+ &= \mathbf{\hat{x}}_k^- + \mathbf{K}_k (\mathbf{z}_k - \mathbf{H}_k \mathbf{\hat{x}}_k^-) \\ &= \mathbf{\hat{x}}_k^- + \mathbf{K}_k \mathbf{\delta z}_k^{-} \end{align*} \qquad \text{P. Groves}\,(3.24)
121 x = x + K * (z - H * x);
122
123 // Math: \mathbf{P}_k^+ = (\mathbf{I} - \mathbf{K}_k \mathbf{H}_k) \mathbf{P}_k^- \qquad \text{P. Groves}\,(3.25)
124 P = (I - K * H) * P;
125 }
126
133 {
134 // Math: \mathbf{K}_k = \mathbf{P}_k^- \mathbf{H}_k^T (\mathbf{H}_k \mathbf{P}_k^- \mathbf{H}_k^T + R_k)^{-1} \qquad \text{P. Groves}\,(3.21)
135 K = P * H.transpose() * (H * P * H.transpose() + R).inverse();
136
137 // Math: \begin{align*} \mathbf{\hat{x}}_k^+ &= \mathbf{\hat{x}}_k^- + \mathbf{K}_k (\mathbf{z}_k - \mathbf{H}_k \mathbf{\hat{x}}_k^-) \\ &= \mathbf{\hat{x}}_k^- + \mathbf{K}_k \mathbf{\delta z}_k^{-} \end{align*} \qquad \text{P. Groves}\,(3.24)
138 x = x + K * z;
139
140 // Math: \mathbf{P}_k^+ = (\mathbf{I} - \mathbf{K}_k \mathbf{H}_k) \mathbf{P}_k^- \qquad \text{P. Groves}\,(3.25)
141 // P = (I - K * H) * P;
142
143 // Math: \mathbf{P}_k^+ = (\mathbf{I} - \mathbf{K}_k \mathbf{H}_k) \mathbf{P}_k^- (\mathbf{I} - \mathbf{K}_k \mathbf{H}_k)^T + \mathbf{K}_k \mathbf{R}_k \mathbf{K}_k^T \qquad \text{Brown & Hwang}\,(p. 145, eq. 4.2.11)
144 P = (I - K * H) * P * (I - K * H).transpose() + K * R * K.transpose();
145 }
146
151 static Eigen::MatrixXd transitionMatrix(const Eigen::MatrixXd& F, double tau_s)
152 {
153 // Transition matrix ๐šฝ
154 return Eigen::MatrixXd::Identity(F.rows(), F.cols()) + F * tau_s;
155 }
156
158 Eigen::MatrixXd x;
159
161 Eigen::MatrixXd P;
162
164 Eigen::MatrixXd Phi;
165
167 Eigen::MatrixXd Q;
168
170 Eigen::MatrixXd z;
171
173 Eigen::MatrixXd H;
174
176 Eigen::MatrixXd R;
177
179 Eigen::MatrixXd S;
180
182 Eigen::MatrixXd K;
183
184 private:
186 Eigen::MatrixXd I;
187};
188
194template<typename Derived>
195typename Derived::PlainObject transitionMatrix_Phi_Taylor(const Eigen::MatrixBase<Derived>& F, double tau_s, size_t order)
196{
197 // Transition matrix ๐šฝ
198 typename Derived::PlainObject Phi;
199
200 if constexpr (Derived::RowsAtCompileTime == Eigen::Dynamic)
201 {
202 Phi = Eigen::MatrixBase<Derived>::Identity(F.rows(), F.cols());
203 }
204 else
205 {
206 Phi = Eigen::MatrixBase<Derived>::Identity();
207 }
208 // std::cout << "Phi = I";
209
210 for (size_t i = 1; i <= order; i++)
211 {
212 typename Derived::PlainObject Fpower = F;
213 // std::cout << " + (F";
214
215 for (size_t j = 1; j < i; j++) // F^j
216 {
217 // std::cout << "*F";
218 Fpower *= F;
219 }
220 // std::cout << "*tau_s^" << i << ")";
221 // std::cout << "/" << math::factorial(i);
222 Phi += (Fpower * std::pow(tau_s, i)) / math::factorial(i);
223 }
224 // std::cout << "\n";
225
226 return Phi;
227}
228
234template<typename Derived>
235typename Derived::PlainObject transitionMatrix_Phi_exp(const Eigen::MatrixBase<Derived>& F, typename Derived::Scalar tau_s)
236{
237 // Transition matrix ๐šฝ
238 return (F * tau_s).exp();
239}
240
241} // namespace NAV
Derived::PlainObject transitionMatrix_Phi_exp(const Eigen::MatrixBase< Derived > &F, typename Derived::Scalar tau_s)
Calculates the state transition matrix ๐šฝ using the exponential matrix.
Definition KalmanFilter.hpp:235
Derived::PlainObject transitionMatrix_Phi_Taylor(const Eigen::MatrixBase< Derived > &F, double tau_s, size_t order)
Calculates the state transition matrix ๐šฝ limited to specified order in ๐…๐œโ‚›
Definition KalmanFilter.hpp:195
Simple Math functions.
Generalized Kalman Filter class.
Definition KalmanFilter.hpp:25
void correctWithMeasurementInnovation()
Do a Measurement Update with a Measurement Innovation ๐œน๐ณ
Definition KalmanFilter.hpp:132
KalmanFilter(int n, int m)
Constructor.
Definition KalmanFilter.hpp:30
Eigen::MatrixXd K
๐Š Kalman gain matrix
Definition KalmanFilter.hpp:182
Eigen::MatrixXd x
xฬ‚ State vector
Definition KalmanFilter.hpp:158
KalmanFilter()=delete
Default constructor.
void predict()
Do a Time Update.
Definition KalmanFilter.hpp:100
Eigen::MatrixXd z
๐ณ Measurement vector
Definition KalmanFilter.hpp:170
Eigen::MatrixXd P
๐ Error covariance matrix
Definition KalmanFilter.hpp:161
void setZero()
Sets all Vectors and matrices to 0.
Definition KalmanFilter.hpp:67
Eigen::MatrixXd R
๐‘ = ๐ธ{๐ฐโ‚˜๐ฐโ‚˜แต€} Measurement noise covariance matrix
Definition KalmanFilter.hpp:176
Eigen::MatrixXd Q
๐ System/Process noise covariance matrix
Definition KalmanFilter.hpp:167
static Eigen::MatrixXd transitionMatrix(const Eigen::MatrixXd &F, double tau_s)
Updates the state transition matrix ๐šฝ limited to first order in ๐…๐œโ‚›
Definition KalmanFilter.hpp:151
Eigen::MatrixXd Phi
๐šฝ State transition matrix
Definition KalmanFilter.hpp:164
Eigen::MatrixXd H
๐‡ Measurement sensitivity Matrix
Definition KalmanFilter.hpp:173
Eigen::MatrixXd S
๐—ฆ Measurement prediction covariance matrix
Definition KalmanFilter.hpp:179
void correct()
Do a Measurement Update with a Measurement ๐ณ
Definition KalmanFilter.hpp:113