Class PID

  • All Implemented Interfaces:
    edu.wpi.first.wpilibj.Sendable

    public class PID
    extends java.lang.Object
    implements edu.wpi.first.wpilibj.Sendable
    Our custom PID controller implementation. Based off of faceincake's PID code from 2018 and 2019
    • Constructor Summary

      Constructors 
      Constructor Description
      PID​(double kp, double ki, double kd)
      PID Constructor
      PID​(PIDProfile profile)
      Create a PID controller using a PIDProfile
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double feed​(double sensor_reading)
      Feed the controller with a sensor reading
      double feedError​(double error)
      Feed the controller with a sensor error
      double getError()  
      void initSendable​(edu.wpi.first.wpilibj.smartdashboard.SendableBuilder builder)
      This is for use by WPIlib.
      boolean isFinished​(double epsilon)
      Checks if the error is within a reasonable range, then waits a bit before returning completion
      void reset()  
      void setGains​(double kp, double ki, double kd)
      Change the P, I and D gains on the fly.
      void setGains​(PIDProfile profile)
      Set PID gains with a PIDProfile
      void setOutputConstraints​(double min, double max)
      Set constraints on PID output.
      void setSetpoint​(double setpoint)
      Change the target value for the PID controller to reach.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface edu.wpi.first.wpilibj.Sendable

        addChild, getName, getSubsystem, setName, setName, setName, setName, setSubsystem
    • Constructor Detail

      • PID

        public PID​(PIDProfile profile)
        Create a PID controller using a PIDProfile
        Parameters:
        profile - PIDProfile
      • PID

        public PID​(double kp,
                   double ki,
                   double kd)
        PID Constructor
        Parameters:
        kp - P gain
        ki - I gain
        kd - D gain
    • Method Detail

      • setSetpoint

        public void setSetpoint​(double setpoint)
        Change the target value for the PID controller to reach. This could be an angle for turning, or an encoder value for moving an elevator.
        Parameters:
        setpoint - The optimal result (ex. the angle to turn to)
      • setGains

        public void setGains​(PIDProfile profile)
        Set PID gains with a PIDProfile
        Parameters:
        profile - New PID gains
      • setGains

        public void setGains​(double kp,
                             double ki,
                             double kd)
        Change the P, I and D gains on the fly. This is for applications where shifting gains are required. If you do not need to change the gains more then once, don't use this.
        Parameters:
        kp - P gain
        ki - I gain
        kd - D gain
      • setOutputConstraints

        public void setOutputConstraints​(double min,
                                         double max)
        Set constraints on PID output. If set, the feed() method will never return a value outside of the range.
        Parameters:
        min - Minimum output
        max - Maximum output
      • feed

        public double feed​(double sensor_reading)
        Feed the controller with a sensor reading
        Parameters:
        sensor_reading - The current reading of the input sensor (ex. gyro angle)
        Returns:
        The output of the controller. This generally should be fed directly into a motor
      • feedError

        public double feedError​(double error)
        Feed the controller with a sensor error
        Parameters:
        error - The current control loop error
        Returns:
        The output of the controller. This generally should be fed directly into a motor
      • getError

        public double getError()
      • isFinished

        public boolean isFinished​(double epsilon)
        Checks if the error is within a reasonable range, then waits a bit before returning completion
        Parameters:
        epsilon - Accepted error
        Returns:
        Has the loop finished?
      • reset

        public void reset()
      • initSendable

        public void initSendable​(edu.wpi.first.wpilibj.smartdashboard.SendableBuilder builder)
        This is for use by WPIlib. Basically tricking it into thinking this is a built-in class.
        Specified by:
        initSendable in interface edu.wpi.first.wpilibj.Sendable