Instructional Material: Regular OpModes

The regular OpMode type that you would write extends the OpMode class in the FTC SDK. To add functionality, your code will override one or more of the following methods that exist in the base OpMode class:

  • init()
  • init_loop()
  • start()
  • stop()
  • loop()

These methods are called at the appropriate time in response to button presses on the driver station (DS).

The init() method is called when you select an OpMode from the list of OpModes on the DS. This is called one time, when you do the selection. It is used for very basic initialization functions your code may need to perform. It is not required.

The init_loop() method is called when you press the Init button on the DS. This method is called each time you press Init, so it will be called each time to run the same OpMode. This is the main place to perform your initialization activities. It is not required.

The start() method is called when the Start button is pressed on the DS. You can perform initialization activities more closely related to the start of program execution here. It is not required.

Note that neither init(), init_loop() or start() is required but one of them must be present to hold your code that sets up your hardware map. You should not set up your hardware map in the loop() method. The init_loop() method is recommended.

The stop() method is called when the Stop button is pressed on the DS. You can perform any activities required to stop your program. It is not required.

The loop() method is the workhorse of the regular OpMode. Once Start is pressed on the DS, the loop() method will be called repeatedly until the Stop button is pressed. In this method you should do your robot control activities. The key idea is that you determine the current state of the robot or control inputs, respond as needed and then exit the method. The loop() method  should be kept short.

Lets look at the NullOp sample OpMode from the FTC SDK:  

This simple example shows the key components of the regular OpMode style of program. You can see this example in the FTC SDK source code and compile it to the controller phone to see it work.

Note that the base OpMode class has useful fields and methods you can access by typing their names. A trick to see what is available is to type this. and wait. AS will show you the available items. You can also look at the documentation for the OpMode class. The field telemetry is an example. This is an object reference field on the base OpMode class that points to an object that allows you to send data to the driver station app to be displayed below the Start button. To use telemetry, you call the addData function with two strings. The first is a label or title for the data you want to display. The second string is the data to be displayed. You can call addData multiple times. The telemetry class has many capabilities to display data on the driver station. It is worthwhile to read the documentation for this class.

Note that the telemetry data sent to the DS is not remembered (by default) between calls to loop() or any of the other methods. You must add everything you want displayed on each loop call. This behavior can be changed if you wish.

The class ElapsedTime is a utility class in the FTC SDK provided for you to track OpMode run time. You can create an instance of ElapsedTime (here called runtime) and use it to track the time the OpMode has been  running. This is done in this example so you can see that the OpMode is running and doing something. Note that the OpMode class provides a field called time and a method called getRuntime() that both provide the same information. Either way is valid.

We also use the built-in Java classes Date and SimpleDateFormat to create a formatted string containing the nicely formatted date and time of  OpMode start as an example of using Java classes and displaying information about what is happening in the OpMode.

 

Material Type: 
Lecture/Presentation
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
Tetrix
SW Platform: 
Java
Interactivity Style: 
Mixed
Audience: 
Learner