Skip to content

Writing Modules & Commands

This guide will teach you how to create advanced modules, use submodules, and write custom commands, including command factories.

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!
}

Commands are reusable actions. You can define them as standalone classes or use helper functions.

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 }
}

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
}
}
}