mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[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:
31
wpilibcExamples/src/main/cpp/templates/opmode/cpp/Robot.cpp
Normal file
31
wpilibcExamples/src/main/cpp/templates/opmode/cpp/Robot.cpp
Normal 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
|
||||
@@ -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). */
|
||||
}
|
||||
@@ -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). */
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user