4#include <boost/noncopyable.hpp>
20template<
typename MotionModelType,
typename ObservationModelType>
89template<
typename MotionModelType,
typename ObservationModelType>
100 typedef typename config_type::motion_model_type::state_type
state_type;
148 {motion_model_ptr_ = &motion_model;}
152 {observation_model_ptr_ = &observation_model;}
167 bool has_matrix(
const std::string& name)
const;
182 real_t get(
const std::string& name)
const{
return motion_model_ptr_->get(name);}
209 std::map<std::string, matrix_type> matrices_;
213template<
typename MotionModelType,
typename ObservationModelType>
214KalmanFilter<MotionModelType,
217 motion_model_ptr_(config.motion_model),
218 observation_model_ptr_(config.observation_model)
229template<
typename MotionModelType,
typename ObservationModelType>
233 auto itr = matrices_.find(name);
235 if(itr == matrices_.end()){
236 throw std::invalid_argument(
"Matrix: "+name+
" does not exist");
242template<
typename MotionModelType,
typename ObservationModelType>
246 auto itr = matrices_.find(name);
248 if(itr == matrices_.end()){
249 throw std::invalid_argument(
"Matrix: "+name+
" does not exist");
255template<
typename MotionModelType,
typename ObservationModelType>
258 ObservationModelType>::with_matrix(
const std::string& name,
const matrix_type& mat){
260 set_matrix(name, mat);
264template<
typename MotionModelType,
typename ObservationModelType>
267 ObservationModelType>::set_matrix(
const std::string& name,
275 throw std::logic_error(
"Invalid matrix name. Name: "+
277 " not in [Q, K, R, P, B]");
280 matrices_.insert_or_assign(name, mat);
283template<
typename MotionModelType,
typename ObservationModelType>
287 auto itr = matrices_.find(name);
288 return itr != matrices_.end();
291template<
typename MotionModelType,
typename ObservationModelType>
301template<
typename MotionModelType,
typename ObservationModelType>
306 if(!motion_model_ptr_){
307 throw std::runtime_error(
"Motion model has not been set");
315 auto& x = motion_model_ptr_->get_state();
319 auto& F = motion_model_ptr_->get_matrix(
"F");
320 auto& B = (*this)[
"B"];
327 auto& P = (*this)[
"P"];
328 auto& Q = (*this)[
"Q"];
329 auto F_T = F.transpose();
334template<
typename MotionModelType,
typename ObservationModelType>
339 if(!motion_model_ptr_){
340 throw std::runtime_error(
"Motion model has not been set");
343 if(!observation_model_ptr_){
344 throw std::runtime_error(
"Observation model has not been set");
347 auto& x = motion_model_ptr_->get_state();
348 auto& P = (*this)[
"P"];
349 auto& R = (*this)[
"R"];
351 auto& H = observation_model_ptr_->get_matrix(
"H");
352 auto H_T = H.transpose();
356 auto S = H*P*H_T + R;
357 auto S_inv = S.inverse();
361 auto& K = matrices_[
"K"];
365 auto K = P*H_T*S_inv;
369 auto& K = (*this)[
"K"];
371 auto innovation = z - H*x;
373 if(K.cols() != innovation.size()){
374 throw std::runtime_error(
"Matrix columns: "+
375 std::to_string(K.cols())+
376 " not equal to vector size: "+
377 std::to_string(innovation.size()));
383 auto I = matrix_type::Identity(x.size(), x.size());
Linear Kalman Filter implementation. See: An Introduction to the Kalman Filter, TR 95-041 by Greg Wel...
Definition kalman_filter.h:91
MotionModelType motion_model_type
Definition kalman_filter.h:98
void set_motion_model(motion_model_type &motion_model)
Set the motion model.
Definition kalman_filter.h:147
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
KalmanFilterConfig< MotionModelType, ObservationModelType > config_type
Definition kalman_filter.h:95
std::map< std::string, std::any > input_type
Definition kalman_filter.h:101
KalmanFilter & with_matrix(const std::string &name, const matrix_type &mat)
Set the matrix and return *this.
Definition kalman_filter.h:258
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:304
void set_matrix(const std::string &name, const matrix_type &mat)
Set the matrix used by the filter.
Definition kalman_filter.h:267
state_type & get_state()
Returns the state.
Definition kalman_filter.h:177
const state_type & get_state() const
Returns the state.
Definition kalman_filter.h:172
bool has_matrix(const std::string &name) const
Returns true if the matrix with the given name exists.
Definition kalman_filter.h:285
const matrix_type & operator[](const std::string &name) const
Returns the name-th matrix.
Definition kalman_filter.h:231
DynMat< real_t > matrix_type
Definition kalman_filter.h:96
ObservationModelType observation_model_type
Definition kalman_filter.h:99
void update(const input_type &input)
Updates the gain matrix , the state vector and covariance matrix P using the given measurement z_k a...
Definition kalman_filter.h:337
real_t get(const std::string &name) const
Returns the state property with the given name.
Definition kalman_filter.h:182
config_type::motion_model_type::state_type state_type
Definition kalman_filter.h:100
void set_observation_model(const observation_model_type &observation_model)
Set the observation model.
Definition kalman_filter.h:151
OutT resolve(const std::string &name, const std::map< std::string, std::any > &input)
Definition std_map_utils.h:25
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
Configuration class for the Kalman filter.
Definition kalman_filter.h:22
DynMat< real_t > R
Definition kalman_filter.h:42
const observation_model_type * observation_model
Pointer to the observation model.
Definition kalman_filter.h:36
DynMat< real_t > B
Definition kalman_filter.h:38
ObservationModelType observation_model_type
Definition kalman_filter.h:26
DynMat< real_t > P
Definition kalman_filter.h:39
MotionModelType motion_model_type
Definition kalman_filter.h:25
DynMat< real_t > Q
Definition kalman_filter.h:40
DynMat< real_t > K
Definition kalman_filter.h:41
motion_model_type * motion_model
Pointer to the motion model.
Definition kalman_filter.h:31