RobotPy wpilib-controller package (2020 PIDController backport)

pid
wpilib

#1

Hey all!

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?


#2

It is too late to add anything else to the usage reporting this year.


#3

Fair enough. I just wasn’t sure whether reporting a normal PIDController instance would be appropriate. I feel it probably isn’t, given it wouldn’t then be reflective of the current WPILib PIDController usage stats.


#4

I just released wpilib-controller 0.4, which fixes a typo in the setInputRange method, and makes putting a PIDController on a dashboard compatible with this year’s dashboards.