There are two ways to create OpModes using the FTC SDK: OpMode and LinearOpMode. The one that you pick determines the structure of your code. Any OpMode should be able to be created using either method. The method you use is up to your preference. An equivalent example will be shown for each method.
Using LinearOpMode
The LinearOpMode allows you to use a method instead of an event-driven system like OpMode. Here is a summary of the structure of a LinearOpMode:
- All code that you write for the
LinearOpModewill remain inside therunOpModemethod. - The code that initializes the robot goes at the start of the method.
- The
waitForStartmethod is called, which pauses the execution until the START button on the driver hub is pressed. - There is generally a
whileloop that runs on the condition of the return value ofopModeIsActive(e.g.while (opModeIsActive())) {}). The following methods can be called to get info about the current state of the robot: isStartedreturnsfalseuntil the START button is pressed, then returnstrue.isStopRequestedreturnsfalseuntil the STOP button is pressed, then returnstrue.idlecallsThread.yield, allowing other threads at the same priority level to run.opModeIsActivereturnsisStarted() && !isStopRequested()and callsidle.opModeInInit()returns!isStarted() && !isStopRequested().
Example
public Example extends LinearOpMode {
@Override
public void runOpMode() {
// runs on init
DcMotor motor = hardwareMap.get(DcMotor.class, "motor");
waitForStart();
// runs once start button is pressed
motor.setPower(1.0);
}
}
Using OpMode
The OpMode allows you to use a class with event listeners instead of a function that runs linearly. These are the methods you can override to run code on an event:
init- runs when the INIT button on the driver hub is pressed.init_loop- loops until the START button is pressed.start- runs when the START button is pressed.loop- runs in a loop after the START button is pressed until the STOP button is pressed.stop- runs once when the STOP button is pressed.
Example
public Example extends OpMode {
DcMotor motor;
@Override
public void init() {
motor = hardwareMap.get(DcMotor.class, "motor");
}
@Override
public void runOpMode() {
motor.setPower(1.0);
}
}