Skip to main content

Lesson 6.1: Keeping a LinearOpMode Safe with while(opModeIsActive())


Technical Context

In a LinearOpMode, failure to check the status of the Robot Controller (RC) can lead to runaway hardware. If a programmer initiates a loop without a safety check, the robot may continue to execute commands even after the "Stop" button is pressed on the Driver Station, potentially causing mechanical damage or safety violations.


Why opModeIsActive() Is Your Safety Check

The opModeIsActive() method is a member of the LinearOpMode class that returns a boolean value. It serves as a listener that monitors the match lifecycle. While true, the program continues to execute logic; if it returns false (due to a stop request or loss of communication), the loop terminates immediately, ensuring the Control Hub ceases command transmission to the DcMotors.

This is in contrast to the standard OpMode you have used in previous units. A LinearOpMode uses a single runOpMode() method where you write all your logic sequentially, wrapped in this safety loop.


Annotated Code

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;

@Autonomous(name="Safe_Loop_Demo")
public class SafeLoopDemo extends LinearOpMode {

@Override
public void runOpMode() {
// Any hardware mapping would go here, before waitForStart()

waitForStart(); // Pause execution until the driver presses Play

// Main execution safeguard — loop exits the instant Stop is pressed
while (opModeIsActive()) {
telemetry.addData("Status", "Running Safe");
telemetry.update(); // Manual update required in LinearOpMode
}
}
}

Fill-in-the-Blank Practice

  1. The opModeIsActive() method returns a __________ data type to determine if the program should continue.
  2. Unlike a standard OpMode where the loop() method is called automatically, a LinearOpMode requires a __________ loop to maintain execution.
  3. To prevent code from executing before the match begins, you must invoke the __________ method before entering your main loop.
Show answers
  1. boolean
  2. while
  3. waitForStart()

Template Challenge

Robot Scenario: You are writing a safety-first autonomous program. The robot must rotate a motor named "spinner" at 40% power, but only as long as the match is active and the hardware is responding.

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="Safety_Spin")
public class SafetySpin extends LinearOpMode {

@Override
public void runOpMode() {
DcMotor spinner = hardwareMap.get(DcMotor.class, "spinner");

waitForStart();

// INSERT CODE HERE: Create a safeguard loop using opModeIsActive()
{
// INSERT CODE HERE: Command spinner to 0.4 power
telemetry.update();
}

spinner.setPower(0); // Safe stop after loop exits
}
}
Show answer
waitForStart();

while (opModeIsActive()) {
spinner.setPower(0.4);
telemetry.addData("Spinner", "Running at 40%");
telemetry.update();
}

spinner.setPower(0);

Ready to move on?

Sign in with Google to save your progress with Telemark, or continue without saving.