Opmodes
What an opmode does from INIT to STOP, and where robot code goes in it.
An opmode
Opmodes are what the drivers pick on the Driver Station. With BearPlatform they extend CoroutineOpMode, and carry
FIRST’s @TeleOp or @Autonomous annotation as usual:
@TeleOp(name = "Driving")
class Driving : CoroutineOpMode() {
private val robot by robot(::MyRobot)
override suspend fun CoroutineScope.onInit() {
everyTick { show("Ready when you are") }
}
override suspend fun CoroutineScope.onStart() {
controller1.a.onPress { robot.claw.grab() }
everyTick { robot.drive.driveWith(controller1.leftStick, controller1.rightStick.x) }
}
override fun onStop() {
// after everything has stopped, rarely needed
}
}
val robot by robot(::MyRobot)declares the opmode’s robot. It is created at INIT, not with the opmode, so a robot that can’t be created or set up, like one whose motor is missing from the robot configuration, fails INIT with the reason on the Driver Station. Use it fromonInitandonStarton. For an app serving several robots,robot(TeamRobots)creates whichever robot this one is (see Robots and parts).onInitruns from INIT until START: for showing that the robot is ready, or INIT buttons of the opmode’s own. START stops it, along with every handler and every-loop block it set up. What the drivers pick before START, like the alliance, are choices.onStartruns from START until the opmode ends. An autonomous puts its steps here; a TeleOp sets up its buttons and its every-loop driving.onStopruns at the very end, after robot code and the robot’s parts have stopped.
Both onInit and onStart are coroutines: they can wait, and what they set up runs alongside them. See
Robot code over time.
INIT
Pressing INIT on the Driver Station:
- creates the robot and its parts;
- loads the robot’s settings (see Settings). A value that can’t be read,
like
drive.maxPower = fast, fails INIT and lists every such value, rather than failing in the middle of a match; - initializes the parts: their
init()sets up the hardware; - picks the remembered options of the opmode’s choices and shows them;
- starts
onInit, and loops until START.
Until START, the Driver Station also shows what needs attention, a line each marked ⚠ in amber, below the choices: settings
nobody uses (often typos, with a “did you mean” hint), warnings from the robot’s parts, and a battery below
battery.low (12 V). The log tells more about each.
Every loop
From INIT to STOP, the opmode loops, and every loop:
- reads the gamepads, digital sensors and the newest readings of slow sensors, so everything in the loop sees the same state;
- updates the robot’s parts, parents before their parts, in the order they were created;
- runs robot code that is due: button handlers, every-loop blocks, actions;
- sends what was shown to the Driver Station and the robot’s graphs page.
A loop that takes less than loop.minTime (5 ms) sleeps for the rest: looping faster than sensors change only takes
time from the hubs. From START, the first lines on the Driver Station tell how long loops take, like
Loop: 2.4 ms average, 6.1 ms longest, and how the battery is doing. Performance
explains what makes loops slow.
STOP
When the opmode ends, by STOP on the Driver Station or the end of autonomous, robot code is stopped and runs its
cleanup (finally blocks), then the robot’s parts stop: motors and servos the platform provides stop by themselves.
Then onStop runs.
The robot’s pose carries over to the next opmode: TeleOp starts where autonomous ended (see Localization).
Showing what happens
Opmodes show lines on the Driver Station, record values for the live graphs, write to the log, and offer commands that start actions from a laptop:
everyTick {
show("To the goal") { robot.localizer.pose.distanceTo(GOAL) } // computed only when lines are sent
graph("shooter rpm", robot.shooter.rpm)
}
command("forward 60 cm") { robot.follower.driveTo(Pose(60.cm, 0.cm, 0.degrees)) }
Telemetry has all of it.
Choices in INIT
Some things the drivers pick on the Driver Station before START: the alliance, where the robot starts, which autonomous routine runs. Each is a property of the opmode, whose value is the picked option’s:
@Autonomous(name = "Auto", preselectTeleOp = "Driving")
class Auto : CoroutineOpMode() {
private val robot by robot(::MyRobot)
private val alliance by choice(Alliance.RED, Alliance.BLUE)
private val start by choice("near goal" to NEAR_GOAL, "far" to FAR)
private val wait by choice(0.seconds, 3.seconds, 5.seconds)
private val routine by choice<suspend () -> Unit>(
"preload" to { robot.scorePreload() },
"3 cycles" to { robot.cycles(3) },
)
override suspend fun CoroutineScope.onStart() {
robot.localizer.pose = Field2026.forAlliance(start, alliance)
delay(wait)
routine()
}
}
INIT shows a line for each choice, called by its property’s name in words (startPose is Start pose):
▸ Alliance: ◂ RED ▸
Start: near goal
Wait: 0s
Routine: preload
They are the Driver Station’s first lines, so they stay in place whatever else is shown. Gamepad 1’s dpad up and down choose a line, marked ▸, and left and right change it. The menu reads only the dpad, so A, B, X and Y stay free for the opmode’s own INIT buttons. Nothing needs confirming: START takes what is picked, and the robot’s log lists it.
Options are named values, like "near goal" to NEAR_GOAL, or values named the way the Driver Station shows them, like
Alliance.RED, 3.seconds, or an enum whose toString() says what each is.
Remembered
Picks are kept on the robot, through restarts and power cycles, and the next run starts with them. They are
remembered by the choice’s name, and opmodes with a choice of the same name share it: TeleOp’s alliance starts as
autonomous picked it. So one TeleOp serves both alliances, and FIRST’s preselectTeleOp queues it after autonomous:
@TeleOp(name = "Driving")
class Driving : CoroutineOpMode() {
private val robot by robot(::MyRobot)
private val alliance by choice(Alliance.RED, Alliance.BLUE)
private val start by choice<Pose?>("where autonomous ended" to null, "center" to Pose.ZERO, remember = false)
override suspend fun CoroutineScope.onStart() {
start?.let { robot.localizer.pose = it }
val driversFace = Field2026.driversFace(alliance)
everyTick {
robot.drive.driveWith(controller1.leftStick, controller1.rightStick.x, robot.localizer.pose.heading, driversFace)
}
}
}
A choice with remember = false starts at its first option every run: this TeleOp keeps the pose autonomous left,
unless the drivers pick where they put the robot down. The season’s field (here Field2026) converts poses for the
alliance and knows where its drivers stand; see Units and geometry.
Remembering has its price: an untouched menu runs with the last run’s picks, so check the alliance before START.
Showing picks on the robot
Until START, code in onInit sees each pick as it changes, so the robot can show it, like the alliance on its
lights:
override suspend fun CoroutineScope.onInit() {
everyTick { robot.alliance = alliance } // the robot's update() shows it on its lights
}
How, and whether, is the robot’s own business: its lights and the colors they can show are its own.