[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

@@ -0,0 +1,31 @@
// 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 "Robot.hpp"
#include "opmode/MyAuto.hpp"
#include "opmode/MyTeleop.hpp"
Robot::Robot() {
// Add opmodes to the robot here.
AddOpMode<MyTeleop>(wpi::RobotMode::TELEOPERATED, "My Teleop", "",
"An example teleop opmode");
AddOpMode<MyAuto>(wpi::RobotMode::AUTONOMOUS, "My Auto", "");
PublishOpModes();
}
/** This function is called exactly once when the DS first connects. */
void Robot::DriverStationConnected() {}
/**
* This function is called periodically anytime when no opmode is selected,
* including when the Driver Station is disconnected.
*/
void Robot::NonePeriodic() {}
#ifndef RUNNING_WPILIB_TESTS
int main() {
return wpi::StartRobot<Robot>();
}
#endif

View File

@@ -0,0 +1,33 @@
// 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 "opmode/MyAuto.hpp"
#include "Robot.hpp"
/** The Robot instance is passed into the opmode via the constructor. */
MyAuto::MyAuto(Robot& robot) : m_robot{robot} {
/*
* Can call the base class constructor with the period to set a different
* periodic time interval.
*
* Additional periodic methods may be configured with AddPeriodic().
*/
}
MyAuto::~MyAuto() {
/* Called when the opmode is de-selected. */
}
void MyAuto::Start() {
/* Called once when the robot is first enabled. */
}
void MyAuto::Periodic() {
/* Called periodically (set time interval) while the robot is enabled. */
}
void MyAuto::End() {
/* Called when the robot is disabled (after previously being enabled). */
}

View File

@@ -0,0 +1,26 @@
// 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 "opmode/MyTeleop.hpp"
#include "Robot.hpp"
/** The Robot instance is passed into the opmode via the constructor. */
MyTeleop::MyTeleop(Robot& robot) : m_robot{robot} {}
MyTeleop::~MyTeleop() {
/* Called when the opmode is de-selected. */
}
void MyTeleop::Start() {
/* Called once when the robot is first enabled. */
}
void MyTeleop::Periodic() {
/* Called periodically (set time interval) while the robot is enabled. */
}
void MyTeleop::End() {
/* Called when the robot is disabled (after previously being enabled). */
}

View File

@@ -0,0 +1,14 @@
// 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 "wpi/framework/OpModeRobot.hpp"
class Robot : public wpi::OpModeRobot<Robot> {
public:
Robot();
void DriverStationConnected() override;
void NonePeriodic() override;
};

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
#include "wpi/opmode/PeriodicOpMode.hpp"
class Robot;
class MyAuto : public wpi::PeriodicOpMode {
public:
/** The Robot instance is passed into the opmode via the constructor. */
explicit MyAuto(Robot& robot);
~MyAuto() override;
void Start() override;
void Periodic() override;
void End() override;
private:
[[maybe_unused]]
Robot& m_robot;
};

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
#include "wpi/opmode/PeriodicOpMode.hpp"
class Robot;
class MyTeleop : public wpi::PeriodicOpMode {
public:
/** The Robot instance is passed into the opmode via the constructor. */
explicit MyTeleop(Robot& robot);
~MyTeleop() override;
void Start() override;
void Periodic() override;
void End() override;
private:
[[maybe_unused]]
Robot& m_robot;
};

View File

@@ -7,7 +7,6 @@
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/internal/DriverStationModeThread.hpp"
#include "wpi/nt/NetworkTable.hpp"
Robot::Robot() {}
@@ -20,7 +19,13 @@ void Robot::Teleop() {}
void Robot::Test() {}
void Robot::StartCompetition() {
wpi::internal::DriverStationModeThread modeThread;
wpi::internal::DriverStationModeThread modeThread{wpi::hal::GetControlWord()};
// Create an opmode per robot mode
wpi::DriverStation::AddOpMode(wpi::RobotMode::AUTONOMOUS, "Auto");
wpi::DriverStation::AddOpMode(wpi::RobotMode::TELEOPERATED, "Teleop");
wpi::DriverStation::AddOpMode(wpi::RobotMode::TEST, "Test");
wpi::DriverStation::PublishOpModes();
wpi::util::Event event{false, false};
wpi::DriverStation::ProvideRefreshedDataEventHandle(event.GetHandle());
@@ -29,31 +34,24 @@ void Robot::StartCompetition() {
HAL_ObserveUserProgramStarting();
while (!m_exit) {
modeThread.InControl(wpi::DriverStation::GetControlWord());
if (IsDisabled()) {
modeThread.InDisabled(true);
Disabled();
modeThread.InDisabled(false);
while (IsDisabled()) {
wpi::util::WaitForObject(event.GetHandle());
}
} else if (IsAutonomous()) {
modeThread.InAutonomous(true);
Autonomous();
modeThread.InAutonomous(false);
while (IsAutonomousEnabled()) {
wpi::util::WaitForObject(event.GetHandle());
}
} else if (IsTest()) {
modeThread.InTest(true);
Test();
modeThread.InTest(false);
while (IsTest() && IsEnabled()) {
wpi::util::WaitForObject(event.GetHandle());
}
} else {
modeThread.InTeleop(true);
Teleop();
modeThread.InTeleop(false);
while (IsTeleopEnabled()) {
wpi::util::WaitForObject(event.GetHandle());
}

View File

@@ -20,6 +20,16 @@
"gradlebase": "cpp",
"commandversion": 2
},
{
"name": "OpMode Robot",
"description": "OpMode style, with explanatory comments and example code.",
"tags": [
"OpMode"
],
"foldername": "opmode",
"gradlebase": "cpp",
"commandversion": 2
},
{
"name": "Timed Robot",
"description": "Timed style, with explanatory comments and example code.",