Lesson: 6 Week FIRST Tech Challenge Workshop 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:

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:

 leftMotor.setDirection(DCMotor.Direction.REVERSE).

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 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 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 a new class file in the package folder. Double click that class file to open it in the editor window.
 
Note that the link view appears in the lower right corner of the example source code displays, as you see above. You can click on it to display the source code by itself. This makes it easier to select and copy the code. Click the back arrow to return to the lesson. Do this and copy all the exercise code and paste into the class file replacing all of the existing contents. The code is now in Android Studio in the file DriveForward.java 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.
 
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 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 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) 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 after the dot.
 

Navigation: