[wpimath] PIDController: Update field and method names for error and errorDerivative (#7088)

This commit is contained in:
Nicholas Armstrong
2024-09-23 13:57:20 -04:00
committed by GitHub
parent 64e5e6db59
commit 6281ec0810
9 changed files with 192 additions and 91 deletions

View File

@@ -103,11 +103,11 @@ units::second_t PIDController::GetPeriod() const {
}
double PIDController::GetPositionTolerance() const {
return m_positionTolerance;
return m_error;
}
double PIDController::GetVelocityTolerance() const {
return m_velocityTolerance;
return m_errorDerivative;
}
double PIDController::GetAccumulatedError() const {
@@ -120,13 +120,12 @@ void PIDController::SetSetpoint(double setpoint) {
if (m_continuous) {
double errorBound = (m_maximumInput - m_minimumInput) / 2.0;
m_positionError =
InputModulus(m_setpoint - m_measurement, -errorBound, errorBound);
m_error = InputModulus(m_setpoint - m_measurement, -errorBound, errorBound);
} else {
m_positionError = m_setpoint - m_measurement;
m_error = m_setpoint - m_measurement;
}
m_velocityError = (m_positionError - m_prevError) / m_period.value();
m_errorDerivative = (m_error - m_prevError) / m_period.value();
}
double PIDController::GetSetpoint() const {
@@ -135,8 +134,8 @@ double PIDController::GetSetpoint() const {
bool PIDController::AtSetpoint() const {
return m_haveMeasurement && m_haveSetpoint &&
std::abs(m_positionError) < m_positionTolerance &&
std::abs(m_velocityError) < m_velocityTolerance;
std::abs(m_error) < m_errorTolerance &&
std::abs(m_errorDerivative) < m_errorDerivativeTolerance;
}
void PIDController::EnableContinuousInput(double minimumInput,
@@ -160,46 +159,61 @@ void PIDController::SetIntegratorRange(double minimumIntegral,
m_maximumIntegral = maximumIntegral;
}
void PIDController::SetTolerance(double positionTolerance,
double velocityTolerance) {
m_positionTolerance = positionTolerance;
m_velocityTolerance = velocityTolerance;
void PIDController::SetTolerance(double errorTolerance,
double errorDerivativeTolerance) {
m_errorTolerance = errorTolerance;
m_errorDerivativeTolerance = errorDerivativeTolerance;
}
double PIDController::GetErrorTolerance() const {
return m_error;
}
double PIDController::GetErrorDerivativeTolerance() const {
return m_errorDerivativeTolerance;
}
double PIDController::GetError() const {
return m_error;
}
double PIDController::GetErrorDerivative() const {
return m_errorDerivative;
}
double PIDController::GetPositionError() const {
return m_positionError;
return m_error;
}
double PIDController::GetVelocityError() const {
return m_velocityError;
return m_errorDerivative;
}
double PIDController::Calculate(double measurement) {
m_measurement = measurement;
m_prevError = m_positionError;
m_prevError = m_error;
m_haveMeasurement = true;
if (m_continuous) {
double errorBound = (m_maximumInput - m_minimumInput) / 2.0;
m_positionError =
InputModulus(m_setpoint - m_measurement, -errorBound, errorBound);
m_error = InputModulus(m_setpoint - m_measurement, -errorBound, errorBound);
} else {
m_positionError = m_setpoint - m_measurement;
m_error = m_setpoint - m_measurement;
}
m_velocityError = (m_positionError - m_prevError) / m_period.value();
m_errorDerivative = (m_error - m_prevError) / m_period.value();
// If the absolute value of the position error is outside of IZone, reset the
// total error
if (std::abs(m_positionError) > m_iZone) {
if (std::abs(m_error) > m_iZone) {
m_totalError = 0;
} else if (m_Ki != 0) {
m_totalError =
std::clamp(m_totalError + m_positionError * m_period.value(),
std::clamp(m_totalError + m_error * m_period.value(),
m_minimumIntegral / m_Ki, m_maximumIntegral / m_Ki);
}
return m_Kp * m_positionError + m_Ki * m_totalError + m_Kd * m_velocityError;
return m_Kp * m_error + m_Ki * m_totalError + m_Kd * m_errorDerivative;
}
double PIDController::Calculate(double measurement, double setpoint) {
@@ -209,10 +223,10 @@ double PIDController::Calculate(double measurement, double setpoint) {
}
void PIDController::Reset() {
m_positionError = 0;
m_error = 0;
m_prevError = 0;
m_totalError = 0;
m_velocityError = 0;
m_errorDerivative = 0;
m_haveMeasurement = false;
}
@@ -230,4 +244,13 @@ void PIDController::InitSendable(wpi::SendableBuilder& builder) {
builder.AddDoubleProperty(
"setpoint", [this] { return GetSetpoint(); },
[this](double value) { SetSetpoint(value); });
builder.AddDoubleProperty(
"measurement", [this] { return m_measurement; }, nullptr);
builder.AddDoubleProperty("error", [this] { return GetError(); }, nullptr);
builder.AddDoubleProperty(
"error derivative", [this] { return GetErrorDerivative(); }, nullptr);
builder.AddDoubleProperty(
"previous error", [this] { return m_prevError; }, nullptr);
builder.AddDoubleProperty(
"total error", [this] { return GetAccumulatedError(); }, nullptr);
}

View File

@@ -121,18 +121,36 @@ class WPILIB_DLLEXPORT PIDController
*/
units::second_t GetPeriod() const;
/**
* Gets the error tolerance of this controller.
*
* @return The error tolerance of the controller.
*/
double GetErrorTolerance() const;
/**
* Gets the error derivative tolerance of this controller.
*
* @return The error derivative tolerance of the controller.
*/
double GetErrorDerivativeTolerance() const;
/**
* Gets the position tolerance of this controller.
*
* @return The position tolerance of the controller.
* @deprecated Use GetErrorTolerance() instead.
*/
[[deprecated("Use the GetErrorTolerance method instead.")]]
double GetPositionTolerance() const;
/**
* Gets the velocity tolerance of this controller.
*
* @return The velocity tolerance of the controller.
* @deprecated Use GetErrorDerivativeTolerance() instead.
*/
[[deprecated("Use the GetErrorDerivativeTolerance method instead.")]]
double GetVelocityTolerance() const;
/**
@@ -201,21 +219,35 @@ class WPILIB_DLLEXPORT PIDController
/**
* 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.
* @param errorTolerance error which is tolerable.
* @param errorDerivativeTolerance error derivative which is tolerable.
*/
void SetTolerance(
double positionTolerance,
double velocityTolerance = std::numeric_limits<double>::infinity());
void SetTolerance(double errorTolerance,
double errorDerivativeTolerance =
std::numeric_limits<double>::infinity());
/**
* Returns the difference between the setpoint and the measurement.
*/
double GetError() const;
/**
* Returns the error derivative.
*/
double GetErrorDerivative() const;
/**
* Returns the difference between the setpoint and the measurement.
* @deprecated Use GetError() instead.
*/
[[deprecated("Use GetError method instead.")]]
double GetPositionError() const;
/**
* Returns the velocity error.
* @deprecated Use GetErrorDerivative() instead.
*/
[[deprecated("Use GetErrorDerivative method instead.")]]
double GetVelocityError() const;
/**
@@ -268,8 +300,8 @@ class WPILIB_DLLEXPORT PIDController
bool m_continuous = false;
// The error at the time of the most recent call to Calculate()
double m_positionError = 0;
double m_velocityError = 0;
double m_error = 0;
double m_errorDerivative = 0;
// The error at the time of the second-most-recent call to Calculate() (used
// to compute velocity)
@@ -279,8 +311,8 @@ class WPILIB_DLLEXPORT PIDController
double m_totalError = 0;
// The error that is considered at setpoint.
double m_positionTolerance = 0.05;
double m_velocityTolerance = std::numeric_limits<double>::infinity();
double m_errorTolerance = 0.05;
double m_errorDerivativeTolerance = std::numeric_limits<double>::infinity();
double m_setpoint = 0;
double m_measurement = 0;

View File

@@ -162,7 +162,7 @@ class ProfiledPIDController
* @return The position tolerance of the controller.
*/
double GetPositionTolerance() const {
return m_controller.GetPositionTolerance();
return m_controller.GetErrorTolerance();
}
/**
@@ -171,7 +171,7 @@ class ProfiledPIDController
* @return The velocity tolerance of the controller.
*/
double GetVelocityTolerance() const {
return m_controller.GetVelocityTolerance();
return m_controller.GetErrorDerivativeTolerance();
}
/**
@@ -300,14 +300,14 @@ class ProfiledPIDController
* @return The error.
*/
Distance_t GetPositionError() const {
return Distance_t{m_controller.GetPositionError()};
return Distance_t{m_controller.GetError()};
}
/**
* Returns the change in error per second.
*/
Velocity_t GetVelocityError() const {
return Velocity_t{m_controller.GetVelocityError()};
return Velocity_t{m_controller.GetErrorDerivative()};
}
/**