11 template <
typename _MatrixType,
int _UpLo>
16 template <
typename _MatrixType,
int _UpLo>
21 template <
typename _MatrixType,
int _UpLo>
25 this->m_isInitialized =
true;
29 template <
typename _MatrixType,
int _UpLo>
32 eigen_assert(this->m_isInitialized &&
"LLT is not initialized.");
33 return this->m_matrix.isIdentity();
36 template <
typename _MatrixType,
int _UpLo>
37 template <
typename Derived>
40 this->m_matrix = matrix.template triangularView<Eigen::Lower>();
41 this->m_isInitialized =
true;
45 template <
typename _MatrixType,
int _UpLo>
46 template <
typename Derived>
49 this->m_matrix = matrix.template triangularView<Eigen::Upper>().adjoint();
50 this->m_isInitialized =
true;
54 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
63 :
Base(f, h, x0), alpha_(alpha), beta_(beta), kappa_(kappa), P_(P0)
67 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
72 :
Base(f, h, ukf_settings.
x0),
73 alpha_(ukf_settings.alpha),
74 beta_(ukf_settings.beta),
75 kappa_(ukf_settings.kappa),
80 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
86 throw std::runtime_error(
"UnscentedKalmanFilter : Numerical error.");
90 computeCovarianceFromSigmaPoints<STATE_DIM>(
91 this->
x_est_, sigmaStatePoints_, this->
f_->computeDerivativeNoise(this->x_est_,
u,
dt,
t), P_);
96 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
108 computeCovarianceFromSigmaPoints<OUTPUT_DIM>(
109 y, sigmaMeasurementPoints, this->
h_->computeDerivativeNoise(this->
x_est_,
t), P);
111 Eigen::Matrix<SCALAR, STATE_DIM, OUTPUT_DIM> K;
115 this->
x_est_ += K * (z - y);
123 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
129 if (llt.info() != Eigen::Success)
132 Eigen::Matrix<SCALAR, STATE_DIM, STATE_DIM> _S = llt.matrixL().toDenseMatrix();
135 this->sigmaStatePoints_.template leftCols<1>() = this->
x_est_;
137 this->sigmaStatePoints_.template block<STATE_DIM, STATE_DIM>(0, 1) = (gamma_ * _S).colwise() + this->
x_est_;
139 this->sigmaStatePoints_.template rightCols<STATE_DIM>() = (-gamma_ * _S).colwise() + this->
x_est_;
144 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
145 template <
size_t SIZE>
147 const Eigen::Matrix<SCALAR, SIZE, 1>& mean,
152 SigmaPoints<SIZE> W = this->sigmaWeights_c_.transpose().template replicate<SIZE, 1>();
154 cov = tmp.cwiseProduct(W) * tmp.transpose() + noiseCov;
159 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
164 Eigen::Matrix<SCALAR, STATE_DIM, OUTPUT_DIM>& K)
167 Eigen::Matrix<SCALAR, STATE_DIM, OUTPUT_DIM> P_xy =
168 (sigmaStatePoints_.colwise() - this->
x_est_).cwiseProduct(W).eval() *
169 (sigmaMeasurementPoints.colwise() - y).transpose();
171 K = P_xy * P_yy.inverse();
175 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
177 const Eigen::Matrix<SCALAR, STATE_DIM, OUTPUT_DIM>& K,
180 P_ -= K * P * K.transpose();
184 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
194 return computePredictionFromSigmaPoints<STATE_DIM>(sigmaStatePoints_);
197 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
207 return computePredictionFromSigmaPoints<OUTPUT_DIM>(sigmaMeasurementPoints);
210 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
215 lambda_ = alpha_ * alpha_ * (L + kappa_) - L;
216 gamma_ = std::sqrt(L + lambda_);
219 assert(std::abs(L + lambda_) > 1e-6);
222 assert(std::abs(L + kappa_) > 1e-6);
224 SCALAR W_m_0 = lambda_ / (L + lambda_);
225 SCALAR W_c_0 = W_m_0 + (
SCALAR(1) - alpha_ * alpha_ + beta_);
231 sigmaWeights_m_[0] = W_m_0;
232 sigmaWeights_c_[0] = W_c_0;
236 sigmaWeights_m_[
i] = W_i;
237 sigmaWeights_c_[
i] = W_i;
241 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
249 sigmaStatePoints_.col(
i) = this->
f_->computeDynamics(sigmaStatePoints_.col(
i),
u,
dt,
t);
253 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
260 sigmaMeasurementPoints.col(
i) = this->
h_->computeMeasurement(sigmaStatePoints_.col(
i),
t);
264 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
size_t OUTPUT_DIM,
typename SCALAR>
265 template <
size_t DIM>
270 return sigmaPoints * sigmaWeights_m_;
UnscentedKalmanFilter(std::shared_ptr< SystemModelBase< STATE_DIM, CONTROL_DIM, SCALAR >> f, std::shared_ptr< LinearMeasurementModel< OUTPUT_DIM, STATE_DIM, SCALAR >> h, const state_vector_t &x0=state_vector_t::Zero(), SCALAR alpha=SCALAR(1.0), SCALAR beta=SCALAR(2.0), SCALAR kappa=SCALAR(0.0), const ct::core::StateMatrix< STATE_DIM, SCALAR > &P0=ct::core::StateMatrix< STATE_DIM, SCALAR >::Identity())
Constructor.
Definition: UnscentedKalmanFilter-impl.h:55
Cholesky & setU(const Eigen::MatrixBase< Derived > &matrix)
Set upper triangular part of the decomposition.
bool computeCovarianceFromSigmaPoints(const Eigen::Matrix< SCALAR, SIZE, 1 > &mean, const SigmaPoints< SIZE > &sigmaPoints, const Covariance< SIZE > &noiseCov, Covariance< SIZE > &cov)
Estimate covariance from sigma points and noise covariance.
Definition: UnscentedKalmanFilter-impl.h:146
bool computeSigmaPoints()
Compute sigma points from current state and covariance estimates.
Definition: UnscentedKalmanFilter-impl.h:124
bool isIdentity() const
Check whether the decomposed matrix is the identity matrix.
Definition: UnscentedKalmanFilter-impl.h:30
const state_vector_t & predict(const control_vector_t &u, const ct::core::Time &dt, const ct::core::Time &t) override
Estimator predict method.
Definition: UnscentedKalmanFilter-impl.h:81
std::shared_ptr< SystemModelBase< STATE_DIM, CONTROL_DIM, SCALAR > > f_
System model for propagating the system.
Definition: EstimatorBase.h:64
void computeSigmaPointTransition(const ct::core::ControlVector< CONTROL_DIM, SCALAR > &u, const ct::core::Time &dt, const ct::core::Time &t)
Propagate sigma points through the system dynamics. Using CTSystemModel here will not provide a desir...
Definition: UnscentedKalmanFilter-impl.h:242
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
bool computeKalmanGain(const ct::core::OutputVector< OUTPUT_DIM, SCALAR > &y, const SigmaPoints< OUTPUT_DIM > &sigmaMeasurementPoints, const Covariance< OUTPUT_DIM > &P_yy, Eigen::Matrix< SCALAR, STATE_DIM, OUTPUT_DIM > &K)
Compute the Kalman Gain using sigma points..
Definition: UnscentedKalmanFilter-impl.h:160
std::shared_ptr< LinearMeasurementModel< OUTPUT_DIM, STATE_DIM, SCALAR > > h_
Observation model used to calculate the output error.
Definition: EstimatorBase.h:67
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
static constexpr size_t SigmaPointCount
Definition: UnscentedKalmanFilter.h:74
const double dt
Definition: LQOCSolverTiming.cpp:18
ct::core::OutputVector< OUTPUT_DIM, SCALAR > computeMeasurementPrediction(SigmaPoints< OUTPUT_DIM > &sigmaMeasurementPoints, const ct::core::Time &t=0)
Predict the next measurement.
Definition: UnscentedKalmanFilter-impl.h:199
void computeWeights()
Compute weights of sigma points.
Definition: UnscentedKalmanFilter-impl.h:211
Linear Measurement Model is an interface for linear measurement models most commonly used in practice...
Definition: LinearMeasurementModel.h:23
const state_vector_t & update(const output_vector_t &y, const ct::core::Time &dt, const ct::core::Time &t) override
Estimator update method.
Definition: UnscentedKalmanFilter-impl.h:97
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< SCALAR, SIZE, SigmaPointCount > SigmaPoints
Definition: UnscentedKalmanFilter.h:77
for i
Definition: mpc_unittest_plotting.m:14
state_vector_t x_est_
State estimate.
Definition: EstimatorBase.h:70
bool updateStateCovariance(const Eigen::Matrix< SCALAR, STATE_DIM, OUTPUT_DIM > &K, const Covariance< OUTPUT_DIM > &P)
Compute the Kalman Gain using sigma points..
Definition: UnscentedKalmanFilter-impl.h:176
void computeSigmaPointMeasurements(SigmaPoints< OUTPUT_DIM > &sigmaMeasurementPoints, const ct::core::Time &t=0)
Predict measurements of sigma points.
Definition: UnscentedKalmanFilter-impl.h:254
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
const state_vector_t computeStatePrediction(const ct::core::ControlVector< CONTROL_DIM, SCALAR > &u, const ct::core::Time &dt, const ct::core::Time &t=0)
Update state covariance from Kalman gain and sigma points.
Definition: UnscentedKalmanFilter-impl.h:185
Estimator base.
Definition: EstimatorBase.h:21
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
Settings for setting up an UnscentedKF.
Definition: FilterSettings.h:182
state_vector_t computePredictionFromSigmaPoints(const SigmaPoints< DIM > &sigmaPoints)
Make a prediction based on sigma points.
Eigen::Matrix< SCALAR, SIZE, SIZE > Covariance
Definition: UnscentedKalmanFilter.h:80
Cholesky & setIdentity()
Set decomposition to identity.
Definition: UnscentedKalmanFilter-impl.h:22