mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
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:
committed by
Peter Johnson
parent
0ca8d667d2
commit
ff8b8f0a8a
@@ -51,31 +51,9 @@ void PIDController::SetSetpoint(double setpoint) {
|
||||
|
||||
double PIDController::GetSetpoint() const { return m_setpoint; }
|
||||
|
||||
bool PIDController::AtSetpoint(double positionTolerance,
|
||||
double velocityTolerance,
|
||||
Tolerance toleranceType) const {
|
||||
if (m_toleranceType == Tolerance::kPercent) {
|
||||
return std::abs(m_positionError) < positionTolerance / 100 * m_inputRange &&
|
||||
std::abs(m_velocityError) < velocityTolerance / 100 * m_inputRange;
|
||||
} else {
|
||||
return std::abs(m_positionError) < positionTolerance &&
|
||||
std::abs(m_velocityError) < velocityTolerance;
|
||||
}
|
||||
}
|
||||
|
||||
bool PIDController::AtSetpoint() const {
|
||||
return AtSetpoint(m_positionTolerance, m_velocityTolerance);
|
||||
}
|
||||
|
||||
void PIDController::SetInputRange(double minimumInput, double maximumInput) {
|
||||
m_minimumInput = minimumInput;
|
||||
m_maximumInput = maximumInput;
|
||||
m_inputRange = maximumInput - minimumInput;
|
||||
|
||||
// Clamp setpoint to new input range
|
||||
if (m_maximumInput > m_minimumInput) {
|
||||
m_setpoint = std::clamp(m_setpoint, m_minimumInput, m_maximumInput);
|
||||
}
|
||||
return std::abs(m_positionError) < m_positionTolerance &&
|
||||
std::abs(m_velocityError) < m_velocityTolerance;
|
||||
}
|
||||
|
||||
void PIDController::EnableContinuousInput(double minimumInput,
|
||||
@@ -91,16 +69,8 @@ void PIDController::SetOutputRange(double minimumOutput, double maximumOutput) {
|
||||
m_maximumOutput = maximumOutput;
|
||||
}
|
||||
|
||||
void PIDController::SetAbsoluteTolerance(double positionTolerance,
|
||||
double velocityTolerance) {
|
||||
m_toleranceType = Tolerance::kAbsolute;
|
||||
m_positionTolerance = positionTolerance;
|
||||
m_velocityTolerance = velocityTolerance;
|
||||
}
|
||||
|
||||
void PIDController::SetPercentTolerance(double positionTolerance,
|
||||
double velocityTolerance) {
|
||||
m_toleranceType = Tolerance::kPercent;
|
||||
void PIDController::SetTolerance(double positionTolerance,
|
||||
double velocityTolerance) {
|
||||
m_positionTolerance = positionTolerance;
|
||||
m_velocityTolerance = velocityTolerance;
|
||||
}
|
||||
@@ -164,3 +134,14 @@ double PIDController::GetContinuousError(double error) const {
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
void PIDController::SetInputRange(double minimumInput, double maximumInput) {
|
||||
m_minimumInput = minimumInput;
|
||||
m_maximumInput = maximumInput;
|
||||
m_inputRange = maximumInput - minimumInput;
|
||||
|
||||
// Clamp setpoint to new input range
|
||||
if (m_maximumInput > m_minimumInput) {
|
||||
m_setpoint = std::clamp(m_setpoint, m_minimumInput, m_maximumInput);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,13 +43,6 @@ units::meter_t ProfiledPIDController::GetGoal() const {
|
||||
return m_goal.position;
|
||||
}
|
||||
|
||||
bool ProfiledPIDController::AtGoal(
|
||||
double positionTolerance, double velocityTolerance,
|
||||
frc2::PIDController::Tolerance toleranceType) const {
|
||||
return AtSetpoint(positionTolerance, velocityTolerance, toleranceType) &&
|
||||
m_goal == m_setpoint;
|
||||
}
|
||||
|
||||
bool ProfiledPIDController::AtGoal() const {
|
||||
return AtSetpoint() && m_goal == m_setpoint;
|
||||
}
|
||||
@@ -63,22 +56,10 @@ double ProfiledPIDController::GetSetpoint() const {
|
||||
return m_controller.GetSetpoint();
|
||||
}
|
||||
|
||||
bool ProfiledPIDController::AtSetpoint(
|
||||
double positionTolerance, double velocityTolerance,
|
||||
frc2::PIDController::Tolerance toleranceType) const {
|
||||
return m_controller.AtSetpoint(positionTolerance, velocityTolerance,
|
||||
toleranceType);
|
||||
}
|
||||
|
||||
bool ProfiledPIDController::AtSetpoint() const {
|
||||
return m_controller.AtSetpoint();
|
||||
}
|
||||
|
||||
void ProfiledPIDController::SetInputRange(double minimumInput,
|
||||
double maximumInput) {
|
||||
m_controller.SetInputRange(minimumInput, maximumInput);
|
||||
}
|
||||
|
||||
void ProfiledPIDController::EnableContinuousInput(double minimumInput,
|
||||
double maximumInput) {
|
||||
m_controller.EnableContinuousInput(minimumInput, maximumInput);
|
||||
@@ -93,14 +74,9 @@ void ProfiledPIDController::SetOutputRange(double minimumOutput,
|
||||
m_controller.SetOutputRange(minimumOutput, maximumOutput);
|
||||
}
|
||||
|
||||
void ProfiledPIDController::SetAbsoluteTolerance(double positionTolerance,
|
||||
double velocityTolerance) {
|
||||
m_controller.SetAbsoluteTolerance(positionTolerance, velocityTolerance);
|
||||
}
|
||||
|
||||
void ProfiledPIDController::SetPercentTolerance(double positionTolerance,
|
||||
double velocityTolerance) {
|
||||
m_controller.SetPercentTolerance(positionTolerance, velocityTolerance);
|
||||
void ProfiledPIDController::SetTolerance(double positionTolerance,
|
||||
double velocityTolerance) {
|
||||
m_controller.SetTolerance(positionTolerance, velocityTolerance);
|
||||
}
|
||||
|
||||
double ProfiledPIDController::GetPositionError() const {
|
||||
|
||||
@@ -21,8 +21,6 @@ namespace frc2 {
|
||||
*/
|
||||
class PIDController : public frc::SendableBase {
|
||||
public:
|
||||
enum class Tolerance { kAbsolute, kPercent };
|
||||
|
||||
/**
|
||||
* Allocates a PIDController with the given constants for Kp, Ki, and Kd.
|
||||
*
|
||||
@@ -116,20 +114,6 @@ class PIDController : public frc::SendableBase {
|
||||
*/
|
||||
double GetSetpoint() const;
|
||||
|
||||
/**
|
||||
* Returns true if the error is within tolerance of the setpoint.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
bool AtSetpoint(
|
||||
double positionTolerance,
|
||||
double velocityTolerance = std::numeric_limits<double>::infinity(),
|
||||
Tolerance toleranceType = Tolerance::kAbsolute) const;
|
||||
|
||||
/**
|
||||
* Returns true if the error is within the tolerance of the error.
|
||||
*
|
||||
@@ -137,14 +121,6 @@ class PIDController : public frc::SendableBase {
|
||||
*/
|
||||
bool AtSetpoint() const;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
void SetInputRange(double minimumInput, double maximumInput);
|
||||
|
||||
/**
|
||||
* Enables continuous input.
|
||||
*
|
||||
@@ -171,24 +147,12 @@ class PIDController : public frc::SendableBase {
|
||||
void SetOutputRange(double minimumOutput, double maximumOutput);
|
||||
|
||||
/**
|
||||
* 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 velociytTolerance Velocity error which is tolerable.
|
||||
*/
|
||||
void SetAbsoluteTolerance(
|
||||
double positionTolerance,
|
||||
double velocityTolerance = std::numeric_limits<double>::infinity());
|
||||
|
||||
/**
|
||||
* Sets the percent error which is considered tolerable for use with
|
||||
* AtSetpoint().
|
||||
*
|
||||
* @param positionTolerance Position error which is tolerable.
|
||||
* @param velociytTolerance Velocity error which is tolerable.
|
||||
*/
|
||||
void SetPercentTolerance(
|
||||
void SetTolerance(
|
||||
double positionTolerance,
|
||||
double velocityTolerance = std::numeric_limits<double>::infinity());
|
||||
|
||||
@@ -276,13 +240,19 @@ class PIDController : public frc::SendableBase {
|
||||
// The sum of the errors for use in the integral calc
|
||||
double m_totalError = 0;
|
||||
|
||||
Tolerance m_toleranceType = Tolerance::kAbsolute;
|
||||
|
||||
// The error that is considered at setpoint.
|
||||
double m_positionTolerance = 0.05;
|
||||
double m_velocityTolerance = std::numeric_limits<double>::infinity();
|
||||
|
||||
double m_setpoint = 0;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
void SetInputRange(double minimumInput, double maximumInput);
|
||||
};
|
||||
|
||||
} // namespace frc2
|
||||
|
||||
@@ -118,21 +118,6 @@ class ProfiledPIDController : public SendableBase {
|
||||
*/
|
||||
units::meter_t GetGoal() const;
|
||||
|
||||
/**
|
||||
* Returns true if the error is within tolerance of the setpoint.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
bool AtGoal(
|
||||
double positionTolerance,
|
||||
double velocityTolerance = std::numeric_limits<double>::infinity(),
|
||||
frc2::PIDController::Tolerance toleranceType =
|
||||
frc2::PIDController::Tolerance::kAbsolute) const;
|
||||
|
||||
/**
|
||||
* Returns true if the error is within the tolerance of the error.
|
||||
*
|
||||
@@ -154,21 +139,6 @@ class ProfiledPIDController : public SendableBase {
|
||||
*/
|
||||
double GetSetpoint() const;
|
||||
|
||||
/**
|
||||
* Returns true if the error is within tolerance of the setpoint.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
bool AtSetpoint(
|
||||
double positionTolerance,
|
||||
double velocityTolerance = std::numeric_limits<double>::infinity(),
|
||||
frc2::PIDController::Tolerance toleranceType =
|
||||
frc2::PIDController::Tolerance::kAbsolute) const;
|
||||
|
||||
/**
|
||||
* Returns true if the error is within the tolerance of the error.
|
||||
*
|
||||
@@ -180,14 +150,6 @@ class ProfiledPIDController : public SendableBase {
|
||||
*/
|
||||
bool AtSetpoint() const;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
void SetInputRange(double minimumInput, double maximumInput);
|
||||
|
||||
/**
|
||||
* Enables continuous input.
|
||||
*
|
||||
@@ -214,24 +176,13 @@ class ProfiledPIDController : public SendableBase {
|
||||
void SetOutputRange(double minimumOutput, double maximumOutput);
|
||||
|
||||
/**
|
||||
* Sets the absolute error which is considered tolerable for use with
|
||||
* 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.
|
||||
*/
|
||||
void SetAbsoluteTolerance(
|
||||
double positionTolerance,
|
||||
double velocityTolerance = std::numeric_limits<double>::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.
|
||||
*/
|
||||
void SetPercentTolerance(
|
||||
void SetTolerance(
|
||||
double positionTolerance,
|
||||
double velocityTolerance = std::numeric_limits<double>::infinity());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user