-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPID.java
45 lines (33 loc) · 1.01 KB
/
PID.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.util.ElapsedTime;
public class PID {
private double kp, ki, kd;
private double totalError, lastError, lastTime;
public PID(double kp, double ki, double kd) {
this.kp = kp;
this.ki = ki;
this.kd = kd;
lastTime = 0;
}
public double getP(double error) {
return kp * error;
}
public double getI(double error) {
if (Math.abs(error) < 1)
totalError = 0;
return ki * totalError;
}
public double getD(double currentError, ElapsedTime runtime) {
return kd * (currentError - lastError)/(runtime.time()-lastTime);
}
public double getCorrection(double error, ElapsedTime runtime) {
if (Math.abs(error) < 0.001) {
return 0;
}
totalError += error;
double output = getP(error) + getI(error) + getD(error, runtime);
lastError = error;
lastTime = runtime.time();
return output;
}
}