Class ProfiledPIDController

java.lang.Object
edu.wpi.first.math.controller.ProfiledPIDController
All Implemented Interfaces:
Sendable

public class ProfiledPIDController
extends Object
implements Sendable
Implements a PID control loop whose setpoint is constrained by a trapezoid profile. Users should call reset() when they first start running the controller to avoid unwanted behavior.
  • Constructor Summary

    Constructors 
    Constructor Description
    ProfiledPIDController​(double Kp, double Ki, double Kd, TrapezoidProfile.Constraints constraints)
    Allocates a ProfiledPIDController with the given constants for Kp, Ki, and Kd.
    ProfiledPIDController​(double Kp, double Ki, double Kd, TrapezoidProfile.Constraints constraints, double period)
    Allocates a ProfiledPIDController with the given constants for Kp, Ki, and Kd.
  • Method Summary

    Modifier and Type Method Description
    boolean atGoal()
    Returns true if the error is within the tolerance of the error.
    boolean atSetpoint()
    Returns true if the error is within the tolerance of the error.
    double calculate​(double measurement)
    Returns the next output of the PID controller.
    double calculate​(double measurement, double goal)
    Returns the next output of the PIDController.
    double calculate​(double measurement, TrapezoidProfile.State goal)
    Returns the next output of the PID controller.
    double calculate​(double measurement, TrapezoidProfile.State goal, TrapezoidProfile.Constraints constraints)
    Returns the next output of the PID controller.
    void disableContinuousInput()
    Disables continuous input.
    void enableContinuousInput​(double minimumInput, double maximumInput)
    Enables continuous input.
    double getD()
    Gets the differential coefficient.
    TrapezoidProfile.State getGoal()
    Gets the goal for the ProfiledPIDController.
    double getI()
    Gets the integral coefficient.
    double getP()
    Gets the proportional coefficient.
    double getPeriod()
    Gets the period of this controller.
    double getPositionError()
    Returns the difference between the setpoint and the measurement.
    double getPositionTolerance()
    Returns the position tolerance of this controller.
    TrapezoidProfile.State getSetpoint()
    Returns the current setpoint of the ProfiledPIDController.
    double getVelocityError()
    Returns the change in error per second.
    double getVelocityTolerance()
    Returns the velocity tolerance of this controller.
    void initSendable​(SendableBuilder builder)
    Initializes this Sendable object.
    void reset​(double measuredPosition)
    Reset the previous error and the integral term.
    void reset​(double measuredPosition, double measuredVelocity)
    Reset the previous error and the integral term.
    void reset​(TrapezoidProfile.State measurement)
    Reset the previous error and the integral term.
    void setConstraints​(TrapezoidProfile.Constraints constraints)
    Set velocity and acceleration constraints for goal.
    void setD​(double Kd)
    Sets the differential coefficient of the PID controller gain.
    void setGoal​(double goal)
    Sets the goal for the ProfiledPIDController.
    void setGoal​(TrapezoidProfile.State goal)
    Sets the goal for the ProfiledPIDController.
    void setI​(double Ki)
    Sets the integral coefficient of the PID controller gain.
    void setIntegratorRange​(double minimumIntegral, double maximumIntegral)
    Sets the minimum and maximum values for the integrator.
    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 setTolerance​(double positionTolerance)
    Sets the error which is considered tolerable for use with atSetpoint().
    void setTolerance​(double positionTolerance, double velocityTolerance)
    Sets the error which is considered tolerable for use with atSetpoint().

    Methods inherited from class java.lang.Object

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

    • ProfiledPIDController

      public ProfiledPIDController​(double Kp, double Ki, double Kd, TrapezoidProfile.Constraints constraints)
      Allocates a ProfiledPIDController with the given constants for Kp, Ki, and Kd.
      Parameters:
      Kp - The proportional coefficient.
      Ki - The integral coefficient.
      Kd - The derivative coefficient.
      constraints - Velocity and acceleration constraints for goal.
    • ProfiledPIDController

      public ProfiledPIDController​(double Kp, double Ki, double Kd, TrapezoidProfile.Constraints constraints, double period)
      Allocates a ProfiledPIDController with the given constants for Kp, Ki, and Kd.
      Parameters:
      Kp - The proportional coefficient.
      Ki - The integral coefficient.
      Kd - The derivative coefficient.
      constraints - Velocity and acceleration constraints for goal.
      period - The period between controller updates in seconds. The default is 0.02 seconds.
  • Method Details

    • setPID

      public void setPID​(double Kp, double Ki, double Kd)
      Sets the PID Controller gain parameters.

      Sets the proportional, integral, and differential coefficients.

      Parameters:
      Kp - Proportional coefficient
      Ki - Integral coefficient
      Kd - Differential coefficient
    • setP

      public void setP​(double Kp)
      Sets the proportional coefficient of the PID controller gain.
      Parameters:
      Kp - proportional coefficient
    • setI

      public void setI​(double Ki)
      Sets the integral coefficient of the PID controller gain.
      Parameters:
      Ki - integral coefficient
    • setD

      public void setD​(double Kd)
      Sets the differential coefficient of the PID controller gain.
      Parameters:
      Kd - differential coefficient
    • getP

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

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

      public double getD()
      Gets the differential coefficient.
      Returns:
      differential coefficient
    • getPeriod

      public double getPeriod()
      Gets the period of this controller.
      Returns:
      The period of the controller.
    • getPositionTolerance

      public double getPositionTolerance()
      Returns the position tolerance of this controller.
      Returns:
      the position tolerance of the controller.
    • getVelocityTolerance

      public double getVelocityTolerance()
      Returns the velocity tolerance of this controller.
      Returns:
      the velocity tolerance of the controller.
    • setGoal

      public void setGoal​(TrapezoidProfile.State goal)
      Sets the goal for the ProfiledPIDController.
      Parameters:
      goal - The desired goal state.
    • setGoal

      public void setGoal​(double goal)
      Sets the goal for the ProfiledPIDController.
      Parameters:
      goal - The desired goal position.
    • getGoal

      Gets the goal for the ProfiledPIDController.
      Returns:
      The goal.
    • atGoal

      public boolean atGoal()
      Returns true if the error is within the tolerance of the error.

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

      Returns:
      True if the error is within the tolerance of the error.
    • setConstraints

      public void setConstraints​(TrapezoidProfile.Constraints constraints)
      Set velocity and acceleration constraints for goal.
      Parameters:
      constraints - Velocity and acceleration constraints for goal.
    • getSetpoint

      Returns the current setpoint of the ProfiledPIDController.
      Returns:
      The current setpoint.
    • atSetpoint

      public boolean atSetpoint()
      Returns true if the error is within the tolerance of the error.

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

      Returns:
      True if the error is within the tolerance of the error.
    • 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.
    • setIntegratorRange

      public void setIntegratorRange​(double minimumIntegral, double maximumIntegral)
      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:
      minimumIntegral - The minimum value of the integrator.
      maximumIntegral - The maximum value of the integrator.
    • setTolerance

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

      public void setTolerance​(double positionTolerance, double velocityTolerance)
      Sets the error which is considered tolerable for use with atSetpoint().
      Parameters:
      positionTolerance - Position error which is tolerable.
      velocityTolerance - Velocity error which is tolerable.
    • getPositionError

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

      public double getVelocityError()
      Returns the change in error per second.
      Returns:
      The change in error per second.
    • 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 controller's next output.
    • calculate

      public double calculate​(double measurement, TrapezoidProfile.State goal)
      Returns the next output of the PID controller.
      Parameters:
      measurement - The current measurement of the process variable.
      goal - The new goal of the controller.
      Returns:
      The controller's next output.
    • calculate

      public double calculate​(double measurement, double goal)
      Returns the next output of the PIDController.
      Parameters:
      measurement - The current measurement of the process variable.
      goal - The new goal of the controller.
      Returns:
      The controller's next output.
    • calculate

      public double calculate​(double measurement, TrapezoidProfile.State goal, TrapezoidProfile.Constraints constraints)
      Returns the next output of the PID controller.
      Parameters:
      measurement - The current measurement of the process variable.
      goal - The new goal of the controller.
      constraints - Velocity and acceleration constraints for goal.
      Returns:
      The controller's next output.
    • reset

      public void reset​(TrapezoidProfile.State measurement)
      Reset the previous error and the integral term.
      Parameters:
      measurement - The current measured State of the system.
    • reset

      public void reset​(double measuredPosition, double measuredVelocity)
      Reset the previous error and the integral term.
      Parameters:
      measuredPosition - The current measured position of the system.
      measuredVelocity - The current measured velocity of the system.
    • reset

      public void reset​(double measuredPosition)
      Reset the previous error and the integral term.
      Parameters:
      measuredPosition - The current measured position of the system. The velocity is assumed to be zero.
    • initSendable

      public void initSendable​(SendableBuilder builder)
      Description copied from interface: Sendable
      Initializes this Sendable object.
      Specified by:
      initSendable in interface Sendable
      Parameters:
      builder - sendable builder