-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyPIDController.java
executable file
·696 lines (622 loc) · 18.5 KB
/
MyPIDController.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008-2012. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
package org.usfirst.frc5122.Fred2;
import java.util.TimerTask;
import edu.wpi.first.wpilibj.PIDOutput;
import edu.wpi.first.wpilibj.PIDSource;
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
import edu.wpi.first.wpilibj.tables.ITableListener;
import edu.wpi.first.wpilibj.util.BoundaryException;
/**
* Class implements a PID Control Loop.
*
* Creates a separate thread which reads the given PIDSource and takes care of
* the integral calculations, as well as writing the given PIDOutput
*/
public class MyPIDController implements LiveWindowSendable {
public static final double kDefaultPeriod = .05;
private static int instances = 0;
private double m_P; // factor for "proportional" control
private double m_I; // factor for "integral" control
private double m_D; // factor for "derivative" control
private double m_F; // factor for feedforward term
private double m_maximumOutput = 1.0; // |maximum output|
private double m_minimumOutput = -1.0; // |minimum output|
private double m_maximumInput = 0.0; // maximum input - limit setpoint to this
private double m_minimumInput = 0.0; // minimum input - limit setpoint to this
private boolean m_continuous = false; // do the endpoints wrap around? eg. Absolute encoder
private boolean m_enabled = false; // is the pid controller enabled
private double m_prevError = 0.0; // the prior sensor input (used to compute velocity)
private double m_totalError = 0.0; // the sum of the errors for use in the integral calc
private Tolerance m_tolerance; // the tolerance object used to check if on target
private double m_setpoint = 0.0;
private double m_error = 0.0;
private double m_result = 0.0;
private double m_period = kDefaultPeriod;
PIDSource m_pidInput;
PIDOutput m_pidOutput;
java.util.Timer m_controlLoop;
private boolean m_freed = false;
private boolean m_usingPercentTolerance;
/**
* Tolerance is the type of tolerance used to specify if the PID controller
* is on target. The various implementations of this class such as
* PercentageTolerance and AbsoluteTolerance specify types of tolerance
* specifications to use.
*/
public interface Tolerance {
public boolean onTarget();
}
public class PercentageTolerance implements Tolerance {
double percentage;
PercentageTolerance(double value) {
percentage = value;
}
@Override
public boolean onTarget() {
return (Math.abs(getError()) < percentage / 100
* (m_maximumInput - m_minimumInput));
}
}
public class AbsoluteTolerance implements Tolerance {
double value;
AbsoluteTolerance(double value) {
this.value = value;
}
@Override
public boolean onTarget() {
return Math.abs(getError()) < value;
}
}
public class NullTolerance implements Tolerance {
@Override
public boolean onTarget() {
throw new RuntimeException(
"No tolerance value set when using PIDController.onTarget()");
}
}
private class PIDTask extends TimerTask {
private MyPIDController m_controller;
public PIDTask(MyPIDController controller) {
if (controller == null) {
throw new NullPointerException("Given PIDController was null");
}
m_controller = controller;
}
@Override
public void run() {
m_controller.calculate();
}
}
/**
* Allocate a PID object with the given constants for P, I, D, and F
*
* @param Kp
* the proportional coefficient
* @param Ki
* the integral coefficient
* @param Kd
* the derivative coefficient
* @param Kf
* the feed forward term
* @param source
* The PIDSource object that is used to get values
* @param output
* The PIDOutput object that is set to the output percentage
* @param period
* the loop time for doing calculations. This particularly
* effects calculations of the integral and differential terms.
* The default is 50ms.
*/
public MyPIDController(double Kp, double Ki, double Kd, double Kf,
PIDSource source, PIDOutput output, double period) {
if (source == null) {
throw new NullPointerException("Null PIDSource was given");
}
if (output == null) {
throw new NullPointerException("Null PIDOutput was given");
}
m_controlLoop = new java.util.Timer();
m_P = Kp;
m_I = Ki;
m_D = Kd;
m_F = Kf;
m_pidInput = source;
m_pidOutput = output;
m_period = period;
m_controlLoop.schedule(new PIDTask(this), 0L, (long) (m_period * 1000));
instances++;
m_tolerance = new NullTolerance();
}
/**
* Allocate a PID object with the given constants for P, I, D and period
*
* @param Kp
* the proportional coefficient
* @param Ki
* the integral coefficient
* @param Kd
* the derivative coefficient
* @param source
* the PIDSource object that is used to get values
* @param output
* the PIDOutput object that is set to the output percentage
* @param period
* the loop time for doing calculations. This particularly
* effects calculations of the integral and differential terms.
* The default is 50ms.
*/
public MyPIDController(double Kp, double Ki, double Kd, PIDSource source,
PIDOutput output, double period) {
this(Kp, Ki, Kd, 0.0, source, output, period);
}
/**
* Allocate a PID object with the given constants for P, I, D, using a 50ms
* period.
*
* @param Kp
* the proportional coefficient
* @param Ki
* the integral coefficient
* @param Kd
* the derivative coefficient
* @param source
* The PIDSource object that is used to get values
* @param output
* The PIDOutput object that is set to the output percentage
*/
public MyPIDController(double Kp, double Ki, double Kd, PIDSource source,
PIDOutput output) {
this(Kp, Ki, Kd, source, output, kDefaultPeriod);
}
/**
* Allocate a PID object with the given constants for P, I, D, using a 50ms
* period.
*
* @param Kp
* the proportional coefficient
* @param Ki
* the integral coefficient
* @param Kd
* the derivative coefficient
* @param Kf
* the feed forward term
* @param source
* The PIDSource object that is used to get values
* @param output
* The PIDOutput object that is set to the output percentage
*/
public MyPIDController(double Kp, double Ki, double Kd, double Kf,
PIDSource source, PIDOutput output) {
this(Kp, Ki, Kd, Kf, source, output, kDefaultPeriod);
}
/**
* Free the PID object
*/
public void free() {
m_controlLoop.cancel();
synchronized (this) {
m_freed = true;
m_pidOutput = null;
m_pidInput = null;
m_controlLoop = null;
}
if (this.table != null)
table.removeTableListener(listener);
}
/**
* Read the input, calculate the output accordingly, and write to the
* output. This should only be called by the PIDTask and is created during
* initialization.
*/
private void calculate() {
boolean enabled;
PIDSource pidInput;
synchronized (this) {
if (m_pidInput == null) {
return;
}
if (m_pidOutput == null) {
return;
}
enabled = m_enabled; // take snapshot of these values...
pidInput = m_pidInput;
}
if (enabled) {
double input;
double result;
PIDOutput pidOutput = null;
synchronized (this) {
input = pidInput.pidGet();
}
synchronized (this) {
m_error = m_setpoint - input;
if (m_continuous) {
if (Math.abs(m_error) > (m_maximumInput - m_minimumInput) / 2) {
if (m_error > 0) {
m_error = m_error - m_maximumInput + m_minimumInput;
} else {
m_error = m_error + m_maximumInput - m_minimumInput;
}
}
}
if (m_I != 0) {
double potentialIGain = (m_totalError + m_error) * m_I;
if (potentialIGain < m_maximumOutput) {
if (potentialIGain > m_minimumOutput) {
m_totalError += m_error;
} else {
m_totalError = m_minimumOutput / m_I;
}
} else {
m_totalError = m_maximumOutput / m_I;
}
}
m_result = m_P * m_error + m_I * m_totalError + m_D
* (m_error - m_prevError) + m_setpoint * m_F;
m_prevError = m_error;
if (m_result > m_maximumOutput) {
m_result = m_maximumOutput;
} else if (m_result < m_minimumOutput) {
m_result = m_minimumOutput;
}
pidOutput = m_pidOutput;
result = m_result;
}
pidOutput.pidWrite(result);
}
}
/**
* Set the PID Controller gain parameters. Set the proportional, integral,
* and differential coefficients.
*
* @param p
* Proportional coefficient
* @param i
* Integral coefficient
* @param d
* Differential coefficient
*/
public synchronized void setPID(double p, double i, double d) {
m_P = p;
m_I = i;
m_D = d;
if (table != null) {
table.putNumber("p", p);
table.putNumber("i", i);
table.putNumber("d", d);
}
}
/**
* Set the PID Controller gain parameters. Set the proportional, integral,
* and differential coefficients.
*
* @param p
* Proportional coefficient
* @param i
* Integral coefficient
* @param d
* Differential coefficient
* @param f
* Feed forward coefficient
*/
public synchronized void setPID(double p, double i, double d, double f) {
m_P = p;
m_I = i;
m_D = d;
m_F = f;
if (table != null) {
table.putNumber("p", p);
table.putNumber("i", i);
table.putNumber("d", d);
table.putNumber("f", f);
}
}
/**
* Get the Proportional coefficient
*
* @return proportional coefficient
*/
public synchronized double getP() {
return m_P;
}
/**
* Get the Integral coefficient
*
* @return integral coefficient
*/
public synchronized double getI() {
return m_I;
}
/**
* Get the Differential coefficient
*
* @return differential coefficient
*/
public synchronized double getD() {
return m_D;
}
/**
* Get the Feed forward coefficient
*
* @return feed forward coefficient
*/
public synchronized double getF() {
return m_F;
}
/**
* Return the current PID result This is always centered on zero and
* constrained the the max and min outs
*
* @return the latest calculated output
*/
public synchronized double get() {
return m_result;
}
/**
* Set the PID controller to consider the input to be continuous, Rather
* then using the max and min in as constraints, it considers them to be the
* same point and automatically calculates the shortest route to the
* setpoint.
*
* @param continuous
* Set to true turns on continuous, false turns off continuous
*/
public synchronized void setContinuous(boolean continuous) {
m_continuous = continuous;
}
/**
* Set the PID controller to consider the input to be continuous, Rather
* then using the max and min in as constraints, it considers them to be the
* same point and automatically calculates the shortest route to the
* setpoint.
*/
public synchronized void setContinuous() {
this.setContinuous(true);
}
/**
* Sets the maximum and minimum values expected from the input and setpoint.
*
* @param minimumInput
* the minimum value expected from the input
* @param maximumInput
* the maximum value expected from the input
*/
public synchronized void setInputRange(double minimumInput,
double maximumInput) {
if (minimumInput > maximumInput) {
throw new BoundaryException(
"Lower bound is greater than upper bound");
}
m_minimumInput = minimumInput;
m_maximumInput = maximumInput;
setSetpoint(m_setpoint);
}
/**
* Sets the minimum and maximum values to write.
*
* @param minimumOutput
* the minimum percentage to write to the output
* @param maximumOutput
* the maximum percentage to write to the output
*/
public synchronized void setOutputRange(double minimumOutput,
double maximumOutput) {
if (minimumOutput > maximumOutput) {
throw new BoundaryException(
"Lower bound is greater than upper bound");
}
m_minimumOutput = minimumOutput;
m_maximumOutput = maximumOutput;
}
/**
* Set the setpoint for the PIDController
*
* @param setpoint
* the desired setpoint
*/
public synchronized void setSetpoint(double setpoint) {
if (m_maximumInput > m_minimumInput) {
if (setpoint > m_maximumInput) {
m_setpoint = m_maximumInput;
} else if (setpoint < m_minimumInput) {
m_setpoint = m_minimumInput;
} else {
m_setpoint = setpoint;
}
} else {
m_setpoint = setpoint;
}
if (table != null)
table.putNumber("setpoint", m_setpoint);
}
/**
* Returns the current setpoint of the PIDController
*
* @return the current setpoint
*/
public synchronized double getSetpoint() {
return m_setpoint;
}
/**
* Returns the current difference of the input from the setpoint
*
* @return the current error
*/
public synchronized double getError() {
// return m_error;
return getSetpoint() - m_pidInput.pidGet();
}
/**
* Set the percentage error which is considered tolerable for use with
* OnTarget. (Input of 15.0 = 15 percent)
*
* @param percent
* error which is tolerable
* @deprecated Use {@link #setPercentTolerance(double)} or
* {@link #setAbsoluteTolerance(double)} instead.
*/
@Deprecated
public synchronized void setTolerance(double percent) {
m_tolerance = new PercentageTolerance(percent);
}
/**
* Set the PID tolerance using a Tolerance object. Tolerance can be
* specified as a percentage of the range or as an absolute value. The
* Tolerance object encapsulates those options in an object. Use it by
* creating the type of tolerance that you want to use: setTolerance(new
* PIDController.AbsoluteTolerance(0.1))
*
* @param tolerance
* a tolerance object of the right type, e.g. PercentTolerance or
* AbsoluteTolerance
*/
private synchronized void setTolerance(Tolerance tolerance) {
m_tolerance = tolerance;
}
/**
* Set the absolute error which is considered tolerable for use with
* OnTarget.
*
* @param absvalue
* absolute error which is tolerable in the units of the input
* object
*/
public synchronized void setAbsoluteTolerance(double absvalue) {
m_tolerance = new AbsoluteTolerance(absvalue);
}
/**
* Set the percentage error which is considered tolerable for use with
* OnTarget. (Input of 15.0 = 15 percent)
*
* @param percentage
* percent error which is tolerable
*/
public synchronized void setPercentTolerance(double percentage) {
m_tolerance = new PercentageTolerance(percentage);
}
/**
* Return true if the error is within the percentage of the total input
* range, determined by setTolerance. This assumes that the maximum and
* minimum input were set using setInput.
*
* @return true if the error is less than the tolerance
*/
public synchronized boolean onTarget() {
return m_tolerance.onTarget();
}
/**
* Begin running the PIDController
*/
public synchronized void enable() {
m_enabled = true;
if (table != null) {
table.putBoolean("enabled", true);
}
}
/**
* Stop running the PIDController, this sets the output to zero before
* stopping.
*/
public synchronized void disable() {
m_pidOutput.pidWrite(0);
m_enabled = false;
if (table != null) {
table.putBoolean("enabled", false);
}
}
/**
* Return true if PIDController is enabled.
*/
public synchronized boolean isEnable() {
return m_enabled;
}
/**
* Reset the previous error,, the integral term, and disable the controller.
*/
public synchronized void reset() {
disable();
m_prevError = 0;
m_totalError = 0;
m_result = 0;
}
@Override
public String getSmartDashboardType() {
return "PIDController";
}
private final ITableListener listener = new ITableListener() {
@Override
public void valueChanged(ITable table, String key, Object value,
boolean isNew) {
if (key.equals("p") || key.equals("i") || key.equals("d")
|| key.equals("f")) {
if (getP() != table.getNumber("p", 0.0)
|| getI() != table.getNumber("i", 0.0)
|| getD() != table.getNumber("d", 0.0)
|| getF() != table.getNumber("f", 0.0))
setPID(table.getNumber("p", 0.0),
table.getNumber("i", 0.0),
table.getNumber("d", 0.0),
table.getNumber("f", 0.0));
} else if (key.equals("setpoint")) {
if (getSetpoint() != ((Double) value).doubleValue())
setSetpoint(((Double) value).doubleValue());
} else if (key.equals("enabled")) {
if (isEnable() != ((Boolean) value).booleanValue()) {
if (((Boolean) value).booleanValue()) {
enable();
} else {
disable();
}
}
}
}
};
private ITable table;
@Override
public void initTable(ITable table) {
if (this.table != null)
this.table.removeTableListener(listener);
this.table = table;
if (table != null) {
table.putNumber("p", getP());
table.putNumber("i", getI());
table.putNumber("d", getD());
table.putNumber("f", getF());
table.putNumber("setpoint", getSetpoint());
table.putBoolean("enabled", isEnable());
table.addTableListener(listener, false);
}
}
/**
* {@inheritDoc}
*/
@Override
public ITable getTable() {
return table;
}
/**
* {@inheritDoc}
*/
@Override
public void updateTable() {
}
/**
* {@inheritDoc}
*/
@Override
public void startLiveWindowMode() {
disable();
}
/**
* {@inheritDoc}
*/
@Override
public void stopLiveWindowMode() {
}
}