API Reference

A backport of the upcoming (in 2020) WPILib PIDController.

class wpilib_controller.PIDController(Kp, Ki, Kd, *, period=0.02)

Bases: wpilib.sendablebase.SendableBase

Class implements a PID Control Loop.

Kd = None

Factor for “derivative” control

Ki = None

Factor for “integral” control

Kp = None

Factor for “proportional” control

__init__(Kp, Ki, Kd, *, period=0.02)

Allocate a PID object with the given constants for Kp, Ki, and Kd.

Parameters
  • Kp (float) – The proportional coefficient.

  • Ki (float) – The integral coefficient.

  • Kd (float) – The derivative coefficient.

  • period (float) – The period between controller updates in seconds. The default is 20ms.

atSetpoint()

Return true if the error is within the percentage of the specified tolerances.

This will return false until at least one input value has been computed.

Return type

bool

calculate(measurement, setpoint=None)

Returns the next output of the PID controller.

Parameters
  • measurement (float) – The current measurement of the process variable.

  • setpoint (Optional[float]) – The new setpoint of the controller if specified.

Return type

float

disableContinuousInput()

Disables continuous input.

Return type

None

enableContinuousInput(minimum_input, maximum_input)

Enable continuous input.

Rather than using the max and min input range as constraints, it considers them to be the same point and automatically calculates the shortest route to the setpoint.

Parameters
  • minimum_input (float) – The minimum value expected from the input.

  • maximum_input (float) – The maximum value expected from the input.

Return type

None

getContinuousError(error)

Wraps error around for continuous inputs.

The original error is returned if continuous mode is disabled.

Parameters

error (float) – The current error of the PID controller.

Return type

float

Returns

Error for continuous inputs.

getPositionError()

Returns the difference between the setpoint and the measurement.

Return type

float

getVelocityError()

Returns the velocity error.

Return type

float

initSendable(builder)

Initializes this Sendable object.

Parameters

builder – sendable builder

Return type

None

period = None

The period (in seconds) of the loop that calls the controller

prev_error = inf

The error at the time of the second-most-recent call to calculate() (used to compute velocity)

reset()

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

Return type

None

setD(Kd)

Set the Differential coefficient of the PID controller gain.

Return type

None

setI(Ki)

Set the Integral coefficient of the PID controller gain.

Return type

None

setIntegratorRange(minimum_integral, maximum_integral)

Sets the minimum and maximum values for the integrator.

When the cap is reached, the integrator value is added to the controller output rather than the integrator value times the integral gain.

Parameters
  • minimum_integral (float) – The minimum value of the integrator.

  • maximum_integral (float) – The maximum value of the integrator.

Return type

None

setP(Kp)

Set the Proportional coefficient of the PID controller gain.

Return type

None

setPID(Kp, Ki, Kd)

Set the PID Controller gain parameters.

Return type

None

setSetpoint(setpoint)

Set the setpoint for the PIDController.

Return type

None

setTolerance(position_tolerance, velocity_tolerance=inf)

Sets the error which is considered tolerable for use with atSetpoint().

Parameters
  • position_tolerance (float) – Position error which is tolerable.

  • velocity_tolerance (float) – Velocity error which is tolerable.

Return type

None

total_error = 0

The sum of the errors for use in the integral calc