> For the complete documentation index, see [llms.txt](https://docs.raidrobotics.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.raidrobotics.org/commands-v3-state-machine/concepts/opmodes.md).

# Opmodes

WPILib 2027 replaces the old "one `robotInit`, one `teleopInit`, one `autonomousInit`" structure with **opmodes**. An opmode is one thing the robot can do (a teleop, an auto, a test routine), and the Driver Station shows a list of them to pick from.

## How opmodes are found

`Robot` extends `OpModeRobot`. When the program starts, WPILib looks in the `first.robot.opmodes` package for every class marked with one of these annotations, and lists it on the Driver Station:

| Annotation                  | Listed under | In the template                                                                |
| --------------------------- | ------------ | ------------------------------------------------------------------------------ |
| `@Teleop(name = "...")`     | Teleop       | `DriveTeleop` ("Drive")                                                        |
| `@Autonomous(name = "...")` | Autonomous   | `ExampleAuto` ("Example: Score, Fetch, Score"), `DoNothingAuto` ("Do Nothing") |
| `@Utility(name = "...")`    | Utility      | `SysIdUtility` ("SysId Drivetrain")                                            |

The `name` is what the drive team sees, so make it say what the opmode does.

## The smallest opmode

An opmode is a class that implements `OpMode` and has a constructor that takes the `Robot`:

{% code title="DoNothingAuto.java" %}

```java
@Autonomous(name = "Do Nothing")
public class DoNothingAuto implements OpMode {
    public DoNothingAuto(Robot robot) {}
}
```

{% endcode %}

That's all it takes. No other file needs to know the opmode exists.

{% hint style="info" %}
Always keep `DoNothingAuto`. It's the safe choice when the robot is broken, or when an alliance partner needs you out of the way. With nothing set up, the drivetrain idles and no state machine runs, so every mechanism stays where it is.
{% endhint %}

## What goes in an opmode, and what goes in `Robot`

An opmode object is created when it's **picked** on the Driver Station. The buttons and commands it sets up in its constructor only work while it's picked. Anything set up in the `Robot` constructor works all the time, whichever opmode is picked.

| Put it in... | When it...                                   | Examples                                                                                  |
| ------------ | -------------------------------------------- | ----------------------------------------------------------------------------------------- |
| `Robot.java` | Is hardware, or must be true in every opmode | The drivetrain, the superstructure, the controller, "idle the drivetrain while disabled"  |
| An opmode    | Belongs to one way of running the robot      | Button bindings, the default drive command, an auto's routine, starting the state machine |

So the hardware is made **once**, in `Robot`, and every opmode reaches it through the `robot` parameter:

{% code title="DriveTeleop.java" %}

```java
public DriveTeleop(Robot robot) {
    final CommandSwerveDrivetrain drivetrain = robot.drivetrain;
    final CommandXboxController controller = robot.controller;
    final Superstructure superstructure = robot.superstructure;
    ...
}
```

{% endcode %}

{% hint style="warning" %}
Never create hardware (a `TalonFX`, a `DigitalInput`, a mechanism) inside an opmode. Opmodes are created every time they're picked, so the device would be created again each time. Hardware belongs in `Robot` (or in a mechanism that `Robot` creates), so there's exactly one of each.
{% endhint %}

## Starting things when the opmode is enabled

Picking an opmode doesn't enable it. To run a command only while the opmode is actually enabled, bind it to a robot mode trigger:

```java
// The state machine runs the whole time teleop is enabled, and starts from IDLE
RobotModeTriggers.teleop().whileTrue(superstructure.createStateMachine());
```

Both `DriveTeleop` and `ExampleAuto` start the superstructure's state machine this way. Each builds its own, so the state machine starts over from IDLE every time the robot is enabled.

## Utility opmodes

`SysIdUtility` is a **utility** opmode. It holds the drivetrain characterization bindings, which used to live in teleop in older projects. As its own opmode, it can't be triggered by accident while driving. See [Drivetrain](https://docs.raidrobotics.org/commands-v3-state-machine/guide/drivetrain.md#characterization-sysid).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.raidrobotics.org/commands-v3-state-machine/concepts/opmodes.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
