mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
ukf.h
Go to the documentation of this file.
1 // clang: MatousFormat
2 #ifndef UKF_H
3 #define UKF_H
4 
11 #include <mrs_lib/kalman_filter.h>
12 
13 namespace mrs_lib
14 {
15 
38  template <int n_states, int n_inputs, int n_measurements>
39  class UKF : KalmanFilter<n_states, n_inputs, n_measurements>
40  {
41  protected:
42  /* protected UKF definitions (typedefs, constants etc) //{ */
43  static constexpr int n = n_states;
44  static constexpr int m = n_inputs;
45  static constexpr int p = n_measurements;
46  static constexpr int w = 2 * n + 1;
50  using X_t = typename Eigen::Matrix<double, n, w>;
51  using Z_t = typename Eigen::Matrix<double, p, w>;
52  using Pzz_t = typename Eigen::Matrix<double, p, p>;
53  using K_t = typename Eigen::Matrix<double, n, p>;
54  //}
55 
56  public:
57  /* public UKF definitions (typedefs, constants etc) //{ */
59  using x_t = typename Base_class::x_t;
61  using u_t = typename Base_class::u_t;
63  using z_t = typename Base_class::z_t;
65  using P_t = typename Base_class::P_t;
67  using R_t = typename Base_class::R_t;
69  using Q_t = typename Base_class::Q_t;
71  using W_t = typename Eigen::Matrix<double, w, 1>;
75  using transition_model_t = typename std::function<x_t(const x_t&, const u_t&, double)>;
77  using observation_model_t = typename std::function<z_t(const x_t&)>;
78 
80  struct square_root_exception : public std::exception
81  {
82  const char* what() const throw()
83  {
84  return "UKF: taking the square root of covariance update produced NANs!!!";
85  }
86  };
87 
89  struct inverse_exception : public std::exception
90  {
91  const char* what() const throw()
92  {
93  return "UKF: inverting of Pzz in correction update produced NANs!!!";
94  }
95  };
96  //}
97 
98  public:
99  /* UKF constructor //{ */
106  UKF();
107 
117  UKF(const transition_model_t& transition_model, const observation_model_t& observation_model, const double alpha = 1e-3, const double kappa = 1, const double beta = 2);
118  //}
119 
120  /* correct() method //{ */
129  virtual statecov_t correct(const statecov_t& sc, const z_t& z, const R_t& R) const override;
130  //}
131 
132  /* predict() method //{ */
142  virtual statecov_t predict(const statecov_t& sc, const u_t& u, const Q_t& Q, double dt) const override;
143  //}
144 
145  /* setConstants() method //{ */
153  void setConstants(const double alpha, const double kappa, const double beta);
154  //}
155 
156  /* setTransitionModel() method //{ */
162  void setTransitionModel(const transition_model_t& transition_model);
163  //}
164 
165  /* setObservationModel() method //{ */
171  void setObservationModel(const observation_model_t& observation_model);
172  //}
173 
174  protected:
175  /* protected methods and member variables //{ */
176 
177  void computeWeights();
178 
179  X_t computeSigmas(const x_t& x, const P_t& P) const;
180 
181  P_t computePaSqrt(const P_t& P) const;
182 
183  Pzz_t computeInverse(const Pzz_t& Pzz) const;
184 
185  virtual K_t computeKalmanGain([[maybe_unused]] const x_t& x, [[maybe_unused]] const z_t& inn, const K_t& Pxz, const Pzz_t& Pzz) const;
186 
187  double m_alpha, m_kappa, m_beta, m_lambda;
188  W_t m_Wm;
189  W_t m_Wc;
190 
191  transition_model_t m_transition_model;
192  observation_model_t m_observation_model;
193 
194  //}
195  };
196 
197 } // namespace mrs_lib
198 
199 #include <mrs_lib/impl/ukf.hpp>
200 
201 #endif
mrs_lib::UKF::X_t
typename Eigen::Matrix< double, n, w > X_t
State sigma points matrix.
Definition: ukf.h:50
mrs_lib::UKF::square_root_exception
is thrown when taking the square root of a matrix fails during sigma generation
Definition: ukf.h:80
mrs_lib::UKF::setConstants
void setConstants(const double alpha, const double kappa, const double beta)
Changes the Unscented Transform parameters.
Definition: ukf.hpp:61
mrs_lib::UKF::UKF
UKF()
Convenience default constructor.
Definition: ukf.hpp:20
mrs_lib::UKF::z_t
typename Base_class::z_t z_t
measurement vector p*1 typedef
Definition: ukf.h:63
mrs_lib::UKF::n
static constexpr int n
Length of the state vector of the system.
Definition: ukf.h:43
mrs_lib::KalmanFilter::R_t
Eigen::Matrix< double, p, p > R_t
Measurement noise covariance matrix type .
Definition: kalman_filter.h:39
mrs_lib::UKF::K_t
typename Eigen::Matrix< double, n, p > K_t
Kalman gain matrix.
Definition: ukf.h:53
ukf.hpp
Implements UKF - a class implementing the Unscented Kalman Filter.
mrs_lib::UKF::p
static constexpr int p
Length of the measurement vector of the system.
Definition: ukf.h:45
mrs_lib::UKF::transition_model_t
typename std::function< x_t(const x_t &, const u_t &, double)> transition_model_t
function of the state transition model typedef
Definition: ukf.h:75
mrs_lib::UKF::correct
virtual statecov_t correct(const statecov_t &sc, const z_t &z, const R_t &R) const override
Implements the state correction step (measurement update).
Definition: ukf.hpp:223
mrs_lib::KalmanFilter::P_t
Eigen::Matrix< double, n, n > P_t
State uncertainty covariance matrix type .
Definition: kalman_filter.h:38
mrs_lib::UKF::statecov_t
typename Base_class::statecov_t statecov_t
typedef of a helper struct for state and covariance
Definition: ukf.h:73
mrs_lib::UKF::W_t
typename Eigen::Matrix< double, w, 1 > W_t
weights vector (2n+1)*1 typedef
Definition: ukf.h:71
mrs_lib::KalmanFilter::u_t
Eigen::Matrix< double, m, 1 > u_t
Input vector type .
Definition: kalman_filter.h:35
mrs_lib::UKF::w
static constexpr int w
Number of sigma points/weights.
Definition: ukf.h:46
mrs_lib::UKF::observation_model_t
typename std::function< z_t(const x_t &)> observation_model_t
function of the observation model typedef
Definition: ukf.h:77
mrs_lib::UKF::setObservationModel
void setObservationModel(const observation_model_t &observation_model)
Changes the observation model function.
Definition: ukf.hpp:85
mrs_lib::KalmanFilter::x_t
Eigen::Matrix< double, n, 1 > x_t
State vector type .
Definition: kalman_filter.h:34
mrs_lib
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition: attitude_converter.h:29
mrs_lib::UKF::setTransitionModel
void setTransitionModel(const transition_model_t &transition_model)
Changes the transition model function.
Definition: ukf.hpp:75
kalman_filter.h
Defines KalmanFilter - an abstract class, defining common interfaces and types for a generic Kalman f...
mrs_lib::UKF::Pzz_t
typename Eigen::Matrix< double, p, p > Pzz_t
Pzz helper matrix.
Definition: ukf.h:52
mrs_lib::UKF::m
static constexpr int m
Length of the input vector of the system.
Definition: ukf.h:44
mrs_lib::UKF::predict
virtual statecov_t predict(const statecov_t &sc, const u_t &u, const Q_t &Q, double dt) const override
Implements the state prediction step (time update).
Definition: ukf.hpp:178
mrs_lib::UKF::x_t
typename Base_class::x_t x_t
state vector n*1 typedef
Definition: ukf.h:59
mrs_lib::KalmanFilter::Q_t
Eigen::Matrix< double, n, n > Q_t
Process noise covariance matrix type .
Definition: kalman_filter.h:40
mrs_lib::KalmanFilter::z_t
Eigen::Matrix< double, p, 1 > z_t
Measurement vector type .
Definition: kalman_filter.h:36
mrs_lib::UKF::Z_t
typename Eigen::Matrix< double, p, w > Z_t
Measurement sigma points matrix.
Definition: ukf.h:51
mrs_lib::KalmanFilter::statecov_t
Helper struct for passing around the state and its covariance in one variable.
Definition: kalman_filter.h:47
mrs_lib::UKF
Implementation of the Unscented Kalman filter .
Definition: ukf.h:39
mrs_lib::UKF::inverse_exception
is thrown when taking the inverse of a matrix fails during kalman gain calculation
Definition: ukf.h:89
mrs_lib::KalmanFilter
This abstract class defines common interfaces and types for a generic Kalman filter.
Definition: kalman_filter.h:26