Skip to content

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.

A Command is defined by three main lifecycle phases:

  1. onStart: Executed once when the command begins.
  2. onLoop: Executed repeatedly while the command is active.
  3. 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 run
myCommand.register()
// OR simply
myCommand()

Executes a single action and finishes immediately.

instant { servo.position = 1.0 }

Runs for a specified duration.

withTiming(2.0, // seconds
onStart = { motor.power = 0.5 },
onStop = { motor.power = 0.0 }
)

Does nothing for a specified duration. Useful in sequences.

wait(1.5) // seconds
// OR
wait(1500L) // milliseconds

Waits until a condition is met.

waitUntil { sensor.isPressed }

Command groups allow you to combine multiple commands into complex behaviors.

Runs commands one after another.

sequentially(
instant { closeClaw() },
wait(0.5),
withTiming(2.0, onStart = { liftUp() })
)

Runs commands simultaneously. Finishes when all commands finish.

inParallel(
withTiming(2.0, onStart = { driveForward() }),
withTiming(1.0, onStart = { raiseLift() })
)

Runs commands simultaneously. Finishes when any command finishes (cancelling the others).

racing(
DriveToTarget(),
wait(5.0) // Timeout after 5 seconds
)

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()
)

Dynamically selects a command to run.

select {
when (visionResult) {
LEFT -> GoLeft()
RIGHT -> GoRight()
else -> wait(1.0)
}
}