mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
ukf.h
Go to the documentation of this file.
1// clang: MatousFormat
2#ifndef UKF_H
3#define UKF_H
4
12
13namespace 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
200#ifndef UKF_HPP
201#include <mrs_lib/impl/ukf.hpp>
202#endif
203
204#endif
This abstract class defines common interfaces and types for a generic Kalman filter.
Definition kalman_filter.h:26
Eigen::Matrix< double, p, 1 > z_t
Measurement vector type .
Definition kalman_filter.h:35
Eigen::Matrix< double, p, p > R_t
Measurement noise covariance matrix type .
Definition kalman_filter.h:38
Eigen::Matrix< double, n, 1 > x_t
State vector type .
Definition kalman_filter.h:33
Eigen::Matrix< double, n, n > P_t
State uncertainty covariance matrix type .
Definition kalman_filter.h:37
Eigen::Matrix< double, n, n > Q_t
Process noise covariance matrix type .
Definition kalman_filter.h:39
Eigen::Matrix< double, m, 1 > u_t
Input vector type .
Definition kalman_filter.h:34
Implementation of the Unscented Kalman filter .
Definition ukf.h:40
static constexpr int p
Length of the measurement vector of the system.
Definition ukf.h:45
typename Eigen::Matrix< double, p, w > Z_t
Measurement sigma points matrix.
Definition ukf.h:51
typename Base_class::R_t R_t
measurement covariance p*p typedef
Definition ukf.h:67
typename Eigen::Matrix< double, n, w > X_t
State sigma points matrix.
Definition ukf.h:50
void setObservationModel(const observation_model_t &observation_model)
Changes the observation model function.
Definition ukf.hpp:95
static constexpr int w
Number of sigma points/weights.
Definition ukf.h:46
static constexpr int n
Length of the state vector of the system.
Definition ukf.h:43
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:191
typename Base_class::x_t x_t
state vector n*1 typedef
Definition ukf.h:59
typename Base_class::statecov_t statecov_t
typedef of a helper struct for state and covariance
Definition ukf.h:73
void setConstants(const double alpha, const double kappa, const double beta)
Changes the Unscented Transform parameters.
Definition ukf.hpp:71
typename Base_class::u_t u_t
input vector m*1 typedef
Definition ukf.h:61
void setTransitionModel(const transition_model_t &transition_model)
Changes the transition model function.
Definition ukf.hpp:85
typename Base_class::z_t z_t
measurement vector p*1 typedef
Definition ukf.h:63
static constexpr int m
Length of the input vector of the system.
Definition ukf.h:44
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
typename Base_class::Q_t Q_t
process covariance n*n typedef
Definition ukf.h:69
typename Base_class::P_t P_t
state covariance n*n typedef
Definition ukf.h:65
typename std::function< z_t(const x_t &)> observation_model_t
function of the observation model typedef
Definition ukf.h:77
typename Eigen::Matrix< double, w, 1 > W_t
weights vector (2n+1)*1 typedef
Definition ukf.h:71
typename Eigen::Matrix< double, n, p > K_t
Kalman gain matrix.
Definition ukf.h:53
UKF()
Convenience default constructor.
Definition ukf.hpp:23
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:237
typename Eigen::Matrix< double, p, p > Pzz_t
Pzz helper matrix.
Definition ukf.h:52
Defines KalmanFilter - an abstract class, defining common interfaces and types for a generic Kalman f...
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Helper struct for passing around the state and its covariance in one variable.
Definition kalman_filter.h:47
is thrown when taking the inverse of a matrix fails during kalman gain calculation
Definition ukf.h:90
is thrown when taking the square root of a matrix fails during sigma generation
Definition ukf.h:81
Implements UKF - a class implementing the Unscented Kalman Filter.