Lesson 6.3: Timing Robot Actions with getRuntime()
Technical Context
Standard sleep() commands block the entire thread, preventing the robot from reading sensor data or reacting to gamepad inputs during the sleep period. Comparing loop iterations against getRuntime() allows for "non-blocking" timing, enabling the robot to perform multiple hardware tasks simultaneously within the same loop cycle.
How getRuntime() Helps You Time Actions Without Guessing
The getRuntime() method returns a double representing the seconds elapsed since the OpMode was initialized. By capturing a "start time" timestamp and comparing it to the current getRuntime() value inside a while or if statement, the programmer creates a timed window for hardware to operate: without freezing any other logic.
The key pattern is: capture a target timestamp, then check against it each loop cycle.
double targetTime = getRuntime() + 2.0; // 2 seconds from now
The clock increments in sub-milliseconds, so you must use < (less than) rather than == for the comparison: the current time will almost never land on the exact target value.
Annotated Code
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.hardware.DcMotor;
@Autonomous(name="Timed_Drive_Demo")
public class TimedDriveDemo extends LinearOpMode {
@Override
public void runOpMode() {
DcMotor driveMotor = hardwareMap.get(DcMotor.class, "drive");
waitForStart();
// Set a timestamp 2 seconds in the future
double targetTime = getRuntime() + 2.0;
// Drive forward until the 2-second window closes
while (opModeIsActive() && getRuntime() < targetTime) {
driveMotor.setPower(0.5);
telemetry.addData("Time Remaining", targetTime - getRuntime());
telemetry.update();
}
driveMotor.setPower(0); // Stop cleanly after the timed move
}
}
Fill-in-the-Blank Practice
- The
getRuntime()method returns the time as a__________data type. - When comparing time, it is critical to use the
__________operator instead of==because the clock increments in sub-milliseconds. - To reset the internal clock to zero at the start of the match, use the
__________method.
Show answers
double<(less than)resetRuntime()
Simulator Challenge
Use the simulator below to practice non-blocking timing with getRuntime() inside a TeleOp-style loop().
A Team Checks Time and Returns
BunyipsLib's periodic task resets a timer once, checks whether its interval has passed, runs its action, and returns. Renamed for this lesson, the core looks like this:
if (!timerStarted) {
timer.reset();
timerStarted = true;
}
if (timer.milliseconds() >= intervalMs) {
runScheduledAction();
timer.reset();
}
The callback wrapper and zero-interval option were omitted. No line waits, so the OpMode can keep doing other work. Adapted from BunyipsLib's Periodic.java at a pinned commit under the MIT License.
Ready to move on?
Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.
Stuck on this lesson?
Ask about anything on this page. It can see which lesson you have open and which part you are reading.