Skip to content

4. Create an Auto Command

In this tutorial, we will learn how to create an auto command. Auto commands are used to implement autonomous behaviors for the robot, allowing it to execute complex sequences of actions automatically under specific conditions. We will create a PickCoral command to automatically pick up corals and a ScoreCoral command to automatically score corals. Code download link

The main differences between auto commands and teleoperated commands are:

  • Condition-based Triggering: Auto commands execute and terminate automatically based on specific conditions.
  • State Management: Precise management of subsystem state transitions is required.
  • Completion Detection: Sensors are used to determine when the command is complete.

Example

Let’s create a PickCoral auto command that automatically controls the intake mechanism to pick up corals:

java
public class PickCoral extends Command {
  private final Intake intake;

  public PickCoral(Intake intake) {
    this.intake = intake;
    addRequirements(intake);
  }

  @Override
  public void initialize() {
    intake.setPivotDegree(90);
    intake.setRollerVoltage(-1);
  }

  @Override
  public void execute() {
  }

  @Override
  public void end(boolean interrupted) {
    intake.setRollerVoltage(0);
    intake.setPivotDegree(0);
  }

  @Override
  public boolean isFinished() {
    return intake.hasCoral();
  }
}

Your Turn

Now you need to create a ScoreCoral auto command that will control the intake mechanism to automatically score corals:

java
public class ScoreCoral extends Command {
  private final Intake intake;

  public ScoreCoral(Intake intake) {
    this.intake = intake;
    addRequirements(intake);
  }

  @Override
  public void initialize() {
    // TODO: set the intake angle
  }

  @Override
  public void execute() {
    // TODO: wait for intake to arrive before starting the roller
  }

  @Override
  public void end(boolean interrupted) {
    // TODO: reset intake and stop the roller
  }

  @Override
  public boolean isFinished() {
    // TODO: if the roller scores the coral
    return false;
  }
}

Demonstration

Trigger the auto pick command with the X key: the intake lowers -> the roller pulls in -> the intake retracts automatically after detecting an object. Trigger the auto score command with the B key: the intake lowers -> the roller pushes out -> the intake resets automatically after detecting the object is scored.