AGX Dynamics  2.35.0.1
PidController1D.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/Vec3.h>
20 #include <agxModel/Controller1D.h>
21 #include <agxStream/Serializable.h>
22 #include <agxModel/export.h>
23 
24 #include <deque>
25 
26 namespace agxModel
27 {
28 
43  {
44  public:
46  {
47  FIRSTORDER = 1 << 0,
48  FIVEPOINTSTENCIL = 1 << 1,
49  THREEPOINTSKEWEDSTENCIL = 1 << 2,
50  };
51 
53 
58  {
59  public:
60  Derivative() : m_lastTime(0), m_lastValue(0) {};
61 
62  virtual agx::Real calculate(const agx::TimeStamp& time, agx::Real value);
63 
64  protected:
66  private:
67  agx::Real m_lastValue;
68  };
69 
78  {
79  public:
80  DerivativeFivePointStencil() : m_fivePoints(5, agx::Real(0)) {};
84  virtual agx::Real calculate(const agx::TimeStamp& time, agx::Real value) override;
85 
86  private:
87  std::vector<agx::Real> m_fivePoints;
88  };
89 
90  AGX_DECLARE_POINTER_TYPES(DerivativeThreePointSkewdStencil);
96  {
97  public:
98  DerivativeThreePointSkewedStencil() : m_threePoints(3, agx::Real(0)) {};
102  virtual agx::Real calculate(const agx::TimeStamp& time, agx::Real value) override;
103  private:
104  std::vector<agx::Real> m_threePoints;
105  };
106 
107  public:
109 
114  virtual void update(const agx::TimeStamp& time, agx::Real measuredProcessVariable) override;
115 
119  virtual agx::Real getManipulatedVariable() const override;
120 
122  virtual void setSetPoint(agx::Real setPoint) override;
123 
125  virtual agx::Real getSetPoint() const override;
126 
130  virtual void reset() override;
131 
134 
136  void setManipulatedVariableLimit(agx::Real lowerLimit, agx::Real upperLimit);
137 
141 
144 
148 
154  void setIntegralWindupProtection(bool doIntegralWindupProtection);
155 
157 
159  void setIntegralWindupClamping(agx::Real lowerLimitIntegralTerm, agx::Real upperLimitIntegralTerm);
160 
164 
167 
170 
172  bool isEnabled();
173 
176 
178  void setGains(agx::Real proportionalGain, agx::Real integralGain, agx::Real derivativeGain);
179 
182 
184  void setProportionalGain(agx::Real proportionalGain);
185 
188 
190  void setIntegralGain(agx::Real integralGain);
191 
194 
198  void setIntegralTime(agx::Real integralTime);
199 
202 
204  void setDerivativeGain(agx::Real derivativeGain);
205 
208 
212  void setDerivativeTime(agx::Real derivativeTime);
213 
215  void setFrequency(agx::Real frequency);
216 
219 
222 
225 
228 
231 
239 
241  void setDerivationMethod(Derivative *derivationMethod);
242 
244  void setDerivationType(DerivationMethod derivationMethod);
245 
246  private:
247  agx::Real integralWindupLimitAcceleration(agx::Real manipulatedVariablePrevious, agx::Real dt) const;
248 
249  agx::Real integralWindupDisableIntegralCalculation(agx::Real u, agx::Real error, agx::Real dt, agx::Real& integralGainTerm) const;
250 
251  agx::Real integralWindupClamping(agx::Real integralGainTerm, agx::Vec2 limits) const;
252 
253  void calculateCost(const agx::TimeStamp& time, const agx::TimeStamp& dt, agx::Real error);
254 
255 
256  private:
257  agx::TimeStamp m_lastTime;
258  agx::Real m_manipulatedVariable;
259  agx::Real m_setPoint;
260  agx::Real m_error;
261  agx::Real m_processVariable;
262  agx::Vec2 m_manipulatedVariableLimit;
263  agx::Vec2 m_manipulatedVariableAccelerationLimit;
264  agx::Real m_proportionalGainCoefficient;
265  agx::Real m_integralGainCoefficient;
266  agx::Real m_derivativeGainCoefficient;
267  // scale derivative/integral gain with this frequency, default is 1
268  agx::Real m_frequency;
269 
270  std::deque<agx::Real> m_cost;
271  std::deque<agx::TimeStamp> m_costTime;
272 
273  // Current value of integral gain, Int(e, 0..t), without coefficient
274  agx::Real m_integralGain;
275  // Current value of derivative gain, dE/dt, without coefficient
276  agx::Real m_derivativeGain;
277  agx::Vec2 m_integralTermLimit;
278 
279  bool m_isEnabled;
280  bool m_doIntegralWindupProtection;
281  DerivativeRef m_derivative;
282 
283  static const int MAX_COST_HISTORY;
284  };
285 }
286 
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXMODEL_EXPORT
A simple base class for a controller with a set point and an out signal.
Definition: Controller1D.h:36
Differentiate a PID controller signal with the "five-point stencil" method.
virtual agx::Real calculate(const agx::TimeStamp &time, agx::Real value) override
Calculate five-point stencil derivative.
Differentiate a PID controller signal with the "three-point stencil" method.
virtual agx::Real calculate(const agx::TimeStamp &time, agx::Real value) override
Calculate three-point skewed stencil derivative.
Differentiate a PID controller signal with a two point estimation.
virtual agx::Real calculate(const agx::TimeStamp &time, agx::Real value)
A PID controller with the classic gain coefficients for proportional, integral, and derivative gain.
void enablePidController()
Enable the PID controller.
void setGains(agx::Real proportionalGain, agx::Real integralGain, agx::Real derivativeGain)
Set the gains.
const agx::Vec2 & getManipulatedVariableLimit() const
void setIntegralGain(agx::Real integralGain)
Set the integral gain, K_i.
virtual void reset() override
Reset calculated gain, error, and cost to 0.
bool hasEnabledIntegralWindupProtection() const
virtual void update(const agx::TimeStamp &time, agx::Real measuredProcessVariable) override
Update the measured Process Variable and calculate a new value for the Manipulated Variable.
virtual agx::Real getManipulatedVariable() const override
agx::Vec3 getGains() const
void setFrequency(agx::Real frequency)
Set PID frequency for gain compensation of the derivative and integral term, default values is 1.
agx::Real getIntegraTerm() const
void disablePidController()
Disable the PID controller. The Manipulated Value will be zero.
agx::Real getMeasuredProcessVariable() const
void setIntegralTime(agx::Real integralTime)
Set the integral time defined as T_i = K_p / K_i.
void setManipulatedVariableLimit(agx::Real lowerLimit, agx::Real upperLimit)
Limit the Manipulated Variable between this lower and upper limit. Default limits are +/- agx::Real m...
void setIntegralWindupProtection(bool doIntegralWindupProtection)
Enable or disable the integral windup backward calculation.
void setDerivativeGain(agx::Real derivativeGain)
Set the derivative gain, K_d.
void setDerivationMethod(Derivative *derivationMethod)
Change the derivation method. Default is the first order numerical derivation method.
virtual void setSetPoint(agx::Real setPoint) override
Set the current Set Point for the controller.
virtual agx::Real getSetPoint() const override
void setIntegralWindupClamping(agx::Real lowerLimitIntegralTerm, agx::Real upperLimitIntegralTerm)
Set the lower and upper limit of the integral gain term, getIntegraTerm().
agx::Real getError() const
void setManipulatedVariableAccelerationLimit(agx::Real lowerLimit, agx::Real upperLimit)
Limit the Manipulated Variable acceleration dMV/dt between this lower and upper limit....
agx::Real getDerivativeGain() const
agx::Real getProportionalGain() const
const agx::Vec2 & getManipulatedVariableAccelerationLimit() const
agx::Real getDerivativeTerm() const
agx::Real getProportionalTerm() const
const agx::Vec2 & getIntegralWindupClamping() const
agx::Real getFrequency() const
void setDerivationType(DerivationMethod derivationMethod)
Change the derivation method. Default is the first order numerical derivation method DerivationMethod...
agx::Real calculateCostFunctionValue(agx::Real deltaTime) const
This will calculate the cost function value between the last time update() was called and last time m...
void setDerivativeTime(agx::Real derivativeTime)
Set the derivative time defined as T_d = K_d / K_p.
agx::Real getIntegralGain() const
void setProportionalGain(agx::Real proportionalGain)
Set the proportional gain, K_p.
Base class providing referencing counted objects.
Definition: Referenced.h:114
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:37
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
double Real
Definition: Real.h:42
agx::Real TimeStamp
Definition: TimeStamp.h:26