Instructional Material: Exercise: Joystick Driving - Tank Mode

Now lets look at a teleop example. Here we will use the joysticks on one of the Xbox controllers to drive the robot. We will use tank style driving. In tank mode each joystick controls the motor on one side of the robot. Moving the joystick forward or backward will cause the motor on the corresponding side of the robot to turn in that direction and the power level will be determined by how much the joystick is moved off of center. In this mode, to drive straight you have to move each joystick the same amount. Varying the deflection of the joysticks causes the robot to turn. Here is the code:

Here we are using the built-in reference variable for the left gamepad. We are interested in the y axis which is forward and backward.The joysticks return negative values when pushed forward and positive when pushed backward, in the range -1 to +1 with zero being no deflection or centered. Our test robot uses positive power values to move forward so we need reverse the sign of the y joystick axis by multiplying the value by -1.

We also introduce an object in the FTC SDK API, Range. This static object (don't need an instance, just the class name) exposes a number of generic functions useful in robot control. Another useful static class is Math in the Java library. This class provides a large number of computation support methods accessed by using the name Math.

Since the motor power level ranges from -1 to +1, we will use the Range.clip() function to apply those limits to the right and left stick values to make sure the input never exceeds the allowed power values. While the values returned from the joystick should never actually exceed the motor power limits, it is good practice to be sure your input values match the range of values accepted by the function you are calling.

Finally, we use the built-in function opModeIsActive() to determine when we should stop the driving loop and exit the program. This function returns true when the OpMode is running and false when stopped.

Copy and paste this code into a new class called DriveTank, compile and demonstrate tank mode driving.


Material Type: 
Lab Activity
Education Level: 
Middle School
High School
Focus Subject: 
Computing / Computer Science
Robotics Software
HW Platform: 
SW Platform: 
Interactivity Style: