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.

This feedback controller runs in discrete time, so time deltas are not used in the integral and derivative calculations. Therefore, the sample rate affects the controller’s behavior for a given set of PID constants.

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
  • 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.
class PIDSourceType

Bases: object

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

kDisplacement = 0
kRate = 1

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


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


Begin running the PIDController.[source]

Free the PID object


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

Returns:the latest calculated output

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

Wraps error around for continuous inputs. The original error is returned if continuous mode is disabled. This is an unsynchronized function.

Parameters:error – The current error of the PID controller.
Returns:Error for continuous inputs.

Get the Differential coefficient.

Returns:differential coefficient

Returns the change in setpoint over time of the PIDController

Returns:the change in setpoint over time

Returns the current difference of the input from the setpoint.

Returns:the current error

Get the Feed forward coefficient.

Returns:feed forward coefficient

Get the Integral coefficient

Returns:integral coefficient

Get the Proportional coefficient.

Returns:proportional coefficient

Returns the type of input the PID controller is using

Returns:the PID controller input type

Returns the current setpoint of the PIDController.

Returns:the current setpoint
PIDController.instances = 0

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.

Return True if PIDController is enabled.

PIDController.kDefaultPeriod = 0.05

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

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


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

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.

  • 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.

  • 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.

  • p – Proportional coefficient
  • i – Integral coefficient
  • d – Differential coefficient
  • f – Feed forward coefficient (optional, default is 0.0)

Sets what type of input the PID controller will use

Parameters:pidSourceType – the type of input

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

Set the setpoint for the PIDController Clears the queue for GetAvgError().

Parameters:setpoint – the desired setpoint

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.


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.