- 3.0.2 core module.
DynamicsLinearizerAD.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 #ifdef CPPADCG
9 
11 
12 namespace ct {
13 namespace core {
14 
16 
27 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR, typename TIME>
28 class DynamicsLinearizerAD : public internal::DynamicsLinearizerADBase<STATE_DIM, CONTROL_DIM, SCALAR, TIME>
29 {
30 public:
31  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
32 
33  typedef internal::DynamicsLinearizerADBase<STATE_DIM, CONTROL_DIM, SCALAR, TIME> Base;
34 
35  typedef typename Base::OUT_SCALAR OUT_SCALAR;
36 
37  typedef typename Base::state_vector_t state_vector_t;
38  typedef typename Base::control_vector_t control_vector_t;
39 
40  typedef typename Base::state_matrix_t state_matrix_t;
41  typedef typename Base::state_control_matrix_t state_control_matrix_t;
42 
43  typedef typename Base::dynamics_fct_t dynamics_fct_t;
44 
46 
49  DynamicsLinearizerAD(dynamics_fct_t dyn)
50  : Base(dyn), dynamics_fct_(dyn), dFdx_(state_matrix_t::Zero()), dFdu_(state_control_matrix_t::Zero())
51  {
52  }
53 
55  DynamicsLinearizerAD(const DynamicsLinearizerAD& rhs)
56  : Base(rhs.dynamics_fct_), dynamics_fct_(rhs.dynamics_fct_), dFdx_(rhs.dFdx_), dFdu_(rhs.dFdu_)
57  {
58  }
59 
60  const state_matrix_t& getDerivativeState(const state_vector_t& x, const control_vector_t& u, const TIME t = TIME(0))
61  {
62  computeA(x, u);
63  return dFdx_;
64  }
65 
66  const state_control_matrix_t& getDerivativeControl(const state_vector_t& x,
67  const control_vector_t& u,
68  const TIME t = TIME(0))
69  {
70  computeB(x, u);
71  return dFdu_;
72  }
73 
74 protected:
76 
80  void computeA(const state_vector_t& x, const control_vector_t& u)
81  {
82  Eigen::Matrix<OUT_SCALAR, Eigen::Dynamic, 1> input(STATE_DIM + CONTROL_DIM);
83  input << x, u;
84 
85  Eigen::Matrix<OUT_SCALAR, Eigen::Dynamic, 1> jac(this->A_entries);
86 
87  this->f_.SparseJacobianForward(input, this->sparsityA_.sparsity(), this->sparsityA_.row(),
88  this->sparsityA_.col(), jac, this->sparsityA_.workJacobian());
89 
90  Eigen::Map<Eigen::Matrix<OUT_SCALAR, STATE_DIM, STATE_DIM>> out(jac.data());
91 
92  dFdx_ = out;
93  }
94 
96 
100  void computeB(const state_vector_t& x, const control_vector_t& u)
101  {
102  Eigen::Matrix<OUT_SCALAR, Eigen::Dynamic, 1> input(STATE_DIM + CONTROL_DIM);
103  input << x, u;
104 
105  Eigen::Matrix<OUT_SCALAR, Eigen::Dynamic, 1> jac(this->B_entries);
106 
107  this->f_.SparseJacobianForward(input, this->sparsityB_.sparsity(), this->sparsityB_.row(),
108  this->sparsityB_.col(), jac, this->sparsityB_.workJacobian());
109 
110  Eigen::Map<Eigen::Matrix<OUT_SCALAR, STATE_DIM, CONTROL_DIM>> out(jac.data());
111 
112  dFdu_ = out;
113  }
114 
115 protected:
116  dynamics_fct_t dynamics_fct_;
117 
118  state_matrix_t dFdx_;
119  state_control_matrix_t dFdu_;
120 };
121 
122 } // namespace core
123 } // namespace ct
124 
125 
126 #endif
Eigen::Matrix< double, nControls, 1 > control_vector_t
Eigen::Matrix< double, nStates, nStates > state_matrix_t
Eigen::Matrix< double, nStates, 1 > state_vector_t