motor_controller
PID.hpp
Go to the documentation of this file.
1 
11 #ifndef CONTROLLERPIDCONTROLLER_H
12 #define CONTROLLERPIDCONTROLLER_H
13 
14 #include <math.h>
15 #include <iostream>
16 #include <iomanip>
17 #include <vector>
18 #include <base/Time.hpp>
19 
21 bool zeroCrossing(double currValue, double prevValue, double refValue = 0);
22 
24 bool positiveZeroCrossing(double currValue, double prevValue, double refValue = 0);
25 
27 bool negativeZeroCrossing(double currValue, double prevValue, double refValue = 0);
28 
29 namespace motor_controller
30 {
32 
36  struct PIDSettings
37  {
39  double Ts;
40 
42  double K;
43 
45  double Ti;
46 
48  double Td;
49 
51 
55  double N;
56 
58 
62  double B;
63 
65 
70  double Tt;
71 
72 
74  double YMin;
76  double YMax;
77 
79  PIDSettings():Ts(0),K(0),Ti(0),Td(0),N(0),B(1),Tt(-1),YMin(0),YMax(0){};
80 
81  bool operator==(const PIDSettings& other) const{
82  return
83  Ts == other.Ts &&
84  K == other.K &&
85  Ti == other.Ti &&
86  Td == other.Td &&
87  N == other.N &&
88  B == other.B &&
89  Tt == other.Tt &&
90  YMin == other.YMin &&
91  YMax == other.YMax;
92  }
93 
94  bool operator!=(const PIDSettings& other) const{
95  return !(*this == other);
96  }
97 
99  double _Kp = 0,
100  double _Ki = 0,
101  double _Kd = 0);
102  };
103 
106  {
108  double Ts;
109 
111  double Kp;
112 
114  double Ki;
115 
117  double Kd;
118 
120 
124  double N;
125 
127 
131  double B;
132 
134 
139  double Tt;
140 
141 
143  double YMin;
145  double YMax;
146 
148  ParallelPIDSettings():Ts(0),Kp(0),Ki(0),Kd(0),N(0),B(1),Tt(-1),YMin(0),YMax(0){};
149 
150  bool operator==(const ParallelPIDSettings& other) const{
151  return
152  Ts == other.Ts &&
153  Kp == other.Kp &&
154  Ki == other.Ki &&
155  Kd == other.Kd &&
156  N == other.N &&
157  B == other.B &&
158  Tt == other.Tt &&
159  YMin == other.YMin &&
160  YMax == other.YMax;
161  }
162 
163  bool operator!=(const ParallelPIDSettings& other) const{
164  return !(*this == other);
165  }
166 
167  void setIdealCoefficients(
168  double _K = 0,
169  double _Ti = 0,
170  double _Td = 0);
171  };
172 
177  struct PIDState
178  {
179  base::Time time;
180 
182  double P, I, D;
183  double input;
184  double reference;
185  double rawOutput;
187  };
188 
196  {
199  };
200 
231  class PID
232  {
233  public:
235  PID();
236 
238 
248  void setParallelCoefficients(double _Ts,
249  double _Kp = 0,
250  double _Ki = 0,
251  double _Kd = 0,
252  double _N = 0,
253  double _B = 1,
254  double _Tt = -1,
255  double _YMin = 0,
256  double _YMax = 0);
257 
259 
269  void setIdealCoefficients (double _Ts,
270  double _K = 0 ,
271  double _Ti = 0,
272  double _Td = 0,
273  double _N = 0,
274  double _B = 1,
275  double _Tt = -1,
276  double _YMin = 0,
277  double _YMax = 0);
279  void setParallelPIDSettings(const ParallelPIDSettings &_settings);
280 
282  void setPIDSettings(const PIDSettings &_settings);
283 
285  double saturate ( double _val );
287 
292  double update ( double _measuredValue, double _referenceValue, double time = 0.0 );
293 
295  void reset();
296 
298  void printCoefficients();
299 
300 
302  void disableIntegral();
303 
305  void enableIntegral();
306 
308  void enableIntegral(double _Ti);
309 
311  bool isIntegralEnabled() const { return bIntegral; }
312 
313 
315  void disableDerivative();
316 
318  void enableDerivative();
319 
321  void enableDerivative(double _Td);
322 
324  bool isDerivativeEnabled() const { return bDerivative; }
325 
327  void disableDerivativeFiltering();
328 
330  void enableDerivativeFiltering();
331 
333  void enableDerivativeFiltering(double _N);
334 
336  bool isDerivativeFilteringEnabled() const { return bDerivativeFiltering; }
337 
339 
343  void setDerivativeMode(DerivativeMode mode);
344 
346  DerivativeMode getDerivativeMode() const;
347 
349  bool isSaturated();
350 
352  PIDState getState() const;
353 
354  private:
355 
357  struct PIDSettings gains;
358 
360  bool initialized;
361 
363  double prevValue;
364 
366  double prevError;
367 
369  double prevReferenceValue;
370 
372  double Bi, Ad, Bd, Ao;
374  double P, I, D, rawCommand, saturatedCommand;
375 
377  bool bIntegral;
379  bool bDerivative;
381  bool bDerivativeFiltering;
383  DerivativeMode derivativeMode;
384 
386  double Kold;
388  double Bold;
389 
391  bool firstRun;
392 
394  bool bSaturated;
395 
397  void computeCoefficients();
398 
400  // whether the corresponding gains are set
401  void autoEnableModes();
402  };
403 
406  {
407  public:
408  PIDAutoTuning();
409  PIDAutoTuning(double _Ts,
410  double _stepRef = 1.0,
411  double _inputAmplitude = 0.1,
412  double _testTimeSec = 10.0);
413 
414  void setCoefficients(double _Ts,
415  double _stepRef = 1.0,
416  double _inputAmplitude = 0.1,
417  double _testTimeSec = 10.0);
418 
419  double update(double _measuredValue);
420 
421  void getTunedP(double &_Kp); // Tuned value for P controller
422  void getTunedPI(double &_Kp, double &_Ti); // Tuned values for interacting PI controller
423  void getTunedPID(double &_Kp, double &_Ti, double &_Td); // Tuned values for interacting PID controller
424 
425  private:
426  double Ts; // Sampling time
427  double testTimeSec; // Total time period of the test
428 
429  double stepRef; // Step reference input
430  double inputAmplitude; // Amplitude of input signal
431 
432  double outputTimePeriodSec; // Time period of output signal
433  double outputAmplitude; // Amplitude of output signal
434 
435  double initialValue, // Start value of the output
436  prevError; // Measured value in the last step
437 
438  double error;
439 
440  bool firstRun; // true if first run
441  bool firstZeroCrossing;
442 
443  double currTime; // Current time
444  double deltaOutputTime; // Delta time for output wave
445 
446  double maxAmplitude, minAmplitude; // Minimum and maximum amplitude
447 
448  double ultimateGain; // Gain for computation of parameters
449  };
450 
461  {
462  public:
464  PIDStepResponseProperties(double _Ts,
465  double _riseTimeFractionReference = 1.0,
466  double _settlingTimeFractionReference = 0.05);
467 
468  void setCoefficients(double _Ts,
469  double _riseTimeFractionReference = 1.0,
470  double _settlingTimeFractionReference = 0.05);
471 
472  void reset();
473 
474  void update(double _actualOutput,
475  double _refInput);
476 
477  void getProperties(double &_riseTimeSec,
478  double &_settlingTimeSec,
479  double &_percentOvershoot,
480  double &_steadyStateError);
481 
482  void printProperties();
483 
484  private:
485  double Ts;
486 
487  double riseTimeSec; // Rise time is seconds
488  double settlingTimeSec; // Settling time in seconds
489  double percentOvershoot; // Percentage Overshoot
490  double steadyStateError; // Steady state error
491  double steadyStateErrorTimeSec;
492  double squaredError;
493  std::vector<double> maxAmplitudes;
494  double maxAmplitude;
495 
496  double riseTimeFractionReference; // Measures the rise time to the fraction of reference value
497  double settlingTimeFractionReference; // Measure the settling time limit within the fraction of reference value
498 
499  bool firstRun;
500  double prevOutput;
501  double currTime; // Current time in seconds
502 
503  bool riseTimeDetected;
504  bool firstZeroCrossing;
505  };
506 }
507 
508 #endif
PID Implementation in C++.
Definition: PID.hpp:231
double Kp
Proportional gain.
Definition: PID.hpp:111
bool operator!=(const ParallelPIDSettings &other) const
Definition: PID.hpp:163
double Ki
Integral gain.
Definition: PID.hpp:114
double N
Derivative term.
Definition: PID.hpp:124
PIDStepResponseProperties()
Definition: PID.hpp:463
double K
Proportional gain.
Definition: PID.hpp:42
bool initialized
Definition: PID.hpp:181
Definition: PID.hpp:198
double saturatedOutput
Definition: PID.hpp:186
double YMax
Maximum output value.
Definition: PID.hpp:145
double YMax
Maximum output value.
Definition: PID.hpp:76
Definition: InputShaper.hpp:22
double reference
Definition: PID.hpp:184
base::Time time
Definition: PID.hpp:179
double Ti
Integral time constant, 0 to disable it.
Definition: PID.hpp:45
bool isIntegralEnabled() const
Returns whether integral part is enabled.
Definition: PID.hpp:311
ParallelPIDSettings()
Constructor.
Definition: PID.hpp:148
double N
Derivative term.
Definition: PID.hpp:55
double rawOutput
Definition: PID.hpp:185
double B
Setpoint weighing term.
Definition: PID.hpp:131
double YMin
Minimum output value.
Definition: PID.hpp:143
Structure to hold the PID parameters for &#39;IDEAL&#39; type PID.
Definition: PID.hpp:36
double Ts
Sampling time in seconds.
Definition: PID.hpp:108
double Ts
Sampling time in seconds.
Definition: PID.hpp:39
double B
Setpoint weighing term.
Definition: PID.hpp:62
double Td
Derivative time constant, 0 to disable it.
Definition: PID.hpp:48
bool isDerivativeEnabled() const
Returns whether derivative part is enabled.
Definition: PID.hpp:324
double P
Definition: PID.hpp:182
bool operator==(const PIDSettings &other) const
Definition: PID.hpp:81
double YMin
Minimum output value.
Definition: PID.hpp:74
bool negativeZeroCrossing(double currValue, double prevValue, double refValue=0)
Detects negative zero crossing.
Definition: PID.cpp:24
double Tt
Anti-integrator-windup.
Definition: PID.hpp:70
PID auto tuning using Relay Feedback.
Definition: PID.hpp:405
double Kd
Derivative gain.
Definition: PID.hpp:117
Definition: PID.hpp:177
PIDSettings()
Constructor.
Definition: PID.hpp:79
DerivativeMode
Definition: PID.hpp:195
bool zeroCrossing(double currValue, double prevValue, double refValue=0)
Detects zero crossing.
Definition: PID.cpp:7
Definition: PID.hpp:197
bool operator==(const ParallelPIDSettings &other) const
Definition: PID.hpp:150
bool operator!=(const PIDSettings &other) const
Definition: PID.hpp:94
Structure to hold the PID parameters for &#39;PARALLEL&#39; type PID.
Definition: PID.hpp:105
void setParallelCoefficients(double _Kp=0, double _Ki=0, double _Kd=0)
Definition: PID.cpp:31
double input
Definition: PID.hpp:183
bool isDerivativeFilteringEnabled() const
Returns whether derivative filtering part is enabled.
Definition: PID.hpp:336
bool positiveZeroCrossing(double currValue, double prevValue, double refValue=0)
Detects positive zero crossing.
Definition: PID.cpp:16
double Tt
Anti-integrator-windup.
Definition: PID.hpp:139