This repository has been archived by the owner on May 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID_Controller.java
79 lines (65 loc) · 2.04 KB
/
PID_Controller.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package org.firstinspires.ftc.utilities;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.firstinspires.ftc.robotcore.external.Telemetry;
/**
* Created by user on 7/10/2018.
*/
public class PID_Controller{
ElapsedTime runtime;
private double setpoint = 0.0;
private double error = 0.0;
private double lastError = 0.0;
private double time = 0.0;
private double lastTime = 0.0;
private double pValue = 0.0;
private double PGAIN;
private double iValue = 0.0;
private double IGAIN;
private double dValue = 0.0;
private double DGAIN;
private boolean firstResetFrame;
public PID_Controller(double PGAIN, double IGAIN, double DGAIN){
this.runtime = new ElapsedTime();
resetPID();
this.PGAIN = PGAIN;
this.IGAIN = IGAIN;
this.DGAIN = DGAIN;
}
public void setSetpoint(double newSetpoint) {
this.setpoint = newSetpoint;
}
public double update(double input){
lastError = error;
lastTime = time;
error = setpoint - input;
time = runtime.seconds();
if (firstResetFrame)
lastError = error;
else
iValue += IGAIN * (lastError + error) * (0.5) * (time - lastTime);
pValue = PGAIN * error;
dValue = DGAIN * (error - lastError) / (time - lastTime);
firstResetFrame = false;
return getPID();
}
public void resetPID() {
resetPID(0.0);
}
public void resetPID(double startingIValue) {
runtime.reset();
iValue = startingIValue;
firstResetFrame = true;
}
public void displayCurrentPID(Telemetry telemetry) {
telemetry.addData("P: ",pValue);
telemetry.addData("I: ",iValue);
telemetry.addData("D: ",dValue);
telemetry.addData("PID: ",getPID());
}
private double getPID() {
return pValue + iValue + dValue;
}
public double getSetpoint() {
return setpoint;
}
}