forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPIDController.java
More file actions
66 lines (45 loc) · 1.5 KB
/
PIDController.java
File metadata and controls
66 lines (45 loc) · 1.5 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
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.firstinspires.ftc.teamcode;
public class PIDController {
double Kp;
double Ki;
double Kd;
double previousError = 0;
double integral = 0.0;
double sampleTime = 1.0;
double target = 0.0;
double output = 0.0;
double deadbandSizeCoef = 0.7;
double deadbandDepthCoef = 0.9;
PIDController(double Kp, double Ki, double Kd) {
this.Kp = Kp;
this.Ki = Ki;
this.Kd = Kd;
this.target = 0;
}
void setCoefs(double Kp, double Ki, double Kd) {
this.Kp = Kp;
this.Ki = Ki;
this.Kd = Kd;
}
void setTarget(double target) {
this.target = target;
}
double loop(double value) {
double error = calculateDeadband(this.target - value);
double proportional = error;
integral += error * sampleTime;
// integral = Math.min(Math.max(integral, -100.0), 100.0);
double derivative = (error - previousError) / sampleTime;
this.output = Kp * proportional + Ki * integral + Kd * derivative;
previousError = error;
return output;
}
public void setDeadbandStuff(double deadbandSizeCoef, double deadbandDepthCoef) {
this.deadbandSizeCoef = deadbandSizeCoef;
this.deadbandDepthCoef = deadbandDepthCoef;
}
double calculateDeadband(double v) {
double x = this.deadbandSizeCoef * v;
return v * (1 - (4 * this.deadbandDepthCoef) * (1/(1+Math.pow(Math.E, x))) * (1-1/(1+Math.pow(Math.E, x))));
}
}