public class PIDController extends SendableBase implements PIDInterface, Sendable
Creates a separate thread which reads the given PIDSource and takes care of the integral calculations, as well as writing the given PIDOutput.
This feedback controller runs in discrete time, so time deltas are not used in the integral and derivative calculations. Therefore, the sample rate affects the controller's behavior for a given set of PID constants.
Modifier and Type | Class and Description |
---|---|
class |
PIDController.AbsoluteTolerance |
class |
PIDController.NullTolerance
Used internally for when Tolerance hasn't been set.
|
class |
PIDController.PercentageTolerance |
static interface |
PIDController.Tolerance
Tolerance is the type of tolerance used to specify if the PID controller is on target.
|
Modifier and Type | Field and Description |
---|---|
static double |
kDefaultPeriod |
protected PIDSource |
m_pidInput |
protected PIDOutput |
m_pidOutput |
Constructor and Description |
---|
PIDController(double Kp,
double Ki,
double Kd,
double Kf,
PIDSource source,
PIDOutput output)
Allocate a PID object with the given constants for P, I, D, using a 50ms period.
|
PIDController(double Kp,
double Ki,
double Kd,
double Kf,
PIDSource source,
PIDOutput output,
double period)
Allocate a PID object with the given constants for P, I, D, and F.
|
PIDController(double Kp,
double Ki,
double Kd,
PIDSource source,
PIDOutput output)
Allocate a PID object with the given constants for P, I, D, using a 50ms period.
|
PIDController(double Kp,
double Ki,
double Kd,
PIDSource source,
PIDOutput output,
double period)
Allocate a PID object with the given constants for P, I, D and period.
|
Modifier and Type | Method and Description |
---|---|
protected void |
calculate()
Read the input, calculate the output accordingly, and write to the output.
|
protected double |
calculateFeedForward()
Calculate the feed forward term.
|
void |
disable()
Stop running the PIDController, this sets the output to zero before stopping.
|
void |
enable()
Begin running the PIDController.
|
void |
free()
Free the PID object.
|
double |
get()
Return the current PID result This is always centered on zero and constrained the the max and
min outs.
|
double |
getAvgError()
Deprecated.
Use getError(), which is now already filtered.
|
protected double |
getContinuousError(double error)
Wraps error around for continuous inputs.
|
double |
getD()
Get the Differential coefficient.
|
double |
getDeltaSetpoint()
Returns the change in setpoint over time of the PIDController.
|
double |
getError()
Returns the current difference of the input from the setpoint.
|
double |
getF()
Get the Feed forward coefficient.
|
double |
getI()
Get the Integral coefficient.
|
double |
getP()
Get the Proportional coefficient.
|
double |
getSetpoint()
Returns the current setpoint of the PIDController.
|
void |
initSendable(SendableBuilder builder)
Initializes this
Sendable object. |
boolean |
isEnabled()
Return true if PIDController is enabled.
|
boolean |
onTarget()
Return true if the error is within the percentage of the total input range, determined by
setTolerance.
|
void |
reset()
Reset the previous error,, the integral term, and disable the controller.
|
void |
setAbsoluteTolerance(double absvalue)
Set the absolute error which is considered tolerable for use with OnTarget.
|
void |
setContinuous()
Set the PID controller to consider the input to be continuous, 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.
|
void |
setContinuous(boolean continuous)
Set the PID controller to consider the input to be continuous, 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.
|
void |
setD(double d)
Set the Differential coefficient of the PID controller gain.
|
void |
setEnabled(boolean enable)
Set the enabled state of the PIDController.
|
void |
setF(double f)
Set the Feed forward coefficient of the PID controller gain.
|
void |
setI(double i)
Set the Integral coefficient of the PID controller gain.
|
void |
setInputRange(double minimumInput,
double maximumInput)
Sets the maximum and minimum values expected from the input and setpoint.
|
void |
setOutputRange(double minimumOutput,
double maximumOutput)
Sets the minimum and maximum values to write.
|
void |
setP(double p)
Set the Proportional coefficient of the PID controller gain.
|
void |
setPercentTolerance(double percentage)
Set the percentage error which is considered tolerable for use with OnTarget.
|
void |
setPID(double p,
double i,
double d)
Set the PID Controller gain parameters.
|
void |
setPID(double p,
double i,
double d,
double f)
Set the PID Controller gain parameters.
|
void |
setSetpoint(double setpoint)
Set the setpoint for the PIDController.
|
void |
setTolerance(PIDController.Tolerance tolerance)
Deprecated.
Use setPercentTolerance() instead.
|
void |
setToleranceBuffer(int bufLength)
Deprecated.
Use a LinearDigitalFilter as the input.
|
addChild, getName, getSubsystem, setName, setName, setName, setSubsystem
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getName, getSubsystem, setName, setName, setSubsystem
public static final double kDefaultPeriod
protected PIDSource m_pidInput
protected PIDOutput m_pidOutput
public PIDController(double Kp, double Ki, double Kd, double Kf, PIDSource source, PIDOutput output, double period)
Kp
- the proportional coefficientKi
- the integral coefficientKd
- the derivative coefficientKf
- the feed forward termsource
- The PIDSource object that is used to get valuesoutput
- The PIDOutput object that is set to the output percentageperiod
- the loop time for doing calculations. This particularly effects calculations of
the integral and differential terms. The default is 50ms.public PIDController(double Kp, double Ki, double Kd, PIDSource source, PIDOutput output, double period)
Kp
- the proportional coefficientKi
- the integral coefficientKd
- the derivative coefficientsource
- the PIDSource object that is used to get valuesoutput
- the PIDOutput object that is set to the output percentageperiod
- the loop time for doing calculations. This particularly effects calculations of
the integral and differential terms. The default is 50ms.public PIDController(double Kp, double Ki, double Kd, PIDSource source, PIDOutput output)
Kp
- the proportional coefficientKi
- the integral coefficientKd
- the derivative coefficientsource
- The PIDSource object that is used to get valuesoutput
- The PIDOutput object that is set to the output percentagepublic PIDController(double Kp, double Ki, double Kd, double Kf, PIDSource source, PIDOutput output)
Kp
- the proportional coefficientKi
- the integral coefficientKd
- the derivative coefficientKf
- the feed forward termsource
- The PIDSource object that is used to get valuesoutput
- The PIDOutput object that is set to the output percentagepublic void free()
free
in class SendableBase
protected void calculate()
protected double calculateFeedForward()
Both of the provided feed forward calculations are velocity feed forwards. If a different feed forward calculation is desired, the user can override this function and provide his or her own. This function does no synchronization because the PIDController class only calls it in synchronized code, so be careful if calling it oneself.
If a velocity PID controller is being used, the F term should be set to 1 over the maximum setpoint for the output. If a position PID controller is being used, the F term should be set to 1 over the maximum speed for the output measured in setpoint units per this controller's update period (see the default period in this class's constructor).
public void setPID(double p, double i, double d)
setPID
in interface PIDInterface
p
- Proportional coefficienti
- Integral coefficientd
- Differential coefficientpublic void setPID(double p, double i, double d, double f)
p
- Proportional coefficienti
- Integral coefficientd
- Differential coefficientf
- Feed forward coefficientpublic void setP(double p)
p
- Proportional coefficientpublic void setI(double i)
i
- Integral coefficientpublic void setD(double d)
d
- differential coefficientpublic void setF(double f)
f
- feed forward coefficientpublic double getP()
getP
in interface PIDInterface
public double getI()
getI
in interface PIDInterface
public double getD()
getD
in interface PIDInterface
public double getF()
public double get()
public void setContinuous(boolean continuous)
continuous
- Set to true turns on continuous, false turns off continuouspublic void setContinuous()
public void setInputRange(double minimumInput, double maximumInput)
minimumInput
- the minimum value expected from the inputmaximumInput
- the maximum value expected from the inputpublic void setOutputRange(double minimumOutput, double maximumOutput)
minimumOutput
- the minimum percentage to write to the outputmaximumOutput
- the maximum percentage to write to the outputpublic void setSetpoint(double setpoint)
setSetpoint
in interface PIDInterface
setpoint
- the desired setpointpublic double getSetpoint()
getSetpoint
in interface PIDInterface
public double getDeltaSetpoint()
public double getError()
getError
in interface PIDInterface
@Deprecated public double getAvgError()
@Deprecated public void setTolerance(PIDController.Tolerance tolerance)
tolerance
- A tolerance object of the right type, e.g. PercentTolerance or
AbsoluteTolerancepublic void setAbsoluteTolerance(double absvalue)
absvalue
- absolute error which is tolerable in the units of the input objectpublic void setPercentTolerance(double percentage)
percentage
- percent error which is tolerable@Deprecated public void setToleranceBuffer(int bufLength)
bufLength
- Number of previous cycles to average.public boolean onTarget()
public void enable()
enable
in interface PIDInterface
public void disable()
disable
in interface PIDInterface
public void setEnabled(boolean enable)
public boolean isEnabled()
isEnabled
in interface PIDInterface
public void reset()
reset
in interface PIDInterface
public void initSendable(SendableBuilder builder)
Sendable
Sendable
object.initSendable
in interface Sendable
builder
- sendable builderprotected double getContinuousError(double error)
error
- The current error of the PID controller.