RobotContainer & Command Groups
Using a RobotContainer
Section titled “Using a RobotContainer”If your project has a RobotContainer (a class that creates your subsystems), it is a good place to register named commands. It already has the subsystems the commands need, and every OpMode can share it.
public class RobotContainer {
private final Shooter shooter; private final Intake intake;
public RobotContainer(HardwareMap hardwareMap) { shooter = new Shooter(hardwareMap); // your own classes intake = new Intake(hardwareMap); registerNamedCommands(); }
private void registerNamedCommands() { NamedCommands.registerCommand("SpinUp", () -> shooter.spinUp()); NamedCommands.registerCommand("Intake", () -> intake.start()); }}class RobotContainer(hardwareMap: HardwareMap) {
private val shooter = Shooter(hardwareMap) // your own classes private val intake = Intake(hardwareMap)
init { registerNamedCommands() }
private fun registerNamedCommands() { NamedCommands.registerCommand("SpinUp") { shooter.spinUp() } NamedCommands.registerCommand("Intake") { intake.start() } }}Then create the container before the generated class:
-
For a command-based export, see Sequential Command Exports.
-
For an OpMode export, point the Command Registrar at a method that creates the container:
public class RobotCommands {public static void register(HardwareMap hardwareMap) {new RobotContainer(hardwareMap);}}
Any project layout works as long as the commands are registered before the generated code runs.
Commands that are more than a method call
Section titled “Commands that are more than a method call”A registered command is a plain Runnable, so it can start a command from your command library. Schedule it rather than calling it directly, because a library command runs over many loops:
NamedCommands.registerCommand("Shoot", () -> CommandScheduler.getInstance().schedule(new ShootCommand(shooter)));NamedCommands.registerCommand("Shoot", () -> new ShootCommand(shooter).schedule());NamedCommands.registerCommand("Shoot", () -> shoot(shooter).schedule() // shoot(...) returns an Ivy Command you built);The Runnable runs on the robot's loop thread when the marker fires. It should start something and return. Don't sleep or wait inside it.
Sequential Command Exports
Section titled “Sequential Command Exports”A Sequential Command export is a class you create and schedule yourself (SolversLib, NextFTC, or Ivy). It has no init(), and the Command Registrar isn't used. Instead, register your commands in your own code before you create the class. In the examples below, creating the RobotContainer from above does that.
This applies to both Embed Pose Data settings. They differ only in when the generated code asks for each command (see When to Register), and registering first covers both.
@Autonomous(name = "Red Auto")public class RedAuto extends CommandOpMode {
@Override public void initialize() { Follower follower = /* however your project creates it */; RobotContainer robot = new RobotContainer(hardwareMap); // 1. registers the named commands
try { schedule(new AutoPath(follower, hardwareMap, telemetry)); // 2. the generated class } catch (IOException e) { throw new RuntimeException(e); } }}@Autonomous(name = "Red Auto")public class RedAuto extends NextFTCOpMode {
private Follower follower; // however your project creates it
@Override public void onInit() { new RobotContainer(hardwareMap); // 1. registers the named commands }
@Override public void onStartButtonPressed() { try { new AutoPath(follower, hardwareMap).schedule(); // 2. the generated class } catch (IOException e) { throw new RuntimeException(e); } }}@Autonomous(name = "Red Auto")public class RedAuto extends OpMode {
private Follower follower; // however your project creates it
@Override public void init() { Scheduler.reset(); new RobotContainer(hardwareMap); // 1. registers the named commands
try { new AutoPath(follower, hardwareMap).command().schedule(); // 2. the generated class } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void loop() { follower.update(); Scheduler.execute(); }}AutoPath is the class Turtle Tracer generated, named by Class Name in the export dialog. The OpMode around it depends on your library and version, so treat these as outlines. Only the order is fixed: register first, then create the generated class. For the library setup around it, see Getting Started and Exporting Code.
What the generated class does with a name
Section titled “What the generated class does with a name”With Embed Pose Data off, the constructor binds each marker name to its command once:
pp = new TurtleTracerReader("AutoPath.turt", hw.appContext);pp.onEvent("SpinUp", NamedCommands.getCommand("SpinUp")).onEvent( "Intake", NamedCommands.getCommand("Intake"));A command registered after this line is not picked up, because getCommand already returned the "unregistered" no-op. With Embed Pose Data on, the lookup happens just before each path runs:
tracker.clearPathEvents();tracker.onParametric(0, 0.400, NamedCommands.getCommand("SpinUp"));tracker.setCurrentPath(startPointTODriveToShoot);Either way, each path registers only its own markers, so SpinUp fires on the path where you placed it, even if a later path reuses the name.