OpMode Structure
What is an OpMode?
An OpMode (Operation Mode) is a Java class that controls your robot during a match. OpModes define the robot's behavior for autonomous or teleop periods and are selected and started from the Driver Station app.
See: FTC Docs: Creating and Running an Op Mode (OnBot Java)
See: FTC Docs: Creating and Running an Op Mode (OnBot Java)
Types of OpModes
OpModes can be:
@TeleOp: For driver-controlled periods (TeleOp)@Autonomous: For autonomous periods (Autonomous)- LinearOpMode: Runs code sequentially (most common for beginners)
- Iterative OpMode: Runs code in a loop (advanced)
OpMode Annotations
Annotations tell the SDK how to display your OpMode in the Driver Station.
-
-
-
-
@TeleOp(name=...) — TeleOp mode-
@Autonomous(name=...) — Autonomous mode-
@Disabled — Hide this OpMode<Sample LinearOpMode
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.Servo;
@TeleOp(name = "MyFIRSTJavaOpMode")
public class MyFIRSTJavaOpMode extends LinearOpMode {
private DcMotor motorTest;
private Servo servoTest;
@Override
public void runOpMode() {
motorTest = hardwareMap.get(DcMotor.class, "motorTest");
servoTest = hardwareMap.get(Servo.class, "servoTest");
telemetry.addData("Status", "Initialized");
telemetry.update();
waitForStart();
while (opModeIsActive()) {
telemetry.addData("Status", "Running");
telemetry.update();
}
}
}
// Source: https://ftc-docs.firstinspires.org/en/latest/programming_resources/tutorial_specific/onbot_java/creating_op_modes/Creating-and-Running-an-Op-Mode-(OnBot-Java).htmlHardware Initialization Example
private void initHardware() {
motorTest = hardwareMap.get(DcMotor.class, "motorTest");
servoTest = hardwareMap.get(Servo.class, "servoTest");
}
// Call this method at the start of runOpMode() for clarity.Organizing Your OpModes
- Use clear method names for hardware initialization, input processing, and telemetry.
- Separate hardware mapping from robot logic.
- Use comments to explain each section.
- Keep OpModes focused on a single purpose.
- Separate hardware mapping from robot logic.
- Use comments to explain each section.
- Keep OpModes focused on a single purpose.
Best Practices
For clean, reliable OpModes:
- Initialize hardware in a dedicated method.
- Use meaningful variable and method names.
- Add telemetry for debugging.
- Handle errors gracefully.
- Test OpModes on the robot.
Further Reading & Resources
- FTC Docs: Creating an OpMode
- FTC Docs: OnBot Java Reference Info
- FTC OpMode API (Javadoc)
- FTC SDK Example OpModes
- FTC Docs: OnBot Java Reference Info
- FTC OpMode API (Javadoc)
- FTC SDK Example OpModes
Next Steps
Practice: OpMode Structure
Try these to reinforce your understanding:
- Create a TeleOp OpMode that drives a motor with the left stick.
- Add telemetry to show motor power.
- Use
@Autonomousto create a simple autonomous OpMode. - Organize your code into methods for init, loop, and telemetry.