forked from arjungandhi/PID-ESP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPID-ESP.cpp
More file actions
53 lines (45 loc) · 1.03 KB
/
PID-ESP.cpp
File metadata and controls
53 lines (45 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "Arduino.h"
#include "PID-ESP.h"
public PID::PID(int kp, int ki, int kd, int maxSpeed, int minSpeed){
this.kp = kp;
this.ki = ki;
this.kd = kd;
this.maxSpeed = maxSpeed;
this.minSpeed = minSpeed;
this.startTime = millis();
oldError = 0;
integralVal = 0;
lastTime = startTime;
}
public double PIDCalculate(double error){
if((PCalc(error)+ICalc(error)+DCalc(error))<minSpeed){
return minSpeed;
}
else if((PCalc(error)+ICalc(error)+DCalc(error))>maxSpeed){
return maxSpeed;
}
else{
return PCalc(error)+ICalc(error)+DCalc(error);
}
PID.this.lastTime=millis();
PID.this.oldError=error;
}
public void setKP(int newKP){
PID.this.kp = newKP;
}
public void setKI(int newKI){
PID.this.ki = newKi;
}
public void setKD(int newKD){
PID.this.kd = newKD;
}
private double PCalc(double error){
return kp*error;
}
private double ICalc(double error){
PID.this.integralVal+=(error*(millis()-lastTime));
return kI*(integralVal);
}
private double DCalc(double error){
return kD*((error-oldError)/(millis()-lastTime));
}