libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
pidcontroller.h
Go to the documentation of this file.
1
9#pragma once
10
12#include <functional>
13
15
29template <typename ValueType = double, typename TimeType = double>
31 public:
33 using CurrentTimeFunc = std::function<TimeType()>;
35 using CurrentValueFunc = std::function<ValueType()>;
36
43 : _gainP(1), _gainI(0), _gainD(0),
44 _setPoint(0), _integral(0), _prevError(0),
45 _currentValue(currentValueFunc), _currentTime(currentTimeFunc),
46 _prevUpdate(_currentTime()) {}
47
49 ValueType getGainP() const { return _gainP; }
50
55 void setGainP(const ValueType &val) { _gainP = val; }
56
58 ValueType getGainI() const { return _gainI; }
59
64 void setGainI(const ValueType &val) { _gainI = val; }
65
67 ValueType getGainD() const { return _gainD; }
68
73 void setGainD(const ValueType &val) { _gainD = val; }
74
79 void updateSetPoint(const ValueType &val) { _setPoint = val; }
80
91 TimeType now = _currentTime();
92 TimeType dt = now - _prevUpdate;
93 _prevUpdate = now;
94
95 ValueType pv = _currentValue();
96 ValueType error = _setPoint - pv;
97 _integral += error * dt;
98 ValueType derivative = (error - _prevError) / dt;
99 _prevError = error;
100
101 return _gainP * error + _gainI * _integral + _gainD * derivative;
102 }
103
105 void reset() {
106 _integral = {};
107 _prevError = {};
108 _prevUpdate = _currentTime();
109 return;
110 }
111
112private:
113 ValueType _gainP = 1;
114 ValueType _gainI = 1;
115 ValueType _gainD = 1;
116 ValueType _integral = 0;
117 ValueType _prevError = 0;
118 ValueType _setPoint = 0;
119 CurrentValueFunc _currentValue;
120 CurrentTimeFunc _currentTime;
121 TimeType _prevUpdate = 0;
122};
123
124
126
127
Dynamic array container wrapping std::vector.
Definition list.h:40
Discrete Proportional-Integral-Derivative (PID) controller.
Definition pidcontroller.h:30
void updateSetPoint(const ValueType &val)
Updates the set point (target value the PID should converge on).
Definition pidcontroller.h:79
void reset()
Resets the integral accumulator, previous error, and update timestamp.
Definition pidcontroller.h:105
ValueType step()
Advances the PID by one time step and returns the control output.
Definition pidcontroller.h:90
void setGainI(const ValueType &val)
Sets the integral gain.
Definition pidcontroller.h:64
PIDController(CurrentValueFunc currentValueFunc, CurrentTimeFunc currentTimeFunc)
Constructs a PID controller with the given feedback callbacks.
Definition pidcontroller.h:42
std::function< TimeType()> CurrentTimeFunc
Callback type that returns the current time.
Definition pidcontroller.h:33
void setGainD(const ValueType &val)
Sets the derivative gain.
Definition pidcontroller.h:73
std::function< ValueType()> CurrentValueFunc
Callback type that returns the current measured process value.
Definition pidcontroller.h:35
ValueType getGainD() const
Returns the derivative gain.
Definition pidcontroller.h:67
void setGainP(const ValueType &val)
Sets the proportional gain.
Definition pidcontroller.h:55
ValueType getGainI() const
Returns the integral gain.
Definition pidcontroller.h:58
ValueType getGainP() const
Returns the proportional gain.
Definition pidcontroller.h:49
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const Error & error(const Result< T > &r)
Returns the error from a Result.
Definition result.h:65