bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
kalman_filter.h
Go to the documentation of this file.
1#ifndef BITSIM_ESTIMATION_KALMAN_FILTER_H
2#define BITSIM_ESTIMATION_KALMAN_FILTER_H
3
4#include "Eigen/Dense"
7#include <boost/noncopyable.hpp>
8
9#include <any>
10#include <map>
11#include <stdexcept> //for std::invalid_argument
12#include <string>
13
14namespace bitsim
15{
16namespace estimation
17{
18
22
23template <typename MotionModelType, typename ObservationModelType> struct KalmanFilterConfig
24{
25
26 using motion_model_type = MotionModelType;
27 using observation_model_type = ObservationModelType;
28
33
38
44};
45
90template <typename MotionModelType, typename ObservationModelType>
91class KalmanFilter : private boost::noncopyable
92{
93
94 public:
97
98 using motion_model_type = MotionModelType;
99 using observation_model_type = ObservationModelType;
100 using state_type = typename config_type::motion_model_type::state_type;
101 using input_type = std::map<std::string, std::any>;
102
107
113 state_type &estimate(const input_type &input);
114
133 void predict(const input_type &input);
134
148 void update(const input_type &input);
149
153 void set_motion_model(motion_model_type &motion_model) { motion_model_ptr_ = &motion_model; }
154
156 void set_observation_model(const observation_model_type &observation_model)
157 {
158 observation_model_ptr_ = &observation_model;
159 }
160
164 KalmanFilter &with_matrix(const std::string &name, const matrix_type &mat);
165
169 void set_matrix(const std::string &name, const matrix_type &mat);
170
174 bool has_matrix(const std::string &name) const;
175
179 const state_type &get_state() const { return motion_model_ptr_->get_state(); }
180
184 state_type &get_state() { return motion_model_ptr_->get_state(); }
185
189 real_t get(const std::string &name) const { return motion_model_ptr_->get(name); }
190
194 const matrix_type &operator[](const std::string &name) const;
195
199 matrix_type &operator[](const std::string &name);
200
201 private:
205 motion_model_type *motion_model_ptr_;
206
210 const observation_model_type *observation_model_ptr_;
211
215 std::map<std::string, matrix_type> matrices_;
216};
217
218template <typename MotionModelType, typename ObservationModelType>
221 : motion_model_ptr_(config.motion_model), observation_model_ptr_(config.observation_model)
222{
223 // set the matrices
224 set_matrix("B", config.B);
225 set_matrix("P", config.P);
226 set_matrix("Q", config.Q);
227 set_matrix("K", config.K);
228 set_matrix("R", config.R);
229}
230
231template <typename MotionModelType, typename ObservationModelType>
232const DynMat<real_t> &
234{
235
236 auto itr = matrices_.find(name);
237
238 if (itr == matrices_.end())
239 {
240 throw std::invalid_argument("Matrix: " + name + " does not exist");
241 }
242
243 return itr->second;
244}
245
246template <typename MotionModelType, typename ObservationModelType>
249{
250
251 auto itr = matrices_.find(name);
252
253 if (itr == matrices_.end())
254 {
255 throw std::invalid_argument("Matrix: " + name + " does not exist");
256 }
257
258 return itr->second;
259}
260
261template <typename MotionModelType, typename ObservationModelType>
264 const matrix_type &mat)
265{
266
267 set_matrix(name, mat);
268 return *this;
269}
270
271template <typename MotionModelType, typename ObservationModelType>
273 const matrix_type &mat)
274{
275
276 if (name != "Q" && name != "K" && name != "R" && name != "P" && name != "B")
277 {
278 throw std::logic_error("Invalid matrix name. Name: " + name + " not in [Q, K, R, P, B]");
279 }
280
281 matrices_.insert_or_assign(name, mat);
282}
283
284template <typename MotionModelType, typename ObservationModelType>
286{
287
288 auto itr = matrices_.find(name);
289 return itr != matrices_.end();
290}
291
292template <typename MotionModelType, typename ObservationModelType>
295{
296 predict(input);
297 update(input);
298
299 return get_state();
300}
301
302template <typename MotionModelType, typename ObservationModelType>
304{
305
306 if (!motion_model_ptr_)
307 {
308 throw std::runtime_error("Motion model has not been set");
309 }
310
311 auto u = bitsim::utils::template resolve<DynVec<real_t>>("u", input);
312 auto w = bitsim::utils::template resolve<DynVec<real_t>>("w", input);
313
314 // make a state predicion using the
315 // motion model
316 auto &x = motion_model_ptr_->get_state();
317
318 // get the matrix that describes the dynamics
319 // of the system
320 auto &F = motion_model_ptr_->get_matrix("F");
321 auto &B = (*this)["B"];
322
323 // update the state vector
324 x = F * x + B * u + w;
325 // state.set(x);
326
327 // predict the covariance matrix
328 auto &P = (*this)["P"];
329 auto &Q = (*this)["Q"];
330 auto F_T = F.transpose();
331
332 P = (F * P * F_T) + Q;
333}
334
335template <typename MotionModelType, typename ObservationModelType>
337{
338
339 if (!motion_model_ptr_)
340 {
341 throw std::runtime_error("Motion model has not been set");
342 }
343
344 if (!observation_model_ptr_)
345 {
346 throw std::runtime_error("Observation model has not been set");
347 }
348
349 auto &x = motion_model_ptr_->get_state();
350 auto &P = (*this)["P"];
351 auto &R = (*this)["R"];
352
353 auto &H = observation_model_ptr_->get_matrix("H");
354 auto H_T = H.transpose();
355
356 try
357 {
358
359 auto S = H * P * H_T + R;
360 auto S_inv = S.inverse(); // inv(S);
361
362 // compute the gain matrix
363 if (has_matrix("K"))
364 {
365 auto &K = matrices_["K"];
366 K = P * H_T * S_inv;
367 }
368 else
369 {
370 auto K = P * H_T * S_inv;
371 set_matrix("K", K);
372 }
373
374 auto &K = (*this)["K"];
375 auto z = bitsim::utils::template resolve<DynVec<real_t>>("z", input);
376 auto innovation = z - H * x;
377
378 if (K.cols() != innovation.size())
379 {
380 throw std::runtime_error(
381 "Matrix columns: " + std::to_string(K.cols()) +
382 " not equal to vector size: " + std::to_string(innovation.size()));
383 }
384
385 x += K * innovation;
386 // state.set(x);
387
388 auto I = matrix_type::Identity(x.size(), x.size());
389
390 // update the error covariance matrix
391 P = (I - K * H) * P;
392 }
393 catch (...)
394 {
395
396 // this is a singular matrix what
397 // should we do? Simply use the predicted
398 // values and log the fact that there was a singular matrix
399
400 throw;
401 }
402}
403
404} // namespace estimation
405} // namespace bitsim
406
407#endif // BITSIM_ESTIMATION_KALMAN_FILTER_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Linear Kalman Filter implementation. See: An Introduction to the Kalman Filter, TR 95-041 by Greg Wel...
Definition kalman_filter.h:92
bool has_matrix(const std::string &name) const
Returns true if the matrix with the given name exists.
Definition kalman_filter.h:285
KalmanFilterConfig< MotionModelType, ObservationModelType > config_type
Definition kalman_filter.h:95
KalmanFilter & with_matrix(const std::string &name, const matrix_type &mat)
Set the matrix and return *this.
Definition kalman_filter.h:263
const state_type & get_state() const
Returns the state.
Definition kalman_filter.h:179
void set_motion_model(motion_model_type &motion_model)
Set the motion model.
Definition kalman_filter.h:153
std::map< std::string, std::any > input_type
Definition kalman_filter.h:101
const matrix_type & operator[](const std::string &name) const
Returns the name-th matrix.
Definition kalman_filter.h:233
typename config_type::motion_model_type::state_type state_type
Definition kalman_filter.h:100
MotionModelType motion_model_type
Definition kalman_filter.h:98
void update(const input_type &input)
Updates the gain matrix , the state vector and covariance matrix P using the given measurement acco...
Definition kalman_filter.h:336
void predict(const input_type &input)
Predicts the state vector x and the process covariance matrix P using the given input control u accro...
Definition kalman_filter.h:303
ObservationModelType observation_model_type
Definition kalman_filter.h:99
state_type & get_state()
Returns the state.
Definition kalman_filter.h:184
real_t get(const std::string &name) const
Returns the state property with the given name.
Definition kalman_filter.h:189
DynMat< real_t > matrix_type
Definition kalman_filter.h:96
state_type & estimate(const input_type &input)
Estimate the state. This function simply wraps the predict and update steps described by the function...
Definition kalman_filter.h:294
KalmanFilter(const KalmanFilterConfig< MotionModelType, ObservationModelType > &config)
KalmanFilter Constructor.
Definition kalman_filter.h:219
void set_matrix(const std::string &name, const matrix_type &mat)
Set the matrix used by the filter.
Definition kalman_filter.h:272
void set_observation_model(const observation_model_type &observation_model)
Set the observation model.
Definition kalman_filter.h:156
Definition diff_drive_ekf.cpp:12
Definition dc_motor_backend_base.h:9
Eigen::MatrixX< T > DynMat
Dynamically sized matrix to use around the library.
Definition bitsim_types.h:51
double real_t
real_t
Definition bitsim_types.h:20
Various utilities for std::map.
Configuration class for the Kalman filter.
Definition kalman_filter.h:24
DynMat< real_t > B
Definition kalman_filter.h:39
ObservationModelType observation_model_type
Definition kalman_filter.h:27
DynMat< real_t > R
Definition kalman_filter.h:43
DynMat< real_t > K
Definition kalman_filter.h:42
motion_model_type * motion_model
Pointer to the motion model.
Definition kalman_filter.h:32
MotionModelType motion_model_type
Definition kalman_filter.h:26
const observation_model_type * observation_model
Pointer to the observation model.
Definition kalman_filter.h:37
DynMat< real_t > Q
Definition kalman_filter.h:41
DynMat< real_t > P
Definition kalman_filter.h:40