|
mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
|
6 #ifndef NCKFSYSTEMMODELS_H
7 #define NCKFSYSTEMMODELS_H
9 #include <mrs_lib/lkf.h>
27 template <
int n_states,
int n_inputs,
int n_measurements>
28 class NCLKF :
public LKF<n_states, n_inputs, n_measurements>
32 static const int n = n_states;
33 static const int m = n_inputs;
34 static const int p = n_measurements;
75 virtual K_t computeKalmanGain(
const statecov_t& sc,
const z_t& z,
const R_t& R,
const H_t&
H)
const override
77 const R_t W =
H * sc.P *
H.transpose() + R;
78 const R_t W_inv = Base_class::invert_W(W);
79 const K_t K_orig = sc.P *
H.transpose() * W_inv;
81 const z_t inn = z - (
H * sc.x);
82 const x_t x = sc.x + K_orig * inn;
83 const double inn_scale = inn.transpose() * W_inv * inn;
85 const double x_norm = x.norm();
86 const K_t K = K_orig + (l/x_norm - 1.0) * x * (inn.transpose() * W_inv) / inn_scale;
111 template <
int n_states,
int n_inputs,
int n_measurements,
int n_norm_constrained_states>
116 static const int n = n_states;
117 static const int m = n_inputs;
118 static const int p = n_measurements;
119 static const int nq = n_norm_constrained_states;
136 using xq_t = Eigen::Matrix<double, nq, 1>;
137 using Hq_t = Eigen::Matrix<double, p, nq>;
138 using Kq_t = Eigen::Matrix<double, nq, p>;
161 norm_indices(norm_constrained_indices)
165 for (
auto& idx : norm_indices)
169 ROS_ERROR(
"[NCLKF_partial]: Index of a norm-constrained state (%d) cannot be higher than the number of states (%d)! Setting it to zero.", idx,
n);
174 ROS_ERROR(
"[NCLKF_partial]: Index of a norm-constrained state (%d) cannot be less than zero! Setting it to zero.", idx);
186 template <
typename T,
int rows,
int cols,
size_t out_rows,
size_t out_cols>
187 Eigen::Matrix<T, out_rows, out_cols> select_indices(
const Eigen::Matrix<T, rows, cols>& mat,
const std::array<int, out_rows>& row_indices,
const std::array<int, out_cols>& col_indices)
const
189 Eigen::Matrix<T, out_rows, cols> tmp;
190 for (
int it = 0; it < (int)out_rows; it++)
192 const auto row = row_indices[it];
193 assert(rows < 0 || row < rows);
194 tmp.row(it) = mat.row(row);
197 Eigen::Matrix<T, out_rows, out_cols> ret;
198 for (
int it = 0; it < (int)out_cols; it++)
200 const auto col = col_indices[it];
201 assert(cols < 0 || col < cols);
202 tmp.col(it) = tmp.col(col);
208 template <
typename T,
int rows,
int cols,
size_t out_rows>
209 Eigen::Matrix<T, out_rows, cols> select_rows(
const Eigen::Matrix<T, rows, cols>& mat,
const std::array<int, out_rows>& row_indices)
const
211 Eigen::Matrix<T, out_rows, cols> ret;
212 for (
int it = 0; it < (int)out_rows; it++)
214 const auto row = row_indices[it];
215 assert(rows < 0 || row < rows);
216 ret.row(it) = mat.row(row);
221 template <
typename T,
int rows,
int cols,
size_t out_cols>
222 Eigen::Matrix<T, rows, out_cols> select_cols(
const Eigen::Matrix<T, rows, cols>& mat,
const std::array<int, out_cols>& col_indices)
const
224 Eigen::Matrix<T, rows, out_cols> ret;
225 for (
int it = 0; it < (int)out_cols; it++)
227 const auto col = col_indices[it];
228 assert(cols < 0 || col < cols);
229 ret.col(it) = mat.col(col);
234 template <
typename T,
int rows,
size_t out_rows>
235 Eigen::Matrix<T, out_rows, 1> select_indices(
const Eigen::Matrix<T, rows, 1>& vec,
const std::array<int, out_rows>& row_indices)
const
237 return select_rows(vec, row_indices);
240 template <
typename T,
int rows,
int cols,
size_t n_rows>
241 void set_rows(
const Eigen::Matrix<T, (
size_t)n_rows, cols>& from_mat, Eigen::Matrix<T, rows, cols>& to_mat,
const std::array<int, n_rows>& row_indices)
const
243 for (
int rit = 0; rit < (int)n_rows; rit++)
245 const auto ridx = row_indices.at(rit);
246 assert(rows < 0 || ridx < rows);
247 to_mat.row(ridx) = from_mat.row(rit);
255 virtual K_t computeKalmanGain(
const statecov_t& sc,
const z_t& z,
const R_t& R,
const H_t&
H)
const override
257 const R_t W =
H * sc.P *
H.transpose() + R;
258 const R_t W_inv = Base_class::invert_W(W);
259 K_t K = sc.P *
H.transpose() * W_inv;
263 const Kq_t K_orig = select_rows(K, norm_indices);
264 const xq_t xq = select_indices(sc.x, norm_indices);
265 const Hq_t Hq = select_cols(
H, norm_indices);
266 const z_t inn = z - (Hq * xq);
267 const xq_t x = xq + K_orig * inn;
268 const double inn_scale = inn.transpose() * W_inv * inn;
270 const double x_norm = x.norm();
271 const Kq_t Kq = K_orig + (Base_class::l/x_norm - 1.0) * x * (inn.transpose() * W_inv) / inn_scale;
272 set_rows(Kq, K, norm_indices);
283 template <
int n_states,
int n_inputs,
int n_measurements>
284 class NCUKF :
public UKF<n_states, n_inputs, n_measurements>
288 static const int n = n_states;
289 static const int m = n_inputs;
290 static const int p = n_measurements;
313 NCUKF(
const transition_model_t& transition_model,
const observation_model_t& observation_model,
const double l,
const double alpha = 1e-3,
const double kappa = 1,
const double beta = 2)
314 :
Base_class(transition_model, observation_model, alpha, kappa, beta), l(l) {};
318 virtual statecov_t
correct(
const statecov_t& sc,
const z_t& z,
const R_t& R)
const override
320 const auto& x = sc.x;
321 const auto& P = sc.P;
322 const X_t S = Base_class::computeSigmas(x, P);
328 Z_exp.col(i) = Base_class::m_observation_model(S.col(i));
332 z_t z_exp = z_t::Zero();
335 z_exp += Base_class::m_Wm(i) * Z_exp.col(i);
339 Pzz_t Pzz = Pzz_t::Zero();
342 Pzz += Base_class::m_Wc(i) * (Z_exp.col(i) - z_exp) * (Z_exp.col(i) - z_exp).transpose();
347 K_t Pxz = K_t::Zero();
350 Pxz += Base_class::m_Wc(i) * (S.col(i) - x) * (Z_exp.col(i) - z_exp).transpose();
354 const z_t inn = (z - z_exp);
355 const K_t K = computeKalmanGain(sc.x, inn, Pxz, Pzz);
358 if (!K.array().isFinite().all())
360 ROS_ERROR(
"UKF: inverting of Pzz in correction update produced non-finite numbers!!! Fix your covariances (the measurement's is probably too low...)");
367 ret.P = P - K * Pxz.transpose() - Pxz * K.transpose() + K * Pzz * K.transpose();
377 virtual K_t computeKalmanGain(
const x_t& x,
const z_t& inn,
const K_t& Pxz,
const Pzz_t& Pzz)
const override
379 const Pzz_t Pzz_inv = Base_class::computeInverse(Pzz);
380 const K_t K_orig = Pxz * Pzz_inv;
382 const x_t x_pred = x + K_orig * inn;
383 const double inn_scale = inn.transpose() * Pzz_inv * inn;
385 const double x_norm = x_pred.norm();
386 const K_t K = K_orig + (l/x_norm - 1.0) * x_pred * (inn.transpose() * Pzz_inv) / inn_scale;
397 #endif // NCKFSYSTEMMODELS_H
typename Eigen::Matrix< double, n, w > X_t
State sigma points matrix.
Definition: ukf.h:50
typename Base_class::Q_t Q_t
process covariance n*n typedef
Definition: ukf.h:69
typename Base_class::Q_t Q_t
Process noise covariance matrix type .
Definition: lkf.h:52
typename Base_class::B_t B_t
Input to state mapping matrix type .
Definition: nckf.h:46
Eigen::Matrix< double, p, n > H_t
State to measurement mapping matrix type .
Definition: lkf.h:57
typename Base_class::P_t P_t
State uncertainty covariance matrix type .
Definition: lkf.h:50
static const int m
Length of the input vector of the system.
Definition: nckf.h:33
typename Base_class::u_t u_t
Input vector type .
Definition: nckf.h:38
H_t H
The state to measurement mapping matrix .
Definition: lkf.h:150
static const int m
Length of the input vector of the system.
Definition: nckf.h:117
typename Base_class::z_t z_t
measurement vector p*1 typedef
Definition: ukf.h:63
static const int p
Length of the measurement vector of the system.
Definition: nckf.h:34
NCLKF_partial(const A_t &A, const B_t &B, const H_t &H, const double l, const indices_t &norm_constrained_indices)
The main constructor.
Definition: nckf.h:159
typename Base_class::statecov_t statecov_t
Helper struct for passing around the state and its covariance in one variable.
Definition: lkf.h:53
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: nckf.h:318
typename Base_class::R_t R_t
Measurement noise covariance matrix type .
Definition: lkf.h:51
NCLKF()
Convenience default constructor.
Definition: nckf.h:58
Eigen::Matrix< double, p, p > R_t
Measurement noise covariance matrix type .
Definition: kalman_filter.h:39
typename Eigen::Matrix< double, n, p > K_t
Kalman gain matrix.
Definition: ukf.h:53
typename Base_class::z_t z_t
Measurement vector type .
Definition: nckf.h:39
typename Base_class::Q_t Q_t
Process noise covariance matrix type .
Definition: nckf.h:42
typename Base_class::x_t x_t
State vector type .
Definition: nckf.h:37
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
static const int n
Length of the state vector of the system.
Definition: nckf.h:116
Eigen::Matrix< double, p, nq > Hq_t
Norm-constrained measurement mapping type .
Definition: nckf.h:137
Eigen::Matrix< double, n, n > P_t
State uncertainty covariance matrix type .
Definition: kalman_filter.h:38
This class implements the partially norm-constrained linear Kalman filter .
Definition: nckf.h:112
typename Base_class::u_t u_t
input vector m*1 typedef
Definition: ukf.h:61
typename Base_class::P_t P_t
State uncertainty covariance matrix type .
Definition: nckf.h:40
typename Base_class::statecov_t statecov_t
typedef of a helper struct for state and covariance
Definition: ukf.h:73
static const int nq
Number of states to which the norm constraint applies.
Definition: nckf.h:119
typename Base_class::A_t A_t
System transition matrix type .
Definition: nckf.h:45
Eigen::Matrix< double, m, 1 > u_t
Input vector type .
Definition: kalman_filter.h:35
static constexpr int w
Number of sigma points/weights.
Definition: ukf.h:46
Eigen::Matrix< double, n, m > B_t
Input to state mapping matrix type .
Definition: lkf.h:56
typename Base_class::x_t x_t
State vector type .
Definition: lkf.h:47
typename std::function< z_t(const x_t &)> observation_model_t
function of the observation model typedef
Definition: ukf.h:77
Eigen::Matrix< double, n, 1 > x_t
State vector type .
Definition: kalman_filter.h:34
Eigen::Matrix< double, nq, 1 > xq_t
Norm-constrained states vector type .
Definition: nckf.h:136
NCLKF_partial()
Convenience default constructor.
Definition: nckf.h:148
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition: attitude_converter.h:29
typename Base_class::R_t R_t
measurement covariance p*p typedef
Definition: ukf.h:67
typename Base_class::R_t R_t
Measurement noise covariance matrix type .
Definition: nckf.h:41
Eigen::Matrix< double, n, n > A_t
System transition matrix type .
Definition: lkf.h:55
B_t B
The input to state mapping matrix .
Definition: lkf.h:149
typename Eigen::Matrix< double, p, p > Pzz_t
Pzz helper matrix.
Definition: ukf.h:52
std::array< int, nq > indices_t
Indices of the norm-constrained states type.
Definition: nckf.h:135
typename Base_class::P_t P_t
state covariance n*n typedef
Definition: ukf.h:65
NCLKF(const A_t &A, const B_t &B, const H_t &H, const double l)
The main constructor.
Definition: nckf.h:68
static const int n
Length of the state vector of the system.
Definition: nckf.h:32
Implementation of the Linear Kalman filter .
Definition: lkf.h:38
Eigen::Matrix< double, n, p > K_t
Kalman gain matrix type .
Definition: lkf.h:58
typename Base_class::K_t K_t
Kalman gain matrix type .
Definition: nckf.h:48
typename Base_class::statecov_t statecov_t
Helper struct for passing around the state and its covariance in one variable.
Definition: nckf.h:43
static const int p
Length of the measurement vector of the system.
Definition: nckf.h:118
typename Base_class::H_t H_t
State to measurement mapping matrix type .
Definition: nckf.h:47
typename Base_class::x_t x_t
state vector n*1 typedef
Definition: ukf.h:59
Eigen::Matrix< double, n, n > Q_t
Process noise covariance matrix type .
Definition: kalman_filter.h:40
typename Base_class::z_t z_t
Measurement vector type .
Definition: lkf.h:49
Eigen::Matrix< double, p, 1 > z_t
Measurement vector type .
Definition: kalman_filter.h:36
typename Base_class::K_t K_t
Kalman gain matrix type .
Definition: nckf.h:133
typename Eigen::Matrix< double, p, w > Z_t
Measurement sigma points matrix.
Definition: ukf.h:51
A_t A
The system transition matrix .
Definition: lkf.h:144
Defines UKF - a class implementing the Unscented Kalman Filter .
typename Base_class::u_t u_t
Input vector type .
Definition: lkf.h:48
This class implements the norm-constrained linear Kalman filter .
Definition: nckf.h:28
Eigen::Matrix< double, nq, p > Kq_t
Norm-constrained kalman gain type .
Definition: nckf.h:138
Implementation of the Unscented Kalman filter .
Definition: ukf.h:39
is thrown when taking the inverse of a matrix fails during kalman gain calculation
Definition: ukf.h:89
This abstract class defines common interfaces and types for a generic Kalman filter.
Definition: kalman_filter.h:26