- 3.0.2 core module.
DiscreteControlledSystem.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 #include "DiscreteSystem.h"
10 
11 namespace ct {
12 namespace core {
13 
15 
39 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
40 class DiscreteControlledSystem : public DiscreteSystem<STATE_DIM, CONTROL_DIM, SCALAR>
41 {
42 public:
43  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
44 
45  typedef typename std::shared_ptr<DiscreteControlledSystem<STATE_DIM, CONTROL_DIM, SCALAR>> Ptr;
46 
48 
51  typedef typename Base::time_t time_t;
52 
54 
58  : DiscreteSystem<STATE_DIM, CONTROL_DIM, SCALAR>(type), controller_(nullptr){};
59 
61 
67  const SYSTEM_TYPE& type = SYSTEM_TYPE::GENERAL)
68  : DiscreteSystem<STATE_DIM, CONTROL_DIM, SCALAR>(type), controller_(controller){};
69 
72  : DiscreteSystem<STATE_DIM, CONTROL_DIM, SCALAR>(arg)
73  {
74  if (arg.controller_)
75  controller_ = std::shared_ptr<DiscreteController<STATE_DIM, CONTROL_DIM, SCALAR>>(arg.controller_->clone());
76  }
77 
79  virtual ~DiscreteControlledSystem() = default;
80 
83 
85 
88  void setController(const std::shared_ptr<DiscreteController<STATE_DIM, CONTROL_DIM, SCALAR>>& controller)
89  {
90  controller_ = controller;
91  }
92 
94 
98  void getController(std::shared_ptr<DiscreteController<STATE_DIM, CONTROL_DIM, SCALAR>>& controller) const
99  {
100  controller = controller_;
101  }
102 
104 
107  std::shared_ptr<DiscreteController<STATE_DIM, CONTROL_DIM, SCALAR>> getController() { return controller_; }
109 
115  virtual void propagateDynamics(const state_vector_t& state, const time_t n, state_vector_t& stateNext) override
116  {
117  control_vector_t controlAction;
118  if (controller_)
119  controller_->computeControl(state, n, controlAction);
120  else
121  controlAction.setZero();
122 
123  propagateControlledDynamics(state, n, controlAction, stateNext);
124  }
125 
126 
128 
135  virtual void propagateControlledDynamics(const state_vector_t& state,
136  const time_t n,
137  const control_vector_t& control,
138  state_vector_t& stateNext) = 0;
139 
140 
141 protected:
142  std::shared_ptr<DiscreteController<STATE_DIM, CONTROL_DIM, SCALAR>> controller_;
143 };
144 } // namespace core
145 } // namespace ct
void setController(const std::shared_ptr< DiscreteController< STATE_DIM, CONTROL_DIM, SCALAR >> &controller)
set a new controller
Definition: DiscreteControlledSystem.h:88
virtual void propagateControlledDynamics(const state_vector_t &state, const time_t n, const control_vector_t &control, state_vector_t &stateNext)=0
propagates the controlled system dynamics forward by one step
DiscreteControlledSystem(const SYSTEM_TYPE &type=SYSTEM_TYPE::GENERAL)
default constructor
Definition: DiscreteControlledSystem.h:57
virtual ~DiscreteControlledSystem()=default
destructor
DiscreteControlledSystem(const ControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR > &arg)
copy constructor
Definition: DiscreteControlledSystem.h:71
DiscreteControlledSystem(std::shared_ptr< DiscreteController< STATE_DIM, CONTROL_DIM, SCALAR >> controller, const SYSTEM_TYPE &type=SYSTEM_TYPE::GENERAL)
constructor
Definition: DiscreteControlledSystem.h:66
std::shared_ptr< DiscreteController< STATE_DIM, CONTROL_DIM, SCALAR > > getController()
get the controller instace
Definition: DiscreteControlledSystem.h:107
Base::state_vector_t state_vector_t
Definition: DiscreteControlledSystem.h:49
Base::control_vector_t control_vector_t
Definition: DiscreteControlledSystem.h:50
A general, non-linear discrete dynamic system with a control input.
Definition: DiscreteControlledSystem.h:40
std::shared_ptr< DiscreteController< STATE_DIM, CONTROL_DIM, SCALAR > > controller_
the controller instance
Definition: DiscreteControlledSystem.h:142
CppAD::AD< CppAD::cg::CG< double > > SCALAR
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef std::shared_ptr< DiscreteControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR > > Ptr
Definition: DiscreteControlledSystem.h:45
DiscreteSystem< STATE_DIM, CONTROL_DIM, SCALAR > Base
Definition: DiscreteControlledSystem.h:47
Interface class for all controllers.
Definition: DiscreteController.h:22
std::shared_ptr< Controller< STATE_DIM, CONTROL_DIM, SCALAR > > controller_
the controller instance
Definition: ControlledSystem.h:145
SYSTEM_TYPE
type of system
Definition: System.h:15
any non-specific system
Definition: System.h:17
Base::time_t time_t
Definition: DiscreteControlledSystem.h:51
void getController(std::shared_ptr< DiscreteController< STATE_DIM, CONTROL_DIM, SCALAR >> &controller) const
get the controller instance
Definition: DiscreteControlledSystem.h:98
Definition: DiscreteSystem.h:12
int time_t
the type of the time variable
Definition: DiscreteSystem.h:15
virtual DiscreteControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const override=0
deep copy
A general, non-linear dynamic system with a control input.
Definition: ControlledSystem.h:46
virtual void propagateDynamics(const state_vector_t &state, const time_t n, state_vector_t &stateNext) override
propagates the system dynamics forward by one step
Definition: DiscreteControlledSystem.h:115