[hal, wpilib] Add OpMode support (#7744)

User code:
- OpModeRobot used as the robot base class
- LinearOpMode and PeriodicOpMode are provided opmode base classes
- In Java, annotations can be used to automatically register opmode classes

Additional user code functionality:
- OpMode (string) is available in addition to the overall
auto/teleop/test robot mode
- OpMode does not indicate enable (enable/disable is still separate)
- The HAL API uses integer UIDs; these are exposed at the user API level
as well for faster checks
- User code creates opmodes on startup (these have name, category,
description, etc).

DS:
- DS will present opmode selection lists for auto and teleop for
match/practice. During a match, the DS will automatically activate the
selected opmode in the corresponding match period.
- For testing, an overall mode is selected (e.g. teleop/auto/test) and a
single opmode is selected

Future work:
- Command framework support/integration
- Python annotation support
- Unit tests (needs race-free DS sim updates)
- Porting of examples

Co-authored-by: Joseph Eng <91924258+KangarooKoala@users.noreply.github.com>
This commit is contained in:
Peter Johnson
2025-12-12 21:25:57 -07:00
committed by GitHub
parent 2a41b80e00
commit dacded37e5
163 changed files with 7454 additions and 2175 deletions

View File

@@ -16,6 +16,8 @@ using namespace wpi::glass;
static const char* stations[] = {"Invalid", "Red 1", "Red 2", "Red 3",
"Blue 1", "Blue 2", "Blue 3"};
static const char* robotModes[] = {"Unknown", "Autonomous", "Teleoperated",
"Test"};
void wpi::glass::DisplayFMS(FMSModel* model, bool editableDsAttached) {
if (!model->Exists() || model->IsReadOnly()) {
@@ -107,17 +109,12 @@ void wpi::glass::DisplayFMSReadOnly(FMSModel* model) {
ImGui::SameLine();
ImGui::TextUnformatted(exists ? (data->GetValue() ? "Yes" : "No") : "?");
}
if (auto data = model->GetTestData()) {
ImGui::Selectable("Test Mode: ");
if (auto data = model->GetRobotModeData()) {
ImGui::Selectable("Robot Mode: ");
data->EmitDrag();
ImGui::SameLine();
ImGui::TextUnformatted(exists ? (data->GetValue() ? "Yes" : "No") : "?");
}
if (auto data = model->GetAutonomousData()) {
ImGui::Selectable("Autonomous Mode: ");
data->EmitDrag();
ImGui::SameLine();
ImGui::TextUnformatted(exists ? (data->GetValue() ? "Yes" : "No") : "?");
ImGui::TextUnformatted(
exists ? robotModes[static_cast<int>(data->GetValue())] : "?");
}
if (auto data = model->GetFmsAttachedData()) {
ImGui::Selectable("FMS Attached: ");

View File

@@ -22,14 +22,20 @@ class StringSource;
class FMSModel : public Model {
public:
enum RobotMode {
kUnknown = 0,
kAutonomous = 1,
kTeleop = 2,
kTest = 3,
};
virtual BooleanSource* GetFmsAttachedData() = 0;
virtual BooleanSource* GetDsAttachedData() = 0;
virtual IntegerSource* GetAllianceStationIdData() = 0;
virtual DoubleSource* GetMatchTimeData() = 0;
virtual BooleanSource* GetEStopData() = 0;
virtual BooleanSource* GetEnabledData() = 0;
virtual BooleanSource* GetTestData() = 0;
virtual BooleanSource* GetAutonomousData() = 0;
virtual IntegerSource* GetRobotModeData() = 0;
virtual StringSource* GetGameSpecificMessageData() = 0;
virtual void SetFmsAttached(bool val) = 0;
@@ -38,8 +44,7 @@ class FMSModel : public Model {
virtual void SetMatchTime(double val) = 0;
virtual void SetEStop(bool val) = 0;
virtual void SetEnabled(bool val) = 0;
virtual void SetTest(bool val) = 0;
virtual void SetAutonomous(bool val) = 0;
virtual void SetRobotMode(RobotMode val) = 0;
virtual void SetGameSpecificMessage(std::string_view val) = 0;
};

View File

@@ -10,8 +10,17 @@
#include <fmt/format.h>
#include "wpi/util/SmallVector.hpp"
#include "wpi/util/timestamp.h"
#include "wpi/util/Endian.hpp"
// FIXME: use dynamic struct decoding
// Duplicated here from DriverStationTypes.h to avoid HAL dependency
#define HAL_CONTROLWORD_OPMODE_HASH_MASK 0x00FFFFFFFFFFFFFFLL
#define HAL_CONTROLWORD_ROBOT_MODE_MASK 0x0300000000000000LL
#define HAL_CONTROLWORD_ROBOT_MODE_SHIFT 56
#define HAL_CONTROLWORD_ENABLED_MASK 0x0400000000000000LL
#define HAL_CONTROLWORD_ESTOP_MASK 0x0800000000000000LL
#define HAL_CONTROLWORD_FMS_ATTACHED_MASK 0x1000000000000000LL
#define HAL_CONTROLWORD_DS_ATTACHED_MASK 0x2000000000000000LL
using namespace wpi::glass;
@@ -28,15 +37,14 @@ NTFMSModel::NTFMSModel(wpi::nt::NetworkTableInstance inst,
.Subscribe(false)},
m_station{inst.GetIntegerTopic(fmt::format("{}/StationNumber", path))
.Subscribe(0)},
m_controlWord{inst.GetIntegerTopic(fmt::format("{}/FMSControlData", path))
.Subscribe(0)},
m_controlWord{inst.GetRawTopic(fmt::format("{}/ControlWord", path))
.Subscribe("struct:ControlWord", {})},
m_fmsAttached{fmt::format("NT_FMS:FMSAttached:{}", path)},
m_dsAttached{fmt::format("NT_FMS:DSAttached:{}", path)},
m_allianceStationId{fmt::format("NT_FMS:AllianceStationID:{}", path)},
m_estop{fmt::format("NT_FMS:EStop:{}", path)},
m_enabled{fmt::format("NT_FMS:RobotEnabled:{}", path)},
m_test{fmt::format("NT_FMS:TestMode:{}", path)},
m_autonomous{fmt::format("NT_FMS:AutonomousMode:{}", path)},
m_robotMode{fmt::format("NT_FMS:RobotMode:{}", path)},
m_gameSpecificMessageData{
fmt::format("NT_FMS:GameSpecificMessage:{}", path)} {}
@@ -55,14 +63,24 @@ void NTFMSModel::Update() {
m_allianceStationId.SetValue(v.value - 1 + 3 * (isRed ? 0 : 1), v.time);
}
for (auto&& v : m_controlWord.ReadQueue()) {
uint32_t controlWord = v.value;
// See HAL_ControlWord definition
m_enabled.SetValue(((controlWord & 0x01) != 0) ? 1 : 0, v.time);
m_autonomous.SetValue(((controlWord & 0x02) != 0) ? 1 : 0, v.time);
m_test.SetValue(((controlWord & 0x04) != 0) ? 1 : 0, v.time);
m_estop.SetValue(((controlWord & 0x08) != 0) ? 1 : 0, v.time);
m_fmsAttached.SetValue(((controlWord & 0x10) != 0) ? 1 : 0, v.time);
m_dsAttached.SetValue(((controlWord & 0x20) != 0) ? 1 : 0, v.time);
if (v.value.size() != sizeof(uint64_t)) {
continue;
}
uint64_t controlWord = wpi::util::support::endian::read64le(v.value.data());
// See wpi::Struct<HAL_ControlWord> definition
m_enabled.SetValue(
((controlWord & HAL_CONTROLWORD_ENABLED_MASK) != 0) ? 1 : 0, v.time);
m_robotMode.SetValue((controlWord & HAL_CONTROLWORD_ROBOT_MODE_MASK) >>
HAL_CONTROLWORD_ROBOT_MODE_SHIFT,
v.time);
m_estop.SetValue(((controlWord & HAL_CONTROLWORD_ESTOP_MASK) != 0) ? 1 : 0,
v.time);
m_fmsAttached.SetValue(
((controlWord & HAL_CONTROLWORD_FMS_ATTACHED_MASK) != 0) ? 1 : 0,
v.time);
m_dsAttached.SetValue(
((controlWord & HAL_CONTROLWORD_DS_ATTACHED_MASK) != 0) ? 1 : 0,
v.time);
}
for (auto&& v : m_gameSpecificMessage.ReadQueue()) {
m_gameSpecificMessageData.SetValue(std::move(v.value), v.time);

View File

@@ -11,6 +11,7 @@
#include "wpi/nt/BooleanTopic.hpp"
#include "wpi/nt/IntegerTopic.hpp"
#include "wpi/nt/NetworkTableInstance.hpp"
#include "wpi/nt/RawTopic.hpp"
#include "wpi/nt/StringTopic.hpp"
namespace wpi::glass {
@@ -32,8 +33,7 @@ class NTFMSModel : public FMSModel {
DoubleSource* GetMatchTimeData() override { return nullptr; }
BooleanSource* GetEStopData() override { return &m_estop; }
BooleanSource* GetEnabledData() override { return &m_enabled; }
BooleanSource* GetTestData() override { return &m_test; }
BooleanSource* GetAutonomousData() override { return &m_autonomous; }
IntegerSource* GetRobotModeData() override { return &m_robotMode; }
StringSource* GetGameSpecificMessageData() override {
return &m_gameSpecificMessageData;
}
@@ -45,8 +45,7 @@ class NTFMSModel : public FMSModel {
void SetMatchTime(double val) override {}
void SetEStop(bool val) override {}
void SetEnabled(bool val) override {}
void SetTest(bool val) override {}
void SetAutonomous(bool val) override {}
void SetRobotMode(RobotMode val) override {}
void SetGameSpecificMessage(std::string_view val) override {}
void Update() override;
@@ -58,15 +57,14 @@ class NTFMSModel : public FMSModel {
wpi::nt::StringSubscriber m_gameSpecificMessage;
wpi::nt::BooleanSubscriber m_alliance;
wpi::nt::IntegerSubscriber m_station;
wpi::nt::IntegerSubscriber m_controlWord;
wpi::nt::RawSubscriber m_controlWord;
BooleanSource m_fmsAttached;
BooleanSource m_dsAttached;
IntegerSource m_allianceStationId;
BooleanSource m_estop;
BooleanSource m_enabled;
BooleanSource m_test;
BooleanSource m_autonomous;
IntegerSource m_robotMode;
StringSource m_gameSpecificMessageData;
};