Class PIDController

java.lang.Object
org.wpilib.math.controller.PIDController
All Implemented Interfaces:
TelemetryLoggable, ComplexTunable

public class PIDController extends Object implements TelemetryLoggable, ComplexTunable
Implements a PID control loop.
  • Constructor Summary

    Constructors
    Constructor
    Description
    PIDController(double kp, double ki, double kd)
    Allocates a PIDController with the given constants for kp, ki, and kd and a default period of 0.02 seconds.
    PIDController(double kp, double ki, double kd, double period)
    Allocates a PIDController with the given constants for kp, ki, and kd.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if the error is within the tolerance of the setpoint.
    double
    calculate(double measurement)
    Returns the next output of the PID controller.
    double
    calculate(double measurement, double setpoint)
    Returns the next output of the PID controller.
    void
    Disables continuous input.
    void
    enableContinuousInput(double minimumInput, double maximumInput)
    Enables continuous input.
    double
    Returns the accumulated error used in the integral calculation of this controller.
    double
    Get the Differential coefficient.
    double
    Returns the difference between the setpoint and the measurement.
    double
    Returns the error derivative.
    double
    Returns the error derivative tolerance of this controller.
    double
    Returns the error tolerance of this controller.
    double
    Get the Integral coefficient.
    double
    Get the IZone range.
    double
    Get the Proportional coefficient.
    double
    Returns the period of this controller.
    double
    Returns the current setpoint of the PIDController.
    Gets the telemetry table type.
    Gets the tunable table type.
    boolean
    Returns true if continuous input is enabled.
    void
    Logs the object to a TelemetryTable.
    void
    Publishes the tunable to the given table.
    void
    Resets the previous error and the integral term.
    void
    setD(double kd)
    Sets the Differential coefficient of the PID controller gain.
    void
    setI(double ki)
    Sets the Integral coefficient of the PID controller gain.
    void
    setIntegratorRange(double minimumIntegral, double maximumIntegral)
    Sets the minimum and maximum contributions of the integral term.
    void
    setIZone(double iZone)
    Sets the IZone range.
    void
    setP(double kp)
    Sets the Proportional coefficient of the PID controller gain.
    void
    setPID(double kp, double ki, double kd)
    Sets the PID Controller gain parameters.
    void
    setSetpoint(double setpoint)
    Sets the setpoint for the PIDController.
    void
    setTolerance(double errorTolerance)
    Sets the error which is considered tolerable for use with atSetpoint().
    void
    setTolerance(double errorTolerance, double errorDerivativeTolerance)
    Sets the error which is considered tolerable for use with atSetpoint().

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PIDController

      public PIDController(double kp, double ki, double kd)
      Allocates a PIDController with the given constants for kp, ki, and kd and a default period of 0.02 seconds.
      Parameters:
      kp - The proportional coefficient.
      ki - The integral coefficient.
      kd - The derivative coefficient.
      Throws:
      IllegalArgumentException - if kp < 0
      IllegalArgumentException - if ki < 0
      IllegalArgumentException - if kd < 0
    • PIDController

      public PIDController(double kp, double ki, double kd, double period)
      Allocates a PIDController with the given constants for kp, ki, and kd.
      Parameters:
      kp - The proportional coefficient.
      ki - The integral coefficient.
      kd - The derivative coefficient.
      period - The period between controller updates in seconds.
      Throws:
      IllegalArgumentException - if kp < 0
      IllegalArgumentException - if ki < 0
      IllegalArgumentException - if kd < 0
      IllegalArgumentException - if period <= 0
  • Method Details

    • setPID

      public void setPID(double kp, double ki, double kd)
      Sets the PID Controller gain parameters.

      Set the proportional, integral, and differential coefficients.

      This setter is intended for online tuning.

      Parameters:
      kp - The proportional coefficient.
      ki - The integral coefficient.
      kd - The derivative coefficient.
    • setP

      public void setP(double kp)
      Sets the Proportional coefficient of the PID controller gain.

      This setter is intended for online tuning.

      Parameters:
      kp - The proportional coefficient. Must be >= 0.
    • setI

      public void setI(double ki)
      Sets the Integral coefficient of the PID controller gain.

      This setter is intended for online tuning.

      Parameters:
      ki - The integral coefficient. Must be >= 0.
    • setD

      public void setD(double kd)
      Sets the Differential coefficient of the PID controller gain.

      This setter is intended for online tuning.

      Parameters:
      kd - The differential coefficient. Must be >= 0.
    • setIZone

      public void setIZone(double iZone)
      Sets the IZone range. When the absolute value of the position error is greater than IZone, the total accumulated error will reset to zero, disabling integral gain until the absolute value of the position error is less than IZone. This is used to prevent integral windup. Must be non-negative. Passing a value of zero will effectively disable integral gain. Passing a value of Double.POSITIVE_INFINITY disables IZone functionality.
      Parameters:
      iZone - Maximum magnitude of error to allow integral control.
      Throws:
      IllegalArgumentException - if iZone < 0
    • getP

      public double getP()
      Get the Proportional coefficient.
      Returns:
      proportional coefficient
    • getI

      public double getI()
      Get the Integral coefficient.
      Returns:
      integral coefficient
    • getD

      public double getD()
      Get the Differential coefficient.
      Returns:
      differential coefficient
    • getIZone

      public double getIZone()
      Get the IZone range.
      Returns:
      Maximum magnitude of error to allow integral control.
    • getPeriod

      public double getPeriod()
      Returns the period of this controller.
      Returns:
      the period of the controller.
    • getErrorTolerance

      public double getErrorTolerance()
      Returns the error tolerance of this controller. Defaults to 0.05.
      Returns:
      the error tolerance of the controller.
    • getErrorDerivativeTolerance

      public double getErrorDerivativeTolerance()
      Returns the error derivative tolerance of this controller. Defaults to ∞.
      Returns:
      the error derivative tolerance of the controller.
    • getAccumulatedError

      public double getAccumulatedError()
      Returns the accumulated error used in the integral calculation of this controller.
      Returns:
      The accumulated error of this controller.
    • setSetpoint

      public void setSetpoint(double setpoint)
      Sets the setpoint for the PIDController.
      Parameters:
      setpoint - The desired setpoint.
    • getSetpoint

      public double getSetpoint()
      Returns the current setpoint of the PIDController.
      Returns:
      The current setpoint.
    • atSetpoint

      public boolean atSetpoint()
      Returns true if the error is within the tolerance of the setpoint. The error tolerance defaults to 0.05, and the error derivative tolerance defaults to ∞.

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

      Returns:
      Whether the error is within the acceptable bounds.
    • enableContinuousInput

      public void enableContinuousInput(double minimumInput, double maximumInput)
      Enables continuous input.

      Rather then 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:
      minimumInput - The minimum value expected from the input.
      maximumInput - The maximum value expected from the input.
    • disableContinuousInput

      public void disableContinuousInput()
      Disables continuous input.
    • isContinuousInputEnabled

      public boolean isContinuousInputEnabled()
      Returns true if continuous input is enabled.
      Returns:
      True if continuous input is enabled.
    • setIntegratorRange

      public void setIntegratorRange(double minimumIntegral, double maximumIntegral)
      Sets the minimum and maximum contributions of the integral term.

      The internal integrator is clamped so that the integral term's contribution to the output stays between minimumIntegral and maximumIntegral. This prevents integral windup.

      Parameters:
      minimumIntegral - The minimum contribution of the integral term.
      maximumIntegral - The maximum contribution of the integral term.
    • setTolerance

      public void setTolerance(double errorTolerance)
      Sets the error which is considered tolerable for use with atSetpoint().
      Parameters:
      errorTolerance - Error which is tolerable.
    • setTolerance

      public void setTolerance(double errorTolerance, double errorDerivativeTolerance)
      Sets the error which is considered tolerable for use with atSetpoint().
      Parameters:
      errorTolerance - Error which is tolerable.
      errorDerivativeTolerance - Error derivative which is tolerable.
    • getError

      public double getError()
      Returns the difference between the setpoint and the measurement.
      Returns:
      The error.
    • getErrorDerivative

      public double getErrorDerivative()
      Returns the error derivative.
      Returns:
      The error derivative.
    • calculate

      public double calculate(double measurement, double setpoint)
      Returns the next output of the PID controller.
      Parameters:
      measurement - The current measurement of the process variable.
      setpoint - The new setpoint of the controller.
      Returns:
      The next controller output.
    • calculate

      public double calculate(double measurement)
      Returns the next output of the PID controller.
      Parameters:
      measurement - The current measurement of the process variable.
      Returns:
      The next controller output.
    • reset

      public void reset()
      Resets the previous error and the integral term.
    • logTo

      public void logTo(TelemetryTable table)
      Description copied from interface: TelemetryLoggable
      Logs the object to a TelemetryTable.
      Specified by:
      logTo in interface TelemetryLoggable
      Parameters:
      table - telemetry table
    • getTelemetryType

      public String getTelemetryType()
      Description copied from interface: TelemetryLoggable
      Gets the telemetry table type. Default is no specified table type (null). A mismatch in type during logging results in a reported warning, so this should always return the same value.
      Specified by:
      getTelemetryType in interface TelemetryLoggable
      Returns:
      Table type
    • publishTunable

      public void publishTunable(TunableTable table)
      Description copied from interface: ComplexTunable
      Publishes the tunable to the given table.

      This function may be called when the tunable is initially published or republished during backend migration, and it must not throw. If an exception escapes, registry and backend state is not guaranteed to be restored.

      Specified by:
      publishTunable in interface ComplexTunable
      Parameters:
      table - table
    • getTunableType

      public String getTunableType()
      Description copied from interface: ComplexTunable
      Gets the tunable table type. Default is no specified table type (null).
      Specified by:
      getTunableType in interface ComplexTunable
      Returns:
      Table type