1#ifndef BITSIM_ESTIMATION_EXTENDED_KALMAN_FILTER_H
2#define BITSIM_ESTIMATION_EXTENDED_KALMAN_FILTER_H
7#include <boost/noncopyable.hpp>
66template <
typename MotionModelTp,
typename ObservationModelTp =
void>
74 using state_type =
typename motion_model_type::state_type;
84 template <
typename O = ObservationModelTp>
103 template <
typename ObservationModel>
106 real_t gate = std::numeric_limits<real_t>::infinity());
150template <
typename Model>
155 } -> std::convertible_to<bool>;
158template <
typename Model>
165template <
typename MotionModelTp,
typename ObservationModelTp>
172template <
typename MotionModelTp,
typename ObservationModelTp>
180template <
typename MotionModelTp,
typename ObservationModelTp>
184 if (name !=
"Q" && name !=
"K" && name !=
"R" && name !=
"P")
186 throw std::logic_error(
"Invalid matrix name. Name: " + name +
" not in [Q, K, R, P]");
191template <
typename MotionModelTp,
typename ObservationModelTp>
200template <
typename MotionModelTp,
typename ObservationModelTp>
202 const std::string &name)
const
207template <
typename MotionModelTp,
typename ObservationModelTp>
214 throw std::invalid_argument(
"Matrix: " + name +
" does not exist");
219template <
typename MotionModelTp,
typename ObservationModelTp>
226 throw std::invalid_argument(
"Matrix: " + name +
" does not exist");
231template <
typename MotionModelTp,
typename ObservationModelTp>
238template <
typename MotionModelTp,
typename ObservationModelTp>
244 auto &P = (*this)[
"P"];
247 P = F * P * F.transpose() + L * Q * L.transpose();
250template <
typename MotionModelTp,
typename ObservationModelTp>
251template <
typename ObservationModel>
256 auto &P = (*this)[
"P"];
260 if (z.size() != zpred.size() || H.rows() != z.size() || H.cols() != P.rows())
262 throw std::runtime_error(
"ExtendedKalmanFilter::update: the measurement has " +
263 std::to_string(z.size()) +
" values, the model predicts " +
264 std::to_string(zpred.size()) +
" and H is " +
265 std::to_string(H.rows()) +
" x " + std::to_string(H.cols()));
271 if (model.has_matrix(
"M"))
274 MRM = M * R * M.transpose();
289 const auto S_ldlt = S.ldlt();
298 const matrix_type K = S_ldlt.solve(H * P).transpose();
300 state += dx.transpose();
302 const auto n = P.rows();
303 P = (matrix_type::Identity(n, n) - K * H) * P;
305 P = 0.5 * (P + P.transpose()).eval();
319template <
typename MotionModelTp,
typename ObservationModelTp>
323 if constexpr (std::is_void_v<ObservationModelTp>)
325 throw std::logic_error(
"ExtendedKalmanFilter::update(z) needs a filter made with an "
326 "observation model; use update(model, z, R)");
332 throw std::logic_error(
"ExtendedKalmanFilter::update(z): no observation model");
339template <
typename MotionModelTp,
typename ObservationModelTp>
344 update(std::get<1>(input));
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
The extended Kalman filter: it predicts the state with a non-linear motion model and corrects it with...
Definition extended_kalman_filter.h:68
ExtendedKalmanFilter(motion_model_type &motion_model)
A filter for the motion model, with the observation models given to update().
Definition extended_kalman_filter.h:166
std::map< std::string, matrix_type > matrices_
Definition extended_kalman_filter.h:145
const void * observation_model_ptr_
Definition extended_kalman_filter.h:144
typename motion_model_type::state_type state_type
Definition extended_kalman_filter.h:74
state_type & get_state()
Definition extended_kalman_filter.h:130
MotionModelTp motion_model_type
Definition extended_kalman_filter.h:70
typename motion_model_type::input_type motion_model_input_type
Definition extended_kalman_filter.h:72
bool has_matrix(const std::string &name) const
Definition extended_kalman_filter.h:201
ObservationModelTp observation_model_type
Definition extended_kalman_filter.h:71
typename motion_model_type::matrix_type matrix_type
Definition extended_kalman_filter.h:73
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").
Definition extended_kalman_filter.h:232
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 noi...
Definition extended_kalman_filter.h:181
real_t get(const std::string &name) const
Definition extended_kalman_filter.h:137
motion_model_type * motion_model_ptr_
Definition extended_kalman_filter.h:143
DynVec< real_t > state_vector() const
The state as a vector.
Definition extended_kalman_filter.h:135
const state_type & get_state() const
Definition extended_kalman_filter.h:129
const matrix_type & operator[](const std::string &name) const
Definition extended_kalman_filter.h:209
ExtendedKalmanFilter & with_matrix(const std::string &name, const matrix_type &mat)
Definition extended_kalman_filter.h:193
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....
Definition extended_kalman_filter.h:252
void estimate(const std::tuple< motion_model_input_type, DynVec< real_t > > &input)
predict(), then update(z)
Definition extended_kalman_filter.h:340
ExtendedKalmanFilter(motion_model_type &motion_model)
A filter for the motion model, with the observation models given to update().
Definition extended_kalman_filter.h:166
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.
Definition extended_kalman_filter.h:239
ExtendedKalmanFilter(motion_model_type &motion_model, const O &observation_model)
A filter with an observation model of its own, for update(z).
Definition extended_kalman_filter.h:174
bitsim::estimation::DiffDriveGyroModel motion_model_type
Definition extended_kalman_filter.h:70
typename motion_model_type::input_type motion_model_input_type
Definition extended_kalman_filter.h:72
bool has_matrix(const std::string &name) const
Definition extended_kalman_filter.h:201
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_m...
Definition extended_kalman_filter.h:321
typename motion_model_type::matrix_type matrix_type
Definition extended_kalman_filter.h:73
matrix_type & operator[](const std::string &name)
Definition extended_kalman_filter.h:221
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").
Definition extended_kalman_filter.h:232
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 noi...
Definition extended_kalman_filter.h:181
const matrix_type & operator[](const std::string &name) const
Definition extended_kalman_filter.h:209
ExtendedKalmanFilter & with_matrix(const std::string &name, const matrix_type &mat)
Definition extended_kalman_filter.h:193
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....
Definition extended_kalman_filter.h:252
void estimate(const std::tuple< motion_model_input_type, DynVec< real_t > > &input)
predict(), then update(z)
Definition extended_kalman_filter.h:340
Definition extended_kalman_filter.h:151
Definition extended_kalman_filter.h:159
Definition extended_kalman_filter.h:149
Definition diff_drive_ekf.cpp:12
Definition dc_motor_backend_base.h:9
double real_t
real_t
Definition bitsim_types.h:20
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101
Eigen::VectorX< T > ColVec
Column vector. Some maths operations are easier using column vectors rather than DynVec.
Definition bitsim_types.h:117
What an update of an ExtendedKalmanFilter did with a measurement.
Definition extended_kalman_filter.h:25
DynVec< real_t > innovation
z - h(x), or the model's residual of them
Definition extended_kalman_filter.h:28
bool accepted
false if the gate rejected the measurement
Definition extended_kalman_filter.h:26
real_t mahalanobis
the squared Mahalanobis distance of the innovation
Definition extended_kalman_filter.h:27