PIDController

class wpilib.PIDController(*args, **kwargs)[source]

Bases: wpilib.LiveWindowSendable

Can be used to control devices via 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.

Allocate a PID object with the given constants for P, I, D, and F

Arguments can be structured as follows:

  • Kp, Ki, Kd, Kf, PIDSource, PIDOutput, period
  • Kp, Ki, Kd, PIDSource, PIDOutput, period
  • Kp, Ki, Kd, PIDSource, PIDOutput
  • Kp, Ki, Kd, Kf, PIDSource, PIDOutput
Parameters:
  • Kp (float or int) – the proportional coefficient
  • Ki (float or int) – the integral coefficient
  • Kd (float or int) – the derivative coefficient
  • Kf (float or int) – the feed forward term
  • source (A function, or an object that implements PIDSource) – Called to get values
  • output (A function, or an object that implements PIDOutput) – Receives the output percentage
  • period (float or int) – the loop time for doing calculations. This particularly effects calculations of the integral and differential terms. The default is 50ms.
AbsoluteTolerance_onTarget(value)[source]
class PIDSourceType

Bases: object

A description for the type of output value to provide to a PIDController

kDisplacement = 0
kRate = 1
PIDController.PercentageTolerance_onTarget(percentage)[source]
PIDController.calculateFeedForward()[source]

Calculate the feed forward term

Both of the provided feed forward calculations are velocity feed forwards. If a different feed forward calculation is desired, the user can override this function and provide his or her own. This function does no synchronization because the PIDController class only calls it in synchronized code, so be careful if calling it oneself.

If a velocity PID controller is being used, the F term should be set to 1 over the maximum setpoint for the output. If a position PID controller is being used, the F term should be set to 1 over the maximum speed for the output measured in setpoint units per this controller’s update period (see the default period in this class’s constructor).

PIDController.disable()[source]

Stop running the PIDController, this sets the output to zero before stopping.

PIDController.enable()[source]

Begin running the PIDController.

PIDController.free()[source]

Free the PID object

PIDController.get()[source]

Return the current PID result. This is always centered on zero and constrained the the max and min outs.

Returns:the latest calculated output
PIDController.getAvgError()[source]

Returns the current difference of the error over the past few iterations. You can specify the number of iterations to average with setToleranceBuffer() (defaults to 1). getAvgError() is used for the onTarget() function.

Returns:the current average of the error
PIDController.getD()[source]

Get the Differential coefficient.

Returns:differential coefficient
PIDController.getDeltaSetpoint()[source]

Returns the change in setpoint over time of the PIDController

Returns:the change in setpoint over time
PIDController.getError()[source]

Returns the current difference of the input from the setpoint.

Returns:the current error
PIDController.getF()[source]

Get the Feed forward coefficient.

Returns:feed forward coefficient
PIDController.getI()[source]

Get the Integral coefficient

Returns:integral coefficient
PIDController.getP()[source]

Get the Proportional coefficient.

Returns:proportional coefficient
PIDController.getPIDSourceType(pidSourceType)[source]

Returns the type of input the PID controller is using

Returns:the PID controller input type
PIDController.getSetpoint()[source]

Returns the current setpoint of the PIDController.

Returns:the current setpoint
PIDController.instances = 0
PIDController.isAvgErrorValid()[source]

Returns whether or not any values have been collected. If no values have been collected, getAvgError is 0, which is invalid.

Returns:True if getAvgError() is currently valid.
PIDController.isEnable()[source]

Return True if PIDController is enabled.

PIDController.kDefaultPeriod = 0.05
PIDController.onTarget()[source]

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().

Returns:True if the error is less than the tolerance
PIDController.reset()[source]

Reset the previous error, the integral term, and disable the controller.

PIDController.setAbsoluteTolerance(absvalue)[source]

Set the absolute error which is considered tolerable for use with onTarget().

Parameters:absvalue – absolute error which is tolerable in the units of the input object
PIDController.setContinuous(continuous=True)[source]

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.

Parameters:continuous – Set to True turns on continuous, False turns off continuous
PIDController.setInputRange(minimumInput, maximumInput)[source]

Sets the maximum and minimum values expected from the input.

Parameters:
  • minimumInput – the minimum percentage expected from the input
  • maximumInput – the maximum percentage expected from the output
PIDController.setOutputRange(minimumOutput, maximumOutput)[source]

Sets the minimum and maximum values to write.

Parameters:
  • minimumOutput – the minimum percentage to write to the output
  • maximumOutput – the maximum percentage to write to the output
PIDController.setPID(p, i, d, f=0.0)[source]

Set the PID Controller gain parameters. Set the proportional, integral, and differential coefficients.

Parameters:
  • p – Proportional coefficient
  • i – Integral coefficient
  • d – Differential coefficient
  • f – Feed forward coefficient (optional, default is 0.0)
PIDController.setPIDSourceType(pidSourceType)[source]

Sets what type of input the PID controller will use

Parameters:pidSourceType – the type of input
PIDController.setPercentTolerance(percentage)[source]

Set the percentage error which is considered tolerable for use with onTarget(). (Input of 15.0 = 15 percent)

Parameters:percentage – percent error which is tolerable
PIDController.setSetpoint(setpoint)[source]

Set the setpoint for the PIDController.

Parameters:setpoint – the desired setpoint
PIDController.setTolerance(percent)[source]

Set the percentage error which is considered tolerable for use with onTarget(). (Input of 15.0 = 15 percent)

Parameters:percent – error which is tolerable

Deprecated since version 2015.1: Use setPercentTolerance() or setAbsoluteTolerance() instead.

PIDController.setToleranceBuffer(bufLength)[source]

Set the number of previous error samples to average for tolerancing. When determining whether a mechanism is on target, the user may want to use a rolling average of previous measurements instead of a precise position or velocity. This is useful for noisy sensors which return a few erroneous measurements when the mechanism is on target. However, the mechanism will not register as on target for at least the specified bufLength cycles.

Parameters:bufLength (int) – Number of previous cycles to average.