13 template<
typename T>
class PID
38 bool proportialOnError =
true) :
50 fLastTime = millis() - fSampleTime;
55 uint32_t now = millis();
56 uint32_t diff = (now - fLastTime);
57 if (fAuto && diff >= fSampleTime)
61 T error = fSetpoint - input;
62 T dInput = (input - fLastInput);
63 fOutputSum += (fKi * error);
67 fOutputSum-= fKp * dInput;
68 if (fOutputSum > fOutMax)
70 else if (fOutputSum < fOutMin)
74 output += fOutputSum - fKd * dInput;
78 else if (output < fOutMin)
91 if (automatic && !fAuto)
113 if (fOutput > fOutMax)
115 else if (fOutput < fOutMin)
118 if (fOutputSum > fOutMax)
119 fOutputSum = fOutMax;
120 else if (fOutputSum < fOutMin)
121 fOutputSum = fOutMin;
127 if (Kp < 0 || Ki < 0 || Kd < 0)
130 fPOnError = pOnError;
136 T sampleTimeInSec = ((T)fSampleTime) / 1000;
138 fKi = Ki * sampleTimeInSec;
139 fKd = Kd / sampleTimeInSec;
156 if (fAuto && fDirection != direction)
162 fDirection = direction;
167 T ratio = (T)sampleTime / (T)fSampleTime;
170 fSampleTime = (
unsigned long)sampleTime;
176 inline T
getKp()
const {
return fKpOrg; }
177 inline T
getKi()
const {
return fKiOrg; }
178 inline T
getKd()
const {
return fKdOrg; }
203 uint32_t fSampleTime;
209 fOutputSum = fOutput;
211 if (fOutputSum > fOutMax)
212 fOutputSum = fOutMax;
213 else if (fOutputSum < fOutMin)
214 fOutputSum = fOutMin;