- 3.0.2 core module.
DiscreteSystemLinearizer.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 namespace ct {
8 namespace core {
9 
11 
37 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
38 class DiscreteSystemLinearizer : public DiscreteLinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>
39 {
40 public:
41  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
42 
44 
46 
49 
50  typedef typename Base::state_matrix_t state_matrix_t;
51  typedef typename Base::state_control_matrix_t state_control_matrix_t;
52 
54 
60  DiscreteSystemLinearizer(std::shared_ptr<system_t> nonlinearSystem, bool doubleSidedDerivative = true)
61  : Base(nonlinearSystem->getType()),
62  nonlinearSystem_(nonlinearSystem),
63  linearizer_(std::bind(&system_t::propagateControlledDynamics,
64  nonlinearSystem_.get(),
65  std::placeholders::_1,
66  std::placeholders::_2,
67  std::placeholders::_3,
68  std::placeholders::_4),
69  doubleSidedDerivative)
70  {
71  if (nonlinearSystem == nullptr)
72  throw std::runtime_error("SystemLinearizer: Nonlinear system is nullptr!");
73 
74  dFdx_.setZero();
75  dFdu_.setZero();
76  }
77 
80  : Base(arg),
82  linearizer_(std::bind(&system_t::propagateControlledDynamics,
83  nonlinearSystem_.get(),
84  std::placeholders::_1,
85  std::placeholders::_2,
86  std::placeholders::_3,
87  std::placeholders::_4),
88  arg.linearizer_.getDoubleSidedDerivativeFlag()),
89  dFdx_(arg.dFdx_),
90  dFdu_(arg.dFdu_)
91  {
92  }
93 
98  {
100  }
101 
103 
116  virtual const state_matrix_t& getDerivativeState(const state_vector_t& x,
117  const control_vector_t& u,
118  const int n = 0)
119  {
121  return dFdx_;
122  }
123 
125 
138  virtual const state_control_matrix_t& getDerivativeControl(const state_vector_t& x,
139  const control_vector_t& u,
140  const int n = 0)
141  {
143  return dFdu_;
144  }
145 
147 
163  virtual void getAandB(const state_vector_t& x,
164  const control_vector_t& u,
165  const state_vector_t& x_next,
166  const int n,
167  size_t numSteps,
168  state_matrix_t& A,
169  state_control_matrix_t& B) override
170  {
173 
174  A = dFdx_;
175  B = dFdu_;
176  }
177 
178 protected:
179  std::shared_ptr<system_t> nonlinearSystem_;
180 
182 
183  state_matrix_t dFdx_;
184  state_control_matrix_t dFdu_;
185 };
186 
187 } // namespace core
188 } // namespace ct
Base::state_vector_t state_vector_t
state vector type
Definition: DiscreteSystemLinearizer.h:47
virtual void getAandB(const state_vector_t &x, const control_vector_t &u, const state_vector_t &x_next, const int n, size_t numSteps, state_matrix_t &A, state_control_matrix_t &B) override
retrieve discrete-time linear system matrices A and B.
Definition: DiscreteSystemLinearizer.h:163
DiscreteSystemLinearizer< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const override
deep cloning
Definition: DiscreteSystemLinearizer.h:97
DiscreteSystemLinearizer(std::shared_ptr< system_t > nonlinearSystem, bool doubleSidedDerivative=true)
control Jacobian type (B)
Definition: DiscreteSystemLinearizer.h:60
std::shared_ptr< system_t > nonlinearSystem_
instance of non-linear system
Definition: DiscreteSystemLinearizer.h:179
interface class for a general discrete linear system or linearized discrete system ...
Definition: DiscreteLinearSystem.h:23
virtual void propagateControlledDynamics(const state_vector_t &state, const time_t n, const control_vector_t &control, state_vector_t &stateNext) override
compute the system dynamics
Definition: DiscreteLinearSystem.h:63
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef DiscreteLinearSystem< STATE_DIM, CONTROL_DIM, SCALAR > Base
Definition: DiscreteSystemLinearizer.h:43
virtual const state_control_matrix_t & getDerivativeControl(const state_vector_t &x, const control_vector_t &u, const int n=0)
get the Jacobian with respect to the input
Definition: DiscreteSystemLinearizer.h:138
DynamicsLinearizerNumDiff< STATE_DIM, CONTROL_DIM, SCALAR, int > linearizer_
instance of numerical-linearizer
Definition: DiscreteSystemLinearizer.h:181
Definition: ControlVector.h:12
A general, non-linear discrete dynamic system with a control input.
Definition: DiscreteControlledSystem.h:40
virtual ~DiscreteSystemLinearizer()
destructor
Definition: DiscreteSystemLinearizer.h:95
Base::state_control_matrix_t state_control_matrix_t
Definition: DiscreteSystemLinearizer.h:51
const state_control_matrix_t & getDerivativeControl(const state_vector_t &x, const control_vector_t &u, const TIME t=TIME(0))
get the Jacobian with respect to the input
Definition: DynamicsLinearizerNumDiff.h:137
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
Base::state_matrix_t state_matrix_t
state Jacobian type (A)
Definition: DiscreteSystemLinearizer.h:50
virtual const state_matrix_t & getDerivativeState(const state_vector_t &x, const control_vector_t &u, const int n=0)
get the Jacobian with respect to the state
Definition: DiscreteSystemLinearizer.h:116
Definition: StateVector.h:12
const state_matrix_t & getDerivativeState(const state_vector_t &x, const control_vector_t &u, const TIME t=TIME(0))
get the Jacobian with respect to the state
Definition: DynamicsLinearizerNumDiff.h:82
Base::control_vector_t control_vector_t
control vector type
Definition: DiscreteSystemLinearizer.h:48
DiscreteSystemLinearizer(const DiscreteSystemLinearizer &arg)
copy constructor
Definition: DiscreteSystemLinearizer.h:79
state_matrix_t dFdx_
Jacobian wrt state.
Definition: DiscreteSystemLinearizer.h:183
SYSTEM_TYPE getType() const
get the type of system
Definition: DiscreteSystem.h:40
DiscreteControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR > system_t
type of system to be linearized
Definition: DiscreteSystemLinearizer.h:45
Computes the linearization of a general non-linear DiscreteControlledSystem using numerical different...
Definition: DiscreteSystemLinearizer.h:38
state_control_matrix_t dFdu_
Jacobian wrt input.
Definition: DiscreteSystemLinearizer.h:184