Fixes control data packet delay (#875)

Because of an expected change in 2018 that didn't happen, we had a race
condition causing a 1 packet delay on all DS values. This fixes that.
This commit is contained in:
Thad House
2018-01-18 21:54:33 -08:00
committed by Peter Johnson
parent e4e1eab413
commit 07f70cf784
3 changed files with 76 additions and 2 deletions

View File

@@ -589,7 +589,22 @@ void DriverStation::WaitForData() { WaitForData(0); }
* @return true if new data, otherwise false
*/
bool DriverStation::WaitForData(double timeout) {
return static_cast<bool>(HAL_WaitForDSDataTimeout(timeout));
auto timeoutTime =
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
std::unique_lock<wpi::mutex> lock(m_waitForDataMutex);
int currentCount = m_waitForDataCounter;
while (m_waitForDataCounter == currentCount) {
if (timeout > 0) {
auto timedOut = m_waitForDataCond.wait_until(lock, timeoutTime);
if (timedOut == std::cv_status::timeout) {
return false;
}
} else {
m_waitForDataCond.wait(lock);
}
}
return true;
}
/**
@@ -674,6 +689,11 @@ void DriverStation::GetData() {
m_joystickButtons.swap(m_joystickButtonsCache);
m_joystickDescriptor.swap(m_joystickDescriptorCache);
m_matchInfo.swap(m_matchInfoCache);
std::lock_guard<wpi::mutex> waitLock(m_waitForDataMutex);
// Nofify all threads
m_waitForDataCounter++;
m_waitForDataCond.notify_all();
}
/**
@@ -682,6 +702,7 @@ void DriverStation::GetData() {
* This is only called once the first time GetInstance() is called
*/
DriverStation::DriverStation() {
m_waitForDataCounter = 0;
m_joystickAxes = std::make_unique<HAL_JoystickAxes[]>(kJoystickPorts);
m_joystickPOVs = std::make_unique<HAL_JoystickPOVs[]>(kJoystickPorts);
m_joystickButtons = std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);