PIDController help

We’ve been experimenting with the PIDController class in WPILib. So far, we were able to get our motors spinning, but because of the amount of switching back and forth around the setpoint, the motors end up cycling a lot, dangerously. In order to remedy this, we attempted to use the setAbsoluteTolerance and setPercentTolerance methods from the PIDController, however, they appear to have no effect. For example, when setting the percent tolerance to 15, and with a setpoint of 200, the loop would continue to run even with a value of 195. Absolute tolerance showed the exact same problems. Does anyone know why these methods have no effect on the loop?

We DO have a quick fix, manually checking the error in the loop against a preset tolerance, but we would like to see if we can get it running in the built-in class.

If it helps, we’re running this on a CANTalon with a USDigital encoder set to 4x mode. The loop is running on the RoboRio, not the talon.

Code:

Thanks!

There are a few things causing that. With the distance per count of 1, and a P of 1, that means anytime the encoder is off by more then 1 tick, it will run full output. I would start by setting distance per count to a real unit, such as either rotations, or inches. I would need more information to help you calculate this, but its not too difficult. Just figure out how far your device travels when you rotate the encoder 1 full rotation, then divide that number by the CPR of the encoder. I would then set your P to about 0.01, and start working it up or down from there depending on how you device is set up.

I’ll add that the Set*Tolerance functions are there so that you can later call the OnTarget function to see if the controller is within your desired range. As you observed, the tolerances do not have any effect on the control loop.
If you want the controller to not control when it’s in range you can check to see if the controller is on target. If it is, you can disable, else enable.