bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp > Class Template Reference

The extended Kalman filter: it predicts the state with a non-linear motion model and corrects it with measurements from any number of observation models, each linearised about the current estimate. More...

#include <extended_kalman_filter.h>

Inheritance diagram for bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >:
Collaboration diagram for bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >:

Public Types

using motion_model_type = MotionModelTp
using observation_model_type = ObservationModelTp
using motion_model_input_type = typename motion_model_type::input_type
using matrix_type = typename motion_model_type::matrix_type
using state_type = typename motion_model_type::state_type

Public Member Functions

 ExtendedKalmanFilter (motion_model_type &motion_model)
 A filter for the motion model, with the observation models given to update().
template<typename O = ObservationModelTp>
 ExtendedKalmanFilter (motion_model_type &motion_model, const O &observation_model)
 A filter with an observation model of its own, for update(z).
void predict (const motion_model_input_type &input)
 Predict the state one step with the input, and its covariance with the Q set by set_matrix("Q").
void predict (const motion_model_input_type &input, const matrix_type &Q)
 Predict with the process noise covariance Q of this step, for noise that depends on the input.
template<typename ObservationModel>
EKFUpdateResult update (const ObservationModel &model, const DynVec< real_t > &z, const matrix_type &R, real_t gate=std::numeric_limits< real_t >::infinity())
 Correct the state with the measurement z of the observation model, of noise covariance R. A measurement whose squared Mahalanobis distance is above gate is not used.
EKFUpdateResult update (const DynVec< real_t > &z)
 Correct the state with the measurement z of the filter's own observation model and the R set by set_matrix("R").
void estimate (const std::tuple< motion_model_input_type, DynVec< real_t > > &input)
 predict(), then update(z)
void set_matrix (const std::string &name, const matrix_type &mat)
 Set P, the covariance of the state, Q, of the process noise, or R, of the own observation model's noise.
ExtendedKalmanFilter & with_matrix (const std::string &name, const matrix_type &mat)
bool has_matrix (const std::string &name) const
const matrix_type & operator[] (const std::string &name) const
matrix_type & operator[] (const std::string &name)
const state_type & get_state () const
state_type & get_state ()
DynVec< real_t > state_vector () const
 The state as a vector.
real_t get (const std::string &name) const

Protected Attributes

motion_model_type * motion_model_ptr_
const void * observation_model_ptr_ {nullptr}
std::map< std::string, matrix_type > matrices_

Detailed Description

template<typename MotionModelTp, typename ObservationModelTp = void>
class bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >

The extended Kalman filter: it predicts the state with a non-linear motion model and corrects it with measurements from any number of observation models, each linearised about the current estimate.

The motion model keeps the state. It has

  • evaluate(input), which moves the state by one step, and
  • get_matrix("F") and get_matrix("L"), the Jacobians of that step with respect to the state and to the process noise, at the state before the step.

predict() then propagates the covariance, \(P \leftarrow F P F^T + L Q L^T\).

An observation model says what a sensor should measure in a state. It has

  • evaluate(x), the predicted measurement \(h(x)\) for the state as a vector, and
  • get_matrix("H"), the Jacobian of h with respect to the state, valid after evaluate;
  • optionally has_matrix(name): if it says it has "M", get_matrix("M") is the Jacobian with respect to the measurement noise; otherwise that is the identity;
  • optionally residual(z, zpred), for measurements such as angles whose difference needs wrapping.

update(model, z, R, gate) corrects the state with a measurement z of noise covariance R:

\[S = H P H^T + M R M^T, \quad K = P H^T S^{-1}, \quad x \leftarrow x + K (z - h(x)), \quad P \leftarrow (I - K H) P \]

unless the squared Mahalanobis distance of the innovation, \(\nu^T S^{-1} \nu\), is above the gate: then it leaves the state alone, which keeps an outlier, a sonar echo off something the map does not have, from pulling the estimate away. Each sensor can have its own model, noise and gate, and be used whenever it has a reading.

The filter can also hold one observation model with its R, as set_matrix("R"), for update(z).

Member Typedef Documentation

◆ matrix_type

template<typename MotionModelTp, typename ObservationModelTp = void>
using bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::matrix_type = typename motion_model_type::matrix_type

◆ motion_model_input_type

template<typename MotionModelTp, typename ObservationModelTp = void>
using bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::motion_model_input_type = typename motion_model_type::input_type

◆ motion_model_type

