Understanding OpModes - Dutton-Christian-Robotics/Learning-to-Code GitHub Wiki

What's an OpMode?

The code that you write to control an FTC robot is referred to as an "operational mode"—or "OpMode", for short. This code, along with any supporting files, ultimately needs to be installed onto the robot controller for the robot to have access to them. OpModes can be added either through using the OnBot Java system, or by building the Robot Controller app in Android Studio and loading it onto the robot controller.

The "lifecycle" of an OpMode

  • The driver station presents a list of OpModes that are available on the robot.
  • Once an OpMode is selected, the driver station presents an "INIT" button. Pressing this will run any initialization (init) code within the OpMode.
  • After the OpMode has initialized, the driver station presents a "play" button. Pressing this will execute the OpMode's main code. This is the period during which we say that the OpMode is "running."
  • At some point the OpMode will stop—either because the "stop" button was pressed on the controller, an automatic timer ended its countdown, or an error occurred in the OpMode.

OpMode Base Classes

Each OpMode is written as a separate Java class that inherits from a parent class.

OpMode

This is the most basic parent class and contains a number of different methods for running code at different points of an OpMode's lifecycle.

  • init() and init_loop() can be used for code to run when an OpMode is initialized. The difference is in whether the code is run once or repeatedly.
  • start() can be used for code that needs to be run a single time when the OpMode starts
  • loop() is used for code that runs repeatedly while the OpMode is running.
  • stop() is called once when the OpMode is first stopped.

LinearOpMode

This class inherits directly from OpMode and simplifies the process of writing a basic OpMode.

  • runOpMode() will contain all the code to be executed. It will be executed once the OpMode has been initialized.
  • The method should include a call to waitForStart(). This will pause execution until the start button has been pressed on the driver controller.
  • The presumption is that the code in the method will be called once and will run from start to end—hence while it's called a linear OpMode.
  • If there is code that should run in a loop, the loop should call opModeIsActive() each time. Otherwise the OpMode could become stuck in a loop if the driver presses the stop button. This will usually lead to a crash and will probably require the robot controller to be restarted.

OpMode Types

FTC games are split into two parts: an autonomous period during which the robot runs on its own without intervention, and a period during which two human drivers control the robot using gamepads. The driver controller app organizes OpModes into two menus based on which period of the game they're intended for. From a code perspective, the type of OpModes are designated using a java annotation above the class declaration

  • Autonomous. Once an autonomous OpMode is started (note, not once it's initialized), it runs with an automatic 30 second timer. Nothing needs to be done to make this timer happens—it's a function of the robot controller itself.
  • TeleOp. This is the term for OpModes that are intended to be run with human input.