Commands
Commands are reusable units of behavior in CoreControl. They encapsulate logic that can be started, executed over time, and stopped. Commands are the primary way to define robot actions, from simple motor movements to complex autonomous sequences.
Overview
Section titled “Overview”A Command is defined by three main lifecycle phases:
- onStart: Executed once when the command begins.
- onLoop: Executed repeatedly while the command is active.
- onStop: Executed once when the command finishes or is interrupted.
A command runs until its shouldStop condition returns true.
You can create commands by instantiating the Command class or using the lifecycle helper function.
// Using the helper function (Recommended)val myCommand = lifecycle( onStart = { motor.power = 1.0 }, onLoop = { /* continuous updates */ }, onStop = { motor.power = 0.0 }, shouldStop = { sensor.distance < 10.0 })
// Registering the command to runmyCommand.register()// OR simplymyCommand()// Using the helper function (Recommended)Command myCommand = lifecycle( () -> motor.setPower(1.0), // onStart () -> {}, // onLoop () -> motor.setPower(0.0), // onStop () -> sensor.getDistance() < 10.0 // shouldStop);
// Registering the command to runmyCommand.register();// OR simplymyCommand.start();Basic Commands
Section titled “Basic Commands”InstantCommand
Section titled “InstantCommand”Executes a single action and finishes immediately.
instant { servo.position = 1.0 }instant(() -> servo.setPosition(1.0));TimedCommand
Section titled “TimedCommand”Runs for a specified duration.
withTiming(2.0, // seconds onStart = { motor.power = 0.5 }, onStop = { motor.power = 0.0 })withTiming(2.0, // seconds () -> motor.setPower(0.5), () -> {}, () -> motor.setPower(0.0));WaitCommand
Section titled “WaitCommand”Does nothing for a specified duration. Useful in sequences.
wait(1.5) // seconds// ORwait(1500L) // millisecondswait(1.5); // seconds// ORwait(1500L); // millisecondsWaitUntilCommand
Section titled “WaitUntilCommand”Waits until a condition is met.
waitUntil { sensor.isPressed }waitUntil(() -> sensor.isPressed());Command Groups
Section titled “Command Groups”Command groups allow you to combine multiple commands into complex behaviors.
SequentialCommandGroup
Section titled “SequentialCommandGroup”Runs commands one after another.
sequentially( instant { closeClaw() }, wait(0.5), withTiming(2.0, onStart = { liftUp() }))sequentially( instant(() -> closeClaw()), wait(0.5), withTiming(2.0, () -> liftUp()));ParallelCommandGroup
Section titled “ParallelCommandGroup”Runs commands simultaneously. Finishes when all commands finish.
inParallel( withTiming(2.0, onStart = { driveForward() }), withTiming(1.0, onStart = { raiseLift() }))inParallel( withTiming(2.0, () -> driveForward()), withTiming(1.0, () -> raiseLift()));RacingCommandGroup
Section titled “RacingCommandGroup”Runs commands simultaneously. Finishes when any command finishes (cancelling the others).
racing( DriveToTarget(), wait(5.0) // Timeout after 5 seconds)racing( new DriveToTarget(), wait(5.0) // Timeout after 5 seconds);ConditionalCommandGroup
Section titled “ConditionalCommandGroup”Runs one of two commands based on a condition checked at runtime.
chooseOne( { sensor.hasObject }, ScoreObject(), // If true SearchForObject() // If false)
// OR
runIf( { sensor.isTooClose }, StopMoving())chooseOne( () -> sensor.hasObject(), new ScoreObject(), // If true new SearchForObject() // If false);
// OR
runIf( () -> sensor.isTooClose(), new StopMoving());SelectCommandGroup
Section titled “SelectCommandGroup”Dynamically selects a command to run.
select { when (visionResult) { LEFT -> GoLeft() RIGHT -> GoRight() else -> wait(1.0) }}select(() -> { switch (visionResult) { case LEFT: return new GoLeft(); case RIGHT: return new GoRight(); default: return wait(1.0); }});