[hal] Update DS API to new format (#7977)

This commit is contained in:
Thad House
2025-05-16 22:15:14 -07:00
committed by GitHub
parent 1596e2fd7a
commit 231ec348fe
35 changed files with 878 additions and 766 deletions

View File

@@ -136,11 +136,11 @@ Java_edu_wpi_first_hal_DriverStationJNI_nativeGetAllianceStation
/*
* Class: edu_wpi_first_hal_DriverStationJNI
* Method: getJoystickAxesRaw
* Signature: (B[I)I
* Signature: (B[S)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickAxesRaw
(JNIEnv* env, jclass, jbyte joystickNum, jintArray axesRawArray)
(JNIEnv* env, jclass, jbyte joystickNum, jshortArray axesRawArray)
{
HAL_JoystickAxes axes;
HAL_GetJoystickAxes(joystickNum, &axes);
@@ -155,11 +155,7 @@ Java_edu_wpi_first_hal_DriverStationJNI_getJoystickAxesRaw
return 0;
}
jint raw[HAL_kMaxJoystickAxes];
for (int16_t i = 0; i < axes.count; i++) {
raw[i] = axes.raw[i];
}
env->SetIntArrayRegion(axesRawArray, 0, axes.count, raw);
env->SetShortArrayRegion(axesRawArray, 0, axes.count, axes.raw);
return axes.count;
}
@@ -194,11 +190,11 @@ Java_edu_wpi_first_hal_DriverStationJNI_getJoystickAxes
/*
* Class: edu_wpi_first_hal_DriverStationJNI
* Method: getJoystickPOVs
* Signature: (B[S)I
* Signature: (B[B)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickPOVs
(JNIEnv* env, jclass, jbyte joystickNum, jshortArray povsArray)
(JNIEnv* env, jclass, jbyte joystickNum, jbyteArray povsArray)
{
HAL_JoystickPOVs povs;
HAL_GetJoystickPOVs(joystickNum, &povs);
@@ -213,7 +209,8 @@ Java_edu_wpi_first_hal_DriverStationJNI_getJoystickPOVs
return 0;
}
env->SetShortArrayRegion(povsArray, 0, povs.count, povs.povs);
env->SetByteArrayRegion(povsArray, 0, povs.count,
reinterpret_cast<const jbyte*>(povs.povs));
return povs.count;
}
@@ -221,12 +218,12 @@ Java_edu_wpi_first_hal_DriverStationJNI_getJoystickPOVs
/*
* Class: edu_wpi_first_hal_DriverStationJNI
* Method: getAllJoystickData
* Signature: ([F[B[S[J)V
* Signature: ([F[S[B[J)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_DriverStationJNI_getAllJoystickData
(JNIEnv* env, jclass cls, jfloatArray axesArray, jbyteArray rawAxesArray,
jshortArray povsArray, jlongArray buttonsAndMetadataArray)
(JNIEnv* env, jclass cls, jfloatArray axesArray, jshortArray rawAxesArray,
jbyteArray povsArray, jlongArray buttonsAndMetadataArray)
{
HAL_JoystickAxes axes[HAL_kMaxJoysticks];
HAL_JoystickPOVs povs[HAL_kMaxJoysticks];
@@ -235,8 +232,8 @@ Java_edu_wpi_first_hal_DriverStationJNI_getAllJoystickData
HAL_GetAllJoystickData(axes, povs, buttons);
CriticalJSpan<jfloat> jAxes(env, axesArray);
CriticalJSpan<jbyte> jRawAxes(env, rawAxesArray);
CriticalJSpan<jshort> jPovs(env, povsArray);
CriticalJSpan<jshort> jRawAxes(env, rawAxesArray);
CriticalJSpan<jbyte> jPovs(env, povsArray);
CriticalJSpan<jlong> jButtons(env, buttonsAndMetadataArray);
static_assert(sizeof(jAxes[0]) == sizeof(axes[0].axes[0]));
@@ -288,14 +285,14 @@ Java_edu_wpi_first_hal_DriverStationJNI_setJoystickOutputs
/*
* Class: edu_wpi_first_hal_DriverStationJNI
* Method: getJoystickIsXbox
* Method: getJoystickIsGamepad
* Signature: (B)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickIsXbox
Java_edu_wpi_first_hal_DriverStationJNI_getJoystickIsGamepad
(JNIEnv*, jclass, jbyte port)
{
return HAL_GetJoystickIsXbox(port);
return HAL_GetJoystickIsGamepad(port);
}
/*

View File

@@ -452,15 +452,15 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickAxes
/*
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
* Method: setJoystickPOVs
* Signature: (B[S)V
* Signature: (B[B)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickPOVs
(JNIEnv* env, jclass, jbyte joystickNum, jshortArray povsArray)
(JNIEnv* env, jclass, jbyte joystickNum, jbyteArray povsArray)
{
HAL_JoystickPOVs povs;
{
JSpan<const jshort> jArrayRef(env, povsArray);
JSpan<const jbyte> jArrayRef(env, povsArray);
auto arrayRef = jArrayRef.array();
auto arraySize = arrayRef.size();
int maxCount =
@@ -701,14 +701,14 @@ Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickButtonCount
/*
* Class: edu_wpi_first_hal_simulation_DriverStationDataJNI
* Method: setJoystickIsXbox
* Method: setJoystickIsGamepad
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickIsXbox
(JNIEnv*, jclass, jint stick, jboolean isXbox)
Java_edu_wpi_first_hal_simulation_DriverStationDataJNI_setJoystickIsGamepad
(JNIEnv*, jclass, jint stick, jboolean isGamepad)
{
HALSIM_SetJoystickIsXbox(stick, isXbox);
HALSIM_SetJoystickIsGamepad(stick, isGamepad);
}
/*

View File

@@ -2,9 +2,6 @@
// 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 <string>
#include <utility>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "hal/proto/ControlData.h"
@@ -13,6 +10,8 @@ static_assert(sizeof(mrc::ControlFlags) == sizeof(uint32_t));
namespace {
constexpr uint32_t EnabledMask = 0x1;
constexpr uint32_t AutoMask = 0x2;
constexpr uint32_t TestMask = 0x4;
constexpr uint32_t EStopMask = 0x8;
constexpr uint32_t FmsConnectedMask = 0x10;
constexpr uint32_t DsConnectedMask = 0x20;
@@ -20,6 +19,8 @@ constexpr uint32_t WatchdogActiveMask = 0x40;
constexpr uint32_t AllianceMask = 0x1F80;
constexpr uint32_t EnabledShift = 0;
constexpr uint32_t AutoShift = 1;
constexpr uint32_t TestShift = 2;
constexpr uint32_t EStopShift = 3;
constexpr uint32_t FmsConnectedShift = 4;
constexpr uint32_t DsConnectedShift = 5;
@@ -31,6 +32,8 @@ constexpr uint32_t AllianceShift = 7;
constexpr uint32_t FromControlWord(mrc::ControlFlags Word) {
uint32_t Ret = 0;
WORD_TO_INT(Enabled);
WORD_TO_INT(Auto);
WORD_TO_INT(Test);
WORD_TO_INT(EStop);
WORD_TO_INT(FmsConnected);
WORD_TO_INT(DsConnected);
@@ -46,6 +49,8 @@ constexpr uint32_t FromControlWord(mrc::ControlFlags Word) {
constexpr mrc::ControlFlags ToControlWord(uint32_t Word) {
mrc::ControlFlags Ret = {};
INT_TO_WORD(Enabled);
INT_TO_WORD(Auto);
INT_TO_WORD(Test);
INT_TO_WORD(EStop);
INT_TO_WORD(FmsConnected);
INT_TO_WORD(DsConnected);
@@ -59,13 +64,12 @@ constexpr mrc::ControlFlags ToControlWord(uint32_t Word) {
std::optional<mrc::ControlData> wpi::Protobuf<mrc::ControlData>::Unpack(
InputStream& Stream) {
wpi::UnpackCallback<mrc::Joystick, MRC_MAX_NUM_JOYSTICKS> JoystickCb;
wpi::UnpackCallback<std::string> OpModeCb;
mrc_proto_ProtobufControlData Msg{
.ControlWord = 0,
.MatchTime = 0,
.Joysticks = JoystickCb.Callback(),
.OpMode = OpModeCb.Callback(),
.CurrentOpMode = 0,
};
if (!Stream.Decode(Msg)) {
@@ -73,16 +77,12 @@ std::optional<mrc::ControlData> wpi::Protobuf<mrc::ControlData>::Unpack(
}
auto Joysticks = JoystickCb.Items();
auto OpMode = OpModeCb.Items();
mrc::ControlData ControlData;
if (!OpMode.empty()) {
ControlData.MoveOpMode(std::move(OpMode[0]));
}
ControlData.ControlWord = ToControlWord(Msg.ControlWord);
ControlData.MatchTime = Msg.MatchTime;
ControlData.CurrentOpMode = Msg.CurrentOpMode;
ControlData.SetJoystickCount(Joysticks.size());
for (size_t i = 0; i < ControlData.GetJoystickCount(); i++) {
@@ -94,8 +94,6 @@ std::optional<mrc::ControlData> wpi::Protobuf<mrc::ControlData>::Unpack(
bool wpi::Protobuf<mrc::ControlData>::Pack(OutputStream& Stream,
const mrc::ControlData& Value) {
std::string_view OpMode = Value.GetOpMode();
wpi::PackCallback OpModeCb{&OpMode};
std::span<const mrc::Joystick> Sticks = Value.Joysticks();
wpi::PackCallback Joysticks{Sticks};
@@ -103,7 +101,7 @@ bool wpi::Protobuf<mrc::ControlData>::Pack(OutputStream& Stream,
.ControlWord = FromControlWord(Value.ControlWord),
.MatchTime = Value.MatchTime,
.Joysticks = Joysticks.Callback(),
.OpMode = OpModeCb.Callback(),
.CurrentOpMode = Value.CurrentOpMode,
};
return Stream.Encode(Msg);
@@ -111,14 +109,14 @@ bool wpi::Protobuf<mrc::ControlData>::Pack(OutputStream& Stream,
std::optional<mrc::Joystick> wpi::Protobuf<mrc::Joystick>::Unpack(
InputStream& Stream) {
wpi::UnpackCallback<float, MRC_MAX_NUM_AXES> AxesCb;
wpi::UnpackCallback<int16_t, MRC_MAX_NUM_POVS> PovsCb;
wpi::UnpackCallback<int16_t, MRC_MAX_NUM_AXES> AxesCb;
mrc_proto_ProtobufJoystickData Msg{
.ButtonCount = 0,
.Buttons = 0,
.Axes = AxesCb.Callback(),
.POVs = PovsCb.Callback(),
.POVCount = 0,
.POVs = 0,
};
if (!Stream.Decode(Msg)) {
@@ -126,7 +124,6 @@ std::optional<mrc::Joystick> wpi::Protobuf<mrc::Joystick>::Unpack(
}
auto Axes = AxesCb.Items();
auto Povs = PovsCb.Items();
mrc::Joystick Joystick;
Joystick.Axes.SetCount(Axes.size());
@@ -135,28 +132,36 @@ std::optional<mrc::Joystick> wpi::Protobuf<mrc::Joystick>::Unpack(
Joystick.Axes.Axes()[i] = Axes[i];
}
Joystick.Povs.SetCount(Povs.size());
for (size_t i = 0; i < Joystick.Povs.GetCount(); i++) {
Joystick.Povs.Povs()[i] = Povs[i];
}
Joystick.Buttons.SetCount(Msg.ButtonCount);
Joystick.Buttons.Buttons = Msg.Buttons;
Joystick.Povs.SetCount(Msg.POVCount);
uint32_t PovsStore = Msg.POVs;
for (size_t i = 0; i < Joystick.Povs.GetCount(); i++) {
uint8_t Val = PovsStore & 0xF;
PovsStore >>= 4;
Joystick.Povs.Povs()[i] = Val;
}
return Joystick;
}
bool wpi::Protobuf<mrc::Joystick>::Pack(OutputStream& Stream,
const mrc::Joystick& Value) {
wpi::PackCallback AxesCb{Value.Axes.Axes()};
wpi::PackCallback PovsCb{Value.Povs.Povs()};
uint32_t PovsStore = 0;
for (size_t i = 0; i < Value.Povs.GetCount(); i++) {
PovsStore <<= 4;
PovsStore |= Value.Povs.Povs()[i] & 0xF;
}
mrc_proto_ProtobufJoystickData Msg{
.ButtonCount = static_cast<uint32_t>(Value.Buttons.GetCount()),
.Buttons = Value.Buttons.Buttons,
.Axes = AxesCb.Callback(),
.POVs = PovsCb.Callback(),
.POVCount = static_cast<uint32_t>(Value.Povs.GetCount()),
.POVs = PovsStore,
};
return Stream.Encode(Msg);

View File

@@ -41,8 +41,9 @@ wpi::Protobuf<mrc::JoystickDescriptor>::Unpack(InputStream& Stream) {
OutputData.SetPovsCount(Msg.PovCount);
OutputData.SetButtonsCount(Msg.ButtonCount);
OutputData.IsXbox = Msg.IsXbox ? 1 : 0;
OutputData.IsGamepad = Msg.IsGamepad ? 1 : 0;
OutputData.Type = Msg.JoystickType;
OutputData.RumbleCount = Msg.RumbleCount;
return OutputData;
}
@@ -58,10 +59,11 @@ bool wpi::Protobuf<mrc::JoystickDescriptor>::Pack(
mrc_proto_ProtobufJoystickDescriptor Msg{
.JoystickName = JoystickNameCb.Callback(),
.AxisTypes = AxisTypesCb.Callback(),
.IsXbox = Value.IsXbox ? true : false,
.IsGamepad = Value.IsGamepad ? true : false,
.JoystickType = Value.Type,
.ButtonCount = static_cast<int32_t>(Value.GetButtonsCount()),
.PovCount = static_cast<int32_t>(Value.GetPovsCount()),
.RumbleCount = Value.RumbleCount,
};
return Stream.Encode(Msg);

View File

@@ -1,33 +0,0 @@
// 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.
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "hal/proto/JoystickOutputData.h"
std::optional<mrc::JoystickOutputData>
wpi::Protobuf<mrc::JoystickOutputData>::Unpack(InputStream& Stream) {
mrc_proto_ProtobufJoystickOutputData Msg;
if (!Stream.Decode(Msg)) {
return {};
}
return mrc::JoystickOutputData{
.HidOutputs = Msg.HidOutputs,
.LeftRumble = Msg.LeftRumble,
.RightRumble = Msg.RightRumble,
};
}
bool wpi::Protobuf<mrc::JoystickOutputData>::Pack(
OutputStream& Stream, const mrc::JoystickOutputData& Value) {
mrc_proto_ProtobufJoystickOutputData Msg{
.HidOutputs = Value.HidOutputs,
.LeftRumble = Value.LeftRumble,
.RightRumble = Value.RightRumble,
};
return Stream.Encode(Msg);
}

View File

@@ -0,0 +1,42 @@
// 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.
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "hal/proto/JoystickRumbleData.h"
std::optional<mrc::JoystickRumbleData>
wpi::Protobuf<mrc::JoystickRumbleData>::Unpack(InputStream& Stream) {
wpi::UnpackCallback<uint16_t, MRC_MAX_NUM_RUMBLE> RumbleCb;
mrc_proto_ProtobufJoystickRumbleData Msg{
.Value = RumbleCb.Callback(),
};
if (!Stream.Decode(Msg)) {
return {};
}
auto Rumbles = RumbleCb.Items();
mrc::JoystickRumbleData Rumble;
Rumble.SetCount(Rumbles.size());
for (size_t i = 0; i < Rumble.GetCount(); i++) {
Rumble.Rumbles()[i] = Rumbles[i];
}
return Rumble;
}
bool wpi::Protobuf<mrc::JoystickRumbleData>::Pack(
OutputStream& Stream, const mrc::JoystickRumbleData& Value) {
wpi::PackCallback RumbleCb{Value.Rumbles()};
mrc_proto_ProtobufJoystickRumbleData Msg{
.Value = RumbleCb.Callback(),
};
return Stream.Encode(Msg);
}

View File

@@ -0,0 +1,76 @@
// 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.
#include <string>
#include <utility>
#include <vector>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "hal/proto/OpMode.h"
std::optional<mrc::OpMode> wpi::Protobuf<mrc::OpMode>::Unpack(
InputStream& Stream) {
wpi::UnpackCallback<std::string> NameCb;
mrc_proto_ProtobufOpMode Msg;
Msg.Name = NameCb.Callback();
if (!Stream.Decode(Msg)) {
return {};
}
auto Name = NameCb.Items();
if (Name.empty()) {
return {};
}
mrc::OpMode OutputData;
OutputData.MoveName(std::move(Name[0]));
OutputData.Hash = Msg.Hash;
return OutputData;
}
bool wpi::Protobuf<mrc::OpMode>::Pack(OutputStream& Stream,
const mrc::OpMode& Value) {
std::string_view EventNameStr = Value.GetName();
wpi::PackCallback EventName{&EventNameStr};
mrc_proto_ProtobufOpMode Msg{
.Hash = Value.Hash,
.Name = EventName.Callback(),
};
return Stream.Encode(Msg);
}
std::optional<std::vector<mrc::OpMode>>
wpi::Protobuf<std::vector<mrc::OpMode>>::Unpack(InputStream& Stream) {
wpi::StdVectorUnpackCallback<mrc::OpMode> ModesCb;
ModesCb.SetLimits(DecodeLimits::Add);
mrc_proto_ProtobufAvailableOpModes Msg;
Msg.Modes = ModesCb.Callback();
if (!Stream.Decode(Msg)) {
return {};
}
return ModesCb.Vec();
}
bool wpi::Protobuf<std::vector<mrc::OpMode>>::Pack(
OutputStream& Stream, const std::vector<mrc::OpMode>& Value) {
std::span<const mrc::OpMode> ModesSpan = Value;
wpi::PackCallback Modes{ModesSpan};
mrc_proto_ProtobufAvailableOpModes Msg{
.Modes = Modes.Callback(),
};
return Stream.Encode(Msg);
}

View File

@@ -1,56 +0,0 @@
// 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.
#include <string>
#include <utility>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "hal/proto/VersionInfo.h"
std::optional<mrc::VersionInfo> wpi::Protobuf<mrc::VersionInfo>::Unpack(
InputStream& Stream) {
wpi::UnpackCallback<std::string> NameCb;
wpi::UnpackCallback<std::string> VersionCb;
mrc_proto_ProtobufVersionInfo Msg{
.DeviceId = 0,
.Name = NameCb.Callback(),
.Version = VersionCb.Callback(),
};
if (!Stream.Decode(Msg)) {
return {};
}
auto Name = NameCb.Items();
auto Version = VersionCb.Items();
if (Name.empty() || Version.empty()) {
return {};
}
mrc::VersionInfo ToRet;
ToRet.DeviceId = Msg.DeviceId;
ToRet.MoveName(std::move(Name[0]));
ToRet.MoveVersion(std::move(Version[0]));
return ToRet;
}
bool wpi::Protobuf<mrc::VersionInfo>::Pack(OutputStream& Stream,
const mrc::VersionInfo& Value) {
std::string_view NameView = Value.GetName();
std::string_view VersionView = Value.GetVersion();
wpi::PackCallback NameCb{&NameView};
wpi::PackCallback VersionCb{&VersionView};
mrc_proto_ProtobufVersionInfo Msg{
.DeviceId = Value.DeviceId,
.Name = NameCb.Callback(),
.Version = VersionCb.Callback(),
};
return Stream.Encode(Msg);
}

View File

@@ -122,12 +122,12 @@ int32_t HAL_GetJoystickDescriptor(int32_t joystickNum,
HAL_JoystickDescriptor* desc);
/**
* Gets whether a specific joystick is considered to be an XBox controller.
* Gets whether a specific joystick is considered to be an Gamepad.
*
* @param joystickNum the joystick number
* @return true if xbox, false otherwise
* @return true if gamepad, false otherwise
*/
HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum);
HAL_Bool HAL_GetJoystickIsGamepad(int32_t joystickNum);
/**
* Gets the type of joystick connected.

View File

@@ -75,7 +75,7 @@ HAL_ENUM(HAL_MatchType) {
* struct. This is used for allocating buffers, not bounds checking, since there
* are usually less POVs in practice.
*/
#define HAL_kMaxJoystickPOVs 12
#define HAL_kMaxJoystickPOVs 8
/**
* The maximum number of joysticks.
*/
@@ -84,13 +84,13 @@ HAL_ENUM(HAL_MatchType) {
struct HAL_JoystickAxes {
int16_t count;
float axes[HAL_kMaxJoystickAxes];
uint8_t raw[HAL_kMaxJoystickAxes];
int16_t raw[HAL_kMaxJoystickAxes];
};
typedef struct HAL_JoystickAxes HAL_JoystickAxes;
struct HAL_JoystickPOVs {
int16_t count;
int16_t povs[HAL_kMaxJoystickPOVs];
uint8_t povs[HAL_kMaxJoystickPOVs];
};
typedef struct HAL_JoystickPOVs HAL_JoystickPOVs;
@@ -101,7 +101,7 @@ struct HAL_JoystickButtons {
typedef struct HAL_JoystickButtons HAL_JoystickButtons;
struct HAL_JoystickDescriptor {
uint8_t isXbox;
uint8_t isGamepad;
uint8_t type;
char name[256];
uint8_t axisCount;

View File

@@ -1,21 +0,0 @@
// 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 <stdint.h>
#include <wpi/protobuf/Protobuf.h>
#include "MrcComm.npb.h"
#include "mrc/NetComm.h"
template <>
struct wpi::Protobuf<mrc::JoystickOutputData> {
using MessageStruct = mrc_proto_ProtobufJoystickOutputData;
using InputStream = wpi::ProtoInputStream<mrc::JoystickOutputData>;
using OutputStream = wpi::ProtoOutputStream<mrc::JoystickOutputData>;
static std::optional<mrc::JoystickOutputData> Unpack(InputStream& Stream);
static bool Pack(OutputStream& Stream, const mrc::JoystickOutputData& Value);
};

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
#include <stdint.h>
#include <wpi/protobuf/Protobuf.h>
#include "MrcComm.npb.h"
#include "mrc/NetComm.h"
template <>
struct wpi::Protobuf<mrc::JoystickRumbleData> {
using MessageStruct = mrc_proto_ProtobufJoystickRumbleData;
using InputStream = wpi::ProtoInputStream<mrc::JoystickRumbleData>;
using OutputStream = wpi::ProtoOutputStream<mrc::JoystickRumbleData>;
static std::optional<mrc::JoystickRumbleData> Unpack(InputStream& Stream);
static bool Pack(OutputStream& Stream, const mrc::JoystickRumbleData& Value);
};

View File

@@ -0,0 +1,32 @@
// 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 <stdint.h>
#include <vector>
#include <wpi/protobuf/Protobuf.h>
#include "MrcComm.npb.h"
#include "mrc/NetComm.h"
template <>
struct wpi::Protobuf<mrc::OpMode> {
using MessageStruct = mrc_proto_ProtobufOpMode;
using InputStream = wpi::ProtoInputStream<mrc::OpMode>;
using OutputStream = wpi::ProtoOutputStream<mrc::OpMode>;
static std::optional<mrc::OpMode> Unpack(InputStream& Stream);
static bool Pack(OutputStream& Stream, const mrc::OpMode& Value);
};
template <>
struct wpi::Protobuf<std::vector<mrc::OpMode>> {
using MessageStruct = mrc_proto_ProtobufAvailableOpModes;
using InputStream = wpi::ProtoInputStream<std::vector<mrc::OpMode>>;
using OutputStream = wpi::ProtoOutputStream<std::vector<mrc::OpMode>>;
static std::optional<std::vector<mrc::OpMode>> Unpack(InputStream& Stream);
static bool Pack(OutputStream& Stream, const std::vector<mrc::OpMode>& Value);
};

View File

@@ -147,7 +147,7 @@ void HALSIM_SetJoystickButtonCount(int32_t stick, int32_t count);
void HALSIM_GetJoystickCounts(int32_t stick, int32_t* axisCount,
int32_t* buttonCount, int32_t* povCount);
void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox);
void HALSIM_SetJoystickIsGamepad(int32_t stick, HAL_Bool isGamepad);
void HALSIM_SetJoystickType(int32_t stick, int32_t type);
void HALSIM_SetJoystickName(int32_t stick, const struct WPI_String* name);
void HALSIM_SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type);

View File

@@ -285,12 +285,12 @@ int32_t HAL_GetJoystickDescriptor(int32_t joystickNum,
return 0;
}
HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) {
HAL_Bool HAL_GetJoystickIsGamepad(int32_t joystickNum) {
HAL_JoystickDescriptor joystickDesc;
if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
return 0;
} else {
return joystickDesc.isXbox;
return joystickDesc.isGamepad;
}
}

