- 3.0.2 core module.
SecondOrderSystem.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 <cmath>
9 #include <memory>
10 #include <iostream>
11 
12 #include "ControlledSystem.h"
13 
14 namespace ct {
15 namespace core {
16 
17 namespace tpl {
18 
20 
61 template <typename SCALAR>
62 class SecondOrderSystem : public ControlledSystem<2, 1, SCALAR>
63 {
64 public:
65  static const size_t STATE_DIM = 2;
66  static const size_t CONTROL_DIM = 1;
67 
69  typedef typename Base::time_t time_t;
70 
72  SecondOrderSystem() = delete;
73 
75 
83  SCALAR zeta = SCALAR(1.0),
84  SCALAR g_dc = SCALAR(1.0),
85  std::shared_ptr<Controller<2, 1, SCALAR>> controller = nullptr)
86  : ControlledSystem<2, 1, SCALAR>(controller, SYSTEM_TYPE::SECOND_ORDER),
87  w_n_(w_n),
88  w_n_square_(w_n_ * w_n_),
89  zeta_(zeta),
90  g_dc_(g_dc)
91  {
92  }
93 
96  : ControlledSystem<2, 1, SCALAR>(arg),
97  w_n_(arg.w_n_),
98  w_n_square_(arg.w_n_square_),
99  zeta_(arg.zeta_),
100  g_dc_(arg.g_dc_)
101  {
102  }
103 
105 
112  SecondOrderSystem(SCALAR k, SCALAR m, SCALAR d, SCALAR g_dc, std::shared_ptr<Controller<2, 1>> controller = nullptr)
113  : ControlledSystem<2, 1>(controller),
114  w_n_(std::sqrt(k / m)),
115  w_n_square_(w_n_ * w_n_),
116  zeta_(d / (2.0 * m * k)),
117  g_dc_(g_dc)
118  {
119  }
120 
122  SecondOrderSystem* clone() const override { return new SecondOrderSystem(*this); }
124  virtual ~SecondOrderSystem() {}
126 
131  void setDynamics(SCALAR w_n, SCALAR zeta = SCALAR(1.0), SCALAR g_dc = SCALAR(1.0))
132  {
133  w_n_ = w_n;
134  w_n_square_ = w_n_ * w_n_;
135  zeta_ = zeta;
136  g_dc_ = g_dc;
137  }
138 
140 
147  const time_t& t,
148  const ControlVector<1, SCALAR>& control,
149  StateVector<2, SCALAR>& derivative) override
150  {
151  derivative(0) = state(1);
152  derivative(1) = g_dc_ * control(0) - 2.0 * zeta_ * w_n_ * state(1) - w_n_square_ * state(0);
153  }
154 
156 
160  {
161  if (zeta_ < 0)
162  {
163  std::cout << "Warning: Damping is negative!" << std::endl;
164  return false;
165  }
166  if (w_n_ < 0)
167  {
168  std::cout << "Warning: Frequency w_n is negative!" << std::endl;
169  return false;
170  }
171  if (g_dc_ < 0)
172  {
173  std::cout << "Warning: Steady state gain is negative!" << std::endl;
174  return false;
175  }
176  if (g_dc_ == 0)
177  {
178  std::cout << "Warning: Steady state gain is zero!" << std::endl;
179  return false;
180  }
181 
182  return true;
183  }
184 
187  {
188  std::cout << "Frequency: " << w_n_ << std::endl;
189  std::cout << "Zeta: " << zeta_ << std::endl;
190  std::cout << "DC gain: " << g_dc_ << std::endl;
191 
192  std::cout << "System is ";
193  if (zeta_ == 0.0)
194  {
195  std::cout << "undamped" << std::endl;
196  }
197  if (zeta_ == 1.0)
198  {
199  std::cout << "critically damped" << std::endl;
200  }
201  if (zeta_ > 1.0)
202  {
203  std::cout << "overdamped" << std::endl;
204  }
205  if (zeta_ < 1.0)
206  {
207  std::cout << "underdamped" << std::endl;
208  }
209  }
210 
211 private:
212  SCALAR w_n_;
213  SCALAR w_n_square_;
214  SCALAR zeta_;
215  SCALAR g_dc_;
216 };
217 
218 } // namespace tpl
219 
221 
222 } // namespace core
223 } // namespace ct
void setDynamics(SCALAR w_n, SCALAR zeta=SCALAR(1.0), SCALAR g_dc=SCALAR(1.0))
set the dynamics
Definition: SecondOrderSystem.h:131
Base::time_t time_t
Definition: ControlledSystem.h:53
SecondOrderSystem * clone() const override
deep copy
Definition: SecondOrderSystem.h:122
const double zeta
SecondOrderSystem(const SecondOrderSystem &arg)
copy constructor
Definition: SecondOrderSystem.h:95
Describes a damped oscillator.
Definition: SecondOrderSystem.h:62
bool checkParameters()
check the parameters
Definition: SecondOrderSystem.h:159
virtual void computeControlledDynamics(const StateVector< 2, SCALAR > &state, const time_t &t, const ControlVector< 1, SCALAR > &control, StateVector< 2, SCALAR > &derivative) override
evaluate the system dynamics
Definition: SecondOrderSystem.h:146
virtual ~SecondOrderSystem()
destructor
Definition: SecondOrderSystem.h:124
a pure second-order system
Definition: System.h:18
Definition: ControlVector.h:12
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void printSystemInfo()
print out infos about the system
Definition: SecondOrderSystem.h:186
Base::time_t time_t
Definition: SecondOrderSystem.h:69
SecondOrderSystem()=delete
default constructor
SecondOrderSystem(SCALAR k, SCALAR m, SCALAR d, SCALAR g_dc, std::shared_ptr< Controller< 2, 1 >> controller=nullptr)
constructor using a more mechanical definition (spring-mass-damping)
Definition: SecondOrderSystem.h:112
static const size_t CONTROL_DIM
control dimension (force)
Definition: SecondOrderSystem.h:66
SecondOrderSystem(SCALAR w_n, SCALAR zeta=SCALAR(1.0), SCALAR g_dc=SCALAR(1.0), std::shared_ptr< Controller< 2, 1, SCALAR >> controller=nullptr)
constructor directly using frequency and damping coefficients
Definition: SecondOrderSystem.h:82
static const size_t STATE_DIM
state dimension (position, velocity)
Definition: SecondOrderSystem.h:65
SYSTEM_TYPE
type of system
Definition: System.h:15
ControlledSystem< 2, 1, SCALAR > Base
Definition: SecondOrderSystem.h:68
A general, non-linear dynamic system with a control input.
Definition: ControlledSystem.h:46
Interface class for all controllers.
Definition: Controller.h:26