- 3.0.2 optimal control module.
UnscentedKalmanFilter.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include "EstimatorBase.h"
9 
10 namespace ct {
11 namespace optcon {
12 
21 template <typename MatrixType, int UpLo = Eigen::Lower>
22 class Cholesky : public Eigen::LLT<MatrixType, UpLo>
23 {
24 public:
26  Cholesky();
28  Cholesky(const MatrixType& m);
31 
33  bool isIdentity() const;
34 
39  template <typename Derived>
40  Cholesky& setL(const Eigen::MatrixBase<Derived>& matrix);
41 
46  template <typename Derived>
47  Cholesky& setU(const Eigen::MatrixBase<Derived>& matrix);
48 };
49 
50 template <size_t STATE_DIM, typename SCALAR>
52 
61 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t OUTPUT_DIM, typename SCALAR = double>
62 class UnscentedKalmanFilter final : public EstimatorBase<STATE_DIM, CONTROL_DIM, OUTPUT_DIM, SCALAR>
63 {
64 public:
65  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
66 
68  using typename Base::control_vector_t;
69  using typename Base::output_matrix_t;
70  using typename Base::output_vector_t;
71  using typename Base::state_matrix_t;
72  using typename Base::state_vector_t;
73 
74  static constexpr size_t SigmaPointCount = 2 * STATE_DIM + 1;
75 
76  template <size_t SIZE>
77  using SigmaPoints = Eigen::Matrix<SCALAR, SIZE, SigmaPointCount>;
78 
79  template <size_t SIZE>
80  using Covariance = Eigen::Matrix<SCALAR, SIZE, SIZE>;
81 
82  template <size_t SIZE>
84 
88  const state_vector_t& x0 = state_vector_t::Zero(),
89  SCALAR alpha = SCALAR(1.0),
90  SCALAR beta = SCALAR(2.0),
91  SCALAR kappa = SCALAR(0.0),
93 
98 
100  const state_vector_t& predict(const control_vector_t& u,
101  const ct::core::Time& dt,
102  const ct::core::Time& t) override;
103 
105  const state_vector_t& update(const output_vector_t& y, const ct::core::Time& dt, const ct::core::Time& t) override;
106 
108  bool computeSigmaPoints();
109 
111  template <size_t SIZE>
112  bool computeCovarianceFromSigmaPoints(const Eigen::Matrix<SCALAR, SIZE, 1>& mean,
113  const SigmaPoints<SIZE>& sigmaPoints,
114  const Covariance<SIZE>& noiseCov,
115  Covariance<SIZE>& cov);
116 
118  bool computeKalmanGain(const ct::core::OutputVector<OUTPUT_DIM, SCALAR>& y,
119  const SigmaPoints<OUTPUT_DIM>& sigmaMeasurementPoints,
120  const Covariance<OUTPUT_DIM>& P_yy,
121  Eigen::Matrix<SCALAR, STATE_DIM, OUTPUT_DIM>& K);
122 
124  bool updateStateCovariance(const Eigen::Matrix<SCALAR, STATE_DIM, OUTPUT_DIM>& K, const Covariance<OUTPUT_DIM>& P);
125 
127  const state_vector_t computeStatePrediction(const ct::core::ControlVector<CONTROL_DIM, SCALAR>& u,
128  const ct::core::Time& dt,
129  const ct::core::Time& t = 0);
130 
132  ct::core::OutputVector<OUTPUT_DIM, SCALAR> computeMeasurementPrediction(
133  SigmaPoints<OUTPUT_DIM>& sigmaMeasurementPoints,
134  const ct::core::Time& t = 0);
135 
137  void computeWeights();
138 
143  void computeSigmaPointTransition(const ct::core::ControlVector<CONTROL_DIM, SCALAR>& u,
144  const ct::core::Time& dt,
145  const ct::core::Time& t);
146 
148  void computeSigmaPointMeasurements(SigmaPoints<OUTPUT_DIM>& sigmaMeasurementPoints, const ct::core::Time& t = 0);
149 
151  template <size_t DIM>
152  state_vector_t computePredictionFromSigmaPoints(const SigmaPoints<DIM>& sigmaPoints);
153 
154 private:
155  state_matrix_t P_;
156  Eigen::Matrix<SCALAR, SigmaPointCount, 1> sigmaWeights_m_;
157  Eigen::Matrix<SCALAR, SigmaPointCount, 1> sigmaWeights_c_;
158  SigmaPoints<STATE_DIM> sigmaStatePoints_;
159  SCALAR alpha_;
160  SCALAR beta_;
161  SCALAR kappa_;
162  SCALAR gamma_;
163  SCALAR lambda_;
164 };
165 
166 } // namespace optcon
167 } // namespace ct
Cholesky & setU(const Eigen::MatrixBase< Derived > &matrix)
Set upper triangular part of the decomposition.
bool isIdentity() const
Check whether the decomposed matrix is the identity matrix.
Definition: UnscentedKalmanFilter-impl.h:30
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
Cholesky square root decomposition of a symmetric positive-definite matrix.
Definition: UnscentedKalmanFilter.h:22
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
const double dt
Definition: LQOCSolverTiming.cpp:18
Linear Measurement Model is an interface for linear measurement models most commonly used in practice...
Definition: LinearMeasurementModel.h:23
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< SCALAR, SIZE, SigmaPointCount > SigmaPoints
Definition: UnscentedKalmanFilter.h:77
Cholesky()
Constructor.
Definition: UnscentedKalmanFilter-impl.h:12
Cholesky & setL(const Eigen::MatrixBase< Derived > &matrix)
Set lower triangular part of the decomposition.
System model is an interface that encapsulates the integrator to be able to propagate the system...
Definition: SystemModelBase.h:21
Estimator base.
Definition: EstimatorBase.h:21
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
Settings for setting up an UnscentedKF.
Definition: FilterSettings.h:182
Eigen::Matrix< SCALAR, SIZE, SIZE > Covariance
Definition: UnscentedKalmanFilter.h:80
Cholesky & setIdentity()
Set decomposition to identity.
Definition: UnscentedKalmanFilter-impl.h:22
Unscented Kalman Filter is a nonlinear estimator best suited for highly nonlinear systems...
Definition: UnscentedKalmanFilter.h:62
double Time