Instructional Material: Exercise: Drive Forward and Stop

So lets do some actual robot programming. Assuming you have constructed a simple robot chassis with two DC motors driving two wheels, one on the left and one on the right side of the robot, we will also assume you have configured the controller phone for this hardware and named the motors left_motor and right_motor. Given all this, we can write a simple LinearOpMode to drive the robot forward for 2 seconds and then stop:

When we construct a robot, there will  be one end of the robot that is naturally the "front". Motor power is expressed as -1.0 to +1.0 (0 being no power), with - turning the motor in one direction and + in the other. We have decided for our robot that + power should move the robot in the forward direction. So we set both motor power levels to some + value and observe the motion of the robot. One motor will turn in the correct direction for forward movement and the other motor will turn opposite of what it should. For the motor turning the wrong way (on our robot it is the left one) we will reverese it. We use the method call:


Now the robot moves forward with both motors when given a + power value. You must do this exercise with each robot you build as the gearing you use may change which motor should be reversed. Note that the reason we have to reverse one motor is because the motors are mounted (typically) on opposite sides of the robot. If they both turn the same direction the robot will spin around instead of move forward.

Note the use of the enum data type exposed by the DCMotor class: Direction.REVERSE. We could have figured out what constant value to use here to indicate reverse direction but the enum makes it very easy to set the correct value. You can see the other choices by typing a period right after Direction which causes AS to show you the available choices.

Note the use of the sleep() method. This causes your program to wait (block or delay) for the number of milliseconds specified. 1000 milliseconds equals 1 second. The power settings on your motors will remain in effect during the wait.
Note that after each call to telemetry.addData(), we call telemetry.update(). Update sends any preceeding data queued with the addData method to the Driver Station. In Linear OpModes you must call telemetry.update() to send telemetry data to the DS. In regular OpModes, this update is done automatically when the loop() or init_loop() methods end.

Ok! Let's make this happen:

If you are using OnBot, click the + sign in the left navigation pane to create a new Java file called DriveForward. Use the default location.
If you are using Android Studio, go into the ftc_app-master project. Locate the org.firstinspires.ftc.teamcode package node under TeamCode\java in the project navigation pane. Right click on package to open it. Right click on any item below the package (like the readme file) and select New, then Java Class. Give the class the name DriveForward. This will create the new class file in the package folder. Double click that new class file to open it in the editor window.
Now note that when your cursor is over the code a Copy button appears upper right. Click this to copy the exercise code and then paste it into the new class file replacing all of the existing contents. The code is now in OnBot or Android Studio in the file and ready for you to compile.

The OpMode is registered with the FtcRobotContoller app with the @Autonomous annotation before the class name. This categorizes the OpMode as Autonomous and puts it in a group called "Exercises". You can un-comment the @Disabled annotation to remove the OpMode from the list of OpModes that appear on the Driver Station without having to remove the class from the project.

Note the annotation @Overrides on the runOpMode() function. This tells the Java compiler that we intended to have this runOpMode() function replace the function with the same name in the base OpMode class.
Now, for OnBot, you can click the Build All (wrench) button (lower right). This will compile all of your code and if there are no errors, you can look at the OpMode list on the driver station app. You should see DriveForward in the OpMode list. Select it as the current OpMode. Press Init and then press Start to test your program. Your robot should drive forward for 2 seconds and then stop. You should see the telemetry messages at the bottom of the driver station app.

For Android Studio, unplug your controller device from the robot and plug it into your PC. When the device is recognized click the green arrow button (top center of the AS menu bar). This will launch the compile and deployment process. You will be prompted to select your device on a list of deployment targets and approve the download of the program. Two notes: the downloaded program is called an APK in Android parlance, and the first compile you do after starting AS make take some time whereas subsequent compiles should be quick. When the download has completed, the new controller app on your device should restart.

Now plug the controller device back into the robot. When the controller and driver station devices have connected and the controller has connected to the robot hardware, you can look at the OpMode list on the driver station app. You should see DriveForward in the OpMode list. Select it as the current OpMode. Press Init and then press Start to test your program. Your robot should drive forward for 2 seconds and then stop. You should see the telemetry messages at the bottom of the driver station app.

You can now experiment with different power settings and directions and different amounts of time by changing the program, compiling and deploying to the robot.

Note that in addition to the Java and FTC SDK documentation mentioned earlier, if you have questions about a class or method, and you are using AS, you can place the cursor over the name of an instance of that class or method, click, then press ctrl-q. This will open a window that will contain any documentation available for the selected item. You can see a list of available fields and methods for a class by typing the name of the class (or instance variable) followed by a dot. The list will be displayed and you can arrow up and down to view any available documentation for the selected item. With an item highlighted, pressing enter will add the field or method name to your code after the dot.
Material Type: 
Lab Activity
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
SW Platform: 
Interactivity Style: