bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
gymnasium_part_1::KalmanFilter< MotionModelType, ObservationModelType > Class Template Reference

Linear Kalman Filter implementation. See: An Introduction to the Kalman Filter, TR 95-041 by Greg Welch1 and Gary Bishop https://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf. More...

#include <kalman_filter.h>

Inheritance diagram for gymnasium_part_1::KalmanFilter< MotionModelType, ObservationModelType >:
Collaboration diagram for gymnasium_part_1::KalmanFilter< MotionModelType, ObservationModelType >:

Public Types

using config_type = KalmanFilterConfig<MotionModelType, ObservationModelType>
using matrix_type = DynMat<real_t>
using motion_model_type = MotionModelType
using observation_model_type = ObservationModelType
using state_type = typename config_type::motion_model_type::state_type
using input_type = std::map<std::string, std::any>

Public Member Functions

 KalmanFilter (const KalmanFilterConfig< MotionModelType, ObservationModelType > &config)
 KalmanFilter Constructor.
state_type & estimate (const input_type &input)
 Estimate the state. This function simply wraps the predict and update steps described by the functions below.
void predict (const input_type &input)
 Predicts the state vector x and the process covariance matrix P using the given input control u accroding to the following equations.
void update (const input_type &input)
 Updates the gain matrix \(K\), the state vector \(x\) and covariance matrix P using the given measurement \(z_k\) according to the following equations.
void set_motion_model (motion_model_type &motion_model)
 Set the motion model.
void set_observation_model (const observation_model_type &observation_model)
 Set the observation model.
KalmanFilter & with_matrix (const std::string &name, const matrix_type &mat)
 Set the matrix and return *this.
void set_matrix (const std::string &name, const matrix_type &mat)
 Set the matrix used by the filter.
bool has_matrix (const std::string &name) const
 Returns true if the matrix with the given name exists.
const state_type & get_state () const
 Returns the state.
state_type & get_state ()
 Returns the state.
real_t get (const std::string &name) const
 Returns the state property with the given name.
const matrix_type & operator[] (const std::string &name) const
 Returns the name-th matrix.
matrix_type & operator[] (const std::string &name)
 Returns the name-th matrix.

Detailed Description

template<typename MotionModelType, typename ObservationModelType>
class gymnasium_part_1::KalmanFilter< MotionModelType, ObservationModelType >

Linear Kalman Filter implementation. See: An Introduction to the Kalman Filter, TR 95-041 by Greg Welch1 and Gary Bishop https://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf.

The algorithm is implemented as follows:

prediction step:

\[\hat{\mathbf{x}}_{k} = F_k \mathbf{x}_{k-1} + B_k \mathbf{u}_k + \mathbf{w}_k\]

\[\hat{P}_{k} = F_{k-1} P_{k-1} F_{k-1}^T + Q_{k-1}\]

update step:

\[K_k = \hat{P}_{k} H_{k}^T * (H_k \hat{P}_{k} * H_{k}^T + R_k )^{-1}\]

\[\mathbf{x}_k = \hat{\mathbf{x}}_{k} + K_k (z_k - h( \hat{x}_{k}, 0))\]

\[P_k = (I - K_k H_k) \hat{P}_{k}\]

where \(w_k\) and \(v_k\) represent process and measurement noise respectively. They are assumed independent and normally distributed:

\[p(w) \sim N(0,Q)\]

\[p(v) \sim N(0,R)\]

The gain matrix K says how much the predictions should be corrected The following matrices dimensions are assumed:

state vector: x n x 1 control vector: u l x 1 meas. vector: y m x 1

\[ \mathbf{F} n \times n \]

\[ \mathfb{P} n \times n \]

\[ \mathbf{B} n \times l \]

\[ \mathbf{H} m \times n \]

\[ \mathbf{K} n \times m \]

\[ \mathbf{Q} n \times n \]

\[ \mathbf{R} m \times m \]

Member Typedef Documentation

◆ config_type

template<typename MotionModelType, typename ObservationModelType>
using bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::config_type = KalmanFilterConfig<MotionModelType, ObservationModelType>

◆ input_type

template<typename MotionModelType, typename ObservationModelType>
using bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::input_type = std::map<std::string, std::any>

◆ matrix_type

