From c93b5bedf918d4f6cf3feacb535d9a76cca79538 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Thu, 14 Jul 2016 13:51:32 -0700 Subject: [PATCH] Miscellaneous cleanups for wpilibc PID controller (#175) * Reorder wpilibc PIDController's SetAbsoluteTolerance() and SetPercentTolerance() implementations to be consistent with header and wpilibj * Added std:: prefix to fabs() calls in wpilibc PIDController --- wpilibc/athena/src/PIDController.cpp | 34 +++++++++++++++------------- wpilibc/sim/src/PIDController.cpp | 34 +++++++++++++++------------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/wpilibc/athena/src/PIDController.cpp b/wpilibc/athena/src/PIDController.cpp index 5ecc2b8fee..20412df1d4 100644 --- a/wpilibc/athena/src/PIDController.cpp +++ b/wpilibc/athena/src/PIDController.cpp @@ -6,8 +6,10 @@ /*----------------------------------------------------------------------------*/ #include "PIDController.h" -#include + +#include #include + #include "HAL/HAL.h" #include "Notifier.h" #include "PIDOutput.h" @@ -110,7 +112,7 @@ void PIDController::Calculate() { m_error = m_setpoint - input; if (m_continuous) { - if (fabs(m_error) > (m_maximumInput - m_minimumInput) / 2) { + if (std::fabs(m_error) > (m_maximumInput - m_minimumInput) / 2) { if (m_error > 0) { m_error = m_error - m_maximumInput + m_minimumInput; } else { @@ -454,18 +456,6 @@ void PIDController::SetTolerance(float percent) { m_tolerance = percent; } -/* - * Set the percentage error which is considered tolerable for use with - * OnTarget. - * - * @param percentage error which is tolerable - */ -void PIDController::SetPercentTolerance(float percent) { - std::lock_guard sync(m_mutex); - m_toleranceType = kPercentTolerance; - m_tolerance = percent; -} - /* * Set the absolute error which is considered tolerable for use with * OnTarget. @@ -478,6 +468,18 @@ void PIDController::SetAbsoluteTolerance(float absTolerance) { m_tolerance = absTolerance; } +/* + * Set the percentage error which is considered tolerable for use with + * OnTarget. + * + * @param percentage error which is tolerable + */ +void PIDController::SetPercentTolerance(float percent) { + std::lock_guard sync(m_mutex); + m_toleranceType = kPercentTolerance; + m_tolerance = percent; +} + /* * Set the number of previous error samples to average for tolerancing. When * determining whether a mechanism is on target, the user may want to use a @@ -516,11 +518,11 @@ bool PIDController::OnTarget() const { double error = GetAvgError(); switch (m_toleranceType) { case kPercentTolerance: - return fabs(error) < + return std::fabs(error) < m_tolerance / 100 * (m_maximumInput - m_minimumInput); break; case kAbsoluteTolerance: - return fabs(error) < m_tolerance; + return std::fabs(error) < m_tolerance; break; case kNoTolerance: // TODO: this case needs an error diff --git a/wpilibc/sim/src/PIDController.cpp b/wpilibc/sim/src/PIDController.cpp index d78508a866..59764a6267 100644 --- a/wpilibc/sim/src/PIDController.cpp +++ b/wpilibc/sim/src/PIDController.cpp @@ -6,7 +6,9 @@ /*----------------------------------------------------------------------------*/ #include "PIDController.h" -#include + +#include + #include "Notifier.h" #include "PIDOutput.h" #include "PIDSource.h" @@ -122,7 +124,7 @@ void PIDController::Calculate() { std::lock_guard sync(m_mutex); m_error = m_setpoint - input; if (m_continuous) { - if (fabs(m_error) > (m_maximumInput - m_minimumInput) / 2) { + if (std::fabs(m_error) > (m_maximumInput - m_minimumInput) / 2) { if (m_error > 0) { m_error = m_error - m_maximumInput + m_minimumInput; } else { @@ -455,18 +457,6 @@ void PIDController::SetTolerance(float percent) { m_tolerance = percent; } -/** - * Set the percentage error which is considered tolerable for use with - * OnTarget. - * - * @param percent percentage error which is tolerable - */ -void PIDController::SetPercentTolerance(float percent) { - std::lock_guard lock(m_mutex); - m_toleranceType = kPercentTolerance; - m_tolerance = percent; -} - /** * Set the absolute error which is considered tolerable for use with * OnTarget. @@ -479,6 +469,18 @@ void PIDController::SetAbsoluteTolerance(float absTolerance) { m_tolerance = absTolerance; } +/** + * Set the percentage error which is considered tolerable for use with + * OnTarget. + * + * @param percent percentage error which is tolerable + */ +void PIDController::SetPercentTolerance(float percent) { + std::lock_guard lock(m_mutex); + m_toleranceType = kPercentTolerance; + m_tolerance = percent; +} + /** * Set the number of previous error samples to average for tolerancing. * @@ -515,11 +517,11 @@ bool PIDController::OnTarget() const { double error = GetError(); switch (m_toleranceType) { case kPercentTolerance: - return fabs(error) < + return std::fabs(error) < m_tolerance / 100 * (m_maximumInput - m_minimumInput); break; case kAbsoluteTolerance: - return fabs(error) < m_tolerance; + return std::fabs(error) < m_tolerance; break; case kNoTolerance: // TODO: this case needs an error return false;