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:

// 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:

Last updated

Was this helpful?