[wpilib] Split DriverStation into smaller classes (#8628)

This commit is contained in:
Thad House
2026-04-18 19:56:45 -07:00
committed by GitHub
parent 58ad633ae2
commit ab2aef2c29
108 changed files with 4406 additions and 3211 deletions

View File

@@ -505,14 +505,14 @@ BooleanEvent Gamepad::AxisGreaterThan(Axis axis, double threshold,
}
double Gamepad::GetAxisForSendable(Axis axis) const {
return DriverStation::GetStickAxisIfAvailable(GetPort(),
static_cast<int>(axis))
return wpi::internal::DriverStationBackend::GetStickAxisIfAvailable(
GetPort(), static_cast<int>(axis))
.value_or(0.0);
}
bool Gamepad::GetButtonForSendable(Button button) const {
return DriverStation::GetStickButtonIfAvailable(GetPort(),
static_cast<int>(button))
return wpi::internal::DriverStationBackend::GetStickButtonIfAvailable(
GetPort(), static_cast<int>(button))
.value_or(false);
}

View File

@@ -6,7 +6,7 @@
#include <string>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/event/BooleanEvent.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/system/Errors.hpp"
@@ -14,7 +14,7 @@
using namespace wpi;
GenericHID::GenericHID(int port) {
if (port < 0 || port >= DriverStation::JOYSTICK_PORTS) {
if (port < 0 || port >= wpi::internal::DriverStationBackend::JOYSTICK_PORTS) {
throw WPILIB_MakeError(warn::BadJoystickIndex, "port {} out of range",
port);
}
@@ -22,15 +22,17 @@ GenericHID::GenericHID(int port) {
}
bool GenericHID::GetRawButton(int button) const {
return DriverStation::GetStickButton(m_port, button);
return wpi::internal::DriverStationBackend::GetStickButton(m_port, button);
}
bool GenericHID::GetRawButtonPressed(int button) {
return DriverStation::GetStickButtonPressed(m_port, button);
return wpi::internal::DriverStationBackend::GetStickButtonPressed(m_port,
button);
}
bool GenericHID::GetRawButtonReleased(int button) {
return DriverStation::GetStickButtonReleased(m_port, button);
return wpi::internal::DriverStationBackend::GetStickButtonReleased(m_port,
button);
}
BooleanEvent GenericHID::Button(int button, EventLoop* loop) const {
@@ -39,58 +41,57 @@ BooleanEvent GenericHID::Button(int button, EventLoop* loop) const {
}
double GenericHID::GetRawAxis(int axis) const {
return DriverStation::GetStickAxis(m_port, axis);
return wpi::internal::DriverStationBackend::GetStickAxis(m_port, axis);
}
DriverStation::POVDirection GenericHID::GetPOV(int pov) const {
return DriverStation::GetStickPOV(m_port, pov);
POVDirection GenericHID::GetPOV(int pov) const {
return wpi::internal::DriverStationBackend::GetStickPOV(m_port, pov);
}
BooleanEvent GenericHID::POV(DriverStation::POVDirection angle,
EventLoop* loop) const {
BooleanEvent GenericHID::POV(POVDirection angle, EventLoop* loop) const {
return POV(0, angle, loop);
}
BooleanEvent GenericHID::POV(int pov, DriverStation::POVDirection angle,
BooleanEvent GenericHID::POV(int pov, POVDirection angle,
EventLoop* loop) const {
return BooleanEvent(
loop, [this, pov, angle] { return this->GetPOV(pov) == angle; });
}
BooleanEvent GenericHID::POVUp(EventLoop* loop) const {
return POV(DriverStation::POVDirection::UP, loop);
return POV(POVDirection::UP, loop);
}
BooleanEvent GenericHID::POVUpRight(EventLoop* loop) const {
return POV(DriverStation::POVDirection::UP_RIGHT, loop);
return POV(POVDirection::UP_RIGHT, loop);
}
BooleanEvent GenericHID::POVRight(EventLoop* loop) const {
return POV(DriverStation::POVDirection::RIGHT, loop);
return POV(POVDirection::RIGHT, loop);
}
BooleanEvent GenericHID::POVDownRight(EventLoop* loop) const {
return POV(DriverStation::POVDirection::DOWN_RIGHT, loop);
return POV(POVDirection::DOWN_RIGHT, loop);
}
BooleanEvent GenericHID::POVDown(EventLoop* loop) const {
return POV(DriverStation::POVDirection::DOWN, loop);
return POV(POVDirection::DOWN, loop);
}
BooleanEvent GenericHID::POVDownLeft(EventLoop* loop) const {
return POV(DriverStation::POVDirection::DOWN_LEFT, loop);
return POV(POVDirection::DOWN_LEFT, loop);
}
BooleanEvent GenericHID::POVLeft(EventLoop* loop) const {
return POV(DriverStation::POVDirection::LEFT, loop);
return POV(POVDirection::LEFT, loop);
}
BooleanEvent GenericHID::POVUpLeft(EventLoop* loop) const {
return POV(DriverStation::POVDirection::UP_LEFT, loop);
return POV(POVDirection::UP_LEFT, loop);
}
BooleanEvent GenericHID::POVCenter(EventLoop* loop) const {
return POV(DriverStation::POVDirection::CENTER, loop);
return POV(POVDirection::CENTER, loop);
}
BooleanEvent GenericHID::AxisLessThan(int axis, double threshold,
@@ -108,44 +109,46 @@ BooleanEvent GenericHID::AxisGreaterThan(int axis, double threshold,
}
int GenericHID::GetAxesMaximumIndex() const {
return DriverStation::GetStickAxesMaximumIndex(m_port);
return wpi::internal::DriverStationBackend::GetStickAxesMaximumIndex(m_port);
}
int GenericHID::GetAxesAvailable() const {
return DriverStation::GetStickAxesAvailable(m_port);
return wpi::internal::DriverStationBackend::GetStickAxesAvailable(m_port);
}
int GenericHID::GetPOVsMaximumIndex() const {
return DriverStation::GetStickPOVsMaximumIndex(m_port);
return wpi::internal::DriverStationBackend::GetStickPOVsMaximumIndex(m_port);
}
int GenericHID::GetPOVsAvailable() const {
return DriverStation::GetStickPOVsAvailable(m_port);
return wpi::internal::DriverStationBackend::GetStickPOVsAvailable(m_port);
}
int GenericHID::GetButtonsMaximumIndex() const {
return DriverStation::GetStickButtonsMaximumIndex(m_port);
return wpi::internal::DriverStationBackend::GetStickButtonsMaximumIndex(
m_port);
}
uint64_t GenericHID::GetButtonsAvailable() const {
return DriverStation::GetStickButtonsAvailable(m_port);
return wpi::internal::DriverStationBackend::GetStickButtonsAvailable(m_port);
}
bool GenericHID::IsConnected() const {
return DriverStation::IsJoystickConnected(m_port);
return wpi::internal::DriverStationBackend::IsJoystickConnected(m_port);
}
GenericHID::HIDType GenericHID::GetGamepadType() const {
return static_cast<HIDType>(DriverStation::GetJoystickGamepadType(m_port));
return static_cast<HIDType>(
wpi::internal::DriverStationBackend::GetJoystickGamepadType(m_port));
}
GenericHID::SupportedOutputs GenericHID::GetSupportedOutputs() const {
return static_cast<SupportedOutputs>(
DriverStation::GetJoystickSupportedOutputs(m_port));
wpi::internal::DriverStationBackend::GetJoystickSupportedOutputs(m_port));
}
std::string GenericHID::GetName() const {
return DriverStation::GetJoystickName(m_port);
return wpi::internal::DriverStationBackend::GetJoystickName(m_port);
}
int GenericHID::GetPort() const {
@@ -178,11 +181,11 @@ void GenericHID::SetRumble(RumbleType type, double value) {
}
bool GenericHID::GetTouchpadFingerAvailable(int touchpad, int finger) const {
return DriverStation::GetStickTouchpadFingerAvailable(m_port, touchpad,
finger);
return wpi::internal::DriverStationBackend::GetStickTouchpadFingerAvailable(
m_port, touchpad, finger);
}
DriverStation::TouchpadFinger GenericHID::GetTouchpadFinger(int touchpad,
int finger) const {
return DriverStation::GetStickTouchpadFinger(m_port, touchpad, finger);
TouchpadFinger GenericHID::GetTouchpadFinger(int touchpad, int finger) const {
return wpi::internal::DriverStationBackend::GetStickTouchpadFinger(
m_port, touchpad, finger);
}

View File

@@ -2,7 +2,7 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include <stdint.h>
@@ -40,6 +40,7 @@
#include "wpi/util/timestamp.hpp"
using namespace wpi;
using namespace wpi::internal;
static constexpr int availableToCount(uint64_t available) {
return 64 - std::countl_zero(available);
@@ -136,7 +137,8 @@ class DataLogSender {
wpi::log::StringLogEntry m_logOpMode;
bool m_logJoysticks;
std::array<JoystickLogSender, DriverStation::JOYSTICK_PORTS> m_joysticks;
std::array<JoystickLogSender, DriverStationBackend::JOYSTICK_PORTS>
m_joysticks;
};
struct Instance {
@@ -149,10 +151,12 @@ struct Instance {
// Joystick button rising/falling edge flags
wpi::util::mutex buttonEdgeMutex;
std::array<HAL_JoystickButtons, DriverStation::JOYSTICK_PORTS>
std::array<HAL_JoystickButtons, DriverStationBackend::JOYSTICK_PORTS>
previousButtonStates;
std::array<uint32_t, DriverStation::JOYSTICK_PORTS> joystickButtonsPressed;
std::array<uint32_t, DriverStation::JOYSTICK_PORTS> joystickButtonsReleased;
std::array<uint32_t, DriverStationBackend::JOYSTICK_PORTS>
joystickButtonsPressed;
std::array<uint32_t, DriverStationBackend::JOYSTICK_PORTS>
joystickButtonsReleased;
bool silenceJoystickWarning = false;
@@ -211,7 +215,7 @@ Instance::Instance() {
// All joysticks should default to having zero axes, povs and buttons, so
// uninitialized memory doesn't get sent to motor controllers.
for (unsigned int i = 0; i < DriverStation::JOYSTICK_PORTS; i++) {
for (unsigned int i = 0; i < DriverStationBackend::JOYSTICK_PORTS; i++) {
joystickButtonsPressed[i] = 0;
joystickButtonsReleased[i] = 0;
previousButtonStates[i].available = 0;
@@ -225,7 +229,7 @@ Instance::~Instance() {
}
}
bool DriverStation::GetStickButton(int stick, int button) {
bool DriverStationBackend::GetStickButton(int stick, int button) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return false;
@@ -250,8 +254,8 @@ bool DriverStation::GetStickButton(int stick, int button) {
return (buttons.buttons & mask) != 0;
}
std::optional<bool> DriverStation::GetStickButtonIfAvailable(int stick,
int button) {
std::optional<bool> DriverStationBackend::GetStickButtonIfAvailable(
int stick, int button) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return false;
@@ -274,7 +278,7 @@ std::optional<bool> DriverStation::GetStickButtonIfAvailable(int stick,
return (buttons.buttons & mask) != 0;
}
bool DriverStation::GetStickButtonPressed(int stick, int button) {
bool DriverStationBackend::GetStickButtonPressed(int stick, int button) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return false;
@@ -305,7 +309,7 @@ bool DriverStation::GetStickButtonPressed(int stick, int button) {
return false;
}
bool DriverStation::GetStickButtonReleased(int stick, int button) {
bool DriverStationBackend::GetStickButtonReleased(int stick, int button) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return false;
@@ -336,7 +340,7 @@ bool DriverStation::GetStickButtonReleased(int stick, int button) {
return false;
}
double DriverStation::GetStickAxis(int stick, int axis) {
double DriverStationBackend::GetStickAxis(int stick, int axis) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return 0.0;
@@ -360,8 +364,9 @@ double DriverStation::GetStickAxis(int stick, int axis) {
return axes.axes[axis];
}
DriverStation::TouchpadFinger DriverStation::GetStickTouchpadFinger(
int stick, int touchpad, int finger) {
TouchpadFinger DriverStationBackend::GetStickTouchpadFinger(int stick,
int touchpad,
int finger) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return TouchpadFinger{false, 0.0f, 0.0f};
@@ -396,8 +401,9 @@ DriverStation::TouchpadFinger DriverStation::GetStickTouchpadFinger(
return TouchpadFinger{false, 0.0f, 0.0f};
}
bool DriverStation::GetStickTouchpadFingerAvailable(int stick, int touchpad,
int finger) {
bool DriverStationBackend::GetStickTouchpadFingerAvailable(int stick,
int touchpad,
int finger) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return false;
@@ -425,8 +431,8 @@ bool DriverStation::GetStickTouchpadFingerAvailable(int stick, int touchpad,
return false;
}
std::optional<double> DriverStation::GetStickAxisIfAvailable(int stick,
int axis) {
std::optional<double> DriverStationBackend::GetStickAxisIfAvailable(int stick,
int axis) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return 0.0;
@@ -448,7 +454,7 @@ std::optional<double> DriverStation::GetStickAxisIfAvailable(int stick,
return axes.axes[axis];
}
DriverStation::POVDirection DriverStation::GetStickPOV(int stick, int pov) {
POVDirection DriverStationBackend::GetStickPOV(int stick, int pov) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return POVDirection::CENTER;
@@ -472,7 +478,7 @@ DriverStation::POVDirection DriverStation::GetStickPOV(int stick, int pov) {
return static_cast<POVDirection>(povs.povs[pov]);
}
uint64_t DriverStation::GetStickButtons(int stick) {
uint64_t DriverStationBackend::GetStickButtons(int stick) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return 0;
@@ -484,11 +490,11 @@ uint64_t DriverStation::GetStickButtons(int stick) {
return buttons.buttons;
}
int DriverStation::GetStickAxesMaximumIndex(int stick) {
int DriverStationBackend::GetStickAxesMaximumIndex(int stick) {
return availableToCount(GetStickAxesAvailable(stick));
}
int DriverStation::GetStickAxesAvailable(int stick) {
int DriverStationBackend::GetStickAxesAvailable(int stick) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return 0;
@@ -500,11 +506,11 @@ int DriverStation::GetStickAxesAvailable(int stick) {
return axes.available;
}
int DriverStation::GetStickPOVsMaximumIndex(int stick) {
int DriverStationBackend::GetStickPOVsMaximumIndex(int stick) {
return availableToCount(GetStickPOVsAvailable(stick));
}
int DriverStation::GetStickPOVsAvailable(int stick) {
int DriverStationBackend::GetStickPOVsAvailable(int stick) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return 0;
@@ -516,11 +522,11 @@ int DriverStation::GetStickPOVsAvailable(int stick) {
return povs.available;
}
int DriverStation::GetStickButtonsMaximumIndex(int stick) {
int DriverStationBackend::GetStickButtonsMaximumIndex(int stick) {
return availableToCount(GetStickButtonsAvailable(stick));
}
uint64_t DriverStation::GetStickButtonsAvailable(int stick) {
uint64_t DriverStationBackend::GetStickButtonsAvailable(int stick) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return 0;
@@ -532,7 +538,7 @@ uint64_t DriverStation::GetStickButtonsAvailable(int stick) {
return buttons.available;
}
bool DriverStation::GetJoystickIsGamepad(int stick) {
bool DriverStationBackend::GetJoystickIsGamepad(int stick) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return false;
@@ -544,7 +550,7 @@ bool DriverStation::GetJoystickIsGamepad(int stick) {
return static_cast<bool>(descriptor.isGamepad);
}
int DriverStation::GetJoystickGamepadType(int stick) {
int DriverStationBackend::GetJoystickGamepadType(int stick) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return -1;
@@ -556,7 +562,7 @@ int DriverStation::GetJoystickGamepadType(int stick) {
return static_cast<int>(descriptor.gamepadType);
}
int DriverStation::GetJoystickSupportedOutputs(int stick) {
int DriverStationBackend::GetJoystickSupportedOutputs(int stick) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
return 0;
@@ -568,7 +574,7 @@ int DriverStation::GetJoystickSupportedOutputs(int stick) {
return static_cast<int>(descriptor.supportedOutputs);
}
std::string DriverStation::GetJoystickName(int stick) {
std::string DriverStationBackend::GetJoystickName(int stick) {
if (stick < 0 || stick >= JOYSTICK_PORTS) {
WPILIB_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick);
}
@@ -579,7 +585,7 @@ std::string DriverStation::GetJoystickName(int stick) {
return descriptor.name;
}
bool DriverStation::IsJoystickConnected(int stick) {
bool DriverStationBackend::IsJoystickConnected(int stick) {
return GetStickAxesAvailable(stick) != 0 ||
GetStickButtonsAvailable(stick) != 0 ||
GetStickPOVsAvailable(stick) != 0;
@@ -624,23 +630,23 @@ static int32_t ConvertColorToInt(const wpi::util::Color& color) {
(static_cast<int32_t>(color.blue * 255) & 0xff);
}
int64_t DriverStation::AddOpMode(RobotMode mode, std::string_view name,
std::string_view group,
std::string_view description,
const wpi::util::Color& textColor,
const wpi::util::Color& backgroundColor) {
int64_t DriverStationBackend::AddOpMode(
RobotMode mode, std::string_view name, std::string_view group,
std::string_view description, const wpi::util::Color& textColor,
const wpi::util::Color& backgroundColor) {
return DoAddOpMode(mode, name, group, description,
ConvertColorToInt(textColor),
ConvertColorToInt(backgroundColor));
}
int64_t DriverStation::AddOpMode(RobotMode mode, std::string_view name,
std::string_view group,
std::string_view description) {
int64_t DriverStationBackend::AddOpMode(RobotMode mode, std::string_view name,
std::string_view group,
std::string_view description) {
return DoAddOpMode(mode, name, group, description, -1, -1);
}
int64_t DriverStation::RemoveOpMode(RobotMode mode, std::string_view name) {
int64_t DriverStationBackend::RemoveOpMode(RobotMode mode,
std::string_view name) {
if (wpi::util::trim(name).empty()) {
return 0;
}
@@ -662,7 +668,7 @@ int64_t DriverStation::RemoveOpMode(RobotMode mode, std::string_view name) {
return 0;
}
void DriverStation::PublishOpModes() {
void DriverStationBackend::PublishOpModes() {
auto& inst = ::GetInstance();
std::scoped_lock lock{inst.opModeMutex};
std::vector<HAL_OpModeOption> options;
@@ -673,19 +679,19 @@ void DriverStation::PublishOpModes() {
HAL_SetOpModeOptions(options.data(), options.size());
}
void DriverStation::ClearOpModes() {
void DriverStationBackend::ClearOpModes() {
auto& inst = ::GetInstance();
std::scoped_lock lock{inst.opModeMutex};
inst.opModes.clear();
HAL_SetOpModeOptions(nullptr, 0);
}
void DriverStation::ObserveUserProgramStarting() {
void DriverStationBackend::ObserveUserProgramStarting() {
::GetInstance().userProgramStarted = true;
HAL_ObserveUserProgramStarting();
}
int64_t DriverStation::GetOpModeId() {
int64_t DriverStationBackend::GetOpModeId() {
if (!::GetInstance().userProgramStarted) {
return 0;
}
@@ -693,7 +699,7 @@ int64_t DriverStation::GetOpModeId() {
return GetControlWord().GetOpModeId();
}
std::string DriverStation::GetOpMode() {
std::string DriverStationBackend::GetOpMode() {
if (!::GetInstance().userProgramStarted) {
return "";
}
@@ -701,7 +707,7 @@ std::string DriverStation::GetOpMode() {
return GetInstance().OpModeToString(GetOpModeId());
}
std::optional<std::string> DriverStation::GetGameData() {
std::optional<std::string> DriverStationBackend::GetGameData() {
HAL_GameData info;
HAL_GetGameData(&info);
std::string_view gameDataView{reinterpret_cast<char*>(info.gameData)};
@@ -711,31 +717,31 @@ std::optional<std::string> DriverStation::GetGameData() {
return std::string(gameDataView);
}
std::string DriverStation::GetEventName() {
std::string DriverStationBackend::GetEventName() {
HAL_MatchInfo info;
HAL_GetMatchInfo(&info);
return info.eventName;
}
DriverStation::MatchType DriverStation::GetMatchType() {
MatchType DriverStationBackend::GetMatchType() {
HAL_MatchInfo info;
HAL_GetMatchInfo(&info);
return static_cast<DriverStation::MatchType>(info.matchType);
return static_cast<MatchType>(info.matchType);
}
int DriverStation::GetMatchNumber() {
int DriverStationBackend::GetMatchNumber() {
HAL_MatchInfo info;
HAL_GetMatchInfo(&info);
return info.matchNumber;
}
int DriverStation::GetReplayNumber() {
int DriverStationBackend::GetReplayNumber() {
HAL_MatchInfo info;
HAL_GetMatchInfo(&info);
return info.replayNumber;
}
std::optional<DriverStation::Alliance> DriverStation::GetAlliance() {
std::optional<Alliance> DriverStationBackend::GetAlliance() {
int32_t status = 0;
auto allianceStationID = HAL_GetAllianceStation(&status);
switch (allianceStationID) {
@@ -752,7 +758,7 @@ std::optional<DriverStation::Alliance> DriverStation::GetAlliance() {
}
}
std::optional<int> DriverStation::GetLocation() {
std::optional<int> DriverStationBackend::GetLocation() {
int32_t status = 0;
auto allianceStationID = HAL_GetAllianceStation(&status);
switch (allianceStationID) {
@@ -770,12 +776,12 @@ std::optional<int> DriverStation::GetLocation() {
}
}
wpi::units::second_t DriverStation::GetMatchTime() {
wpi::units::second_t DriverStationBackend::GetMatchTime() {
int32_t status = 0;
return wpi::units::second_t{HAL_GetMatchTime(&status)};
}
double DriverStation::GetBatteryVoltage() {
double DriverStationBackend::GetBatteryVoltage() {
int32_t status = 0;
double voltage = HAL_GetVinVoltage(&status);
WPILIB_CheckErrorStatus(status, "getVinVoltage");
@@ -789,7 +795,7 @@ double DriverStation::GetBatteryVoltage() {
* If no new data exists, it will just be returned, otherwise
* the data will be copied from the DS polling loop.
*/
void DriverStation::RefreshData() {
void DriverStationBackend::RefreshData() {
HAL_RefreshDSData();
auto& inst = ::GetInstance();
{
@@ -797,7 +803,7 @@ void DriverStation::RefreshData() {
HAL_JoystickButtons currentButtons;
std::unique_lock lock(inst.buttonEdgeMutex);
for (int32_t i = 0; i < DriverStation::JOYSTICK_PORTS; i++) {
for (int32_t i = 0; i < DriverStationBackend::JOYSTICK_PORTS; i++) {
HAL_GetJoystickButtons(i, &currentButtons);
// If buttons weren't pressed and are now, set flags in m_buttonsPressed
@@ -820,25 +826,28 @@ void DriverStation::RefreshData() {
}
}
void DriverStation::ProvideRefreshedDataEventHandle(WPI_EventHandle handle) {
void DriverStationBackend::ProvideRefreshedDataEventHandle(
WPI_EventHandle handle) {
auto& inst = ::GetInstance();
inst.refreshEvents.Add(handle);
}
void DriverStation::RemoveRefreshedDataEventHandle(WPI_EventHandle handle) {
void DriverStationBackend::RemoveRefreshedDataEventHandle(
WPI_EventHandle handle) {
auto& inst = ::GetInstance();
inst.refreshEvents.Remove(handle);
}
void DriverStation::SilenceJoystickConnectionWarning(bool silence) {
void DriverStationBackend::SilenceJoystickConnectionWarning(bool silence) {
::GetInstance().silenceJoystickWarning = silence;
}
bool DriverStation::IsJoystickConnectionWarningSilenced() {
bool DriverStationBackend::IsJoystickConnectionWarningSilenced() {
return !IsFMSAttached() && ::GetInstance().silenceJoystickWarning;
}
void DriverStation::StartDataLog(wpi::log::DataLog& log, bool logJoysticks) {
void DriverStationBackend::StartDataLog(wpi::log::DataLog& log,
bool logJoysticks) {
auto& inst = ::GetInstance();
// Note: cannot safely replace, because we wouldn't know when to delete the
// "old" one. Instead do a compare and exchange with nullptr. We check first
@@ -859,10 +868,10 @@ void DriverStation::StartDataLog(wpi::log::DataLog& log, bool logJoysticks) {
void ReportJoystickWarningV(int stick, fmt::string_view format,
fmt::format_args args) {
auto& inst = GetInstance();
if (DriverStation::IsFMSAttached() || !inst.silenceJoystickWarning) {
if (DriverStationBackend::IsFMSAttached() || !inst.silenceJoystickWarning) {
auto currentTime = Timer::GetTimestamp();
if (currentTime > inst.nextMessageTime) {
if (DriverStation::IsJoystickConnected(stick)) {
if (DriverStationBackend::IsJoystickConnected(stick)) {
ReportErrorV(warn::Warning, "", 0, "", format, args);
} else {
ReportError(

View File

@@ -4,7 +4,8 @@
#include "wpi/framework/IterativeRobotBase.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/RobotState.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/hal/DriverStationTypes.h"
#include "wpi/nt/NetworkTableInstance.hpp"
@@ -91,7 +92,7 @@ wpi::units::second_t IterativeRobotBase::GetPeriod() const {
}
void IterativeRobotBase::LoopFunc() {
DriverStation::RefreshData();
wpi::internal::DriverStationBackend::RefreshData();
m_watchdog.Reset();
// Get current mode; treat disabled as unknown

View File

@@ -12,7 +12,8 @@
#include <fmt/format.h>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/RobotState.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/hal/DriverStationTypes.h"
#include "wpi/hal/HAL.h"
@@ -54,10 +55,11 @@ void OpModeRobotBase::AddPeriodic(std::function<void()> callback,
}
void OpModeRobotBase::LoopFunc() {
DriverStation::RefreshData();
wpi::internal::DriverStationBackend::RefreshData();
// Get current enabled state and opmode
const hal::ControlWord word = DriverStation::GetControlWord();
const hal::ControlWord word =
wpi::internal::DriverStationBackend::GetControlWord();
m_watchdog.Reset();
const bool enabled = word.IsEnabled();
int64_t modeId = word.IsDSAttached() ? word.GetOpModeId() : 0;
@@ -194,7 +196,7 @@ void OpModeRobotBase::StartCompetition() {
}
// Tell the DS that the robot is ready to be enabled
DriverStation::ObserveUserProgramStarting();
wpi::internal::DriverStationBackend::ObserveUserProgramStarting();
// Loop forever, calling the callback system which handles periodic functions
while (true) {
@@ -215,8 +217,8 @@ void OpModeRobotBase::AddOpModeFactory(
std::string_view group, std::string_view description,
const wpi::util::Color& textColor,
const wpi::util::Color& backgroundColor) {
int64_t id = DriverStation::AddOpMode(mode, name, group, description,
textColor, backgroundColor);
int64_t id = RobotState::AddOpMode(mode, name, group, description, textColor,
backgroundColor);
if (id != 0) {
m_opModes[id] = OpModeData{std::string{name}, std::move(factory)};
}
@@ -226,24 +228,24 @@ void OpModeRobotBase::AddOpModeFactory(OpModeFactory factory, RobotMode mode,
std::string_view name,
std::string_view group,
std::string_view description) {
int64_t id = DriverStation::AddOpMode(mode, name, group, description);
int64_t id = RobotState::AddOpMode(mode, name, group, description);
if (id != 0) {
m_opModes[id] = OpModeData{std::string{name}, std::move(factory)};
}
}
void OpModeRobotBase::RemoveOpMode(RobotMode mode, std::string_view name) {
int64_t id = DriverStation::RemoveOpMode(mode, name);
int64_t id = RobotState::RemoveOpMode(mode, name);
if (id != 0) {
m_opModes.erase(id);
}
}
void OpModeRobotBase::PublishOpModes() {
DriverStation::PublishOpModes();
RobotState::PublishOpModes();
}
void OpModeRobotBase::ClearOpModes() {
DriverStation::ClearOpModes();
RobotState::ClearOpModes();
m_opModes.clear();
}

View File

@@ -10,6 +10,7 @@
#include <utility>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/hal/DriverStation.hpp"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/system/Errors.hpp"
@@ -24,7 +25,7 @@ void TimedRobot::StartCompetition() {
// Tell the DS that the robot is ready to be enabled
std::puts("\n********** Robot program startup complete **********");
DriverStation::ObserveUserProgramStarting();
wpi::internal::DriverStationBackend::ObserveUserProgramStarting();
// Loop forever, calling the appropriate mode-dependent function
while (true) {

View File

@@ -6,7 +6,7 @@
#include <utility>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/RobotState.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/hal/DriverStationTypes.h"
#include "wpi/system/Errors.hpp"
@@ -153,7 +153,7 @@ void MotorSafety::Check() {
stopTime = m_stopTime;
}
if (!enabled || DriverStation::IsDisabled() || DriverStation::IsTest()) {
if (!enabled || RobotState::IsDisabled() || RobotState::IsTest()) {
return;
}

View File

@@ -4,7 +4,7 @@
#include "wpi/internal/DriverStationModeThread.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/util/Synchronization.hpp"
@@ -33,7 +33,7 @@ void DriverStationModeThread::Run() {
if (!m_keepAlive) {
break;
}
wpi::DriverStation::RefreshData();
wpi::internal::DriverStationBackend::RefreshData();
HAL_ObserveUserProgram({.value = m_userControlWord});
}
}

View File

@@ -6,7 +6,7 @@
#include <memory>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/hal/simulation/DriverStationData.h"
#include "wpi/hal/simulation/MockHooks.h"
@@ -175,7 +175,7 @@ void DriverStationSim::NotifyNewData() {
HALSIM_NotifyDriverStationNewData();
wpi::util::WaitForObject(waitEvent.GetHandle());
HAL_RemoveNewDataEventHandle(waitEvent.GetHandle());
wpi::DriverStation::RefreshData();
wpi::internal::DriverStationBackend::RefreshData();
}
void DriverStationSim::SetSendError(bool shouldSend) {
@@ -232,8 +232,7 @@ void DriverStationSim::SetJoystickAxis(int stick, int axis, double value) {
HALSIM_SetJoystickAxis(stick, axis, value);
}
void DriverStationSim::SetJoystickPOV(int stick, int pov,
DriverStation::POVDirection value) {
void DriverStationSim::SetJoystickPOV(int stick, int pov, POVDirection value) {
HALSIM_SetJoystickPOV(stick, pov, static_cast<HAL_JoystickPOV>(value));
}
@@ -313,7 +312,7 @@ void DriverStationSim::SetEventName(std::string_view name) {
HALSIM_SetEventName(&str);
}
void DriverStationSim::SetMatchType(DriverStation::MatchType type) {
void DriverStationSim::SetMatchType(MatchType type) {
HALSIM_SetMatchType(static_cast<HAL_MatchType>(static_cast<int>(type)));
}

View File

@@ -4,8 +4,8 @@
#include "wpi/simulation/GenericHIDSim.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/simulation/DriverStationSim.hpp"
using namespace wpi;
@@ -28,11 +28,11 @@ void GenericHIDSim::SetRawAxis(int axis, double value) {
DriverStationSim::SetJoystickAxis(m_port, axis, value);
}
void GenericHIDSim::SetPOV(int pov, DriverStation::POVDirection value) {
void GenericHIDSim::SetPOV(int pov, POVDirection value) {
DriverStationSim::SetJoystickPOV(m_port, pov, value);
}
void GenericHIDSim::SetPOV(DriverStation::POVDirection value) {
void GenericHIDSim::SetPOV(POVDirection value) {
SetPOV(0, value);
}

View File

@@ -15,7 +15,9 @@
#include "wpi/datalog/DataLog.hpp"
#include "wpi/datalog/DataLogBackgroundWriter.hpp"
#include "wpi/datalog/FileLogger.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/MatchState.hpp"
#include "wpi/driverstation/RobotState.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/framework/RobotBase.hpp"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/nt/NetworkTableInstance.hpp"
@@ -190,7 +192,8 @@ void Thread::Main() {
"{\"source\":\"DataLogManager\",\"format\":\"time_t_us\"}"};
wpi::util::Event newDataEvent;
DriverStation::ProvideRefreshedDataEventHandle(newDataEvent.GetHandle());
wpi::internal::DriverStationBackend::ProvideRefreshedDataEventHandle(
newDataEvent.GetHandle());
for (;;) {
bool timedOut = false;
@@ -218,7 +221,7 @@ void Thread::Main() {
if (!dsRenamed) {
// track DS attach
if (DriverStation::IsDSAttached()) {
if (RobotState::IsDSAttached()) {
++dsAttachCount;
} else {
dsAttachCount = 0;
@@ -236,7 +239,7 @@ void Thread::Main() {
if (!fmsRenamed) {
// track FMS attach
if (DriverStation::IsFMSAttached()) {
if (RobotState::IsFMSAttached()) {
++fmsAttachCount;
} else {
fmsAttachCount = 0;
@@ -244,18 +247,18 @@ void Thread::Main() {
if (fmsAttachCount > 250) { // 5 seconds
// match info comes through TCP, so we need to double-check we've
// actually received it
auto matchType = DriverStation::GetMatchType();
if (matchType != DriverStation::MatchType::NONE) {
auto matchType = MatchState::GetMatchType();
if (matchType != wpi::MatchType::NONE) {
// rename per match info
char matchTypeChar;
switch (matchType) {
case DriverStation::MatchType::PRACTICE:
case wpi::MatchType::PRACTICE:
matchTypeChar = 'P';
break;
case DriverStation::MatchType::QUALIFICATION:
case wpi::MatchType::QUALIFICATION:
matchTypeChar = 'Q';
break;
case DriverStation::MatchType::ELIMINATION:
case wpi::MatchType::ELIMINATION:
matchTypeChar = 'E';
break;
default:
@@ -265,8 +268,8 @@ void Thread::Main() {
auto now = std::chrono::system_clock::now();
m_log.SetFilename(
fmt::format("WPILIB_{:%Y%m%d_%H%M%S}_{}_{}{}.wpilog", now,
DriverStation::GetEventName(), matchTypeChar,
DriverStation::GetMatchNumber()));
MatchState::GetEventName(), matchTypeChar,
MatchState::GetMatchNumber()));
fmsRenamed = true;
dsRenamed = true; // don't override FMS rename
}
@@ -282,7 +285,8 @@ void Thread::Main() {
}
}
}
DriverStation::RemoveRefreshedDataEventHandle(newDataEvent.GetHandle());
wpi::internal::DriverStationBackend::RemoveRefreshedDataEventHandle(
newDataEvent.GetHandle());
}
void Thread::StartNTLog() {

View File

@@ -7,7 +7,7 @@
#include <chrono>
#include <thread>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/MatchState.hpp"
#include "wpi/system/RobotController.hpp"
namespace wpi {
@@ -99,5 +99,5 @@ wpi::units::second_t Timer::GetMonotonicTimestamp() {
}
wpi::units::second_t Timer::GetMatchTime() {
return wpi::DriverStation::GetMatchTime();
return wpi::MatchState::GetMatchTime();
}

View File

@@ -16,7 +16,8 @@
#include <utility>
#include "wpi/cameraserver/CameraServerShared.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/RobotState.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/hal/HAL.h"
#include "wpi/hal/UsageReporting.hpp"
#include "wpi/math/util/MathShared.hpp"
@@ -42,7 +43,7 @@ int wpi::RunHALInitialization() {
std::puts("FATAL ERROR: HAL could not be initialized");
return -1;
}
DriverStation::RefreshData();
wpi::internal::DriverStationBackend::RefreshData();
HAL_ReportUsage("Language", "C++");
HAL_ReportUsage("WPILibVersion", GetWPILibVersion());
@@ -134,43 +135,43 @@ static void SetupMathShared() {
}
bool RobotBase::IsEnabled() {
return DriverStation::IsEnabled();
return RobotState::IsEnabled();
}
bool RobotBase::IsDisabled() {
return DriverStation::IsDisabled();
return RobotState::IsDisabled();
}
bool RobotBase::IsAutonomous() {
return DriverStation::IsAutonomous();
return RobotState::IsAutonomous();
}
bool RobotBase::IsAutonomousEnabled() {
return DriverStation::IsAutonomousEnabled();
return RobotState::IsAutonomousEnabled();
}
bool RobotBase::IsTeleop() {
return DriverStation::IsTeleop();
return RobotState::IsTeleop();
}
bool RobotBase::IsTeleopEnabled() {
return DriverStation::IsTeleopEnabled();
return RobotState::IsTeleopEnabled();
}
bool RobotBase::IsTest() {
return DriverStation::IsTest();
return RobotState::IsTest();
}
bool RobotBase::IsTestEnabled() {
return DriverStation::IsTestEnabled();
return RobotState::IsTestEnabled();
}
int64_t RobotBase::GetOpModeId() {
return DriverStation::GetOpModeId();
return RobotState::GetOpModeId();
}
std::string RobotBase::GetOpMode() {
return DriverStation::GetOpMode();
return RobotState::GetOpMode();
}
std::thread::id RobotBase::GetThreadId() {
@@ -219,6 +220,6 @@ RobotBase::RobotBase() {
SmartDashboard::init();
// Call DriverStation::RefreshData() to kick things off
DriverStation::RefreshData();
// Call wpi::internal::DriverStationBackend::RefreshData() to kick things off
wpi::internal::DriverStationBackend::RefreshData();
}

View File

@@ -0,0 +1,19 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
namespace wpi {
/**
* The robot alliance that the robot is a part of.
*/
enum class Alliance {
/// Red alliance.
RED,
/// Blue alliance.
BLUE
};
} // namespace wpi

View File

@@ -4,672 +4,59 @@
#pragma once
#include <optional>
#include <string>
#include <string_view>
#include "wpi/hal/DriverStation.hpp"
#include "wpi/hal/DriverStationTypes.hpp"
#include "wpi/math/geometry/Rotation2d.hpp"
#include "wpi/units/time.hpp"
#include "wpi/util/Synchronization.h"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
namespace wpi::log {
class DataLog;
} // namespace wpi::log
namespace wpi::util {
class Color;
} // namespace wpi::util
namespace wpi {
using wpi::hal::RobotMode;
/**
* Provide access to the network communication data to / from the Driver
* Station.
* Provides access to Driver Station functionality.
*/
class DriverStation final {
public:
/**
* The robot alliance that the robot is a part of.
*/
enum class Alliance {
/// Red alliance.
RED,
/// Blue alliance.
BLUE
};
DriverStation() = delete;
/**
* The type of robot match that the robot is part of.
*/
enum class MatchType {
/// None.
NONE,
/// Practice.
PRACTICE,
/// Qualification.
QUALIFICATION,
/// Elimination.
ELIMINATION
};
/**
* A controller POV direction.
*/
enum class POVDirection : uint8_t {
/// POV center.
CENTER = HAL_JOYSTICK_POV_CENTERED,
/// POV up.
UP = HAL_JOYSTICK_POV_UP,
/// POV up right.
UP_RIGHT = HAL_JOYSTICK_POV_RIGHT_UP,
/// POV right.
RIGHT = HAL_JOYSTICK_POV_RIGHT,
/// POV down right.
DOWN_RIGHT = HAL_JOYSTICK_POV_RIGHT_DOWN,
/// POV down.
DOWN = HAL_JOYSTICK_POV_DOWN,
/// POV down left.
DOWN_LEFT = HAL_JOYSTICK_POV_LEFT_DOWN,
/// POV left.
LEFT = HAL_JOYSTICK_POV_LEFT,
/// POV up left.
UP_LEFT = HAL_JOYSTICK_POV_LEFT_UP,
};
struct TouchpadFinger final {
bool down = false;
float x = 0.0f;
float y = 0.0f;
};
/**
* Gets the angle of a POVDirection.
* Starts logging DriverStation data to data log, including joystick data.
* Repeated calls are ignored.
*
* @param angle The POVDirection to convert.
* @return The angle clockwise from straight up, or std::nullopt if the
* POVDirection is CENTER.
* @param log data log
*/
static constexpr std::optional<wpi::math::Rotation2d> GetAngle(
POVDirection angle) {
switch (angle) {
case POVDirection::CENTER:
return std::nullopt;
case POVDirection::UP:
return wpi::math::Rotation2d{0_deg};
case POVDirection::UP_RIGHT:
return wpi::math::Rotation2d{45_deg};
case POVDirection::RIGHT:
return wpi::math::Rotation2d{90_deg};
case POVDirection::DOWN_RIGHT:
return wpi::math::Rotation2d{135_deg};
case POVDirection::DOWN:
return wpi::math::Rotation2d{180_deg};
case POVDirection::DOWN_LEFT:
return wpi::math::Rotation2d{225_deg};
case POVDirection::LEFT:
return wpi::math::Rotation2d{270_deg};
case POVDirection::UP_LEFT:
return wpi::math::Rotation2d{315_deg};
default:
return std::nullopt;
}
static void StartDataLog(wpi::log::DataLog& log) {
wpi::internal::DriverStationBackend::StartDataLog(log);
}
/// Number of Joystick ports.
static constexpr int JOYSTICK_PORTS = 6;
/**
* The state of one joystick button. Button indexes begin at 0.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 0.
* @return The state of the joystick button.
*/
static bool GetStickButton(int stick, int button);
/**
* The state of one joystick button, only if available. Button indexes begin
* at 0.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 0.
* @return The state of the joystick button, or empty if unavailable.
*/
static std::optional<bool> GetStickButtonIfAvailable(int stick, int button);
/**
* Whether one joystick button was pressed since the last check. %Button
* indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 0.
* @return Whether the joystick button was pressed since the last check.
*/
static bool GetStickButtonPressed(int stick, int button);
/**
* Whether one joystick button was released since the last check. %Button
* indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 0.
* @return Whether the joystick button was released since the last check.
*/
static bool GetStickButtonReleased(int stick, int button);
/**
* Get the value of the axis on a joystick.
*
* This depends on the mapping of the joystick connected to the specified
* port.
*
* @param stick The joystick to read.
* @param axis The analog axis value to read from the joystick.
* @return The value of the axis on the joystick.
*/
static double GetStickAxis(int stick, int axis);
/**
* Get the finger data of a touchpad on a joystick, if available.
*
* @param stick The joystick to read.
* @param touchpad The touchpad index to read from the joystick.
* @param finger The finger index to read from the touchpad.
* @return The finger data of the touchpad on the joystick.
*/
static TouchpadFinger GetStickTouchpadFinger(int stick, int touchpad,
int finger);
/**
* Whether a finger on a touchpad is available.
*
* @param stick The joystick to read.
* @param touchpad The touchpad index to read from the joystick.
* @param finger The finger index to read from the touchpad.
* @return True if the finger data is available.
*/
static bool GetStickTouchpadFingerAvailable(int stick, int touchpad,
int finger);
/**
* Get the value of the axis on a joystick, if available.
*
* This depends on the mapping of the joystick connected to the specified
* port.
*
* @param stick The joystick to read.
* @param axis The analog axis value to read from the joystick.
* @return The value of the axis on the joystick, or empty if not available.
*/
static std::optional<double> GetStickAxisIfAvailable(int stick, int axis);
/**
* Get the state of a POV on the joystick.
*
* @return the angle of the POV.
*/
static POVDirection GetStickPOV(int stick, int pov);
/**
* The state of the buttons on the joystick.
*
* @param stick The joystick to read.
* @return The state of the buttons on the joystick.
*/
static uint64_t GetStickButtons(int stick);
/**
* Returns the maximum axis index on a given joystick port.
*
* @param stick The joystick port number
* @return The maximum axis index on the indicated joystick
*/
static int GetStickAxesMaximumIndex(int stick);
/**
* Returns the mask of available axes on a given joystick port.
*
* @param stick The joystick port number
* @return The mask of available axes on the indicated joystick
*/
static int GetStickAxesAvailable(int stick);
/**
* Returns the maximum POV index on a given joystick port.
*
* @param stick The joystick port number
* @return The maximum POV index on the indicated joystick
*/
static int GetStickPOVsMaximumIndex(int stick);
/**
* Returns the mask of available POVs on a given joystick port.
*
* @param stick The joystick port number
* @return The mask of available POVs on the indicated joystick
*/
static int GetStickPOVsAvailable(int stick);
/**
* Returns the maximum button index on a given joystick port.
*
* @param stick The joystick port number
* @return The maximum button index on the indicated joystick
*/
static int GetStickButtonsMaximumIndex(int stick);
/**
* Returns the mask of available buttons on a given joystick port.
*
* @param stick The joystick port number
* @return The mask of available buttons on the indicated joystick
*/
static uint64_t GetStickButtonsAvailable(int stick);
/**
* Returns a boolean indicating if the controller is an xbox controller.
*
* @param stick The joystick port number
* @return A boolean that is true if the controller is an xbox controller.
*/
static bool GetJoystickIsGamepad(int stick);
/**
* Returns the type of joystick at a given port.
*
* This maps to SDL_GamepadType
*
* @param stick The joystick port number
* @return The HID type of joystick at the given port
*/
static int GetJoystickGamepadType(int stick);
/**
* Returns the number of outputs supported by the joystick at the given
* port.
*
* @param stick The joystick port number
* @return The number of outputs supported by the joystick at the given port
*/
static int GetJoystickSupportedOutputs(int stick);
/**
* Returns the name of the joystick at the given port.
*
* @param stick The joystick port number
* @return The name of the joystick at the given port
*/
static std::string GetJoystickName(int stick);
/**
* Returns if a joystick is connected to the Driver Station.
*
* This makes a best effort guess by looking at the reported number of axis,
* buttons, and POVs attached.
*
* @param stick The joystick port number
* @return true if a joystick is connected
*/
static bool IsJoystickConnected(int stick);
/**
* Check if the DS has enabled the robot.
*
* @return True if the robot is enabled and the DS is connected
*/
static bool IsEnabled() {
hal::ControlWord controlWord = GetControlWord();
return controlWord.IsEnabled() && controlWord.IsDSAttached();
}
/**
* Check if the robot is disabled.
*
* @return True if the robot is explicitly disabled or the DS is not connected
*/
static bool IsDisabled() { return !IsEnabled(); }
/**
* Check if the robot is e-stopped.
*
* @return True if the robot is e-stopped
*/
static bool IsEStopped() { return GetControlWord().IsEStopped(); }
/**
* Gets the current robot mode.
*
* <p>Note that this does not indicate whether the robot is enabled or
* disabled.
*
* @return robot mode
*/
static RobotMode GetRobotMode() { return GetControlWord().GetRobotMode(); }
/**
* Check if the DS is commanding autonomous mode.
*
* @return True if the robot is being commanded to be in autonomous mode
*/
static bool IsAutonomous() { return GetControlWord().IsAutonomous(); }
/**
* Check if the DS is commanding autonomous mode and if it has enabled the
* robot.
*
* @return True if the robot is being commanded to be in autonomous mode and
* enabled.
*/
static bool IsAutonomousEnabled() {
return GetControlWord().IsAutonomousEnabled();
}
/**
* Check if the DS is commanding teleop mode.
*
* @return True if the robot is being commanded to be in teleop mode
*/
static bool IsTeleop() { return GetControlWord().IsTeleop(); }
/**
* Check if the DS is commanding teleop mode and if it has enabled the robot.
*
* @return True if the robot is being commanded to be in teleop mode and
* enabled.
*/
static bool IsTeleopEnabled() { return GetControlWord().IsTeleopEnabled(); }
/**
* Check if the DS is commanding test mode.
*
* @return True if the robot is being commanded to be in test mode
*/
static bool IsTest() { return GetControlWord().IsTest(); }
/**
* Check if the DS is commanding Test mode and if it has enabled the robot.
*
* @return True if the robot is being commanded to be in Test mode and
* enabled.
*/
static bool IsTestEnabled() { return GetControlWord().IsTestEnabled(); }
/**
* Adds an operating mode option. It's necessary to call PublishOpModes() to
* make the added modes visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @param description description of the operating mode
* @param textColor text color
* @param backgroundColor background color
* @return unique ID used to later identify the operating mode; if a blank
* name is passed, 0 is returned; identical names for the same robot
* mode result in a 0 return value
*/
static int64_t AddOpMode(RobotMode mode, std::string_view name,
std::string_view group, std::string_view description,
const wpi::util::Color& textColor,
const wpi::util::Color& backgroundColor);
/**
* Adds an operating mode option. It's necessary to call PublishOpModes() to
* make the added modes visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @param description description of the operating mode
* @return unique ID used to later identify the operating mode; if a blank
* name is passed, 0 is returned; identical names for the same robot
* mode result in a 0 return value
*/
static int64_t AddOpMode(RobotMode mode, std::string_view name,
std::string_view group = {},
std::string_view description = {});
/**
* Removes an operating mode option. It's necessary to call PublishOpModes()
* to make the removed mode no longer visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @return unique ID for the opmode, or 0 if not found
*/
static int64_t RemoveOpMode(RobotMode mode, std::string_view name);
/**
* Publishes the operating mode options to the driver station.
*/
static void PublishOpModes();
/**
* Clears all operating mode options and publishes an empty list to the driver
* station.
*/
static void ClearOpModes();
/**
* Sets the program starting flag in the DS. This will also allow
* getOpModeId() and getOpMode() to return values for the selected
* OpMode in the DS application, if the DS is connected by the time this
* method is called.
*
* <p>Most users will not need to use this method; the TimedRobot and
* OpModeRobot robot framework classes will call it automatically after
* the main robot class is instantiated.
*
* <p>This is what changes the DS to showing robot code ready.
*/
static void ObserveUserProgramStarting();
/**
* Gets the operating mode selected on the driver station. Note this does not
* mean the robot is enabled; use IsEnabled() for that. In a match, this will
* indicate the operating mode selected for auto before the match starts
* (i.e., while the robot is disabled in auto mode); after the auto period
* ends, this will change to reflect the operating mode selected for teleop.
*
* @return the unique ID provided by the AddOpMode() function; may return 0 or
* a unique ID not added, so callers should be prepared to handle that case
*/
static int64_t GetOpModeId();
/**
* Gets the operating mode selected on the driver station. Note this does not
* mean the robot is enabled; use IsEnabled() for that. In a match, this will
* indicate the operating mode selected for auto before the match starts
* (i.e., while the robot is disabled in auto mode); after the auto period
* ends, this will change to reflect the operating mode selected for teleop.
*
* @return Operating mode string; may return a string not in the list of
* options, so callers should be prepared to handle that case
*/
static std::string GetOpMode();
/**
* Check to see if the selected operating mode is a particular value. Note
* this does not mean the robot is enabled; use IsEnabled() for that.
*
* @param id operating mode unique ID
* @return True if that mode is the current mode
*/
static bool IsOpMode(int64_t id) { return GetOpModeId() == id; }
/**
* Check to see if the selected operating mode is a particular value. Note
* this does not mean the robot is enabled; use IsEnabled() for that.
*
* @param mode operating mode
* @return True if that mode is the current mode
*/
static bool IsOpMode(std::string_view mode) { return GetOpMode() == mode; }
/**
* Check if the DS is attached.
*
* @return True if the DS is connected to the robot
*/
static bool IsDSAttached() { return GetControlWord().IsDSAttached(); }
/**
* Is the driver station attached to a Field Management System?
*
* @return True if the robot is competing on a field being controlled by a
* Field Management System
*/
static bool IsFMSAttached() { return GetControlWord().IsFMSAttached(); }
/**
* Returns the game specific message provided by the FMS.
*
* If the FMS is not connected, it is set from the game data setting on the
* driver station.
*
* @return A string containing the game specific message.
*/
static std::optional<std::string> GetGameData();
/**
* Returns the name of the competition event provided by the FMS.
*
* @return A string containing the event name
*/
static std::string GetEventName();
/**
* Returns the type of match being played provided by the FMS.
*
* @return The match type enum (NONE, PRACTICE, QUALIFICATION, ELIMINATION)
*/
static MatchType GetMatchType();
/**
* Returns the match number provided by the FMS.
*
* @return The number of the match
*/
static int GetMatchNumber();
/**
* Returns the number of times the current match has been replayed from the
* FMS.
*
* @return The number of replays
*/
static int GetReplayNumber();
/**
* Get the current alliance from the FMS.
*
* If the FMS is not connected, it is set from the team alliance setting on
* the driver station.
*
* @return The alliance (red or blue) or an empty optional if the alliance is
* invalid
*/
static std::optional<Alliance> GetAlliance();
/**
* Return the driver station location from the FMS.
*
* If the FMS is not connected, it is set from the team alliance setting on
* the driver station.
*
* This could return 1, 2, or 3.
*
* @return The location of the driver station (1-3, 0 for invalid)
*/
static std::optional<int> GetLocation();
/**
* Return the approximate match time. The FMS does not send an official match
* time to the robots, but does send an approximate match time. The value will
* count down the time remaining in the current period (auto or teleop).
* Warning: This is not an official time (so it cannot be used to dispute ref
* calls or guarantee that a function will trigger before the match ends).
*
* <p>When connected to the real field, this number only changes in full
* integer increments, and always counts down.
*
* <p>When the DS is in practice mode, this number is a floating point number,
* and counts down.
*
* <p>When the DS is in teleop or autonomous mode, this number returns -1.0.
*
* <p>Simulation matches DS behavior without an FMS connected.
*
* @return Time remaining in current match period (auto or teleop) in seconds
*/
static wpi::units::second_t GetMatchTime();
/**
* Read the battery voltage.
*
* @return The battery voltage in Volts.
*/
static double GetBatteryVoltage();
/**
* Get the current control word.
*
* @return control word
*/
static hal::ControlWord GetControlWord() { return hal::GetControlWord(); }
/**
* Copy data from the DS task for the user. If no new data exists, it will
* just be returned, otherwise the data will be copied from the DS polling
* loop.
*/
static void RefreshData();
/**
* Registers the given handle for DS data refresh notifications.
*
* @param handle The event handle.
*/
static void ProvideRefreshedDataEventHandle(WPI_EventHandle handle);
/**
* Unregisters the given handle from DS data refresh notifications.
*
* @param handle The event handle.
*/
static void RemoveRefreshedDataEventHandle(WPI_EventHandle handle);
/**
* Allows the user to specify whether they want joystick connection warnings
* to be printed to the console. This setting is ignored when the FMS is
* connected -- warnings will always be on in that scenario.
*
* @param silence Whether warning messages should be silenced.
*/
static void SilenceJoystickConnectionWarning(bool silence);
/**
* Returns whether joystick connection warnings are silenced. This will
* always return false when connected to the FMS.
*
* @return Whether joystick connection warnings are silenced.
*/
static bool IsJoystickConnectionWarningSilenced();
/**
* Starts logging DriverStation data to data log. Repeated calls are ignored.
*
* @param log data log
* @param logJoysticks if true, log joystick data
*/
static void StartDataLog(wpi::log::DataLog& log, bool logJoysticks = true);
static void StartDataLog(wpi::log::DataLog& log, bool logJoysticks) {
wpi::internal::DriverStationBackend::StartDataLog(log, logJoysticks);
}
private:
DriverStation() = default;
/**
* Registers the given handle for DS data refresh notifications.
*
* @param handle The event handle.
*/
static void ProvideRefreshedDataEventHandle(WPI_EventHandle handle) {
wpi::internal::DriverStationBackend::ProvideRefreshedDataEventHandle(
handle);
}
/**
* Unregisters the given handle from DS data refresh notifications.
*
* @param handle The event handle.
*/
static void RemoveRefreshedDataEventHandle(WPI_EventHandle handle) {
wpi::internal::DriverStationBackend::RemoveRefreshedDataEventHandle(handle);
}
};
} // namespace wpi

View File

@@ -8,7 +8,9 @@
#include <string>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/POVDirection.hpp"
#include "wpi/driverstation/TouchpadFinger.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
namespace wpi {
@@ -156,7 +158,7 @@ class GenericHID {
* @param pov The index of the POV to read (starting at 0)
* @return the angle of the POV.
*/
DriverStation::POVDirection GetPOV(int pov = 0) const;
POVDirection GetPOV(int pov = 0) const;
/**
* Constructs a BooleanEvent instance based around this angle of a POV on the
@@ -167,7 +169,7 @@ class GenericHID {
* @return a BooleanEvent instance based around this angle of a POV on the
* HID.
*/
BooleanEvent POV(DriverStation::POVDirection angle, EventLoop* loop) const;
BooleanEvent POV(POVDirection angle, EventLoop* loop) const;
/**
* Constructs a BooleanEvent instance based around this angle of a POV on the
@@ -179,8 +181,7 @@ class GenericHID {
* @return a BooleanEvent instance based around this angle of a POV on the
* HID.
*/
BooleanEvent POV(int pov, DriverStation::POVDirection angle,
EventLoop* loop) const;
BooleanEvent POV(int pov, POVDirection angle, EventLoop* loop) const;
/**
* Constructs a BooleanEvent instance based around the up direction of
@@ -385,8 +386,7 @@ class GenericHID {
* @param finger The finger to read.
* @return The touchpad finger data.
*/
DriverStation::TouchpadFinger GetTouchpadFinger(int touchpad,
int finger) const;
TouchpadFinger GetTouchpadFinger(int touchpad, int finger) const;
private:
int m_port;

View File

@@ -0,0 +1,124 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <optional>
#include <string>
#include "wpi/driverstation/Alliance.hpp"
#include "wpi/driverstation/MatchType.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
namespace wpi {
/**
* Provides access to match state information from the Driver Station.
*/
class MatchState final {
public:
MatchState() = delete;
/**
* Return the approximate match time. The FMS does not send an official match
* time to the robots, but does send an approximate match time. The value will
* count down the time remaining in the current period (auto or teleop).
* Warning: This is not an official time (so it cannot be used to dispute ref
* calls or guarantee that a function will trigger before the match ends).
*
* When connected to the real field, this number only changes in full integer
* increments, and always counts down.
*
* When the DS is in practice mode, this number is a floating point number,
* and counts down.
*
* When the DS is in teleop or autonomous mode, this number returns -1.0.
*
* Simulation matches DS behavior without an FMS connected.
*
* @return Time remaining in current match period (auto or teleop) in seconds
*/
static wpi::units::second_t GetMatchTime() {
return wpi::internal::DriverStationBackend::GetMatchTime();
}
/**
* Get the current alliance from the FMS.
*
* If the FMS is not connected, it is set from the team alliance setting on
* the driver station.
*
* @return The alliance (red or blue) or an empty optional if the alliance is
* invalid
*/
static std::optional<Alliance> GetAlliance() {
return wpi::internal::DriverStationBackend::GetAlliance();
}
/**
* Return the driver station location from the FMS.
*
* If the FMS is not connected, it is set from the team alliance setting on
* the driver station.
*
* This could return 1, 2, or 3.
*
* @return The location of the driver station (1-3, 0 for invalid)
*/
static std::optional<int> GetLocation() {
return wpi::internal::DriverStationBackend::GetLocation();
}
/**
* Returns the number of times the current match has been replayed from the
* FMS.
*
* @return The number of replays
*/
static int GetReplayNumber() {
return wpi::internal::DriverStationBackend::GetReplayNumber();
}
/**
* Returns the match number provided by the FMS.
*
* @return The number of the match
*/
static int GetMatchNumber() {
return wpi::internal::DriverStationBackend::GetMatchNumber();
}
/**
* Returns the type of match being played provided by the FMS.
*
* @return The match type enum (kNone, kPractice, kQualification,
* kElimination)
*/
static MatchType GetMatchType() {
return wpi::internal::DriverStationBackend::GetMatchType();
}
/**
* Returns the name of the competition event provided by the FMS.
*
* @return A string containing the event name
*/
static std::string GetEventName() {
return wpi::internal::DriverStationBackend::GetEventName();
}
/**
* Returns the game specific message provided by the FMS.
*
* If the FMS is not connected, it is set from the game data setting on the
* driver station.
*
* @return A string containing the game specific message.
*/
static std::optional<std::string> GetGameData() {
return wpi::internal::DriverStationBackend::GetGameData();
}
};
} // namespace wpi

View File

@@ -0,0 +1,23 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
namespace wpi {
/**
* The type of robot match that the robot is part of.
*/
enum class MatchType {
/// None.
NONE,
/// Practice.
PRACTICE,
/// Qualification.
QUALIFICATION,
/// Elimination.
ELIMINATION
};
} // namespace wpi

View File

@@ -0,0 +1,72 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <cstdint>
#include <optional>
#include "wpi/hal/DriverStationTypes.h"
#include "wpi/math/geometry/Rotation2d.hpp"
namespace wpi {
/**
* A controller POV direction.
*/
enum class POVDirection : uint8_t {
/// POV center.
CENTER = HAL_JOYSTICK_POV_CENTERED,
/// POV up.
UP = HAL_JOYSTICK_POV_UP,
/// POV up right.
UP_RIGHT = HAL_JOYSTICK_POV_RIGHT_UP,
/// POV right.
RIGHT = HAL_JOYSTICK_POV_RIGHT,
/// POV down right.
DOWN_RIGHT = HAL_JOYSTICK_POV_RIGHT_DOWN,
/// POV down.
DOWN = HAL_JOYSTICK_POV_DOWN,
/// POV down left.
DOWN_LEFT = HAL_JOYSTICK_POV_LEFT_DOWN,
/// POV left.
LEFT = HAL_JOYSTICK_POV_LEFT,
/// POV up left.
UP_LEFT = HAL_JOYSTICK_POV_LEFT_UP,
};
/**
* Gets the angle of a POVDirection.
*
* @param angle The POVDirection to convert.
* @return The angle clockwise from straight up, or std::nullopt if the
* POVDirection is CENTER.
*/
constexpr std::optional<wpi::math::Rotation2d> GetPOVAngle(POVDirection angle) {
using enum POVDirection;
switch (angle) {
case CENTER:
return std::nullopt;
case UP:
return wpi::math::Rotation2d{0_deg};
case UP_RIGHT:
return wpi::math::Rotation2d{45_deg};
case RIGHT:
return wpi::math::Rotation2d{90_deg};
case DOWN_RIGHT:
return wpi::math::Rotation2d{135_deg};
case DOWN:
return wpi::math::Rotation2d{180_deg};
case DOWN_LEFT:
return wpi::math::Rotation2d{225_deg};
case LEFT:
return wpi::math::Rotation2d{270_deg};
case UP_LEFT:
return wpi::math::Rotation2d{315_deg};
default:
return std::nullopt;
}
}
} // namespace wpi

View File

@@ -0,0 +1,256 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <cstdint>
#include <string>
#include <string_view>
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
namespace wpi {
using wpi::hal::RobotMode;
/**
* Provides access to robot state information from the Driver Station.
*/
class RobotState final {
public:
RobotState() = delete;
/**
* Check if the DS has enabled the robot.
*
* @return True if the robot is enabled and the DS is connected
*/
static bool IsEnabled() {
return wpi::internal::DriverStationBackend::IsEnabled();
}
/**
* Check if the robot is disabled.
*
* @return True if the robot is explicitly disabled or the DS is not connected
*/
static bool IsDisabled() {
return wpi::internal::DriverStationBackend::IsDisabled();
}
/**
* Check if the robot is e-stopped.
*
* @return True if the robot is e-stopped
*/
static bool IsEStopped() {
return wpi::internal::DriverStationBackend::IsEStopped();
}
/**
* Gets the current robot mode.
*
* Note that this does not indicate whether the robot is enabled or disabled.
*
* @return robot mode
*/
static RobotMode GetRobotMode() {
return wpi::internal::DriverStationBackend::GetRobotMode();
}
/**
* Check if the DS is commanding autonomous mode.
*
* @return True if the robot is being commanded to be in autonomous mode
*/
static bool IsAutonomous() {
return wpi::internal::DriverStationBackend::IsAutonomous();
}
/**
* Check if the DS is commanding autonomous mode and if it has enabled the
* robot.
*
* @return True if the robot is being commanded to be in autonomous mode and
* enabled.
*/
static bool IsAutonomousEnabled() {
return wpi::internal::DriverStationBackend::IsAutonomousEnabled();
}
/**
* Check if the DS is commanding teleop mode.
*
* @return True if the robot is being commanded to be in teleop mode
*/
static bool IsTeleop() {
return wpi::internal::DriverStationBackend::IsTeleop();
}
/**
* Check if the DS is commanding teleop mode and if it has enabled the robot.
*
* @return True if the robot is being commanded to be in teleop mode and
* enabled.
*/
static bool IsTeleopEnabled() {
return wpi::internal::DriverStationBackend::IsTeleopEnabled();
}
/**
* Check if the DS is commanding test mode.
*
* @return True if the robot is being commanded to be in test mode
*/
static bool IsTest() { return wpi::internal::DriverStationBackend::IsTest(); }
/**
* Check if the DS is commanding Test mode and if it has enabled the robot.
*
* @return True if the robot is being commanded to be in Test mode and
* enabled.
*/
static bool IsTestEnabled() {
return wpi::internal::DriverStationBackend::IsTestEnabled();
}
/**
* Adds an operating mode option. It's necessary to call PublishOpModes() to
* make the added modes visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @param description description of the operating mode
* @param textColor text color
* @param backgroundColor background color
* @return unique ID used to later identify the operating mode; if a blank
* name is passed, 0 is returned; identical names for the same robot
* mode result in a 0 return value
*/
static int64_t AddOpMode(RobotMode mode, std::string_view name,
std::string_view group, std::string_view description,
const wpi::util::Color& textColor,
const wpi::util::Color& backgroundColor) {
return wpi::internal::DriverStationBackend::AddOpMode(
mode, name, group, description, textColor, backgroundColor);
}
/**
* Adds an operating mode option. It's necessary to call PublishOpModes() to
* make the added modes visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @param description description of the operating mode
* @return unique ID used to later identify the operating mode; if a blank
* name is passed, 0 is returned; identical names for the same robot
* mode result in a 0 return value
*/
static int64_t AddOpMode(RobotMode mode, std::string_view name,
std::string_view group = {},
std::string_view description = {}) {
return wpi::internal::DriverStationBackend::AddOpMode(mode, name, group,
description);
}
/**
* Removes an operating mode option. It's necessary to call PublishOpModes()
* to make the removed mode no longer visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @return unique ID for the opmode, or 0 if not found
*/
static int64_t RemoveOpMode(RobotMode mode, std::string_view name) {
return wpi::internal::DriverStationBackend::RemoveOpMode(mode, name);
}
/**
* Publishes the operating mode options to the driver station.
*/
static void PublishOpModes() {
wpi::internal::DriverStationBackend::PublishOpModes();
}
/**
* Clears all operating mode options and publishes an empty list to the driver
* station.
*/
static void ClearOpModes() {
wpi::internal::DriverStationBackend::ClearOpModes();
}
/**
* Gets the operating mode selected on the driver station. Note this does not
* mean the robot is enabled; use IsEnabled() for that. In a match, this will
* indicate the operating mode selected for auto before the match starts
* (i.e., while the robot is disabled in auto mode); after the auto period
* ends, this will change to reflect the operating mode selected for teleop.
*
* @return the unique ID provided by the AddOpMode() function; may return 0 or
* a unique ID not added, so callers should be prepared to handle that case
*/
static int64_t GetOpModeId() {
return wpi::internal::DriverStationBackend::GetOpModeId();
}
/**
* Gets the operating mode selected on the driver station. Note this does not
* mean the robot is enabled; use IsEnabled() for that. In a match, this will
* indicate the operating mode selected for auto before the match starts
* (i.e., while the robot is disabled in auto mode); after the auto period
* ends, this will change to reflect the operating mode selected for teleop.
*
* @return Operating mode string; may return a string not in the list of
* options, so callers should be prepared to handle that case
*/
static std::string GetOpMode() {
return wpi::internal::DriverStationBackend::GetOpMode();
}
/**
* Check to see if the selected operating mode is a particular value. Note
* this does not mean the robot is enabled; use IsEnabled() for that.
*
* @param id operating mode unique ID
* @return True if that mode is the current mode
*/
static bool IsOpMode(int64_t id) {
return wpi::internal::DriverStationBackend::IsOpMode(id);
}
/**
* Check to see if the selected operating mode is a particular value. Note
* this does not mean the robot is enabled; use IsEnabled() for that.
*
* @param mode operating mode
* @return True if that mode is the current mode
*/
static bool IsOpMode(std::string_view mode) {
return wpi::internal::DriverStationBackend::IsOpMode(mode);
}
/**
* Check if the DS is attached.
*
* @return True if the DS is connected to the robot
*/
static bool IsDSAttached() {
return wpi::internal::DriverStationBackend::IsDSAttached();
}
/**
* Is the driver station attached to a Field Management System?
*
* @return True if the robot is competing on a field being controlled by a
* Field Management System
*/
static bool IsFMSAttached() {
return wpi::internal::DriverStationBackend::IsFMSAttached();
}
};
} // namespace wpi

View File

@@ -0,0 +1,21 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
namespace wpi {
/**
* Touchpad finger data from a joystick.
*/
struct TouchpadFinger final {
/// Whether the finger is touching the touchpad.
bool down = false;
/// The x position of the finger on the touchpad.
float x = 0.0f;
/// The y position of the finger on the touchpad.
float y = 0.0f;
};
} // namespace wpi

View File

@@ -0,0 +1,595 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <optional>
#include <string>
#include <string_view>
#include "wpi/driverstation/Alliance.hpp"
#include "wpi/driverstation/MatchType.hpp"
#include "wpi/driverstation/POVDirection.hpp"
#include "wpi/driverstation/TouchpadFinger.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/hal/DriverStation.hpp"
#include "wpi/hal/DriverStationTypes.h"
#include "wpi/hal/DriverStationTypes.hpp"
#include "wpi/math/geometry/Rotation2d.hpp"
#include "wpi/units/time.hpp"
#include "wpi/util/Synchronization.h"
namespace wpi::log {
class DataLog;
} // namespace wpi::log
namespace wpi::util {
class Color;
} // namespace wpi::util
namespace wpi::internal {
using wpi::hal::RobotMode;
/**
* Provide access to the network communication data to / from the Driver
* Station.
*/
class DriverStationBackend final {
public:
/// Number of Joystick ports.
static constexpr int JOYSTICK_PORTS = 6;
/**
* The state of one joystick button. Button indexes begin at 0.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 0.
* @return The state of the joystick button.
*/
static bool GetStickButton(int stick, int button);
/**
* The state of one joystick button, only if available. Button indexes begin
* at 0.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 0.
* @return The state of the joystick button, or empty if unavailable.
*/
static std::optional<bool> GetStickButtonIfAvailable(int stick, int button);
/**
* Whether one joystick button was pressed since the last check. %Button
* indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 0.
* @return Whether the joystick button was pressed since the last check.
*/
static bool GetStickButtonPressed(int stick, int button);
/**
* Whether one joystick button was released since the last check. %Button
* indexes begin at 1.
*
* @param stick The joystick to read.
* @param button The button index, beginning at 0.
* @return Whether the joystick button was released since the last check.
*/
static bool GetStickButtonReleased(int stick, int button);
/**
* Get the value of the axis on a joystick.
*
* This depends on the mapping of the joystick connected to the specified
* port.
*
* @param stick The joystick to read.
* @param axis The analog axis value to read from the joystick.
* @return The value of the axis on the joystick.
*/
static double GetStickAxis(int stick, int axis);
/**
* Get the finger data of a touchpad on a joystick, if available.
*
* @param stick The joystick to read.
* @param touchpad The touchpad index to read from the joystick.
* @param finger The finger index to read from the touchpad.
* @return The finger data of the touchpad on the joystick.
*/
static TouchpadFinger GetStickTouchpadFinger(int stick, int touchpad,
int finger);
/**
* Whether a finger on a touchpad is available.
*
* @param stick The joystick to read.
* @param touchpad The touchpad index to read from the joystick.
* @param finger The finger index to read from the touchpad.
* @return True if the finger data is available.
*/
static bool GetStickTouchpadFingerAvailable(int stick, int touchpad,
int finger);
/**
* Get the value of the axis on a joystick, if available.
*
* This depends on the mapping of the joystick connected to the specified
* port.
*
* @param stick The joystick to read.
* @param axis The analog axis value to read from the joystick.
* @return The value of the axis on the joystick, or empty if not available.
*/
static std::optional<double> GetStickAxisIfAvailable(int stick, int axis);
/**
* Get the state of a POV on the joystick.
*
* @return the angle of the POV.
*/
static POVDirection GetStickPOV(int stick, int pov);
/**
* The state of the buttons on the joystick.
*
* @param stick The joystick to read.
* @return The state of the buttons on the joystick.
*/
static uint64_t GetStickButtons(int stick);
/**
* Returns the maximum axis index on a given joystick port.
*
* @param stick The joystick port number
* @return The maximum axis index on the indicated joystick
*/
static int GetStickAxesMaximumIndex(int stick);
/**
* Returns the mask of available axes on a given joystick port.
*
* @param stick The joystick port number
* @return The mask of available axes on the indicated joystick
*/
static int GetStickAxesAvailable(int stick);
/**
* Returns the maximum POV index on a given joystick port.
*
* @param stick The joystick port number
* @return The maximum POV index on the indicated joystick
*/
static int GetStickPOVsMaximumIndex(int stick);
/**
* Returns the mask of available POVs on a given joystick port.
*
* @param stick The joystick port number
* @return The mask of available POVs on the indicated joystick
*/
static int GetStickPOVsAvailable(int stick);
/**
* Returns the maximum button index on a given joystick port.
*
* @param stick The joystick port number
* @return The maximum button index on the indicated joystick
*/
static int GetStickButtonsMaximumIndex(int stick);
/**
* Returns the mask of available buttons on a given joystick port.
*
* @param stick The joystick port number
* @return The mask of available buttons on the indicated joystick
*/
static uint64_t GetStickButtonsAvailable(int stick);
/**
* Returns a boolean indicating if the controller is an xbox controller.
*
* @param stick The joystick port number
* @return A boolean that is true if the controller is an xbox controller.
*/
static bool GetJoystickIsGamepad(int stick);
/**
* Returns the type of joystick at a given port.
*
* This maps to SDL_GamepadType
*
* @param stick The joystick port number
* @return The HID type of joystick at the given port
*/
static int GetJoystickGamepadType(int stick);
/**
* Returns the number of outputs supported by the joystick at the given
* port.
*
* @param stick The joystick port number
* @return The number of outputs supported by the joystick at the given port
*/
static int GetJoystickSupportedOutputs(int stick);
/**
* Returns the name of the joystick at the given port.
*
* @param stick The joystick port number
* @return The name of the joystick at the given port
*/
static std::string GetJoystickName(int stick);
/**
* Returns if a joystick is connected to the Driver Station.
*
* This makes a best effort guess by looking at the reported number of axis,
* buttons, and POVs attached.
*
* @param stick The joystick port number
* @return true if a joystick is connected
*/
static bool IsJoystickConnected(int stick);
/**
* Check if the DS has enabled the robot.
*
* @return True if the robot is enabled and the DS is connected
*/
static bool IsEnabled() {
hal::ControlWord controlWord = GetControlWord();
return controlWord.IsEnabled() && controlWord.IsDSAttached();
}
/**
* Check if the robot is disabled.
*
* @return True if the robot is explicitly disabled or the DS is not connected
*/
static bool IsDisabled() { return !IsEnabled(); }
/**
* Check if the robot is e-stopped.
*
* @return True if the robot is e-stopped
*/
static bool IsEStopped() { return GetControlWord().IsEStopped(); }
/**
* Gets the current robot mode.
*
* <p>Note that this does not indicate whether the robot is enabled or
* disabled.
*
* @return robot mode
*/
static RobotMode GetRobotMode() { return GetControlWord().GetRobotMode(); }
/**
* Check if the DS is commanding autonomous mode.
*
* @return True if the robot is being commanded to be in autonomous mode
*/
static bool IsAutonomous() { return GetControlWord().IsAutonomous(); }
/**
* Check if the DS is commanding autonomous mode and if it has enabled the
* robot.
*
* @return True if the robot is being commanded to be in autonomous mode and
* enabled.
*/
static bool IsAutonomousEnabled() {
return GetControlWord().IsAutonomousEnabled();
}
/**
* Check if the DS is commanding teleop mode.
*
* @return True if the robot is being commanded to be in teleop mode
*/
static bool IsTeleop() { return GetControlWord().IsTeleop(); }
/**
* Check if the DS is commanding teleop mode and if it has enabled the robot.
*
* @return True if the robot is being commanded to be in teleop mode and
* enabled.
*/
static bool IsTeleopEnabled() { return GetControlWord().IsTeleopEnabled(); }
/**
* Check if the DS is commanding test mode.
*
* @return True if the robot is being commanded to be in test mode
*/
static bool IsTest() { return GetControlWord().IsTest(); }
/**
* Check if the DS is commanding Test mode and if it has enabled the robot.
*
* @return True if the robot is being commanded to be in Test mode and
* enabled.
*/
static bool IsTestEnabled() { return GetControlWord().IsTestEnabled(); }
/**
* Adds an operating mode option. It's necessary to call PublishOpModes() to
* make the added modes visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @param description description of the operating mode
* @param textColor text color
* @param backgroundColor background color
* @return unique ID used to later identify the operating mode; if a blank
* name is passed, 0 is returned; identical names for the same robot
* mode result in a 0 return value
*/
static int64_t AddOpMode(RobotMode mode, std::string_view name,
std::string_view group, std::string_view description,
const wpi::util::Color& textColor,
const wpi::util::Color& backgroundColor);
/**
* Adds an operating mode option. It's necessary to call PublishOpModes() to
* make the added modes visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @param group group of the operating mode
* @param description description of the operating mode
* @return unique ID used to later identify the operating mode; if a blank
* name is passed, 0 is returned; identical names for the same robot
* mode result in a 0 return value
*/
static int64_t AddOpMode(RobotMode mode, std::string_view name,
std::string_view group = {},
std::string_view description = {});
/**
* Removes an operating mode option. It's necessary to call PublishOpModes()
* to make the removed mode no longer visible to the driver station.
*
* @param mode robot mode
* @param name name of the operating mode
* @return unique ID for the opmode, or 0 if not found
*/
static int64_t RemoveOpMode(RobotMode mode, std::string_view name);
/**
* Publishes the operating mode options to the driver station.
*/
static void PublishOpModes();
/**
* Clears all operating mode options and publishes an empty list to the driver
* station.
*/
static void ClearOpModes();
/**
* Sets the program starting flag in the DS. This will also allow
* getOpModeId() and getOpMode() to return values for the selected
* OpMode in the DS application, if the DS is connected by the time this
* method is called.
*
* <p>Most users will not need to use this method; the TimedRobot and
* OpModeRobot robot framework classes will call it automatically after
* the main robot class is instantiated.
*
* <p>This is what changes the DS to showing robot code ready.
*/
static void ObserveUserProgramStarting();
/**
* Gets the operating mode selected on the driver station. Note this does not
* mean the robot is enabled; use IsEnabled() for that. In a match, this will
* indicate the operating mode selected for auto before the match starts
* (i.e., while the robot is disabled in auto mode); after the auto period
* ends, this will change to reflect the operating mode selected for teleop.
*
* @return the unique ID provided by the AddOpMode() function; may return 0 or
* a unique ID not added, so callers should be prepared to handle that case
*/
static int64_t GetOpModeId();
/**
* Gets the operating mode selected on the driver station. Note this does not
* mean the robot is enabled; use IsEnabled() for that. In a match, this will
* indicate the operating mode selected for auto before the match starts
* (i.e., while the robot is disabled in auto mode); after the auto period
* ends, this will change to reflect the operating mode selected for teleop.
*
* @return Operating mode string; may return a string not in the list of
* options, so callers should be prepared to handle that case
*/
static std::string GetOpMode();
/**
* Check to see if the selected operating mode is a particular value. Note
* this does not mean the robot is enabled; use IsEnabled() for that.
*
* @param id operating mode unique ID
* @return True if that mode is the current mode
*/
static bool IsOpMode(int64_t id) { return GetOpModeId() == id; }
/**
* Check to see if the selected operating mode is a particular value. Note
* this does not mean the robot is enabled; use IsEnabled() for that.
*
* @param mode operating mode
* @return True if that mode is the current mode
*/
static bool IsOpMode(std::string_view mode) { return GetOpMode() == mode; }
/**
* Check if the DS is attached.
*
* @return True if the DS is connected to the robot
*/
static bool IsDSAttached() { return GetControlWord().IsDSAttached(); }
/**
* Is the driver station attached to a Field Management System?
*
* @return True if the robot is competing on a field being controlled by a
* Field Management System
*/
static bool IsFMSAttached() { return GetControlWord().IsFMSAttached(); }
/**
* Returns the game specific message provided by the FMS.
*
* If the FMS is not connected, it is set from the game data setting on the
* driver station.
*
* @return A string containing the game specific message.
*/
static std::optional<std::string> GetGameData();
/**
* Returns the name of the competition event provided by the FMS.
*
* @return A string containing the event name
*/
static std::string GetEventName();
/**
* Returns the type of match being played provided by the FMS.
*
* @return The match type enum (kNone, kPractice, kQualification,
* kElimination)
*/
static MatchType GetMatchType();
/**
* Returns the match number provided by the FMS.
*
* @return The number of the match
*/
static int GetMatchNumber();
/**
* Returns the number of times the current match has been replayed from the
* FMS.
*
* @return The number of replays
*/
static int GetReplayNumber();
/**
* Get the current alliance from the FMS.
*
* If the FMS is not connected, it is set from the team alliance setting on
* the driver station.
*
* @return The alliance (red or blue) or an empty optional if the alliance is
* invalid
*/
static std::optional<Alliance> GetAlliance();
/**
* Return the driver station location from the FMS.
*
* If the FMS is not connected, it is set from the team alliance setting on
* the driver station.
*
* This could return 1, 2, or 3.
*
* @return The location of the driver station (1-3, 0 for invalid)
*/
static std::optional<int> GetLocation();
/**
* Return the approximate match time. The FMS does not send an official match
* time to the robots, but does send an approximate match time. The value will
* count down the time remaining in the current period (auto or teleop).
* Warning: This is not an official time (so it cannot be used to dispute ref
* calls or guarantee that a function will trigger before the match ends).
*
* <p>When connected to the real field, this number only changes in full
* integer increments, and always counts down.
*
* <p>When the DS is in practice mode, this number is a floating point number,
* and counts down.
*
* <p>When the DS is in teleop or autonomous mode, this number returns -1.0.
*
* <p>Simulation matches DS behavior without an FMS connected.
*
* @return Time remaining in current match period (auto or teleop) in seconds
*/
static wpi::units::second_t GetMatchTime();
/**
* Read the battery voltage.
*
* @return The battery voltage in Volts.
*/
static double GetBatteryVoltage();
/**
* Get the current control word.
*
* @return control word
*/
static hal::ControlWord GetControlWord() { return hal::GetControlWord(); }
/**
* Copy data from the DS task for the user. If no new data exists, it will
* just be returned, otherwise the data will be copied from the DS polling
* loop.
*/
static void RefreshData();
/**
* Registers the given handle for DS data refresh notifications.
*
* @param handle The event handle.
*/
static void ProvideRefreshedDataEventHandle(WPI_EventHandle handle);
/**
* Unregisters the given handle from DS data refresh notifications.
*
* @param handle The event handle.
*/
static void RemoveRefreshedDataEventHandle(WPI_EventHandle handle);
/**
* Allows the user to specify whether they want joystick connection warnings
* to be printed to the console. This setting is ignored when the FMS is
* connected -- warnings will always be on in that scenario.
*
* @param silence Whether warning messages should be silenced.
*/
static void SilenceJoystickConnectionWarning(bool silence);
/**
* Returns whether joystick connection warnings are silenced. This will
* always return false when connected to the FMS.
*
* @return Whether joystick connection warnings are silenced.
*/
static bool IsJoystickConnectionWarningSilenced();
/**
* Starts logging DriverStation data to data log. Repeated calls are ignored.
*
* @param log data log
* @param logJoysticks if true, log joystick data
*/
static void StartDataLog(wpi::log::DataLog& log, bool logJoysticks = true);
private:
DriverStationBackend() = default;
};
} // namespace wpi::internal

View File

@@ -210,7 +210,7 @@ class RobotBase {
* Gets the currently selected operating mode of the driver station. Note this
* does not mean the robot is enabled; use IsEnabled() for that.
*
* @return the unique ID provided by the DriverStation::AddOpMode() function;
* @return the unique ID provided by the RobotState::AddOpMode() function;
* may return 0 or a unique ID not added, so callers should be prepared to
* handle that case
*/

View File

@@ -8,7 +8,7 @@
#include <memory>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
#include "wpi/hal/DriverStationTypes.h"
#include "wpi/hal/simulation/DriverStationData.h"
#include "wpi/simulation/CallbackStore.hpp"
@@ -281,14 +281,15 @@ class DriverStationSim {
static void NotifyNewData();
/**
* Sets suppression of DriverStation::ReportError and ReportWarning messages.
* Sets suppression of DriverStationErrors::ReportError and ReportWarning
* messages.
*
* @param shouldSend If false then messages will be suppressed.
*/
static void SetSendError(bool shouldSend);
/**
* Sets suppression of DriverStation::SendConsoleLine messages.
* Sets suppression of DriverStationErrors::SendConsoleLine messages.
*
* @param shouldSend If false then messages will be suppressed.
*/
@@ -337,8 +338,7 @@ class DriverStationSim {
* @param pov The POV number
* @param value the angle of the POV
*/
static void SetJoystickPOV(int stick, int pov,
DriverStation::POVDirection value);
static void SetJoystickPOV(int stick, int pov, POVDirection value);
/**
* Sets the number of axes for a joystick.
@@ -433,7 +433,7 @@ class DriverStationSim {
*
* @param type the match type
*/
static void SetMatchType(DriverStation::MatchType type);
static void SetMatchType(MatchType type);
/**
* Sets the match number.

View File

@@ -6,8 +6,8 @@
#include <stdint.h>
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/driverstation/GenericHID.hpp"
#include "wpi/driverstation/internal/DriverStationBackend.hpp"
namespace wpi {
@@ -61,14 +61,14 @@ class GenericHIDSim {
* @param pov the POV to set
* @param value the new value
*/
void SetPOV(int pov, DriverStation::POVDirection value);
void SetPOV(int pov, POVDirection value);
/**
* Set the value of the default POV (port 0).
*
* @param value the new value
*/
void SetPOV(DriverStation::POVDirection value);
void SetPOV(POVDirection value);
void SetAxesMaximumIndex(int maximumIndex);