# Lesson 2.?: run Command

Accompanying coding lesson: <https://github.com/icrobotics-team167/CotcCurriculum/tree/main/CommandBasics/run>

A `run` command takes in a `Runnable` and runs that every loop in `execute()`. This is used for Command actions that must run periodically, such as motor control loops.

Syntax example:

```java
// Example.java
public Command runCommand() {
  return run(() -> {
    // Do something every robot loop
  }
}

// Robot.java
var example = new Example(mode != mode.REPLAY ? new ExampleIOPhoenix() : new ExampleIO() {});

primary.a().whileTrue(example.runCommand());
```

The `run` method from the `Subsystem` class automatically declares the subsystem as a requirement, while the `run` method from the `Commands` class does not have requirements unless explicitly declared in the parameters.

Code examples:

* Kernel Overflow (Team 167, Reefscape)
  * [Swerve teleop drive](https://github.com/icrobotics-team167/2025_Reefscape/blob/230afa18a2fbffd82682add448cd1f2adc900073/src/main/java/frc/cotc/drive/Swerve.java#L352)
  * [Elevator position control](https://github.com/icrobotics-team167/2025_Reefscape/blob/230afa18a2fbffd82682add448cd1f2adc900073/src/main/java/frc/cotc/superstructure/Elevator.java#L118)
  * [Algae roller intaking](https://github.com/icrobotics-team167/2025_Reefscape/blob/230afa18a2fbffd82682add448cd1f2adc900073/src/main/java/frc/cotc/superstructure/AlgaeRollers.java#L28)


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.iowacityrobotics.org/programming/frc-programming/section-2-commands/lesson-2.-run-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
