1#ifndef EXTENDED_KALMAN_FILTER_H
2#define EXTENDED_KALMAN_FILTER_H
6#include <boost/noncopyable.hpp>
42template<
typename MotionModelTp,
typename ObservationModelTp>
51 typedef typename motion_model_type::state_type
state_type;
108 bool has_matrix(
const std::string& name)
const;
143template<
typename MotionModelTp,
typename ObservationModelTp>
146 motion_model_ptr_(nullptr),
147 observation_model_ptr_(nullptr)
150template<
typename MotionModelTp,
typename ObservationModelTp>
155 motion_model_ptr_(&motion_model),
156 observation_model_ptr_(&observation_model)
159template<
typename MotionModelTp,
typename ObservationModelTp>
161 ObservationModelTp>::~ExtendedKalmanFilter()
164template<
typename MotionModelTp,
typename ObservationModelTp>
167 ObservationModelTp>::set_matrix(
const std::string& name,
170 if(name !=
"Q" && name !=
"K" && name !=
"R" && name !=
"P"){
171 throw std::logic_error(
"Invalid matrix name. Name: "+
173 " not in [Q, K, R, P]");
176 matrices_.insert_or_assign(name, mat);
179template<
typename MotionModelTp,
typename ObservationModelTp>
183 set_matrix(name, mat);
187template<
typename MotionModelTp,
typename ObservationModelTp>
191 auto itr = matrices_.find(name);
192 return itr != matrices_.end();
195template<
typename MotionModelTp,
typename ObservationModelTp>
199 auto itr = matrices_.find(name);
201 if(itr == matrices_.end()){
202 throw std::invalid_argument(
"Matrix: "+name+
" does not exist");
208template<
typename MotionModelTp,
typename ObservationModelTp>
212 auto itr = matrices_.find(name);
214 if(itr == matrices_.end()){
215 throw std::invalid_argument(
"Matrix: "+name+
" does not exist");
222template<
typename MotionModelTp,
typename ObservationModelTp>
228 predict(input.template get<0>());
229 update(input.template get<1>());
232template<
typename MotionModelTp,
typename ObservationModelTp>
238 motion_model_ptr_->evaluate(u);
240 auto& P = (*this)[
"P"];
241 auto& Q = (*this)[
"Q"];
243 auto& L = motion_model_ptr_->get_matrix(
"L");
244 auto L_T = L.transpose();
246 auto& F = motion_model_ptr_->get_matrix(
"F");
247 auto F_T = F.transpose();
249 P = F * P * F_T + L*Q*L_T;
252template<
typename MotionModelTp,
typename ObservationModelTp>
257 auto& state = motion_model_ptr_->get_state();
258 auto& P = (*this)[
"P"];
259 auto& R = (*this)[
"R"];
261 auto zpred = observation_model_ptr_->evaluate(z);
263 auto& H = observation_model_ptr_->get_matrix(
"H");
264 auto H_T = H.transpose();
268 auto& M = observation_model_ptr_->get_matrix(
"M");
269 auto M_T = M.transpose();
274 auto S = H*P*H_T + M*R*M_T;
276 auto S_inv = S.inverse();
279 auto& K = (*this)[
"K"];
283 auto K = P*H_T*S_inv;
287 auto& K = (*this)[
"K"];
289 auto innovation = z - zpred;
292 auto innovation_t = innovation.transpose();
294 if(K.cols() != innovation_t.rows()){
295 throw std::runtime_error(
"Matrix columns: "+
296 std::to_string(K.cols())+
297 " not equal to vector size: "+
298 std::to_string(innovation_t.rows()));
302 state += K * innovation_t;
305 auto I = matrix_type::Identity(state.size(), state.size());
Definition extended_kalman_filter.h:44
state_type & get_state()
Returns the state.
Definition extended_kalman_filter.h:114
ExtendedKalmanFilter()
Constructor.
Definition extended_kalman_filter.h:144
bool has_matrix(const std::string &name) const
Returns true if the matrix with the given name exists.
Definition extended_kalman_filter.h:189
motion_model_type::matrix_type matrix_type
Definition extended_kalman_filter.h:50
motion_model_type::state_type state_type
Definition extended_kalman_filter.h:51
void set_observation_model(const observation_model_type &observation_model)
Set the observation model.
Definition extended_kalman_filter.h:101
void update(const observation_model_input_type &z)
Updates the gain matrix K, the state vector x and covariance matrix P using the given measurement z_k...
Definition extended_kalman_filter.h:255
real_t get(const std::string &name) const
Returns the state property with the given name.
Definition extended_kalman_filter.h:117
ExtendedKalmanFilter & with_matrix(const std::string &name, const matrix_type &mat)
Set the matrix and return *this.
Definition extended_kalman_filter.h:181
MotionModelTp motion_model_type
Definition extended_kalman_filter.h:47
motion_model_type * motion_model_ptr_
pointer to the function that computes f
Definition extended_kalman_filter.h:133
const state_type & get_state() const
Returns the state.
Definition extended_kalman_filter.h:111
void set_matrix(const std::string &name, const matrix_type &mat)
Set the matrix used by the filter.
Definition extended_kalman_filter.h:167
ObservationModelTp observation_model_type
Definition extended_kalman_filter.h:48
const observation_model_type * observation_model_ptr_
pointer to the function that computes h
Definition extended_kalman_filter.h:136
~ExtendedKalmanFilter()
Destructor.
Definition extended_kalman_filter.h:161
void estimate(const std::tuple< motion_model_input_type, observation_model_input_type > &input)
Estimate the state. This function simply wraps the predict and update steps described by the function...
Definition extended_kalman_filter.h:225
observation_model_type::input_type observation_model_input_type
Definition extended_kalman_filter.h:52
void set_motion_model(motion_model_type &motion_model)
Set the motion model.
Definition extended_kalman_filter.h:97
const DynMat< real_t > & operator[](const std::string &name) const
Returns the name-th matrix.
Definition extended_kalman_filter.h:197
std::map< std::string, matrix_type > matrices_
Matrices used by the filter internally.
Definition extended_kalman_filter.h:139
void predict(const motion_model_input_type &input)
Predicts the state vector x and the process covariance matrix P using the given input control u accro...
Definition extended_kalman_filter.h:234
motion_model_type::input_type motion_model_input_type
Definition extended_kalman_filter.h:49
Definition bitrl_consts.h:14
double real_t
real_t
Definition bitrl_types.h:23
Eigen::MatrixX< T > DynMat
Dynamically sized matrix to use around the library.
Definition bitrl_types.h:49