- 3.0.2 optimal control module.
ExtendedKalmanFilter-impl.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 namespace ct {
9 namespace optcon {
10 
11 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t OUTPUT_DIM, typename SCALAR>
15  const state_matrix_t& Q,
16  const output_matrix_t& R,
17  const state_vector_t& x0,
18  const state_matrix_t& P0)
19  : Base(f, h, x0), Q_(Q), R_(R), P_(P0)
20 {
21 }
22 
23 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t OUTPUT_DIM, typename SCALAR>
28  : Base(f, h, ekf_settings.x0), Q_(ekf_settings.Q), R_(ekf_settings.R), P_(ekf_settings.P0)
29 {
30 }
31 
32 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t OUTPUT_DIM, typename SCALAR>
34  const ct::core::Time& dt,
35  const ct::core::Time& t) -> const state_vector_t&
36 {
37  // STEP 1 - compute covariance matrix prediction
38 
39  // the system is linearized at the current control input, but using the state estimate from the previous timestep.
40  state_matrix_t dFdx = this->f_->computeDerivativeState(this->x_est_, u, dt, t);
41  state_matrix_t dFdv = this->f_->computeDerivativeNoise(this->x_est_, u, dt, t);
42 
43  // compute covariance update
44  P_ = (dFdx * P_ * dFdx.transpose()) + dFdv * (dt * Q_) * dFdv.transpose();
45 
46  // STEP 2 - compute state prediction (based on last state esimate but current control input)
47 
48  this->x_est_ = this->f_->computeDynamics(this->x_est_, u, dt, t);
49 
50  return this->x_est_;
51 }
52 
53 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t OUTPUT_DIM, typename SCALAR>
55  const ct::core::Time& dt,
56  const ct::core::Time& t) -> const state_vector_t&
57 {
58  // STEP 1 - compute residual convariances
59  ct::core::OutputStateMatrix<OUTPUT_DIM, STATE_DIM, SCALAR> dHdx = this->h_->computeDerivativeState(this->x_est_, t);
60  output_matrix_t dHdw = this->h_->computeDerivativeNoise(this->x_est_, t);
61 
62  // STEP 2 - compute near-optimal Kalman gain
63  const Eigen::Matrix<SCALAR, STATE_DIM, OUTPUT_DIM> K =
64  P_ * dHdx.transpose() * (dHdx * P_ * dHdx.transpose() + dHdw * (R_)*dHdw.transpose()).inverse();
65 
66  // STEP 3 - state estimate correction
67  this->x_est_ += K * (y - this->h_->computeMeasurement(this->x_est_));
68 
69  // STEP 4 - covariance matrix correction
70  P_ -= (K * dHdx * P_).eval();
71 
72  return this->x_est_;
73 }
74 
75 template <size_t STATE_DIM, size_t CONTROL_DIM, size_t OUTPUT_DIM, typename SCALAR>
77 {
78  return P_;
79 }
80 
81 } // namespace optcon
82 } // namespace ct
std::shared_ptr< SystemModelBase< STATE_DIM, CONTROL_DIM, SCALAR > > f_
System model for propagating the system.
Definition: EstimatorBase.h:64
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: ExtendedKalmanFilter-impl.h:54
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
output_matrix_t R_
Filter R matrix.
Definition: ExtendedKalmanFilter.h:68
std::shared_ptr< LinearMeasurementModel< OUTPUT_DIM, STATE_DIM, SCALAR > > h_
Observation model used to calculate the output error.
Definition: EstimatorBase.h:67
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
state_vector_t x_est_
State estimate.
Definition: EstimatorBase.h:70
ExtendedKalmanFilter(std::shared_ptr< SystemModelBase< STATE_DIM, CONTROL_DIM, SCALAR >> f, std::shared_ptr< LinearMeasurementModel< OUTPUT_DIM, STATE_DIM, SCALAR >> h, const state_matrix_t &Q, const output_matrix_t &R, const state_vector_t &x0=state_vector_t::Zero(), const state_matrix_t &P0=state_matrix_t::Zero())
Constructor.
Definition: ExtendedKalmanFilter-impl.h:12
state_matrix_t P_
Covariance estimate.
Definition: ExtendedKalmanFilter.h:71
System model is an interface that encapsulates the integrator to be able to propagate the system...
Definition: SystemModelBase.h:21
Settings for setting up an ExtendedKF.
Definition: ExtendedKalmanFilter.h:14
const state_matrix_t & getCovarianceMatrix()
Definition: ExtendedKalmanFilter-impl.h:76
Estimator base.
Definition: EstimatorBase.h:21
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
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: ExtendedKalmanFilter-impl.h:33
state_matrix_t Q_
Filter Q matrix.
Definition: ExtendedKalmanFilter.h:65
double Time