- 3.0.2 core module.
SystemLinearizer.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 core {
10 
12 
50 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
51 class SystemLinearizer : public LinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>
52 {
53 public:
54  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
55 
57 
58  typedef typename Base::time_t time_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 
67 
69 
75  SystemLinearizer(std::shared_ptr<system_t> nonlinearSystem, bool doubleSidedDerivative = true)
76  : Base(nonlinearSystem->getType()),
77  nonlinearSystem_(nonlinearSystem),
78  linearizer_(std::bind(&system_t::computeControlledDynamics,
79  nonlinearSystem_.get(),
80  std::placeholders::_1,
81  std::placeholders::_2,
82  std::placeholders::_3,
83  std::placeholders::_4),
84  doubleSidedDerivative)
85  {
86  if (nonlinearSystem == nullptr)
87  throw std::runtime_error("SystemLinearizer: Nonlinear system is nullptr!");
88 
89  if (nonlinearSystem->getType() == SECOND_ORDER)
90  isSecondOrderSystem_ = true;
91  else
92  isSecondOrderSystem_ = false;
93 
94  dFdx_.setZero();
95  dFdu_.setZero();
96  }
97 
100  : Base(arg),
102  linearizer_(std::bind(&system_t::computeControlledDynamics,
103  nonlinearSystem_.get(),
104  std::placeholders::_1,
105  std::placeholders::_2,
106  std::placeholders::_3,
107  std::placeholders::_4),
108  arg.linearizer_.getDoubleSidedDerivativeFlag()),
109  dFdx_(arg.dFdx_),
110  dFdu_(arg.dFdu_),
112  {
113  }
114 
116  virtual ~SystemLinearizer() {}
119  {
121  }
122 
124 
137  virtual const state_matrix_t& getDerivativeState(const state_vector_t& x,
138  const control_vector_t& u,
139  const time_t t = 0.0) override
140  {
142 
144  {
145  dFdx_.template topLeftCorner<STATE_DIM / 2, STATE_DIM / 2>().setZero();
146  dFdx_.template topRightCorner<STATE_DIM / 2, STATE_DIM / 2>().setIdentity();
147  }
148 
149  return dFdx_;
150  }
151 
152 
154 
167  virtual const state_control_matrix_t& getDerivativeControl(const state_vector_t& x,
168  const control_vector_t& u,
169  const time_t t = 0.0) override
170  {
172 
174  {
175  dFdu_.template topRows<STATE_DIM / 2>().setZero();
176  }
177 
178  return dFdu_;
179  }
180 
181 
182 protected:
183  std::shared_ptr<system_t> nonlinearSystem_;
184 
187 
188  state_matrix_t dFdx_;
189  state_control_matrix_t dFdu_;
190 
192 };
193 
194 
195 } // namespace core
196 } // namespace ct
SystemLinearizer(std::shared_ptr< system_t > nonlinearSystem, bool doubleSidedDerivative=true)
default constructor
Definition: SystemLinearizer.h:75
interface class for a general linear system or linearized system
Definition: LinearSystem.h:23
SystemLinearizer< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const override
deep cloning
Definition: SystemLinearizer.h:118
SystemLinearizer(const SystemLinearizer &arg)
copy constructor
Definition: SystemLinearizer.h:99
Base::time_t time_t
Time type as defined in System.
Definition: SystemLinearizer.h:58
ControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR > system_t
type of system to be linearized
Definition: SystemLinearizer.h:65
bool isSecondOrderSystem_
flag if system is a second order system
Definition: SystemLinearizer.h:191
virtual const state_control_matrix_t & getDerivativeControl(const state_vector_t &x, const control_vector_t &u, const time_t t=0.0) override
get the Jacobian with respect to the input
Definition: SystemLinearizer.h:167
clear all close all load ct GNMSLog0 mat reformat t
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef LinearSystem< STATE_DIM, CONTROL_DIM, SCALAR > Base
Base class type.
Definition: SystemLinearizer.h:56
virtual const state_matrix_t & getDerivativeState(const state_vector_t &x, const control_vector_t &u, const time_t t=0.0) override
get the Jacobian with respect to the state
Definition: SystemLinearizer.h:137
a pure second-order system
Definition: System.h:18
SYSTEM_TYPE getType() const
get the type of system
Definition: System.h:75
state_matrix_t dFdx_
Jacobian wrt state.
Definition: SystemLinearizer.h:188
virtual void computeControlledDynamics(const state_vector_t &state, const time_t &t, const control_vector_t &control, state_vector_t &derivative) override
compute the system dynamics
Definition: LinearSystem.h:63
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
Base::control_vector_t control_vector_t
input vector type
Definition: SystemLinearizer.h:61
DynamicsLinearizerNumDiff< STATE_DIM, CONTROL_DIM, SCALAR, SCALAR > linearizer_
instance of numerical-linearizer
Definition: SystemLinearizer.h:186
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::state_vector_t state_vector_t
state vector type
Definition: SystemLinearizer.h:60
virtual ~SystemLinearizer()
destructor
Definition: SystemLinearizer.h:116
std::shared_ptr< system_t > nonlinearSystem_
instance of non-linear system
Definition: SystemLinearizer.h:183
Base::state_control_matrix_t state_control_matrix_t
input Jacobian type
Definition: SystemLinearizer.h:63
state_control_matrix_t dFdu_
Jacobian wrt input.
Definition: SystemLinearizer.h:189
Computes the linearization of a general non-linear ControlledSystem using numerical differentiation...
Definition: SystemLinearizer.h:51
Base::state_matrix_t state_matrix_t
state Jacobian type
Definition: SystemLinearizer.h:62
A general, non-linear dynamic system with a control input.
Definition: ControlledSystem.h:46