Skyward boardcore
Loading...
Searching...
No Matches
Kalman.h
Go to the documentation of this file.
1/* Copyright (c) 2020 Skyward Experimental Rocketry
2 * Authors: Alessandro Del Duca, Luca Conterio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <Eigen/Dense>
26
27namespace Boardcore
28{
29
36template <typename T, int N_size, int P_size, int M_size = 1>
37class Kalman
38{
39public:
40 using MatrixNN = Eigen::Matrix<T, N_size, N_size>;
41 using MatrixPN = Eigen::Matrix<T, P_size, N_size>;
42 using MatrixNP = Eigen::Matrix<T, N_size, P_size>;
43 using MatrixPP = Eigen::Matrix<T, P_size, P_size>;
44 using MatrixNM = Eigen::Matrix<T, N_size, M_size>;
45 using CVectorN = Eigen::Vector<T, N_size>;
46 using CVectorP = Eigen::Vector<T, P_size>;
47 using CVectorM = Eigen::Vector<T, M_size>;
48
62
68 Kalman(const KalmanConfig& config)
69 : F(config.F), H(config.H), Q(config.Q), R(config.R), P(config.P),
70 G(config.G), S(MatrixPP::Zero(P_size, P_size)),
71 K(MatrixNP::Zero(N_size, P_size)), x(config.x)
72 {
73 I.setIdentity();
74 }
75
76 void setConfig(const KalmanConfig& config)
77 {
78 F = config.F;
79 H = config.H;
80 Q = config.Q;
81 R = config.R;
82 P = config.P;
83 G = config.G;
84 S = MatrixPP::Zero(P_size, P_size);
85 K = MatrixNP::Zero(N_size, P_size);
86 x = config.x;
87 }
88
92 void predict()
93 {
94 x = F * x;
95 P = F * P * F.transpose() + Q;
96 }
97
103 void predictUpdateF(const MatrixNN& F_new)
104 {
105 F = F_new;
106 predict();
107 }
108
113 void predictWithControl(const CVectorM& control)
114 {
115 x = F * x + G * control;
116 P = F * P * F.transpose() + Q;
117 }
118
126 const CVectorM& control)
127 {
128 F = F_new;
129 predictWithControl(control);
130 }
131
137 bool correct(const CVectorP& y)
138 {
139 S = H * P * H.transpose() + R;
140
141 // TODO: The determinant is computed here and when S is inverted, it
142 // could be optimized
143 if (S.determinant() < 1e-3)
144 return false;
145
146 K = P * H.transpose() * S.inverse();
147 P = (I - K * H) * P;
148
149 x = x + K * (y - H * x);
150
151 res = y - H * x;
152
153 return true;
154 }
155
159 const CVectorN getState() { return x; }
160
165 {
166 yHat = H * x;
167 return yHat;
168 }
169
173 const CVectorP getResidual() { return res; }
174
178 const CVectorN predictState(uint32_t k)
179 {
180 CVectorN xHat = x;
181
182 for (uint32_t i = 0; i < k; i++)
183 xHat = F * xHat;
184
185 return xHat;
186 }
187
191 const CVectorP predictOutput(uint32_t k) { return H * predictState(k); }
192
193private:
194 MatrixNN F;
195 MatrixPN H;
196 MatrixNN Q;
197 MatrixPP R;
198 MatrixNN P;
199 MatrixNM G;
201 MatrixPP S;
202 MatrixNP K;
204 MatrixNN I;
206 CVectorN x;
207 CVectorP yHat;
208 CVectorP res;
209};
210
211} // namespace Boardcore
Implementation of a generic Kalman filter using the Eigen library.
Definition Kalman.h:38
const CVectorP getOutput()
Definition Kalman.h:164
Eigen::Matrix< T, N_size, N_size > MatrixNN
Definition Kalman.h:40
Eigen::Vector< T, N_size > CVectorN
Definition Kalman.h:45
Eigen::Matrix< T, P_size, N_size > MatrixPN
Definition Kalman.h:41
void predictWithControl(const CVectorM &control)
Prediction step with previous F matrix and with the control vector.
Definition Kalman.h:113
void predictUpdateF(const MatrixNN &F_new)
Prediction step.
Definition Kalman.h:103
Eigen::Vector< T, P_size > CVectorP
Definition Kalman.h:46
const CVectorN predictState(uint32_t k)
Predicts k steps ahead the state.
Definition Kalman.h:178
const CVectorP getResidual()
Definition Kalman.h:173
void setConfig(const KalmanConfig &config)
Definition Kalman.h:76
Eigen::Matrix< T, P_size, P_size > MatrixPP
Definition Kalman.h:43
const CVectorN getState()
Definition Kalman.h:159
bool correct(const CVectorP &y)
Correction step.
Definition Kalman.h:137
void predictWithControlUpdateF(const MatrixNN &F_new, const CVectorM &control)
Prediction step.
Definition Kalman.h:125
Eigen::Vector< T, M_size > CVectorM
Definition Kalman.h:47
void predict()
Prediction step with previous F matrix.
Definition Kalman.h:92
Eigen::Matrix< T, N_size, M_size > MatrixNM
Definition Kalman.h:44
Eigen::Matrix< T, N_size, P_size > MatrixNP
Definition Kalman.h:42
const CVectorP predictOutput(uint32_t k)
Predicts k steps ahead the output.
Definition Kalman.h:191
Kalman(const KalmanConfig &config)
Creates a Kalman filter object.
Definition Kalman.h:68
This file includes all the types the logdecoder script will decode.
Configuration struct for the Kalman class.
Definition Kalman.h:53