|
Major WPILib PIDController bug
Over the course of some testing yesterday and today, 449 has uncovered a rather large bug in the java WPILib PIDController object.
The bug concerns the getAvgError() method, and the setSetpoint() method. It occurs as follows:
The getAvgError() method, which is used by the onTarget() method to check if a PID has reached its target, calculates the error (setpoint - position) averaged over the last n iterations of the main loop. The values of the error for each iteration of the loop are stored in a buffer of length n. The sum of the elements in the buffer is stored separately. Each time through the main loop, the main calculate() method of the PID controller object adds the current error to the buffer, adds the value of the current error to the buffer sum, and then, if the buffer has become longer than length n, removes a value from the end of the buffer and subtracts its value from the sum.
Unfortunately, the setSetpoint() method clears the buffer but does not reset the buffer sum to 0! The result of this is that, every time a PID controller's setpoint is changed, any subsequent values returned by getAvgError() will be increased, irreversibly, by the current value of getAvgError().
The upshot of this is that it rapidly renders onTarget() useless for determining if you have, indeed, reached your setpoint.
Edit: Problem was actually in setSetpoint(), not enable(). Fixed to reflect this.
__________________
"Mmmmm, chain grease and aluminum shavings..."
"The breakfast of champions!"
Member, FRC Team 449: 2007-2010
Drive Mechanics Lead, FRC Team 449: 2009-2010
Alumnus/Technical Mentor, FRC Team 449: 2010-Present
Lead Technical Mentor, FRC Team 4464: 2012-2015
Technical Mentor, FRC Team 5830: 2015-2016
Last edited by Oblarg : 17-03-2016 at 21:12.
|