Skip to content

Commit 1379b5e

Browse files
committed
Implements Driver Station Caching to speed up DS reads
The GetData HAL methods take 5 ms combined to grab all the data. So there is easily a period where the data could be locked and contended for. This now grabs the data into a cache, leave all joystick data readable. It then grabs the write lock, does a quick reference swap, and unlocks. This way, the writing only takes a few nanoseconds, rather then 5 milliseconds. Should fix DS Joystick Tests The tests need to wait longer. Extends wait times Might need to actually figure out locking driver station.
1 parent dd86fe3 commit 1379b5e

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

WPILib.Tests/TestDriverStation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public void TestJoystickButtons([Range(0,32)] int numButtons)
256256
}
257257
};
258258
DriverStationHelper.UpdateData();
259-
Thread.Sleep(10);
259+
Thread.Sleep(150);
260260
int buttonCount = DriverStation.Instance.GetStickButtonCount(0);
261261
Assert.That(buttonCount, Is.EqualTo(numButtons));
262262
for (int i = 0; i < buttonCount; i++)
@@ -271,7 +271,7 @@ public void TestJoystickButtons([Range(0,32)] int numButtons)
271271
buttons[i] = true;
272272
}
273273
DriverStationHelper.UpdateData();
274-
Thread.Sleep(10);
274+
Thread.Sleep(150);
275275
for (int i = 0; i < buttonCount; i++)
276276
{
277277
bool button = DriverStation.Instance.GetStickButton(0, (byte)(i + 1));
@@ -301,7 +301,7 @@ public void TestJoystickAxes([Range(0, 6)] int numAxes)
301301
}
302302
};
303303
DriverStationHelper.UpdateData();
304-
Thread.Sleep(10);
304+
Thread.Sleep(150);
305305
int axesCount = DriverStation.Instance.GetStickAxisCount(0);
306306
Assert.That(axesCount, Is.EqualTo(numAxes));
307307
for (int i = 0; i < axesCount; i++)
@@ -316,7 +316,7 @@ public void TestJoystickAxes([Range(0, 6)] int numAxes)
316316
axes[i] = -.598;
317317
}
318318
DriverStationHelper.UpdateData();
319-
Thread.Sleep(10);
319+
Thread.Sleep(150);
320320
for (int i = 0; i < axesCount; i++)
321321
{
322322
double axis = DriverStation.Instance.GetStickAxis(0, i);

WPILib/DriverStation.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,22 @@ public enum Alliance
3939
};
4040

4141
//Private Fields
42-
private readonly HALJoystickAxes[] m_joystickAxes = new HALJoystickAxes[JoystickPorts];
43-
private readonly HALJoystickPOVs[] m_joystickPOVs = new HALJoystickPOVs[JoystickPorts];
44-
private readonly HALJoystickButtons[] m_joystickButtons = new HALJoystickButtons[JoystickPorts];
45-
private readonly HALJoystickDescriptor[] m_joystickDescriptors = new HALJoystickDescriptor[JoystickPorts];
42+
private HALJoystickAxes[] m_joystickAxes = new HALJoystickAxes[JoystickPorts];
43+
private HALJoystickPOVs[] m_joystickPOVs = new HALJoystickPOVs[JoystickPorts];
44+
private HALJoystickButtons[] m_joystickButtons = new HALJoystickButtons[JoystickPorts];
45+
private HALJoystickDescriptor[] m_joystickDescriptors = new HALJoystickDescriptor[JoystickPorts];
46+
47+
private HALJoystickAxes[] m_joystickAxesCache = new HALJoystickAxes[JoystickPorts];
48+
private HALJoystickPOVs[] m_joystickPOVsCache = new HALJoystickPOVs[JoystickPorts];
49+
private HALJoystickButtons[] m_joystickButtonsCache = new HALJoystickButtons[JoystickPorts];
50+
private HALJoystickDescriptor[] m_joystickDescriptorsCache = new HALJoystickDescriptor[JoystickPorts];
4651

4752
//Pointers to the semaphores to the HAL and FPGA
4853
private readonly object m_dataSem;
4954

5055
private readonly IntPtr m_packetDataAvailableMutex;
5156
private readonly IntPtr m_packetDataAvailableSem;
52-
57+
5358
//New Control Data Fast Semaphore Lock
5459
private readonly object m_newControlDataLock = new object();
5560
private bool m_newControlData = false;
@@ -94,6 +99,14 @@ protected DriverStation()
9499
m_joystickDescriptors[i].isXbox = 0;
95100
m_joystickDescriptors[i].type = 0xFF;
96101
m_joystickDescriptors[i].name.byte0 = 0;
102+
103+
m_joystickButtonsCache[i].count = 0;
104+
m_joystickAxesCache[i].count = 0;
105+
m_joystickPOVsCache[i].count = 0;
106+
m_joystickDescriptorsCache[i] = new HALJoystickDescriptor();
107+
m_joystickDescriptorsCache[i].isXbox = 0;
108+
m_joystickDescriptorsCache[i].type = 0xFF;
109+
m_joystickDescriptorsCache[i].name.byte0 = 0;
97110
}
98111

99112
m_readWriteLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
@@ -185,17 +198,34 @@ public void WaitForData(int timeout = Timeout.Infinite)
185198
/// </summary>
186199
protected void GetData()
187200
{
201+
for (byte stick = 0; stick < JoystickPorts; stick++)
202+
{
203+
HALGetJoystickAxes(stick, ref m_joystickAxesCache[stick]);
204+
HALGetJoystickPOVs(stick, ref m_joystickPOVsCache[stick]);
205+
HALGetJoystickButtons(stick, ref m_joystickButtonsCache[stick]);
206+
HALGetJoystickDescriptor(stick, ref m_joystickDescriptorsCache[stick]);
207+
}
188208
bool lockEntered = false;
189209
try
190210
{
191211
m_readWriteLock.EnterWriteLock();
192212
lockEntered = true;
193-
for (byte stick = 0; stick < JoystickPorts; stick++)
194-
{
195-
HALGetJoystickAxes(stick, ref m_joystickAxes[stick]);
196-
HALGetJoystickPOVs(stick, ref m_joystickPOVs[stick]);
197-
HALGetJoystickButtons(stick, ref m_joystickButtons[stick]);
198-
HALGetJoystickDescriptor(stick, ref m_joystickDescriptors[stick]); }
213+
214+
HALJoystickAxes[] currentAxes = m_joystickAxes;
215+
m_joystickAxes = m_joystickAxesCache;
216+
m_joystickAxesCache = currentAxes;
217+
218+
HALJoystickButtons[] currentButtons = m_joystickButtons;
219+
m_joystickButtons = m_joystickButtonsCache;
220+
m_joystickButtonsCache = currentButtons;
221+
222+
HALJoystickPOVs[] currentPOVs = m_joystickPOVs;
223+
m_joystickPOVs = m_joystickPOVsCache;
224+
m_joystickPOVsCache = currentPOVs;
225+
226+
HALJoystickDescriptor[] currentDescriptor = m_joystickDescriptors;
227+
m_joystickDescriptors = m_joystickDescriptorsCache;
228+
m_joystickDescriptorsCache = currentDescriptor;
199229
}
200230
finally
201231
{

0 commit comments

Comments
 (0)