Lesson: Exercise: Drive Forward and Stop

Overview: 
Explore example code that shows how to drive a robot forward and then stop.
Objectives: 

Understand basic motor control and work through an exercise in actually programming a robot.

Content: 

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:

Notice the method call rightMotor.setDirection(DCMotor.Direction.REVERSE). Since the motors are on opposite sides of the robot, for both wheels to turn the same direction and generate forward motion, one of the motors must turn in the opposite of its normal direction. Here we select the right motor and reverse the direction of any power setting we give it. Now both motors will turn in the same direction relative to the front of the robot. You may have to experiment to determine which motor should be reversed. 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 that if your robot actually drives backwards, you need to reverse the sign of the power value in the SetPower() method calls. SetPower accepts values in the range of -1.0 to +1.0 with 0 being no power. The sign controls the direction, forward or backward. Note that forward and backward are relative to the way you have your motors mounted and what gearing you use to connect the motors to the wheels, not mention the idea of which end of your robot you define as the "front". For each robot you build you will have to experimentally determine the appropriate sign for "forward".
 
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 added 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.
 
You can now go into Android Studio to 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 and select New, then Java Class. Give the class the name DriveForward. This will create a new empty class file in the opmodes directory. Double click that class file to open it in the editor window. Copy all the code above and paste into the class file replacing all of the existing contents.
 

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.
 
Next unplug your controller phone from the robot and plug it into your PC. When the phone 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 phone 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 phone should restart.
 
Now plug the controller phone back into the robot. When the controller and driver station phones have connected and the controller has connected to the robot, you can look at the OpMode list on the driver station app. You should see DriveForwardThenStop 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 you can place the cursor over the name of an instance of that class or method and press ctrl-q. This will open a window that will contain any documentation available for the selected item.
 

Lesson navigation: