From 6b740e87b32be5abaa3af1495053ce2663f3669a Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Thu, 7 Jan 2016 20:55:10 -0800 Subject: [PATCH] Fix C++ PIDController SetToleranceBuffer and OnTarget locking. Also implement OnTarget fix in simulation PIDController. Change-Id: Ic4b452759f80aa769a721f22cb6e732c2a9a213a --- wpilibc/Athena/src/PIDController.cpp | 4 ++-- wpilibc/simulation/src/PIDController.cpp | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) 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);