View File

@@ -320,12 +320,13 @@ void DriverStationData::GetJoystickCounts(int32_t stick, int32_t* axisCount,
*povCount = m_joystickData[stick].povs.count;
}
void DriverStationData::SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {
void DriverStationData::SetJoystickIsGamepad(int32_t stick,
HAL_Bool isGamepad) {
if (stick < 0 || stick >= kNumJoysticks) {
return;
}
std::scoped_lock lock(m_joystickDataMutex);
m_joystickData[stick].descriptor.isXbox = isXbox;
m_joystickData[stick].descriptor.isGamepad = isGamepad;
m_joystickDescriptorCallbacks(stick, &m_joystickData[stick].descriptor);
}
@@ -532,8 +533,8 @@ void HALSIM_GetJoystickCounts(int32_t stick, int32_t* axisCount,
povCount);
}
void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {
SimDriverStationData->SetJoystickIsXbox(stick, isXbox);
void HALSIM_SetJoystickIsGamepad(int32_t stick, HAL_Bool isGamepad) {
SimDriverStationData->SetJoystickIsGamepad(stick, isGamepad);
}
void HALSIM_SetJoystickType(int32_t stick, int32_t type) {

View File

@@ -105,7 +105,7 @@ class DriverStationData {
void GetJoystickCounts(int32_t stick, int32_t* axisCount,
int32_t* buttonCount, int32_t* povCount);
void SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox);
void SetJoystickIsGamepad(int32_t stick, HAL_Bool isGamepad);
void SetJoystickType(int32_t stick, int32_t type);
void SetJoystickName(int32_t stick, std::string_view message);
void SetJoystickAxisType(int32_t stick, int32_t axis, int32_t type);

View File

@@ -11,6 +11,7 @@
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <fmt/format.h>
#include <networktables/BooleanTopic.h>
@@ -32,8 +33,9 @@
#include "hal/proto/ControlData.h"
#include "hal/proto/ErrorInfo.h"
#include "hal/proto/JoystickDescriptor.h"
#include "hal/proto/JoystickOutputData.h"
#include "hal/proto/JoystickRumbleData.h"
#include "hal/proto/MatchInfo.h"
#include "hal/proto/OpMode.h"
#include "mrc/NtNetComm.h"
static_assert(sizeof(int32_t) >= sizeof(int),
@@ -60,12 +62,8 @@ static_assert(std::is_standard_layout_v<JoystickDataCache>);
struct SystemServerDriverStation {
nt::NetworkTableInstance ntInst;
nt::BooleanPublisher robotProgramPublisher;
nt::BooleanPublisher codeStartedPublisher;
nt::BooleanPublisher userCodeDisabledPublisher;
nt::BooleanPublisher userCodeAutonomousPublisher;
nt::BooleanPublisher userCodeTeleopPublisher;
nt::BooleanPublisher userCodeTestPublisher;
nt::BooleanPublisher hasUserCodePublisher;
nt::BooleanPublisher hasUserCodeReadyPublisher;
nt::ProtobufSubscriber<mrc::ControlData> controlDataSubscriber;
nt::ProtobufSubscriber<mrc::MatchInfo> matchInfoSubscriber;
@@ -79,9 +77,13 @@ struct SystemServerDriverStation {
nt::StringPublisher consoleLinePublisher;
nt::ProtobufPublisher<mrc::ErrorInfo> errorInfoPublisher;
std::array<nt::ProtobufPublisher<mrc::JoystickOutputData>,
std::array<nt::ProtobufPublisher<mrc::JoystickRumbleData>,
MRC_MAX_NUM_JOYSTICKS>
joystickOutputsTopics;
joystickRumbleTopics;
nt::ProtobufPublisher<std::vector<mrc::OpMode>> teleopOpModes;
nt::ProtobufPublisher<std::vector<mrc::OpMode>> autoOpModes;
nt::ProtobufPublisher<std::vector<mrc::OpMode>> testOpModes;
NT_Listener controlDataListener;
@@ -97,28 +99,20 @@ struct SystemServerDriverStation {
options.keepDuplicates = true;
options.periodic = 0.005;
codeStartedPublisher =
ntInst.GetBooleanTopic(ROBOT_CODE_STARTED_PATH).Publish(options);
userCodeDisabledPublisher =
ntInst.GetBooleanTopic(ROBOT_DISABLED_TRACE_PATH).Publish(options);
userCodeAutonomousPublisher =
ntInst.GetBooleanTopic(ROBOT_AUTON_TRACE_PATH).Publish(options);
userCodeTeleopPublisher =
ntInst.GetBooleanTopic(ROBOT_TELEOP_TRACE_PATH).Publish(options);
userCodeTestPublisher =
ntInst.GetBooleanTopic(ROBOT_TEST_TRACE_PATH).Publish(options);
hasUserCodeReadyPublisher =
ntInst.GetBooleanTopic(ROBOT_HAS_USER_CODE_READY_PATH).Publish(options);
for (size_t count = 0; count < joystickOutputsTopics.size(); count++) {
std::string name = ROBOT_JOYSTICK_OUTPUTS_PATH;
for (size_t count = 0; count < joystickRumbleTopics.size(); count++) {
std::string name = ROBOT_JOYSTICK_RUMBLE_PATH;
name += std::to_string(count);
joystickOutputsTopics[count] =
ntInst.GetProtobufTopic<mrc::JoystickOutputData>(name).Publish(
joystickRumbleTopics[count] =
ntInst.GetProtobufTopic<mrc::JoystickRumbleData>(name).Publish(
options);
}
robotProgramPublisher =
ntInst.GetBooleanTopic(ROBOT_NEW_ROBOT_PROGRAM_PATH).Publish();
robotProgramPublisher.Set(true);
hasUserCodePublisher =
ntInst.GetBooleanTopic(ROBOT_HAS_USER_CODE_PATH).Publish();
hasUserCodePublisher.Set(true);
consoleLinePublisher =
ntInst.GetStringTopic(ROBOT_CONSOLE_LINE_PATH).Publish(options);
@@ -147,6 +141,31 @@ struct SystemServerDriverStation {
ntInst.GetProtobufTopic<mrc::JoystickDescriptor>(name).Subscribe({});
}
teleopOpModes = ntInst
.GetProtobufTopic<std::vector<mrc::OpMode>>(
ROBOT_TELEOP_OP_MODES_PATH)
.Publish();
autoOpModes = ntInst
.GetProtobufTopic<std::vector<mrc::OpMode>>(
ROBOT_AUTO_OP_MODES_PATH)
.Publish();
testOpModes = ntInst
.GetProtobufTopic<std::vector<mrc::OpMode>>(
ROBOT_TEST_OP_MODES_PATH)
.Publish();
std::vector<mrc::OpMode> staticTeleopOpModes;
staticTeleopOpModes.emplace_back(mrc::OpMode{"TeleOp", 2});
teleopOpModes.Set(staticTeleopOpModes);
std::vector<mrc::OpMode> staticAutoOpModes;
staticAutoOpModes.emplace_back(mrc::OpMode{"Auto", 1});
autoOpModes.Set(staticAutoOpModes);
std::vector<mrc::OpMode> staticTestOpModes;
staticTestOpModes.emplace_back(mrc::OpMode{"Test", 3});
testOpModes.Set(staticTestOpModes);
ntInst.AddListener(
controlDataSubscriber, NT_EVENT_VALUE_REMOTE | NT_EVENT_UNPUBLISH,
[this](const nt::Event& event) { HandleListener(event); });
@@ -211,13 +230,8 @@ void JoystickDataCache::Update(const mrc::ControlData& data) {
controlWord.fmsAttached = data.ControlWord.FmsConnected;
controlWord.dsAttached = data.ControlWord.DsConnected;
controlWord.eStop = data.ControlWord.EStop;
auto mode = data.GetOpMode();
if (mode == "Test") {
controlWord.test = true;
} else if (mode == "Auton") {
controlWord.autonomous = true;
}
controlWord.test = data.ControlWord.Test;
controlWord.autonomous = data.ControlWord.Auto;
auto sticks = data.Joysticks();
@@ -228,7 +242,13 @@ void JoystickDataCache::Update(const mrc::ControlData& data) {
axes[count].count = newAxes.size();
for (size_t i = 0; i < newAxes.size(); i++) {
axes[count].axes[i] = newAxes[i];
axes[count].raw[i] = newAxes[i];
int16_t axisValue = newAxes[i];
if (axisValue < 0) {
axes[count].axes[i] = axisValue / 32768.0f;
} else {
axes[count].axes[i] = axisValue / 32767.0f;
}
}
povs[count].count = newPovs.size();
@@ -301,7 +321,7 @@ void TcpCache::Update() {
auto& desc = descriptors[count];
desc.isXbox = newDesc.IsXbox;
desc.isGamepad = newDesc.IsGamepad;
desc.type = newDesc.Type;
desc.buttonCount = newDesc.GetButtonsCount();
desc.povCount = newDesc.GetPovsCount();
@@ -481,12 +501,12 @@ HAL_AllianceStationID HAL_GetAllianceStation(int32_t* status) {
return currentRead->allianceStation;
}
HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) {
HAL_Bool HAL_GetJoystickIsGamepad(int32_t joystickNum) {
HAL_JoystickDescriptor joystickDesc;
if (HAL_GetJoystickDescriptor(joystickNum, &joystickDesc) < 0) {
return 0;
} else {
return joystickDesc.isXbox;
return joystickDesc.isGamepad;
}
}
@@ -523,15 +543,17 @@ int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
int32_t leftRumble, int32_t rightRumble) {
CHECK_JOYSTICK_NUMBER(joystickNum);
mrc::JoystickOutputData outputData{
.HidOutputs = static_cast<uint32_t>(outputs),
.LeftRumble = std::clamp(leftRumble, 0, UINT16_MAX) /
static_cast<float>(UINT16_MAX),
.RightRumble = std::clamp(rightRumble, 0, UINT16_MAX) /
static_cast<float>(UINT16_MAX),
};
// TODO Update this API
systemServerDs->joystickOutputsTopics[joystickNum].Set(outputData);
// mrc::JoystickOutputData outputData{
// .HidOutputs = static_cast<uint32_t>(outputs),
// .LeftRumble = std::clamp(leftRumble, 0, UINT16_MAX) /
// static_cast<float>(UINT16_MAX),
// .RightRumble = std::clamp(rightRumble, 0, UINT16_MAX) /
// static_cast<float>(UINT16_MAX),
// };
// systemServerDs->joystickRumbleTopics[joystickNum].Set(outputData);
return 0;
}
@@ -542,24 +564,16 @@ double HAL_GetMatchTime(int32_t* status) {
}
void HAL_ObserveUserProgramStarting(void) {
systemServerDs->codeStartedPublisher.Set(true);
systemServerDs->hasUserCodeReadyPublisher.Set(true);
}
void HAL_ObserveUserProgramDisabled(void) {
systemServerDs->userCodeDisabledPublisher.Set(true);
}
void HAL_ObserveUserProgramDisabled(void) {}
void HAL_ObserveUserProgramAutonomous(void) {
systemServerDs->userCodeAutonomousPublisher.Set(true);
}
void HAL_ObserveUserProgramAutonomous(void) {}
void HAL_ObserveUserProgramTeleop(void) {
systemServerDs->userCodeTeleopPublisher.Set(true);
}
void HAL_ObserveUserProgramTeleop(void) {}
void HAL_ObserveUserProgramTest(void) {
systemServerDs->userCodeTestPublisher.Set(true);
}
void HAL_ObserveUserProgramTest(void) {}
HAL_Bool HAL_RefreshDSData(void) {
mrc::ControlData newestData;

View File

@@ -99,7 +99,7 @@ void HALSIM_GetJoystickCounts(int32_t stick, int32_t* axisCount,
*povCount = 0;
}
void HALSIM_SetJoystickIsXbox(int32_t stick, HAL_Bool isXbox) {}
void HALSIM_SetJoystickIsGamepad(int32_t stick, HAL_Bool isGamepad) {}
void HALSIM_SetJoystickType(int32_t stick, int32_t type) {}