Explains how the various stages of a LinearOpMode are equivalent to the various functions provided by OpMode
This program has been verified to work using virtual_robot.
Last updated 7-31-23This example TeleOp demonstrates how the various stages of running a LinearOpMode are equivalent to the functions provided by OpMode. This might help you transition between the two.
This program simply writes telemetry log messages to state where it is in the program. Doing only one operation keeps the program small. Also, no, you do not have to call telemetry.update() to send log messages.
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import org.firstinspires.ftc.robotcore.external.Telemetry;
@TeleOp(name="LinearOpModeEquivalent", group="Tutorial")
public class LinearOpModeEquivalent extends OpMode {
Telemetry.Log log = telemetry.log();
@Override
public void init() {
//This would be right at the start of runOpMode()
log.setCapacity(5);
log.add("runOpMode() called");
}
@Override
public void init_loop() {
//This method is optional!
//If you put nothing here, it just behaves like an implied waitForStart()
//If you put something here, it's like replacing waitForStart() with a while (!opModeIsActive()) loop.
log.add("Looped in while (!opModeIsActive())");
}
@Override
public void start() {
//This method is optional!
//This is what happens between waitForStart() and while (opModeIsActive())
log.add("waitForStart() just returned");
}
@Override
public void loop() {
//This is equivalent to the while (opModeIsActive()) loop
log.add("Looped in while (opModeIsActive())");
}
@Override
public void stop() {
//This method is optional!
//This is what happens when the OpMode is stopped, opModeIsActive() becomes false, and the while loop exits. This is the code right after the loop.
log.add("Exited while (opModeIsActive())");
}
}
The comments mostly explain things, so you may not need to read this. It does provide some additional information if you need it.
public class LinearOpModeEquivalent extends OpMode {
Telemetry.Log log = telemetry.log();
This is the very start of the class. This would be even before init() is called in OpMode. The Telemetry object provided by LinearOpMode contains a Telemetry.Log object, and we can call add() on that object to add a message to the log. This is why we are storing the log object for later use.
@Override
public void init() {
//This would be right at the start of runOpMode()
log.setCapacity(5);
log.add("runOpMode() called");
}
The only initialization step for our program is to set the capacity of the log. This is also where you would get your HardwareDevices from the HardwareMap, set the directions of motors, or other such things. If you extended LinearOpMode, then this would go at the start of the runOpMode() method, before waitForStart().
@Override
public void init_loop() {
//This method is optional!
//If you put nothing here, it just behaves like an implied waitForStart()
//If you put something here, it's like replacing waitForStart() with a while (!opModeIsActive()) loop.
log.add("Looped in while (!opModeIsActive())");
}
This runs where waitForStart() would normally sit around and do nothing until the Start button is pressed. If you look at this sample's LinearOpMode counterpart, you'll see that we replicated it using a while (!opModeIsActive())
loop.
@Override
public void start() {
//This method is optional!
//This is what happens between waitForStart() and while (opModeIsActive())
log.add("waitForStart() just returned");
}
This is what would go between waitForStart() and the while (opModeIsActive())
loop. This code will run once when Start is pressed.
@Override
public void loop() {
//This is equivalent to the while (opModeIsActive()) loop
log.add("Looped in while (opModeIsActive())");
}
This code runs continually until Stop is pressed. This performs the function of a while (opModeIsActive())
loop in a LinearOpMode.
@Override
public void stop() {
//This method is optional!
//This is what happens when the OpMode is stopped, opModeIsActive() becomes false, and the while loop exits. This is the code right after the loop.
log.add("Exited while (opModeIsActive())");
}
This is simply the space right after the while (opModeIsActive())
loop. When a LinearOpMode is stopped, opModeIsActive() will become false, causing the loop to exit. The program will then continue on a bit longer and run the code after the loop.