- 3.0.2 core module.
AutoDiffLinearizer.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 CPPAD
9 
10 namespace ct {
11 namespace core {
12 
14 
48 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
49 class AutoDiffLinearizer : public LinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>
50 {
51 public:
52  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
53 
55 
56  typedef CppAD::AD<SCALAR> ADScalar;
57  typedef ControlledSystem<STATE_DIM, CONTROL_DIM, ADScalar> system_t;
58  typedef DynamicsLinearizerAD<STATE_DIM, CONTROL_DIM, ADScalar, ADScalar>
59  linearizer_t;
60 
61  typedef typename Base::state_vector_t state_vector_t;
62  typedef typename Base::control_vector_t control_vector_t;
63  typedef typename Base::state_matrix_t state_matrix_t;
64  typedef typename Base::state_control_matrix_t state_control_matrix_t;
65 
67 
70  AutoDiffLinearizer(std::shared_ptr<system_t> nonlinearSystem)
71  : Base(nonlinearSystem->getType()),
72  dFdx_(state_matrix_t::Zero()),
73  dFdu_(state_control_matrix_t::Zero()),
74  nonlinearSystem_(nonlinearSystem),
75  linearizer_(std::bind(&system_t::computeControlledDynamics,
76  nonlinearSystem_.get(),
77  std::placeholders::_1,
78  std::placeholders::_2,
79  std::placeholders::_3,
80  std::placeholders::_4))
81  {
82  }
83 
85  AutoDiffLinearizer(const AutoDiffLinearizer& arg)
86  : Base(arg.nonlinearSystem_->getType()),
87  dFdx_(arg.dFdx_),
88  dFdu_(arg.dFdu_),
89  nonlinearSystem_(arg.nonlinearSystem_->clone()),
90  linearizer_(std::bind(&system_t::computeControlledDynamics,
91  nonlinearSystem_.get(),
92  std::placeholders::_1,
93  std::placeholders::_2,
94  std::placeholders::_3,
95  std::placeholders::_4))
96  {
97  }
98 
100  virtual ~AutoDiffLinearizer() {}
102  AutoDiffLinearizer<STATE_DIM, CONTROL_DIM, SCALAR>* clone() const override
103  {
104  return new AutoDiffLinearizer<STATE_DIM, CONTROL_DIM, SCALAR>(*this);
105  }
106 
108 
121  virtual const state_matrix_t& getDerivativeState(const state_vector_t& x,
122  const control_vector_t& u,
123  const SCALAR t = SCALAR(0.0)) override
124  {
125  dFdx_ = linearizer_.getDerivativeState(x, u, t);
126  return dFdx_;
127  }
128 
129 
131 
144  virtual const state_control_matrix_t& getDerivativeControl(const state_vector_t& x,
145  const control_vector_t& u,
146  const SCALAR t = SCALAR(0.0)) override
147  {
148  dFdu_ = linearizer_.getDerivativeControl(x, u, t);
149  return dFdu_;
150  }
151 
152 
153 protected:
154  state_matrix_t dFdx_;
155  state_control_matrix_t dFdu_;
156 
157  std::shared_ptr<system_t> nonlinearSystem_;
158 
159  linearizer_t linearizer_;
160 };
161 
162 } // namespace core
163 } // namespace ct
164 
165 #endif
interface class for a general linear system or linearized system
Definition: LinearSystem.h:23
Eigen::Matrix< double, nControls, 1 > control_vector_t
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< double, nStates, nStates > state_matrix_t
Eigen::Matrix< double, nStates, 1 > state_vector_t