Lesson: Exercise: Joystick Driving - Tank Mode

Explore driving the robot with two joysticks (Tank mode) on a controller.

Understand Tank mode driving and using input from two joysticks to drive the robot.


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. 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 object exposes a number of static functions (don't need an instance) useful in robot control. 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 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.

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.

At this point if you are using Android Studio, you are probably getting tired of disconnecting your controller phone from the robot and connecting it to your PC with a USB cable to deploy your code. There is a better way, deploying over WiFi. Check out this lesson.