1#ifndef BITSIM_ESTIMATION_KALMAN_FILTER_H
2#define BITSIM_ESTIMATION_KALMAN_FILTER_H
7#include <boost/noncopyable.hpp>
90template <
typename MotionModelType,
typename ObservationModelType>
100 using state_type =
typename config_type::motion_model_type::state_type;
158 observation_model_ptr_ = &observation_model;
174 bool has_matrix(
const std::string &name)
const;
189 real_t get(
const std::string &name)
const {
return motion_model_ptr_->get(name); }
215 std::map<std::string, matrix_type> matrices_;
218template <
typename MotionModelType,
typename ObservationModelType>
221 : motion_model_ptr_(config.motion_model), observation_model_ptr_(config.observation_model)
231template <
typename MotionModelType,
typename ObservationModelType>
236 auto itr = matrices_.find(name);
238 if (itr == matrices_.end())
240 throw std::invalid_argument(
"Matrix: " + name +
" does not exist");
246template <
typename MotionModelType,
typename ObservationModelType>
251 auto itr = matrices_.find(name);
253 if (itr == matrices_.end())
255 throw std::invalid_argument(
"Matrix: " + name +
" does not exist");
261template <
typename MotionModelType,
typename ObservationModelType>
271template <
typename MotionModelType,
typename ObservationModelType>
276 if (name !=
"Q" && name !=
"K" && name !=
"R" && name !=
"P" && name !=
"B")
278 throw std::logic_error(
"Invalid matrix name. Name: " + name +
" not in [Q, K, R, P, B]");
281 matrices_.insert_or_assign(name, mat);
284template <
typename MotionModelType,
typename ObservationModelType>
288 auto itr = matrices_.find(name);
289 return itr != matrices_.end();
292template <
typename MotionModelType,
typename ObservationModelType>
302template <
typename MotionModelType,
typename ObservationModelType>
306 if (!motion_model_ptr_)
308 throw std::runtime_error(
"Motion model has not been set");
311 auto u = bitsim::utils::template resolve<DynVec<real_t>>(
"u", input);
312 auto w = bitsim::utils::template resolve<DynVec<real_t>>(
"w", input);
316 auto &x = motion_model_ptr_->get_state();
320 auto &F = motion_model_ptr_->get_matrix(
"F");
321 auto &B = (*this)[
"B"];
324 x = F * x + B * u + w;
328 auto &P = (*this)[
"P"];
329 auto &Q = (*this)[
"Q"];
330 auto F_T = F.transpose();
332 P = (F * P * F_T) + Q;
335template <
typename MotionModelType,
typename ObservationModelType>
339 if (!motion_model_ptr_)
341 throw std::runtime_error(
"Motion model has not been set");
344 if (!observation_model_ptr_)
346 throw std::runtime_error(
"Observation model has not been set");
349 auto &x = motion_model_ptr_->get_state();
350 auto &P = (*this)[
"P"];
351 auto &R = (*this)[
"R"];
353 auto &H = observation_model_ptr_->get_matrix(
"H");
354 auto H_T = H.transpose();
359 auto S = H * P * H_T + R;
360 auto S_inv = S.inverse();
365 auto &K = matrices_[
"K"];
370 auto K = P * H_T * S_inv;
374 auto &K = (*this)[
"K"];
375 auto z = bitsim::utils::template resolve<DynVec<real_t>>(
"z", input);
376 auto innovation = z - H * x;
378 if (K.cols() != innovation.size())
380 throw std::runtime_error(
381 "Matrix columns: " + std::to_string(K.cols()) +
382 " not equal to vector size: " + std::to_string(innovation.size()));
388 auto I = matrix_type::Identity(x.size(), x.size());
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