Hello World

Objective: Create a program that displays "Hello World!" on the driver station

Go Back



Create a new OpMode

Open Android Studio.

Open the "Projects" pane on the top left of the window (if it isn't already open), right-click on the "TeamCode" folder, hover to the "New" submenu, and select "Class" (a blue circle with a C inside it).


The name of the class should follow certain criteria:


For this tutorial, we'll call it "Tutorial".
When the dialog comes up, hit [Enter] or click the blue "Add" button. (This adds it to the GitHub repository so it can be shared between multiple computers.)
Studio should create this for you:

package org.firstinspires.ftc.teamcode; public class Tutorial { }

To make a useful FTC OpMode, you will need to make your class extend either OpMode or LinearOpMode (we will look at LinearOpMode now and come back to OpMode later).


Create the Boiler Plate Code

Between "Tutorial" and the curly brace, type "extends LinearOpMode" and hit [Enter]. The IDE will add the import statement for you:

package org.firstinspires.ftc.teamcode; import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; public class Tutorial extends LinearOpMode { }

Additionally, the robot calls the runOpMode() function to start the program, which doesn't exist yet. Let's create it:

public class Tutorial extends LinearOpMode { public void runOpMode() { // Your code will go here } }

Finally, to make it actually appear on the driver station, we need to add the TeleOp annotation above the class (After typing TeleOp, hit [Enter] so it adds the import statement, just like when extending LinearOpMode.):

@TeleOp(name="Tutorial", group="GroupNameHere") public class Tutorial extends LinearOpMode {

IMPORTANT: Annotations do NOT end with semicolons! (but everything else does.)

Now, finally, a barebones TeleOp (driver-controlled OpMode) looks like this:

package org.firstinspires.ftc.teamcode; import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; import com.qualcomm.robotcore.eventloop.opmode.TeleOp; @TeleOp(name="Tutorial", group="Examples") public class Tutorial extends LinearOpMode { public void runOpMode() { // Your code goes here } }

You could save this snippet as a template to paste into every new TeleOp.
This is as simple as it gets:

Of course, it doesn't do anything right now, so let's make it do something that we can easily observe with no extra equipment: send a message back to the driver station.


Add Functionality

This is done with the telemetry.addData(String title, content); method. Here is an example:

telemetry.addData("Status", "Running");

We could also send variables:

int x = 5; telemetry.addData("variable", x);

NOTE: addData() adds the message to a buffer. We also have to call telemetry.update(); to actually send the message.

Let's add this into our runOpMode() function, so we send a message to the driver when the program runs:

public void runOpMode() { telemetry.addData("Message", "Hello World!"); telemetry.update(); }
Build and Run

Now, you can plug a battery into a REV Control Hub and use a USB-C cable to connect it to the computer. Wait for it to appear as the selected device in Studio. (Sometimes you have to open the drop-down menu for it to appear.) Click the Run (green play button) (or Stop and Run (green counterclockwise arrow from a white square)) button towards the upper-right of the screen. If a pop-up appears, click "Terminate". Wait for a message in the bottom-right of the screen that pops up in a green speech bubble and says, "Launch Succeeded". You can now safely disconnect the Control Hub.
To launch the program, open the "FTC Driver Station" (FIRST logo) app on the driver station (either a REV Driver Hub or, usually, an older model Motorola phone). Once it has connected to the Hub and initialized, click the down arrow on the right-middle of the screen to bring up the list of TeleOps available. Find the one with the name of the class you created at the beginning (Tutorial in our example) and tap it. Now, press the Play button. At the very bottom of the screen, you should see a new message in white text saying, "Message : Hello World!".

Congratulations! You have created and run your very first Robotics program!


Practice:

1: Add more code so your program will print multiple statements when you run it. For example, make it say "This is my first program." and then a new line to print "Learning to program takes a lot of work, but it is worth it!" (each of these commands ends with a semicolon ( ; ) )

2: "Break" your code in a few different ways to see what sort of error message you get. Run the code each time and read the compiler's error messages.