- 3.0.2 core module.
DiscreteSystemLinearizerAD.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 #pragma once
6 
7 #ifdef CPPAD
8 
9 namespace ct {
10 namespace core {
11 
13 
47 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
48 class DiscreteSystemLinearizerAD : public DiscreteLinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>
49 {
50 public:
51  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
52 
53  typedef DiscreteLinearSystem<STATE_DIM, CONTROL_DIM, SCALAR> Base;
54 
55  typedef CppAD::AD<SCALAR> ADScalar;
56  typedef DiscreteControlledSystem<STATE_DIM, CONTROL_DIM, ADScalar> system_t;
57  typedef DynamicsLinearizerAD<STATE_DIM, CONTROL_DIM, ADScalar, int>
58  linearizer_t;
59 
60  typedef typename Base::state_vector_t state_vector_t;
61  typedef typename Base::control_vector_t control_vector_t;
62  typedef typename Base::state_matrix_t state_matrix_t;
63  typedef typename Base::state_control_matrix_t state_control_matrix_t;
64 
66 
69  DiscreteSystemLinearizerAD(std::shared_ptr<system_t> nonlinearSystem)
70  : Base(nonlinearSystem->getType()),
71  dFdx_(state_matrix_t::Zero()),
72  dFdu_(state_control_matrix_t::Zero()),
73  nonlinearSystem_(nonlinearSystem),
74  linearizer_(std::bind(&system_t::propagateControlledDynamics,
75  nonlinearSystem_.get(),
76  std::placeholders::_1,
77  std::placeholders::_2,
78  std::placeholders::_3,
79  std::placeholders::_4))
80  {
81  }
82 
84  DiscreteSystemLinearizerAD(const DiscreteSystemLinearizerAD& arg)
85  : Base(arg.nonlinearSystem_->getType()),
86  dFdx_(arg.dFdx_),
87  dFdu_(arg.dFdu_),
88  nonlinearSystem_(arg.nonlinearSystem_->clone()),
89  linearizer_(std::bind(&system_t::propagateControlledDynamics,
90  nonlinearSystem_.get(),
91  std::placeholders::_1,
92  std::placeholders::_2,
93  std::placeholders::_3,
94  std::placeholders::_4))
95  {
96  }
97 
99  virtual ~DiscreteSystemLinearizerAD() {}
101  DiscreteSystemLinearizerAD<STATE_DIM, CONTROL_DIM, SCALAR>* clone() const override
102  {
103  return new DiscreteSystemLinearizerAD<STATE_DIM, CONTROL_DIM, SCALAR>(*this);
104  }
105 
107 
120  const state_matrix_t& getDerivativeState(const state_vector_t& x, const control_vector_t& u, const int t = 0)
121  {
122  dFdx_ = linearizer_.getDerivativeState(x, u, t);
123  return dFdx_;
124  }
125 
126 
128 
141  const state_control_matrix_t& getDerivativeControl(const state_vector_t& x,
142  const control_vector_t& u,
143  const int t = 0)
144  {
145  dFdu_ = linearizer_.getDerivativeControl(x, u, t);
146  return dFdu_;
147  }
148 
150 
166  void getAandB(const state_vector_t& x,
167  const control_vector_t& u,
168  const state_vector_t& x_next,
169  const int n,
170  size_t numSteps,
171  state_matrix_t& A,
172  state_control_matrix_t& B) override
173  {
174  dFdx_ = linearizer_.getDerivativeState(x, u, n);
175  dFdu_ = linearizer_.getDerivativeControl(x, u, n);
176 
177  A = dFdx_;
178  B = dFdu_;
179  }
180 
181 protected:
182  state_matrix_t dFdx_;
183  state_control_matrix_t dFdu_;
184 
185  std::shared_ptr<system_t> nonlinearSystem_;
186 
187  linearizer_t linearizer_;
188 };
189 
190 } // namespace core
191 } // namespace ct
192 
193 #endif
Eigen::Matrix< double, nControls, 1 > control_vector_t
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
Eigen::Matrix< double, nStates, nStates > state_matrix_t
Eigen::Matrix< double, nStates, 1 > state_vector_t