diff --git a/wpilibc/Athena/src/PIDController.cpp b/wpilibc/Athena/src/PIDController.cpp index ef4a49986d..c8fb63f319 100644 --- a/wpilibc/Athena/src/PIDController.cpp +++ b/wpilibc/Athena/src/PIDController.cpp @@ -469,6 +469,7 @@ void PIDController::SetAbsoluteTolerance(float absTolerance) { * @param bufLength Number of previous cycles to average. Defaults to 1. */ void PIDController::SetToleranceBuffer(unsigned bufLength) { + std::lock_guard sync(m_mutex); m_bufLength = bufLength; // Cut the buffer down to size if needed. @@ -489,10 +490,9 @@ void PIDController::SetToleranceBuffer(unsigned bufLength) { * This will return false until at least one input value has been computed. */ bool PIDController::OnTarget() const { + std::lock_guard sync(m_mutex); if (m_buf.size() == 0) return false; double error = GetAvgError(); - - std::lock_guard sync(m_mutex); switch (m_toleranceType) { case kPercentTolerance: return fabs(error) < m_tolerance / 100 * (m_maximumInput - m_minimumInput); diff --git a/wpilibc/simulation/src/PIDController.cpp b/wpilibc/simulation/src/PIDController.cpp index f337d45b30..d3c18b5196 100644 --- a/wpilibc/simulation/src/PIDController.cpp +++ b/wpilibc/simulation/src/PIDController.cpp @@ -500,6 +500,7 @@ void PIDController::SetAbsoluteTolerance(float absTolerance) * @param bufLength Number of previous cycles to average. Defaults to 1. */ void PIDController::SetToleranceBuffer(unsigned bufLength) { + std::lock_guard lock(m_mutex); m_bufLength = bufLength; // Cut the buffer down to size if needed. @@ -518,9 +519,9 @@ void PIDController::SetToleranceBuffer(unsigned bufLength) { */ bool PIDController::OnTarget() const { - double error = GetError(); - std::lock_guard sync(m_mutex); + if (m_buf.size() == 0) return false; + double error = GetError(); switch (m_toleranceType) { case kPercentTolerance: return fabs(error) < m_tolerance / 100 * (m_maximumInput - m_minimumInput);