mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
Adds match specific calls to Java and C++ (#720)
Uses caching, matching the joystick calls.
This commit is contained in:
committed by
Peter Johnson
parent
2225c4fee2
commit
7bbd13d914
@@ -20,6 +20,16 @@
|
||||
#include "Utility.h"
|
||||
#include "WPIErrors.h"
|
||||
|
||||
namespace frc {
|
||||
struct MatchInfoData {
|
||||
std::string eventName;
|
||||
std::string gameSpecificMessage;
|
||||
int matchNumber = 0;
|
||||
int replayNumber = 0;
|
||||
DriverStation::MatchType matchType = DriverStation::MatchType::kNone;
|
||||
};
|
||||
} // namespace frc
|
||||
|
||||
using namespace frc;
|
||||
|
||||
const double JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL = 1.0;
|
||||
@@ -93,7 +103,7 @@ bool DriverStation::GetStickButton(int stick, int button) {
|
||||
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
|
||||
return false;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
|
||||
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
|
||||
if (button > m_joystickButtons[stick].count) {
|
||||
// Unlock early so error printing isn't locked.
|
||||
lock.unlock();
|
||||
@@ -124,7 +134,7 @@ bool DriverStation::GetStickButtonPressed(int stick, int button) {
|
||||
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
|
||||
return false;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
|
||||
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
|
||||
if (button > m_joystickButtons[stick].count) {
|
||||
// Unlock early so error printing isn't locked.
|
||||
lock.unlock();
|
||||
@@ -161,7 +171,7 @@ bool DriverStation::GetStickButtonReleased(int stick, int button) {
|
||||
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
|
||||
return false;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
|
||||
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
|
||||
if (button > m_joystickButtons[stick].count) {
|
||||
// Unlock early so error printing isn't locked.
|
||||
lock.unlock();
|
||||
@@ -194,10 +204,10 @@ double DriverStation::GetStickAxis(int stick, int axis) {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return 0;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
|
||||
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
|
||||
if (axis >= m_joystickAxes[stick].count) {
|
||||
// Unlock early so error printing isn't locked.
|
||||
m_joystickDataMutex.unlock();
|
||||
m_cacheDataMutex.unlock();
|
||||
lock.release();
|
||||
if (axis >= HAL_kMaxJoystickAxes)
|
||||
wpi_setWPIError(BadJoystickAxis);
|
||||
@@ -220,7 +230,7 @@ int DriverStation::GetStickPOV(int stick, int pov) {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return -1;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(m_joystickDataMutex);
|
||||
std::unique_lock<std::mutex> lock(m_cacheDataMutex);
|
||||
if (pov >= m_joystickPOVs[stick].count) {
|
||||
// Unlock early so error printing isn't locked.
|
||||
lock.unlock();
|
||||
@@ -246,7 +256,7 @@ int DriverStation::GetStickButtons(int stick) const {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_joystickButtons[stick].buttons;
|
||||
}
|
||||
|
||||
@@ -261,7 +271,7 @@ int DriverStation::GetStickAxisCount(int stick) const {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_joystickAxes[stick].count;
|
||||
}
|
||||
|
||||
@@ -276,7 +286,7 @@ int DriverStation::GetStickPOVCount(int stick) const {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_joystickPOVs[stick].count;
|
||||
}
|
||||
|
||||
@@ -291,7 +301,7 @@ int DriverStation::GetStickButtonCount(int stick) const {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_joystickButtons[stick].count;
|
||||
}
|
||||
|
||||
@@ -306,7 +316,7 @@ bool DriverStation::GetJoystickIsXbox(int stick) const {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return false;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return static_cast<bool>(m_joystickDescriptor[stick].isXbox);
|
||||
}
|
||||
|
||||
@@ -321,7 +331,7 @@ int DriverStation::GetJoystickType(int stick) const {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return -1;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return static_cast<int>(m_joystickDescriptor[stick].type);
|
||||
}
|
||||
|
||||
@@ -335,7 +345,7 @@ std::string DriverStation::GetJoystickName(int stick) const {
|
||||
if (stick >= kJoystickPorts) {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
std::string retVal(m_joystickDescriptor[stick].name);
|
||||
return retVal;
|
||||
}
|
||||
@@ -351,7 +361,7 @@ int DriverStation::GetJoystickAxisType(int stick, int axis) const {
|
||||
wpi_setWPIError(BadJoystickIndex);
|
||||
return -1;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_joystickDescriptor[stick].axisTypes[axis];
|
||||
}
|
||||
|
||||
@@ -471,6 +481,31 @@ bool DriverStation::IsBrownedOut() const {
|
||||
return retVal;
|
||||
}
|
||||
|
||||
std::string DriverStation::GetGameSpecificMessage() const {
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_matchInfo->gameSpecificMessage;
|
||||
}
|
||||
|
||||
std::string DriverStation::GetEventName() const {
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_matchInfo->eventName;
|
||||
}
|
||||
|
||||
DriverStation::MatchType DriverStation::GetMatchType() const {
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_matchInfo->matchType;
|
||||
}
|
||||
|
||||
int DriverStation::GetMatchNumber() const {
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_matchInfo->matchNumber;
|
||||
}
|
||||
|
||||
int DriverStation::GetReplayNumber() const {
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
return m_matchInfo->replayNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the alliance that the driver station says it is on.
|
||||
*
|
||||
@@ -597,12 +632,25 @@ void DriverStation::GetData() {
|
||||
HAL_GetJoystickButtons(stick, &m_joystickButtonsCache[stick]);
|
||||
HAL_GetJoystickDescriptor(stick, &m_joystickDescriptorCache[stick]);
|
||||
}
|
||||
// Grab match specific data
|
||||
HAL_MatchInfo matchInfo;
|
||||
auto status = HAL_GetMatchInfo(&matchInfo);
|
||||
if (status == 0) {
|
||||
m_matchInfoCache->eventName = matchInfo.eventName;
|
||||
m_matchInfoCache->matchNumber = matchInfo.matchNumber;
|
||||
m_matchInfoCache->replayNumber = matchInfo.replayNumber;
|
||||
m_matchInfoCache->matchType =
|
||||
static_cast<DriverStation::MatchType>(matchInfo.matchType);
|
||||
m_matchInfoCache->gameSpecificMessage = matchInfo.gameSpecificMessage;
|
||||
}
|
||||
HAL_FreeMatchInfo(&matchInfo);
|
||||
|
||||
// Force a control word update, to make sure the data is the newest.
|
||||
HAL_ControlWord controlWord;
|
||||
UpdateControlWord(true, controlWord);
|
||||
// Obtain a write lock on the data, swap the cached data into the
|
||||
// main data arrays
|
||||
std::lock_guard<std::mutex> lock(m_joystickDataMutex);
|
||||
std::lock_guard<std::mutex> lock(m_cacheDataMutex);
|
||||
|
||||
for (int32_t i = 0; i < kJoystickPorts; i++) {
|
||||
// If buttons weren't pressed and are now, set flags in m_buttonsPressed
|
||||
@@ -618,6 +666,7 @@ void DriverStation::GetData() {
|
||||
m_joystickPOVs.swap(m_joystickPOVsCache);
|
||||
m_joystickButtons.swap(m_joystickButtonsCache);
|
||||
m_joystickDescriptor.swap(m_joystickDescriptorCache);
|
||||
m_matchInfo.swap(m_matchInfoCache);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -631,12 +680,14 @@ DriverStation::DriverStation() {
|
||||
m_joystickButtons = std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
|
||||
m_joystickDescriptor =
|
||||
std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
|
||||
m_matchInfo = std::make_unique<MatchInfoData>();
|
||||
m_joystickAxesCache = std::make_unique<HAL_JoystickAxes[]>(kJoystickPorts);
|
||||
m_joystickPOVsCache = std::make_unique<HAL_JoystickPOVs[]>(kJoystickPorts);
|
||||
m_joystickButtonsCache =
|
||||
std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
|
||||
m_joystickDescriptorCache =
|
||||
std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
|
||||
m_matchInfoCache = std::make_unique<MatchInfoData>();
|
||||
|
||||
// All joysticks should default to having zero axes, povs and buttons, so
|
||||
// uninitialized memory doesn't get sent to speed controllers.
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
namespace frc {
|
||||
|
||||
struct MatchInfoData;
|
||||
|
||||
/**
|
||||
* Provide access to the network communication data to / from the Driver
|
||||
* Station.
|
||||
@@ -29,6 +31,7 @@ namespace frc {
|
||||
class DriverStation : public SensorBase, public RobotStateInterface {
|
||||
public:
|
||||
enum Alliance { kRed, kBlue, kInvalid };
|
||||
enum MatchType { kNone, kPractice, kQualification, kElimination };
|
||||
|
||||
virtual ~DriverStation();
|
||||
static DriverStation& GetInstance();
|
||||
@@ -67,6 +70,12 @@ class DriverStation : public SensorBase, public RobotStateInterface {
|
||||
bool IsSysActive() const;
|
||||
bool IsBrownedOut() const;
|
||||
|
||||
std::string GetGameSpecificMessage() const;
|
||||
std::string GetEventName() const;
|
||||
MatchType GetMatchType() const;
|
||||
int GetMatchNumber() const;
|
||||
int GetReplayNumber() const;
|
||||
|
||||
Alliance GetAlliance() const;
|
||||
int GetLocation() const;
|
||||
void WaitForData();
|
||||
@@ -109,12 +118,14 @@ class DriverStation : public SensorBase, public RobotStateInterface {
|
||||
std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVs;
|
||||
std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtons;
|
||||
std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptor;
|
||||
std::unique_ptr<MatchInfoData> m_matchInfo;
|
||||
|
||||
// Joystick Cached Data
|
||||
std::unique_ptr<HAL_JoystickAxes[]> m_joystickAxesCache;
|
||||
std::unique_ptr<HAL_JoystickPOVs[]> m_joystickPOVsCache;
|
||||
std::unique_ptr<HAL_JoystickButtons[]> m_joystickButtonsCache;
|
||||
std::unique_ptr<HAL_JoystickDescriptor[]> m_joystickDescriptorCache;
|
||||
std::unique_ptr<MatchInfoData> m_matchInfoCache;
|
||||
|
||||
// Joystick button rising/falling edge flags
|
||||
std::array<uint32_t, kJoystickPorts> m_joystickButtonsPressed;
|
||||
@@ -124,7 +135,7 @@ class DriverStation : public SensorBase, public RobotStateInterface {
|
||||
std::thread m_dsThread;
|
||||
std::atomic<bool> m_isRunning{false};
|
||||
|
||||
mutable std::mutex m_joystickDataMutex;
|
||||
mutable std::mutex m_cacheDataMutex;
|
||||
|
||||
// Robot state status variables
|
||||
bool m_userInDisabled = false;
|
||||
|
||||
Reference in New Issue
Block a user