[hal] Add initial SystemServer support (#7463)

This commit is contained in:
Thad House
2024-12-01 04:31:26 +00:00
committed by GitHub
parent 82132c3272
commit c51f65bd4f
39 changed files with 1658 additions and 83 deletions

View File

@@ -0,0 +1,308 @@
// 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 <array>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include "mrc/NtNetComm.h"
namespace mrc {
struct ControlFlags {
uint32_t Enabled : 1;
uint32_t ModeReserved : 2;
uint32_t EStop : 1;
uint32_t FmsConnected : 1;
uint32_t DsConnected : 1;
uint32_t WatchdogActive : 1;
uint32_t Alliance : 6;
uint32_t Reserved : 19;
};
struct JoystickAxes {
public:
std::span<float> Axes() { return std::span{AxesStore.data(), GetCount()}; }
std::span<const float> Axes() const {
return std::span{AxesStore.data(), GetCount()};
}
void SetCount(uint8_t NewCount) {
Count = (std::min)(NewCount, static_cast<uint8_t>(MRC_MAX_NUM_AXES));
}
size_t GetCount() const { return Count; }
private:
std::array<float, MRC_MAX_NUM_AXES> AxesStore;
uint8_t Count{0};
};
struct JoystickPovs {
public:
std::span<int16_t> Povs() { return std::span{PovsStore.data(), GetCount()}; }
std::span<const int16_t> Povs() const {
return std::span{PovsStore.data(), GetCount()};
}
void SetCount(uint8_t NewCount) {
Count = (std::min)(NewCount, static_cast<uint8_t>(MRC_MAX_NUM_POVS));
}
size_t GetCount() const { return Count; }
private:
std::array<int16_t, MRC_MAX_NUM_POVS> PovsStore;
uint8_t Count{0};
};
struct JoystickButtons {
uint32_t Buttons;
void SetCount(uint8_t NewCount) {
Count = (std::min)(NewCount, static_cast<uint8_t>(MRC_MAX_NUM_BUTTONS));
}
size_t GetCount() const { return Count; }
private:
uint8_t Count{0};
};
struct Joystick {
JoystickAxes Axes;
JoystickPovs Povs;
JoystickButtons Buttons;
};
struct ControlData {
ControlFlags ControlWord;
float MatchTime;
std::span<Joystick> Joysticks() {
return std::span{JoysticksStore.data(), GetJoystickCount()};
}
std::span<const Joystick> Joysticks() const {
return std::span{JoysticksStore.data(), GetJoystickCount()};
}
size_t GetJoystickCount() const { return JoystickCount; }
void SetJoystickCount(uint8_t NewCount) {
JoystickCount =
(std::min)(NewCount, static_cast<uint8_t>(MRC_MAX_NUM_JOYSTICKS));
}
void SetOpMode(std::string_view Mode) {
if (Mode.size() > MRC_MAX_OPMODE_LEN) {
Mode = Mode.substr(0, MRC_MAX_OPMODE_LEN);
}
OpMode = Mode;
}
void MoveOpMode(std::string&& Mode) {
OpMode = std::move(Mode);
if (OpMode.size() > MRC_MAX_OPMODE_LEN) {
OpMode.resize(MRC_MAX_OPMODE_LEN);
}
}
std::string_view GetOpMode() const { return OpMode; }
std::span<uint8_t> WritableOpModeBuffer(size_t Len) {
if (Len > MRC_MAX_OPMODE_LEN) {
Len = MRC_MAX_OPMODE_LEN;
}
OpMode.resize(Len);
return std::span<uint8_t>{reinterpret_cast<uint8_t*>(OpMode.data()),
OpMode.size()};
}
private:
std::array<Joystick, MRC_MAX_NUM_JOYSTICKS> JoysticksStore;
uint8_t JoystickCount{0};
std::string OpMode;
};
struct JoystickOutputData {
uint32_t HidOutputs{0};
float LeftRumble{0};
float RightRumble{0};
};
enum class MatchType : uint8_t {
None = 0,
Practice = 1,
Qualification = 2,
Playoff = 3,
Test = 4,
};
struct MatchInfo {
uint16_t MatchNumber{0};
uint8_t ReplayNumber{0};
MatchType Type{MatchType::None};
void SetEventName(std::string_view Name) {
if (Name.size() > MRC_MAX_EVENT_NAME_LEN) {
Name = Name.substr(0, MRC_MAX_EVENT_NAME_LEN);
}
EventName = Name;
}
void MoveEventName(std::string&& Name) {
EventName = std::move(Name);
if (EventName.size() > MRC_MAX_EVENT_NAME_LEN) {
EventName.resize(MRC_MAX_EVENT_NAME_LEN);
}
}
std::string_view GetEventName() const { return EventName; }
std::span<uint8_t> WritableNameBuffer(size_t Len) {
if (Len > MRC_MAX_EVENT_NAME_LEN) {
Len = MRC_MAX_EVENT_NAME_LEN;
}
EventName.resize(Len);
return std::span<uint8_t>{reinterpret_cast<uint8_t*>(EventName.data()),
EventName.size()};
}
private:
std::string EventName;
};
struct JoystickDescriptor {
public:
bool IsXbox{0};
uint8_t Type{0};
std::span<uint8_t> AxesTypes() {
return std::span{AxesTypesStore.data(), GetAxesCount()};
}
std::span<const uint8_t> AxesTypes() const {
return std::span{AxesTypesStore.data(), GetAxesCount()};
}
void SetAxesCount(uint8_t NewCount) {
AxesCount = (std::min)(NewCount, static_cast<uint8_t>(MRC_MAX_NUM_AXES));
}
size_t GetAxesCount() const { return AxesCount; }
void SetPovsCount(uint8_t NewCount) {
PovCount = (std::min)(NewCount, static_cast<uint8_t>(MRC_MAX_NUM_POVS));
}
size_t GetPovsCount() const { return PovCount; }
void SetButtonsCount(uint8_t NewCount) {
ButtonCount =
(std::min)(NewCount, static_cast<uint8_t>(MRC_MAX_NUM_BUTTONS));
}
size_t GetButtonsCount() const { return ButtonCount; }
void SetName(std::string_view Name) {
if (Name.size() > MRC_MAX_JOYSTICK_NAME_LEN) {
Name = Name.substr(0, MRC_MAX_JOYSTICK_NAME_LEN);
}
JoystickName = Name;
}
void MoveName(std::string&& Name) {
JoystickName = std::move(Name);
if (JoystickName.size() > MRC_MAX_JOYSTICK_NAME_LEN) {
JoystickName.resize(MRC_MAX_JOYSTICK_NAME_LEN);
}
}
std::string_view GetName() const { return JoystickName; }
std::span<uint8_t> WritableNameBuffer(size_t Len) {
if (Len > MRC_MAX_JOYSTICK_NAME_LEN) {
Len = MRC_MAX_JOYSTICK_NAME_LEN;
}
JoystickName.resize(Len);
return std::span<uint8_t>{reinterpret_cast<uint8_t*>(JoystickName.data()),
JoystickName.size()};
}
private:
std::string JoystickName;
std::array<uint8_t, MRC_MAX_NUM_AXES> AxesTypesStore;
uint8_t AxesCount{0};
uint8_t ButtonCount{0};
uint8_t PovCount{0};
};
struct VersionInfo {
uint32_t DeviceId{0};
void SetName(std::string_view NewName) {
if (NewName.size() > MRC_MAX_VERSION_SIZE) {
NewName = NewName.substr(0, MRC_MAX_VERSION_SIZE);
}
Name = NewName;
}
void MoveName(std::string&& NewName) {
Name = std::move(NewName);
if (Name.size() > MRC_MAX_VERSION_SIZE) {
Name.resize(MRC_MAX_VERSION_SIZE);
}
}
std::string_view GetName() const { return Name; }
std::span<uint8_t> WritableNameBuffer(size_t Len) {
if (Len > MRC_MAX_VERSION_SIZE) {
Len = MRC_MAX_VERSION_SIZE;
}
Name.resize(Len);
return std::span<uint8_t>{reinterpret_cast<uint8_t*>(Name.data()),
Name.size()};
}
void SetVersion(std::string_view NewVersion) {
if (NewVersion.size() > MRC_MAX_VERSION_SIZE) {
NewVersion = NewVersion.substr(0, MRC_MAX_VERSION_SIZE);
}
Version = NewVersion;
}
void MoveVersion(std::string&& NewVersion) {
Version = std::move(NewVersion);
if (Version.size() > MRC_MAX_VERSION_SIZE) {
Version.resize(MRC_MAX_VERSION_SIZE);
}
}
std::string_view GetVersion() const { return Version; }
std::span<uint8_t> WritableVersionBuffer(size_t Len) {
if (Len > MRC_MAX_VERSION_SIZE) {
Len = MRC_MAX_VERSION_SIZE;
}
Version.resize(Len);
return std::span<uint8_t>{reinterpret_cast<uint8_t*>(Version.data()),
Version.size()};
}
private:
std::string Name;
std::string Version;
};
} // namespace mrc

View File

@@ -0,0 +1,60 @@
// 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
#define ROBOT_SYSTEM_SERVER_NT_PORT 6810
#define ROBOT_CONTROL_DATA_PREFIX "/Netcomm/Control/"
#define ROBOT_WATCHDOG_ACTIVE_PATH (ROBOT_CONTROL_DATA_PREFIX "WatchdogActive")
#define ROBOT_CONTROL_DATA_PATH (ROBOT_CONTROL_DATA_PREFIX "ControlData")
#define ROBOT_GAME_SPECIFIC_MESSAGE_PATH \
(ROBOT_CONTROL_DATA_PREFIX "GameSpecificMessage")
#define ROBOT_MATCH_INFO_PATH (ROBOT_CONTROL_DATA_PREFIX "MatchInfo")
#define ROBOT_JOYSTICK_DESCRIPTORS_PATH \
(ROBOT_CONTROL_DATA_PREFIX "JoystickDescriptors/")
#define ROBOT_STATUS_DATA_PREFIX "/Netcomm/Status/"
#define ROBOT_NEW_ROBOT_PROGRAM_PATH \
(ROBOT_STATUS_DATA_PREFIX "NewRobotProgram")
#define ROBOT_CODE_STARTED_PATH (ROBOT_STATUS_DATA_PREFIX "UserCodeStarted")
#define ROBOT_DISABLED_TRACE_PATH (ROBOT_STATUS_DATA_PREFIX "UserCodeDisabled")
#define ROBOT_AUTON_TRACE_PATH (ROBOT_STATUS_DATA_PREFIX "UserCodeAutonomous")
#define ROBOT_TELEOP_TRACE_PATH (ROBOT_STATUS_DATA_PREFIX "UserCodeTeleop")
#define ROBOT_TEST_TRACE_PATH (ROBOT_STATUS_DATA_PREFIX "UserCodeTest")
#define ROBOT_OUTPUTS_DATA_PREFIX "/Netcomm/Outputs/"
#define ROBOT_JOYSTICK_OUTPUTS_PATH (ROBOT_OUTPUTS_DATA_PREFIX "Joysticks/")
#define ROBOT_CONSOLE_DATA_PREFIX "/Netcomm/Console/"
#define ROBOT_CONSOLE_LINE_PATH (ROBOT_CONSOLE_DATA_PREFIX "ConsoleLine")
#define ROBOT_REPORTING_DATA_PREFIX "/Netcomm/Reporting/"
#define ROBOT_LIB_VERSION_PATH (ROBOT_REPORTING_DATA_PREFIX "LibVersion")
#define ROBOT_USER_REPORTING_PATH (ROBOT_REPORTING_DATA_PREFIX "User")
#define ROBOT_MODES_PREFIX "/Netcomm/Modes/"
#define ROBOT_AVAILABLE_OP_MODES_PATH (ROBOT_MODES_PREFIX "AvailableOpModes")
#define ROBOT_SYSTEM_SERVER_PREFIX "/sys/"
#define ROBOT_BATTERY_VOLTAGE_PATH (ROBOT_SYSTEM_SERVER_PREFIX "battery")
#define ROBOT_RAM_USED_PATH (ROBOT_SYSTEM_SERVER_PREFIX "ram")
#define ROBOT_RAM_BYTES_PATH (ROBOT_SYSTEM_SERVER_PREFIX "ramtotal")
#define ROBOT_STORAGE_USED_PATH (ROBOT_SYSTEM_SERVER_PREFIX "storage")
#define ROBOT_STORAGE_BYTES_PATH (ROBOT_SYSTEM_SERVER_PREFIX "storagetotal")
#define ROBOT_CPU_PATH (ROBOT_SYSTEM_SERVER_PREFIX "cpu")
#define ROBOT_CAN_BW_PATH (ROBOT_SYSTEM_SERVER_PREFIX "canbusban")
#define ROBOT_TEAM_PATH (ROBOT_SYSTEM_SERVER_PREFIX "team")
#define MRC_MAX_NUM_JOYSTICKS 6
#define MRC_MAX_NUM_AXES 12
#define MRC_MAX_NUM_POVS 12
#define MRC_MAX_NUM_BUTTONS 32
#define MRC_MAX_OPMODE_LEN 32
#define MRC_MAX_GAME_SPECIFIC_MESSAGE_LEN 128
#define MRC_MAX_EVENT_NAME_LEN 64
#define MRC_MAX_JOYSTICK_NAME_LEN 256
#define MRC_MAX_VERSION_SIZE 256

View File

@@ -0,0 +1,47 @@
syntax = "proto3";
package mrc.proto;
option java_package = "com.mrc.proto";
message ProtobufJoystickData {
uint32 ButtonCount = 1;
fixed32 Buttons = 2;
repeated float Axes = 3;
repeated sint32 POVs = 4;
}
message ProtobufControlData {
uint32 ControlWord = 1;
float MatchTime = 2;
repeated ProtobufJoystickData Joysticks = 3;
string OpMode = 4;
}
message ProtobufJoystickDescriptor {
string JoystickName = 1;
repeated int32 AxisTypes = 2;
bool IsXbox = 3;
int32 JoystickType = 4;
int32 ButtonCount = 5;
int32 PovCount = 6;
}
message ProtobufJoystickOutputData {
fixed32 HidOutputs = 1;
float LeftRumble = 2;
float RightRumble = 3;
}
message ProtobufVersionInfo {
fixed32 DeviceId = 1;
string Name = 2;
string Version = 3;
}
message ProtobufMatchInfo {
string EventName = 1;
int32 MatchNumber = 2;
int32 ReplayNumber = 3;
int32 MatchType = 4;
}