template<typename MotionModelType, typename ObservationModelType>
using bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::matrix_type = DynMat<real_t>

◆ motion_model_type

template<typename MotionModelType, typename ObservationModelType>
using bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::motion_model_type = MotionModelType

◆ observation_model_type

template<typename MotionModelType, typename ObservationModelType>
using bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::observation_model_type = ObservationModelType

◆ state_type

template<typename MotionModelType, typename ObservationModelType>
using bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::state_type = typename config_type::motion_model_type::state_type

Constructor & Destructor Documentation

◆ KalmanFilter()

template<typename MotionModelType, typename ObservationModelType>
bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::KalmanFilter ( const KalmanFilterConfig< MotionModelType, ObservationModelType > & config)

KalmanFilter Constructor.

Member Function Documentation

◆ estimate()

template<typename MotionModelType, typename ObservationModelType>
KalmanFilter< MotionModelType, ObservationModelType >::state_type & bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::estimate ( const input_type & input)

Estimate the state. This function simply wraps the predict and update steps described by the functions below.

◆ get()

template<typename MotionModelType, typename ObservationModelType>
real_t bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::get ( const std::string & name) const
inline

Returns the state property with the given name.

◆ get_state() [1/2]

template<typename MotionModelType, typename ObservationModelType>
state_type & bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::get_state ( )
inline

Returns the state.

◆ get_state() [2/2]

template<typename MotionModelType, typename ObservationModelType>
const state_type & bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::get_state ( ) const
inline

Returns the state.

◆ has_matrix()

template<typename MotionModelType, typename ObservationModelType>
bool bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::has_matrix ( const std::string & name) const

Returns true if the matrix with the given name exists.

◆ operator[]() [1/2]

template<typename MotionModelType, typename ObservationModelType>
DynMat< real_t > & bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::operator[] ( const std::string & name)

Returns the name-th matrix.

◆ operator[]() [2/2]

template<typename MotionModelType, typename ObservationModelType>
const DynMat< real_t > & bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::operator[] ( const std::string & name) const

Returns the name-th matrix.

◆ predict()

template<typename MotionModelType, typename ObservationModelType>
void bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::predict ( const input_type & input)

Predicts the state vector x and the process covariance matrix P using the given input control u accroding to the following equations.

\[\hat{x}_{k = F_k* x_{k-1} + B_k * u_k + w_k\]

\[\hat{P}_{k} = F_{k-1} * P_{k-1} * F_{k-1}^T + Q_{k-1}\]

where \(x_{k-1}\) is the state at the previous step, \(u_{k}\) is the control signal and w_k is the error associated with the control signal. In input argument passed to the function is meant to model in a tuple all the arguments needed. F, is the dynamics matrix and Q is the covariance matrix associate with the control signal

The control input argument should supply both \(u_k\) and \(w_k\) vectors

◆ set_matrix()

template<typename MotionModelType, typename ObservationModelType>
void bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::set_matrix ( const std::string & name,
const matrix_type & mat )

Set the matrix used by the filter.

◆ set_motion_model()

template<typename MotionModelType, typename ObservationModelType>
void bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::set_motion_model ( motion_model_type & motion_model)
inline

Set the motion model.

◆ set_observation_model()

template<typename MotionModelType, typename ObservationModelType>
void bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::set_observation_model ( const observation_model_type & observation_model)
inline

Set the observation model.

◆ update()

template<typename MotionModelType, typename ObservationModelType>
void bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::update ( const input_type & input)

Updates the gain matrix \(K\), the state vector \(x\) and covariance matrix P using the given measurement \(z_k\) according to the following equations.

\[K_k = \hat{P}_{k} H_{k}^T (H_k \hat{P}_{k} H_{k}^T + R_k)^{-1} \]

\[x_k = \hat{x}_{k} + K_k (z_k - H \hat{x}_{k}) \]

\[P_k = (I - K_k H_k) \hat{P}_{k} \]

◆ with_matrix()

template<typename MotionModelType, typename ObservationModelType>
KalmanFilter< MotionModelType, ObservationModelType > & bitsim::estimation::KalmanFilter< MotionModelType, ObservationModelType >::with_matrix ( const std::string & name,
const matrix_type & mat )

Set the matrix and return *this.


The documentation for this class was generated from the following file: