Some of you may be aware that the WPILib folk plan to merge a rewrite of the PIDController class in 2020. I present to you a Python version of this rewrite!
You can find the source code for it here:
A summary of the major differences in this rewrite:
- The PID gains are no longer time dependent.
- This does not distinguish between displacement and rate “PID source types”. Instead this PIDController takes a function as its measurement source.
- The feedforward is calculated by a function passed into the PIDController.
- This provides a simple way of running a PID controller synchronously in your robot code (as opposed to having it run in a thread). Simply call the
update()method which will give you the controller output.
Here is an example of using this new PIDController (in hindsight this would be good to have in the readme):
from wpilib_controller import PIDController # assume gyro is a gyro object created prior controller = PIDController(1, 0, 0, measurement_source=gyro.getAngle) controller.setInputRange(0, 360) controller.setContinuous() # setSetpoint is now called setReference controller.setReference(180) # elsewhere... output = controller.update() # do something with the output, for example: motor.set(output)
You can install it from PyPI using pip:
pip install wpilib-controller # to get it on your robot: robotpy-installer download-pip wpilib-controller # with an internet connection robotpy-installer install-pip wpilib-controller # connected to your robot
If you have any questions, please feel free to ask.
PS: For those in charge of usage reporting, are there any suggestions on the best way to track usage of this at competition?