Fix some errors
[3501/2017steamworks] / src / org / usfirst / frc / team3501 / robot / utils / PIDController.java
CommitLineData
0788fd3d
CZ
1package org.usfirst.frc.team3501.robot.utils;
2
424c070c 3import org.usfirst.frc.team3501.robot.Constants;
0788fd3d
CZ
4import org.usfirst.frc.team3501.robot.MathLib;
5
424c070c
E
6import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
7import edu.wpi.first.wpilibj.tables.ITable;
8import edu.wpi.first.wpilibj.tables.ITableListener;
9
10public class PIDController implements LiveWindowSendable {
0788fd3d
CZ
11
12 private double pConst;
13 private double iConst;
14 private double dConst;
15 private double setPoint;
16 private double previousError;
17 private double errorSum;
18 private double errorIncrement;
19 private double iRange;
20 private double doneRange;
21 private boolean firstCycle;
22 private double maxOutput;
23 private int minDoneCycleCount;
24 private int doneCycleCount;
424c070c 25 private ITable table;
0788fd3d
CZ
26
27 public PIDController() {
28 this(0.0, 0.0, 0.0, 0.0, 0.0);
29 }
30
31 public PIDController(double p, double i, double d, double eps,
32 double iRange) {
33 this.pConst = p;
34 this.iConst = i;
35 this.dConst = d;
36 this.setPoint = 0.0;
37
38 this.iRange = iRange;
39 this.errorIncrement = 1.0;
40
41 this.doneRange = eps;
42 this.doneCycleCount = 0;
43 this.minDoneCycleCount = 5;
44
45 this.firstCycle = true;
46
47 this.maxOutput = 1.0;
48
49 }
50
51 public PIDController(double p, double i, double d, double eps) {
52 this(p, i, d, eps, eps * 0.8);
53 }
54
55 public PIDController(double p, double i, double d) {
56 this(p, i, d, 1.0);
57 }
58
59 public void setConstants(double p, double i, double d) {
60 this.pConst = p;
61 this.iConst = i;
62 this.dConst = d;
63 }
64
65 public void setConstants(double p, double i, double d, double eps,
66 double iRange) {
67 this.pConst = p;
68 this.iConst = i;
69 this.dConst = d;
70 this.doneRange = eps; // range of error
71 this.iRange = iRange;
72 }
73
74 public void setConstants(double p, double i, double d, double eps) {
75 setConstants(p, i, d, eps, eps * 0.8);
76 }
77
78 public void setDoneRange(double range) {
79 this.doneRange = range;
80 }
81
82 public void setIRange(double eps) {
83 this.iRange = eps;
84 }
85
424c070c
E
86 public void setSetPoint(double VAL) {
87 this.setPoint = VAL;
0788fd3d
CZ
88 }
89
90 public void setMaxOutput(double max) {
91 if (max < 0.0) {
92 this.maxOutput = 0.0;
93 } else if (max > 1.0) {
94 this.maxOutput = 1.0;
95 } else {
96 this.maxOutput = max;
97 }
98 }
99
100 public void setMinDoneCycles(int num) {
101 this.minDoneCycleCount = num;
102 }
103
104 public void resetErrorSum() {
105 this.errorSum = 0.0;
106 }
107
424c070c 108 public double getDesiredVAL() {
0788fd3d
CZ
109 return this.setPoint;
110 }
111
112 public double calcPID(double current) {
113 return calcPIDError(this.setPoint - current);
114 }
115
116 public double calcPIDError(double error) {
424c070c
E
117 double pVAL = 0.0;
118 double iVAL = 0.0;
119 double dVAL = 0.0;
0788fd3d
CZ
120
121 if (this.firstCycle) {
122 this.previousError = error;
123 this.firstCycle = false;
124 }
125
424c070c 126 pVAL = this.pConst * error;
0788fd3d
CZ
127
128 // + error outside of acceptable range which might distort the sum calc
129 if (error > this.iRange) {
130 // check if error sum was in the wrong direction
131 if (this.errorSum < 0.0) {
132 this.errorSum = 0.0;
133 }
134 // only allow up to the max contribution per cycle
135 this.errorSum += Math.min(error, this.errorIncrement);
136 } // - error outside of acceptable range
137 else if (error < -1.0 * this.iRange) {
138 // error sum was in the wrong direction
139 if (this.errorSum > 0.0) {
140 this.errorSum = 0.0;
141 }
142 // add either the full error or the max allowable amount to sum
143 this.errorSum += Math.max(error, -1.0 * this.errorIncrement);
144 }
145
146 // i contribution (final) calculation
424c070c 147 iVAL = this.iConst * this.errorSum;
0788fd3d
CZ
148
149 // /////D Calc///////
150 double deriv = error - this.previousError;
424c070c 151 dVAL = this.dConst * deriv;
0788fd3d
CZ
152
153 // overal PID calc
424c070c 154 double output = pVAL + iVAL + dVAL;
0788fd3d
CZ
155
156 // limit the output
157 output = MathLib.limitValue(output, this.maxOutput);
158
424c070c 159 // store current VALue as previous for next cycle
0788fd3d
CZ
160 this.previousError = error;
161 return output;
162 }
163
164 public boolean isDone() {
165 double currError = Math.abs(this.previousError);
166
167 // close enough to target
168 if (currError <= this.doneRange) {
169 this.doneCycleCount++;
0788fd3d
CZ
170 }
171 // not close enough to target
172 else {
173 this.doneCycleCount = 0;
174 }
175
176 return this.doneCycleCount >= this.minDoneCycleCount;
177 }
424c070c
E
178
179 private final ITableListener m_listener = (table, key, value, isNew) -> {
180 if (key.equals(Constants.DriveTrain.P_VAL)
181 || key.equals(Constants.DriveTrain.I_VAL)
da6ceb76 182 || key.equals(Constants.DriveTrain.D_VAL)) {
424c070c
E
183 if (getP() != table.getNumber(Constants.DriveTrain.P_VAL, 0.0)
184 || getP() != table.getNumber(Constants.DriveTrain.I_VAL, 0.0)
185 || getD() != table.getNumber(Constants.DriveTrain.D_VAL, 0.0)) {
186 setConstants(table.getNumber(Constants.DriveTrain.P_VAL, 0.0),
187 table.getNumber(Constants.DriveTrain.I_VAL, 0.0),
da6ceb76 188 table.getNumber(Constants.DriveTrain.D_VAL, 0.0));
424c070c
E
189 }
190 } else if (key.equals(Constants.DriveTrain.SETPOINT)) {
191 if (getSetpoint() != (Double) value) {
192 setSetPoint((Double) value);
193 }
194 }
195 };
196
197 @Override
198 public void initTable(ITable table) {
199
200 if (this.table != null) {
201 table.removeTableListener(m_listener);
202 }
203 this.table = table;
204 if (table != null) {
205 table.putNumber(Constants.DriveTrain.P_VAL, getP());
206 table.putNumber(Constants.DriveTrain.I_VAL, getI());
207 table.putNumber(Constants.DriveTrain.D_VAL, getD());
208 table.putNumber(Constants.DriveTrain.SETPOINT, getSetpoint());
209 table.addTableListener(m_listener, false);
210 }
211
212 }
213
214 private double getP() {
215 return this.pConst;
216 }
217
218 private double getI() {
219 return this.iConst;
220 }
221
222 private double getD() {
223 return this.dConst;
224 }
225
226 private double getSetpoint() {
227 return this.setPoint;
228 }
229
230 @Override
231 public ITable getTable() {
232 // TODO Auto-generated method stub
233 return null;
234 }
235
236 @Override
237 public String getSmartDashboardType() {
238 return "Drive Controller";
239 }
240
241 @Override
242 public void updateTable() {
243
244 }
245
246 @Override
247 public void startLiveWindowMode() {
248 // TODO Auto-generated method stub
249
250 }
251
252 @Override
253 public void stopLiveWindowMode() {
254 // TODO Auto-generated method stub
255
256 }
0788fd3d 257}