Remove percent tolerance from PID controller

It breaks the unit system badly; the tolerance member variable has
different units depending on percent vs absolute. Absolute tolerance is
a lot more natural than percent tolerance anyway.
This commit is contained in:
Tyler Veness
2019-08-25 13:01:51 -07:00
committed by Peter Johnson
parent 0ca8d667d2
commit ff8b8f0a8a
29 changed files with 121 additions and 507 deletions

View File

@@ -63,12 +63,6 @@ public class PIDController extends SendableBase {
// The sum of the errors for use in the integral calc
private double m_totalError;
enum Tolerance {
kAbsolute, kPercent
}
private Tolerance m_toleranceType = Tolerance.kAbsolute;
// The percentage or absolute error that is considered at setpoint.
private double m_positionTolerance = 0.05;
private double m_velocityTolerance = Double.POSITIVE_INFINITY;
@@ -223,43 +217,8 @@ public class PIDController extends SendableBase {
* @return Whether the error is within the acceptable bounds.
*/
public boolean atSetpoint() {
return atSetpoint(m_positionTolerance, m_velocityTolerance, m_toleranceType);
}
/**
* Returns true if the error and change in error are below the specified tolerances.
*
* @param positionTolerance The maximum allowable position error.
* @param velocityTolerance The maximum allowable velocity error.
* @param toleranceType The type of tolerance specified.
* @return Whether the error is within the acceptable bounds.
*/
public boolean atSetpoint(double positionTolerance, double velocityTolerance,
Tolerance toleranceType) {
if (toleranceType == Tolerance.kPercent) {
return Math.abs(m_positionError) < positionTolerance / 100 * m_inputRange
&& Math.abs(m_velocityError) < velocityTolerance / 100 * m_inputRange;
} else {
return Math.abs(m_positionError) < positionTolerance
&& Math.abs(m_velocityError) < velocityTolerance;
}
}
/**
* Sets the minimum and maximum values expected from the input.
*
* @param minimumInput The minimum value expected from the input.
* @param maximumInput The maximum value expected from the input.
*/
public void setInputRange(double minimumInput, double maximumInput) {
m_minimumInput = minimumInput;
m_maximumInput = maximumInput;
m_inputRange = maximumInput - minimumInput;
// Clamp setpoint to new input
if (m_maximumInput > m_minimumInput) {
m_setpoint = MathUtils.clamp(m_setpoint, m_minimumInput, m_maximumInput);
}
return Math.abs(m_positionError) < m_positionTolerance
&& Math.abs(m_velocityError) < m_velocityTolerance;
}
/**
@@ -296,43 +255,21 @@ public class PIDController extends SendableBase {
}
/**
* Sets the absolute error which is considered tolerable for use with atSetpoint().
* Sets the error which is considered tolerable for use with atSetpoint().
*
* @param positionTolerance Position error which is tolerable.
*/
public void setAbsoluteTolerance(double positionTolerance) {
setAbsoluteTolerance(positionTolerance, Double.POSITIVE_INFINITY);
public void setTolerance(double positionTolerance) {
setTolerance(positionTolerance, Double.POSITIVE_INFINITY);
}
/**
* Sets the absolute error which is considered tolerable for use with atSetpoint().
* Sets the error which is considered tolerable for use with atSetpoint().
*
* @param positionTolerance Position error which is tolerable.
* @param velocityTolerance Velocity error which is tolerable.
*/
public void setAbsoluteTolerance(double positionTolerance, double velocityTolerance) {
m_toleranceType = Tolerance.kAbsolute;
m_positionTolerance = positionTolerance;
m_velocityTolerance = velocityTolerance;
}
/**
* Sets the percent error which is considered tolerable for use with atSetpoint().
*
* @param positionTolerance Position error which is tolerable.
*/
public void setPercentTolerance(double positionTolerance) {
setPercentTolerance(positionTolerance, Double.POSITIVE_INFINITY);
}
/**
* Sets the percent error which is considered tolerable for use with atSetpoint().
*
* @param positionTolerance Position error which is tolerable.
* @param velocityTolerance Velocity error which is tolerable.
*/
public void setPercentTolerance(double positionTolerance, double velocityTolerance) {
m_toleranceType = Tolerance.kPercent;
public void setTolerance(double positionTolerance, double velocityTolerance) {
m_positionTolerance = positionTolerance;
m_velocityTolerance = velocityTolerance;
}
@@ -422,4 +359,21 @@ public class PIDController extends SendableBase {
}
return error;
}
/**
* Sets the minimum and maximum values expected from the input.
*
* @param minimumInput The minimum value expected from the input.
* @param maximumInput The maximum value expected from the input.
*/
private void setInputRange(double minimumInput, double maximumInput) {
m_minimumInput = minimumInput;
m_maximumInput = maximumInput;
m_inputRange = maximumInput - minimumInput;
// Clamp setpoint to new input
if (m_maximumInput > m_minimumInput) {
m_setpoint = MathUtils.clamp(m_setpoint, m_minimumInput, m_maximumInput);
}
}
}

View File

@@ -152,46 +152,6 @@ public class ProfiledPIDController extends SendableBase {
return m_goal.position;
}
/**
* Returns true if the error is within tolerance of the setpoint.
*
* <p>This will return false until at least one input value has been computed.
*
* @param positionTolerance The maximum allowable position error.
*/
public boolean atGoal(double positionTolerance) {
return atGoal(positionTolerance, Double.POSITIVE_INFINITY, PIDController.Tolerance.kAbsolute);
}
/**
* Returns true if the error is within tolerance of the setpoint.
*
* <p>This will return false until at least one input value has been computed.
*
* @param positionTolerance The maximum allowable position error.
* @param velocityTolerance The maximum allowable velocity error.
*/
public boolean atGoal(double positionTolerance, double velocityTolerance) {
return atGoal(positionTolerance, velocityTolerance, PIDController.Tolerance.kAbsolute);
}
/**
* Returns true if the error is within tolerance of the setpoint.
*
* <p>This will return false until at least one input value has been computed.
*
* @param positionTolerance The maximum allowable position error.
* @param velocityTolerance The maximum allowable velocity error.
* @param toleranceType The type of tolerance specified.
*/
public boolean atGoal(
double positionTolerance,
double velocityTolerance,
PIDController.Tolerance toleranceType) {
return atSetpoint(positionTolerance, velocityTolerance, toleranceType)
&& m_goal.equals(m_setpoint);
}
/**
* Returns true if the error is within the tolerance of the error.
*
@@ -219,46 +179,6 @@ public class ProfiledPIDController extends SendableBase {
return m_controller.getSetpoint();
}
/**
* Returns true if the error is within tolerance of the setpoint.
*
* <p>This will return false until at least one input value has been computed.
*
* @param positionTolerance The maximum allowable position error.
*/
public boolean atSetpoint(double positionTolerance) {
return atSetpoint(positionTolerance, Double.POSITIVE_INFINITY,
PIDController.Tolerance.kAbsolute);
}
/**
* Returns true if the error is within tolerance of the setpoint.
*
* <p>This will return false until at least one input value has been computed.
*
* @param positionTolerance The maximum allowable position error.
* @param velocityTolerance The maximum allowable velocity error.
*/
public boolean atSetpoint(double positionTolerance, double velocityTolerance) {
return atSetpoint(positionTolerance, velocityTolerance, PIDController.Tolerance.kAbsolute);
}
/**
* Returns true if the error is within tolerance of the setpoint.
*
* <p>This will return false until at least one input value has been computed.
*
* @param positionTolerance The maximum allowable position error.
* @param velocityTolerance The maximum allowable velocity error.
* @param toleranceType The type of tolerance specified.
*/
public boolean atSetpoint(
double positionTolerance,
double velocityTolerance,
PIDController.Tolerance toleranceType) {
return m_controller.atSetpoint(positionTolerance, velocityTolerance, toleranceType);
}
/**
* Returns true if the error is within the tolerance of the error.
*
@@ -268,16 +188,6 @@ public class ProfiledPIDController extends SendableBase {
return m_controller.atSetpoint();
}
/**
* Sets the minimum and maximum values expected from the input.
*
* @param minimumInput The minimum value expected from the input.
* @param maximumInput The maximum value expected from the input.
*/
public void setInputRange(double minimumInput, double maximumInput) {
m_controller.setInputRange(minimumInput, maximumInput);
}
/**
* Enables continuous input.
*
@@ -310,47 +220,22 @@ public class ProfiledPIDController extends SendableBase {
}
/**
* Sets the absolute error which is considered tolerable for use with
* atSetpoint().
* Sets the error which is considered tolerable for use with atSetpoint().
*
* @param positionTolerance Position error which is tolerable.
*/
public void setAbsoluteTolerance(double positionTolerance) {
setAbsoluteTolerance(positionTolerance, Double.POSITIVE_INFINITY);
public void setTolerance(double positionTolerance) {
setTolerance(positionTolerance, Double.POSITIVE_INFINITY);
}
/**
* Sets the absolute error which is considered tolerable for use with
* atSetpoint().
* Sets the error which is considered tolerable for use with atSetpoint().
*
* @param positionTolerance Position error which is tolerable.
* @param velocityTolerance Velocity error which is tolerable.
*/
public void setAbsoluteTolerance(double positionTolerance, double velocityTolerance) {
m_controller.setAbsoluteTolerance(positionTolerance, velocityTolerance);
}
/**
* Sets the percent error which is considered tolerable for use with
* atSetpoint().
*
* @param positionTolerance Position error which is tolerable.
*/
public void setPercentTolerance(double positionTolerance) {
m_controller.setPercentTolerance(positionTolerance, Double.POSITIVE_INFINITY);
}
/**
* Sets the percent error which is considered tolerable for use with
* atSetpoint().
*
* @param positionTolerance Position error which is tolerable.
* @param velocityTolerance Velocity error which is tolerable.
*/
public void setPercentTolerance(
double positionTolerance,
double velocityTolerance) {
m_controller.setPercentTolerance(positionTolerance, velocityTolerance);
public void setTolerance(double positionTolerance, double velocityTolerance) {
m_controller.setTolerance(positionTolerance, velocityTolerance);
}
/**