Writing Modules & Commands
This guide will teach you how to create advanced modules, use submodules, and write custom commands, including command factories.
Modules & Submodules
Section titled “Modules & Submodules”Modules can contain other modules, creating a hierarchy. This is useful for grouping related systems. For example, a Robot module can contain Intake, Outtake, and Drivetrain modules.
object Robot : Module(Intake, Outtake, Drivetrain) { // Robot module manages Intake, Outtake, and Drivetrain // When you register Robot, all submodules are automatically registered!}public class Robot extends Module { public static final Robot INSTANCE = new Robot();
public Robot() { super(Intake.INSTANCE, Outtake.INSTANCE, Drivetrain.INSTANCE); }}Writing Commands
Section titled “Writing Commands”Commands are reusable actions. You can define them as standalone classes or use helper functions.
Basic Commands
Section titled “Basic Commands”You can write simple commands directly inside your modules.
object Intake : Module() { // ... (motor setup)
val absorb = instant { motor.power = 1.0 } val eject = instant { motor.power = -1.0 } val stop = instant { motor.power = 0.0 }}public class Intake extends Module { // ... (motor setup)
public final InstantCommand absorb = new InstantCommand(() -> motor.setPower(1.0)); public final InstantCommand eject = new InstantCommand(() -> motor.setPower(-1.0)); public final InstantCommand stop = new InstantCommand(() -> motor.setPower(0.0));}Command Factories
Section titled “Command Factories”Sometimes you need commands that take parameters, like “Go to position X”. Instead of creating a new command class for every possible position, you can use a Command Factory.
A command factory is simply a method that returns a new Command instance configured with the given parameters.
object Lift : Module() { // ... (motor setup)
fun goToPosition(targetPosition: Int): Command { return instant { motor.targetPosition = targetPosition motor.mode = DcMotor.RunMode.RUN_TO_POSITION motor.power = 1.0 } }}public class Lift extends Module { // ... (motor setup)
public Command goToPosition(int targetPosition) { return new InstantCommand(() -> { motor.setTargetPosition(targetPosition); motor.setMode(DcMotor.RunMode.RUN_TO_POSITION); motor.setPower(1.0); }); }}