Package trclib.robotcore
Class TrcPidController
java.lang.Object
trclib.robotcore.TrcPidController
- Direct Known Subclasses:
TrcCascadePidController
This class implements a PID controller. A PID controller takes a target set point and an input from a feedback
device to calculate the output power of an effector usually a motor or a set of motors.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis class encapsulates all the PID coefficients into a single object and makes it more efficient to pass them around.private static classThis class stores the PID controller state.static interfacePID controller needs input from a feedback device for calculating the output power. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate final TrcDashboardstatic final doubleprivate static final doubleprivate static final doubleprivate static final doubleprivate final Stringprivate booleanprivate doubleprivate doubleprivate booleanprivate doubleprivate final TrcPidController.PidCtrlStateprivate final TrcPidController.PidInputprivate Doublefinal TrcDbgTrace -
Constructor Summary
ConstructorsConstructorDescriptionTrcPidController(String instanceName, TrcPidController.PidCoefficients pidCoeffs, TrcPidController.PidInput pidInput) Constructor: Create an instance of the object. -
Method Summary
Modifier and TypeMethodDescriptionvoiddisplayPidInfo(int lineNum) This method displays the PID information on the dashboard for debugging and tuning purpose.voidThis method ends stall detection.doubleThis method returns the current PID input value.doublegetError()This method returns the error of a previous output calculation.doubleThis method calculates the PID output applying the PID equation to the given set point target and current input value.doublegetOutput(double input, double setPoint) This method calculates the PID output applying the PID equation to the given set point target and current input value.doubleThis method returns the last set output limit.This method returns the current PID coefficients.This method returns the PidInput interface.doubleThis method returns the current set point value.booleanThis method returns true if setpoints are absolute, false otherwise.booleanisOnTarget(double tolerance) This method determines if we have reached the set point target.booleanisOnTarget(double tolerance, double settlingTime) This method determines if we have reached the set point target.booleanThis method detects if PID is stalled.voidThis method prints the PID information to the default debug tracer.voidprintPidInfo(TrcDbgTrace tracer) This method prints the PID information to the tracer console.voidprintPidInfo(TrcDbgTrace tracer, boolean verbose) This method prints the PID information to the tracer console.voidprintPidInfo(TrcDbgTrace msgTracer, boolean verbose, TrcRobotBattery battery) This method prints the PID information to the tracer console.voidreset()This method resets the PID controller clearing the set point, error, total error and output.doubleThis method restores the last saved output limit and return its value.doublesaveAndSetOutputLimit(double limit) This method saves the current output limit of the PID controller and sets it to the given new limit.voidsetAbsoluteSetPoint(boolean absolute) This method sets the set point mode to be absolute.voidsetInverted(boolean inverted) This method inverts the sign of the calculated error.voidsetNoOscillation(boolean noOscillation) This method enables/disables NoOscillation mode.voidsetOutputLimit(double limit) This method sets the output to the range -limit to +limit.voidsetOutputRange(double minOutput, double maxOutput) This method sets a range limit on the calculated output.voidThis method sets new PID coefficients.voidsetRampRate(Double rampRate) This method sets the ramp rate of the PID controller output.voidsetStallDetectionEnabled(boolean enabled) This method enables/disables stall detection.voidsetStallDetectionEnabled(double stallDetectionDelay, double stallDetectionTimeout, double stallErrorRateThreshold) This method enables/disables stall detection.voidsetTarget(double target) This methods sets the target set point.voidsetTarget(double target, boolean resetError) This methods sets the target set point.voidsetTarget(double target, TrcWarpSpace warpSpace) This methods sets the target set point.voidsetTarget(double target, TrcWarpSpace warpSpace, boolean resetError) This methods sets the target set point.voidsetTraceLevel(TrcDbgTrace.MsgLevel msgLevel) This method sets the message trace level for the tracer.voidsetTraceLevel(TrcDbgTrace.MsgLevel msgLevel, boolean verbosePidInfo) This method sets the message trace level for the tracer.voidsetTraceLevel(TrcDbgTrace.MsgLevel msgLevel, boolean verbosePidInfo, TrcRobotBattery battery) This method sets the message trace level for the tracer.voidThis method starts stall detection.toString()This method returns the instance name.
-
Field Details
-
DEF_SETTLING_TIME
public static final double DEF_SETTLING_TIME- See Also:
-
DEF_STALL_DETECTION_DELAY
private static final double DEF_STALL_DETECTION_DELAY- See Also:
-
DEF_STALL_DETECTION_TIMEOUT
private static final double DEF_STALL_DETECTION_TIMEOUT- See Also:
-
DEF_STALL_ERR_RATE_THRESHOLD
private static final double DEF_STALL_ERR_RATE_THRESHOLD- See Also:
-
dashboard
-
tracer
-
instanceName
-
pidInput
-
inverted
private boolean inverted -
absSetPoint
private boolean absSetPoint -
noOscillation
private boolean noOscillation -
minOutput
private double minOutput -
maxOutput
private double maxOutput -
outputLimit
private double outputLimit -
rampRate
-
outputLimitStack
-
pidCtrlState
-
-
Constructor Details
-
TrcPidController
public TrcPidController(String instanceName, TrcPidController.PidCoefficients pidCoeffs, TrcPidController.PidInput pidInput) Constructor: Create an instance of the object.- Parameters:
instanceName- specifies the instance name.pidCoeffs- specifies the PID coefficients.pidInput- specifies the method to call to get PID sensor input.
-
-
Method Details
-
toString
This method returns the instance name. -
setTraceLevel
public void setTraceLevel(TrcDbgTrace.MsgLevel msgLevel, boolean verbosePidInfo, TrcRobotBattery battery) This method sets the message trace level for the tracer.- Parameters:
msgLevel- specifies the message level.verbosePidInfo- specifies true to trace verbose PID info, false otherwise.battery- specifies the battery object to get battery info for the message.
-
setTraceLevel
This method sets the message trace level for the tracer.- Parameters:
msgLevel- specifies the message level.verbosePidInfo- specifies true to trace verbose PID info, false otherwise.
-
setTraceLevel
This method sets the message trace level for the tracer.- Parameters:
msgLevel- specifies the message level.
-
getPidInput
This method returns the PidInput interface.- Returns:
- PidInput interface.
-
setInverted
public void setInverted(boolean inverted) This method inverts the sign of the calculated error. Normally, the calculated error starts with a large positive number and goes down. However, in some sensors such as the ultrasonic sensor, the target is a small number and the error starts with a negative value and increases. In order to calculate a correct output which will go towards the target, the error sign must be inverted.- Parameters:
inverted- specifies true to invert the sign of the calculated error, false otherwise.
-
setAbsoluteSetPoint
public void setAbsoluteSetPoint(boolean absolute) This method sets the set point mode to be absolute. PID controller always calculates the output with an absolute set point comparing to a sensor value representing an absolute input. But by default, it will treat the set point as a value relative to its current input. So it will add the relative set point value to the current input as the absolute set point in its calculation. This method allows the caller to treat the set point as absolute set point.- Parameters:
absolute- specifies true if set point is absolute, false otherwise.
-
hasAbsoluteSetPoint
public boolean hasAbsoluteSetPoint()This method returns true if setpoints are absolute, false otherwise.- Returns:
- true if setpoints are absolute, false otherwise.
-
setNoOscillation
public void setNoOscillation(boolean noOscillation) This method enables/disables NoOscillation mode. In PID control, if the PID constants are not tuned quite correctly, it may cause oscillation that could waste a lot of time. In some scenarios, passing the target beyond the tolerance may be acceptable. This method allows the PID controller to declare "On Target" even though it passes the target beyond tolerance so it doesn't oscillate.- Parameters:
noOscillation- specifies true to enable no oscillation, false to disable.
-
setStallDetectionEnabled
public void setStallDetectionEnabled(double stallDetectionDelay, double stallDetectionTimeout, double stallErrorRateThreshold) This method enables/disables stall detection.- Parameters:
stallDetectionDelay- specifies stall detection start delay in seconds, zero to disable stall detection.stallDetectionTimeout- specifies stall timeout in seconds which is the minimum elapsed time for the motor to be motionless to be considered stalled.stallErrorRateThreshold- specifies the error rate threshold below which it will consider stalling.
-
setStallDetectionEnabled
public void setStallDetectionEnabled(boolean enabled) This method enables/disables stall detection.- Parameters:
enabled- specifies true to enable stall detection, false to disable.
-
startStallDetection
public void startStallDetection()This method starts stall detection. -
endStallDetection
public void endStallDetection()This method ends stall detection. -
getPidCoefficients
This method returns the current PID coefficients.- Returns:
- current PID coefficients.
-
setPidCoefficients
This method sets new PID coefficients.- Parameters:
pidCoeffs- specifies new PID coefficients.
-
setRampRate
This method sets the ramp rate of the PID controller output. It is sometimes useful to limit the acceleration of the output of the PID controller. For example, the strafing PID controller on a mecanum drive base may benefit from a lower acceleration to minimize wheel slippage.- Parameters:
rampRate- specifies the ramp rate in percent power per second, null to disable.
-
setOutputRange
public void setOutputRange(double minOutput, double maxOutput) This method sets a range limit on the calculated output. It is very useful to limit the output range to less than full power for scenarios such as using mecanum wheels on a drive train to prevent wheel slipping or slow down a PID drive in order to detect a line etc.- Parameters:
minOutput- specifies the PID output lower range limit.maxOutput- specifies the PID output higher range limit.
-
setOutputLimit
public void setOutputLimit(double limit) This method sets the output to the range -limit to +limit. It calls setOutputRange. If the caller wants to limit the output power symmetrically, this is the method to call, not setOutputRange.- Parameters:
limit- specifies the output limit as a positive number.
-
getOutputLimit
public double getOutputLimit()This method returns the last set output limit. It is sometimes useful to temporarily change the output range of the PID controller for an operation and restore it afterwards. This method allows the caller to save the last set output limit and restore it later on.- Returns:
- last set output limit.
-
saveAndSetOutputLimit
public double saveAndSetOutputLimit(double limit) This method saves the current output limit of the PID controller and sets it to the given new limit. This is useful if the caller wants to temporarily set a limit for an operation and restore it afterwards. Note: this is implemented with a stack so it is assuming the saving and restoring calls are nested in nature. If this is called in a multi-threading environment where saving and restoring can be interleaved by different threads, unexpected result may happen. It is recommended to avoid this type of scenario if possible.- Parameters:
limit- specifies the new output limit.- Returns:
- return the previous output limit.
-
restoreOutputLimit
public double restoreOutputLimit()This method restores the last saved output limit and return its value. If there was no previous call to saveAndSetOutputLimit, the current output limit is returned and the limit is not changed.- Returns:
- last saved output limit.
-
getTarget
public double getTarget()This method returns the current set point value.- Returns:
- current set point.
-
setTarget
This methods sets the target set point.- Parameters:
target- specifies the target set point.warpSpace- specifies the warp space object if the target is in one, null if not.resetError- specifies true to reset error state, false otherwise. It is important to preserve error state if we are changing target before the PID operation is completed.
-
setTarget
This methods sets the target set point.- Parameters:
target- specifies the target set point.warpSpace- specifies the warp space object if the target is in one, null if not.
-
setTarget
public void setTarget(double target, boolean resetError) This methods sets the target set point.- Parameters:
target- specifies the target set point.resetError- specifies true to reset error state, false otherwise. It is important to preserve error state if we are changing target before the PID operation is completed.
-
setTarget
public void setTarget(double target) This methods sets the target set point.- Parameters:
target- specifies the target set point.
-
getError
public double getError()This method returns the error of a previous output calculation.- Returns:
- previous error.
-
reset
public void reset()This method resets the PID controller clearing the set point, error, total error and output. -
isStalled
public boolean isStalled()This method detects if PID is stalled.- Returns:
- true if PID is stalled, false otherwise.
-
isOnTarget
public boolean isOnTarget(double tolerance, double settlingTime) This method determines if we have reached the set point target. It is considered on target if the previous error is smaller than the tolerance and there is no movement for at least settling time. If NoOscillation mode is set, it is considered on target if we are within tolerance or pass target regardless of setting time.- Parameters:
tolerance- specifies the PID error tolerance.settlingTime- specifies the minimum on target settling time.- Returns:
- true if we reached target, false otherwise.
-
isOnTarget
public boolean isOnTarget(double tolerance) This method determines if we have reached the set point target. It is considered on target if the previous error is smaller than the tolerance and there is no movement for at least settling time. If NoOscillation mode is set, it is considered on target if we are within tolerance or pass target regardless of setting time.- Parameters:
tolerance- specifies the tolerance.- Returns:
- true if we reached target, false otherwise.
-
getCurrentInput
public double getCurrentInput()This method returns the current PID input value.- Returns:
- current PID input value.
-
getOutput
public double getOutput(double input, double setPoint) This method calculates the PID output applying the PID equation to the given set point target and current input value.- Returns:
- PID output value.
-
getOutput
public double getOutput()This method calculates the PID output applying the PID equation to the given set point target and current input value.- Returns:
- PID output value.
-
displayPidInfo
public void displayPidInfo(int lineNum) This method displays the PID information on the dashboard for debugging and tuning purpose. Note that the PID info occupies two dashboard lines.- Parameters:
lineNum- specifies the starting line number of the dashboard to display the info.
-
printPidInfo
This method prints the PID information to the tracer console. If no tracer is provided, it will attempt to use the debug tracer in this module but if the debug tracer is not enabled, no output will be produced.- Parameters:
msgTracer- specifies the tracer object to print the PID info to.verbose- specifies true to print verbose info, false to print summary info.battery- specifies the battery object to get battery info, can be null if not provided.
-
printPidInfo
This method prints the PID information to the tracer console. If no tracer is provided, it will attempt to use the debug tracer in this module but if the debug tracer is not enabled, no output will be produced.- Parameters:
tracer- specifies the tracer object to print the PID info to.verbose- specifies true to print verbose info, false to print summary info.
-
printPidInfo
This method prints the PID information to the tracer console. If no tracer is provided, it will attempt to use the debug tracer in this module but if the debug tracer is not enabled, no output will be produced.- Parameters:
tracer- specifies the tracer object to print the PID info to.
-
printPidInfo
public void printPidInfo()This method prints the PID information to the default debug tracer.
-