Lesson 2.3: Running Real-Time Robot Logic Inside loop()
Technical Context
The loop() method controls the robot's performance during the match. Because it runs at a high frequency (approximately 50 times per second), it allows for smooth movement and rapid sensor polling. Efficiency in this method is critical, any "blocking" code (like a sleep command) will freeze the robot and could cause a communication timeout with the Driver Station.
The loop() Lifecycle
In an OpMode, the loop() method is executed repeatedly after the driver presses the "Play" button and until they press "Stop". It usually follows a sensing → thinking → acting cycle: it reads the latest values from the Gamepads or sensors, calculates the necessary power adjustments, and commands the Control Hub's motor controllers to update voltage outputs.
Since loop() is non-blocking, it is ideal for TeleOp where driver inputs are changing in real-time.
Use the FTC SDK Javadocs when you want to confirm the exact method names and inheritance chain for OpModes: FTC RobotCore Javadocs.
If telemetry says the OpMode is "Initialized" but robot controls do nothing, ask which lifecycle method owns the code. Hardware mapping belongs in init(), live driver logic belongs in loop(), and one-time cleanup belongs in stop().
The SDK exposes gamepad objects to either OpMode style, but an Autonomous routine is expected to run from its programmed logic after Start. Use sensors, timers, and state transitions for match autonomous behavior rather than requiring driver input.
Annotated Code
@TeleOp(name="Loop_Demo")
public class LoopDemo extends OpMode {
@Override
public void init() {
telemetry.addData("Status", "Ready to Play");
}
@Override
public void loop() {
// This logic runs ~50x a second to check for driver input
double drivePower = -gamepad1.left_stick_y;
// Streaming hardware data back to the DS for real-time monitoring
telemetry.addData("Motor Command", drivePower);
telemetry.addData("Cycle Time", getRuntime());
}
}
Fill-in-the-Blank Practice
- The
loop()method runs repeatedly at a frequency of approximately__________times per second. - If you put a long delay inside
loop(), you risk losing connection to the__________Station. - The robot stops executing the
loop()method once the driver presses the__________button.
Show answers
- 50
- Driver Station
- Stop
Simulator Challenge
You need to create a simple diagnostic that shows the status of the "A" button on gamepad1 during the entire match.
Show answer
@Override
public void loop() {
telemetry.addData("Button A", gamepad1.a);
telemetry.update();
}
A Team Loop Keeps Coordination Short
Titan Robotics Club's TeleOp passes driver input to its robot layer during each periodic call. Here is that pattern renamed to match this lesson:
public void loop() {
robot.driveWith(gamepad1);
robot.updateStatus();
}
The framework callback and dashboard details were removed. The two remaining calls show the lesson's point: read controls and update the robot once per loop. Adapted from Team 3543 Titan Robotics Club's FtcTeleOp.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.