Package frc.lib5k.control
Class PID
- java.lang.Object
-
- frc.lib5k.control.PID
-
- All Implemented Interfaces:
edu.wpi.first.wpilibj.Sendable
public class PID extends java.lang.Object implements edu.wpi.first.wpilibj.SendableOur 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 ConstructorPID(PIDProfile profile)Create a PID controller using a PIDProfile
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description doublefeed(double sensor_reading)Feed the controller with a sensor readingdoublefeedError(double error)Feed the controller with a sensor errordoublegetError()voidinitSendable(edu.wpi.first.wpilibj.smartdashboard.SendableBuilder builder)This is for use by WPIlib.booleanisFinished(double epsilon)Checks if the error is within a reasonable range, then waits a bit before returning completionvoidreset()voidsetGains(double kp, double ki, double kd)Change the P, I and D gains on the fly.voidsetGains(PIDProfile profile)Set PID gains with a PIDProfilevoidsetOutputConstraints(double min, double max)Set constraints on PID output.voidsetSetpoint(double setpoint)Change the target value for the PID controller to reach.
-
-
-
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 gainki- I gainkd- 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 gainki- I gainkd- 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 outputmax- 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:
initSendablein interfaceedu.wpi.first.wpilibj.Sendable
-
-