AGX Dynamics  2.37.2.0
PidController1D.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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:
108  PidController1D();
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 
133  agx::Real getMeasuredProcessVariable() const;
134 
136  void setManipulatedVariableLimit(agx::Real lowerLimit, agx::Real upperLimit);
137 
140  const agx::Vec2& getManipulatedVariableLimit() const;
141 
143  void setManipulatedVariableAccelerationLimit(agx::Real lowerLimit, agx::Real upperLimit);
144 
147  const agx::Vec2& getManipulatedVariableAccelerationLimit() const;
148 
154  void setIntegralWindupProtection(bool doIntegralWindupProtection);
155 
156  bool hasEnabledIntegralWindupProtection() const;
157 
159  void setIntegralWindupClamping(agx::Real lowerLimitIntegralTerm, agx::Real upperLimitIntegralTerm);
160 
163  const agx::Vec2& getIntegralWindupClamping() const;
164 
166  void enablePidController();
167 
169  void disablePidController();
170 
172  bool isEnabled();
173 
175  agx::Vec3 getGains() const;
176 
178  void setGains(agx::Real proportionalGain, agx::Real integralGain, agx::Real derivativeGain);
179 
181  agx::Real getProportionalGain() const;
182 
184  void setProportionalGain(agx::Real proportionalGain);
185 
187  agx::Real getIntegralGain() const;
188 
190  void setIntegralGain(agx::Real integralGain);
191 
193  agx::Real getIntegralTime();
194 
198  void setIntegralTime(agx::Real integralTime);
199 
201  agx::Real getDerivativeGain() const;
202 
204  void setDerivativeGain(agx::Real derivativeGain);
205 
207  agx::Real getDerivativeTime();
208 
212  void setDerivativeTime(agx::Real derivativeTime);
213 
215  void setFrequency(agx::Real frequency);
216 
218  agx::Real getFrequency() const;
219 
221  agx::Real getError() const;
222 
224  agx::Real getProportionalTerm() const;
225 
227  agx::Real getIntegraTerm() const;
228 
230  agx::Real getDerivativeTerm() const;
231 
238  agx::Real calculateCostFunctionValue(agx::Real deltaTime) const;
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 
Differentiate a PID controller signal with a two point estimation.
Differentiate a PID controller signal with the "five-point stencil" method.
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
agx::Real TimeStamp
Definition: TimeStamp.h:26
Base class providing referencing counted objects.
Definition: Referenced.h:119
Differentiate a PID controller signal with the "three-point stencil" method.
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:36
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
A simple base class for a controller with a set point and an out signal.
Definition: Controller1D.h:35
A PID controller with the classic gain coefficients for proportional, integral, and derivative gain...
#define AGXMODEL_EXPORT
double Real
Definition: Real.h:42
uint32_t UInt32
Definition: Integer.h:40