template<typename MotionModelTp, typename ObservationModelTp = void>
using bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::motion_model_type = MotionModelTp

◆ observation_model_type

template<typename MotionModelTp, typename ObservationModelTp = void>
using bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::observation_model_type = ObservationModelTp

◆ state_type

template<typename MotionModelTp, typename ObservationModelTp = void>
using bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::state_type = typename motion_model_type::state_type

Constructor & Destructor Documentation

◆ ExtendedKalmanFilter() [1/2]

template<typename MotionModelTp, typename ObservationModelTp>
bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::ExtendedKalmanFilter ( motion_model_type & motion_model)
explicit

A filter for the motion model, with the observation models given to update().

◆ ExtendedKalmanFilter() [2/2]

template<typename MotionModelTp, typename ObservationModelTp>
template<typename O>
bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::ExtendedKalmanFilter ( motion_model_type & motion_model,
const O & observation_model )

A filter with an observation model of its own, for update(z).

Member Function Documentation

◆ estimate()

template<typename MotionModelTp, typename ObservationModelTp>
void bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::estimate ( const std::tuple< motion_model_input_type, DynVec< real_t > > & input)

predict(), then update(z)

◆ get()

template<typename MotionModelTp, typename ObservationModelTp = void>
real_t bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::get ( const std::string & name) const
inline

◆ get_state() [1/2]

template<typename MotionModelTp, typename ObservationModelTp = void>
state_type & bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::get_state ( )
inline

◆ get_state() [2/2]

template<typename MotionModelTp, typename ObservationModelTp = void>
const state_type & bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::get_state ( ) const
inline

◆ has_matrix()

template<typename MotionModelTp, typename ObservationModelTp>
bool bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::has_matrix ( const std::string & name) const

◆ operator[]() [1/2]

template<typename MotionModelTp, typename ObservationModelTp>
ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::matrix_type & bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::operator[] ( const std::string & name)

◆ operator[]() [2/2]

template<typename MotionModelTp, typename ObservationModelTp>
const ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::matrix_type & bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::operator[] ( const std::string & name) const

◆ predict() [1/2]

template<typename MotionModelTp, typename ObservationModelTp>
void bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::predict ( const motion_model_input_type & input)

Predict the state one step with the input, and its covariance with the Q set by set_matrix("Q").

◆ predict() [2/2]

template<typename MotionModelTp, typename ObservationModelTp>
void bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::predict ( const motion_model_input_type & input,
const matrix_type & Q )

Predict with the process noise covariance Q of this step, for noise that depends on the input.

◆ set_matrix()

template<typename MotionModelTp, typename ObservationModelTp>
void bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::set_matrix ( const std::string & name,
const matrix_type & mat )

Set P, the covariance of the state, Q, of the process noise, or R, of the own observation model's noise.

◆ state_vector()

template<typename MotionModelTp, typename ObservationModelTp = void>
DynVec< real_t > bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::state_vector ( ) const
inline

The state as a vector.

◆ update() [1/2]

template<typename MotionModelTp, typename ObservationModelTp>
EKFUpdateResult bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::update ( const DynVec< real_t > & z)

Correct the state with the measurement z of the filter's own observation model and the R set by set_matrix("R").

◆ update() [2/2]

template<typename MotionModelTp, typename ObservationModelTp>
template<typename ObservationModel>
EKFUpdateResult bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::update ( const ObservationModel & model,
const DynVec< real_t > & z,
const matrix_type & R,
real_t gate = std::numeric_limits<real_t>::infinity() )

Correct the state with the measurement z of the observation model, of noise covariance R. A measurement whose squared Mahalanobis distance is above gate is not used.

◆ with_matrix()

template<typename MotionModelTp, typename ObservationModelTp>
ExtendedKalmanFilter< MotionModelTp, ObservationModelTp > & bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::with_matrix ( const std::string & name,
const matrix_type & mat )

Member Data Documentation

◆ matrices_

template<typename MotionModelTp, typename ObservationModelTp = void>
std::map<std::string, matrix_type> bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::matrices_
protected

◆ motion_model_ptr_

template<typename MotionModelTp, typename ObservationModelTp = void>
motion_model_type* bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::motion_model_ptr_
protected

◆ observation_model_ptr_

template<typename MotionModelTp, typename ObservationModelTp = void>
const void* bitsim::estimation::ExtendedKalmanFilter< MotionModelTp, ObservationModelTp >::observation_model_ptr_ {nullptr}
protected

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