Drive with Mecanum Wheels
You’ll need a Mecanum Drive Worksheet for this section!
Mecanum wheels give our drivetrain three degrees of freedom - driving forward and backward, strafing left and right, and rotating clockwise and counterclockwise.
To make our code more readable, we can create variables to capture each motion, which we can use to calculate how we want to move later. Before the lines that set the power of your drive motors, add the following lines:
double drive = -gamepad1.left_stick_y
double strafe = gamepad1.left_stick_x
double rotate = gamepad1.right_stick_x
On your worksheet, start by labeling the directions of the force applied to the robot when each wheel rotates forward and backward. Note that the direction of the force is perpendicular to the direction of the rollers on each wheel.
Consider what happens when we add multiple forces acting in different directions together, as indicated in the diagram on the worksheet. Use that information to fill in the table on the worksheet, indicating whether each motor needs to spin in a positive or negative direction to move the robot in each specified direction.
Once you have that figured out, you can update the code that sets the power of your drive motors by adding up the positive or negative values according to the table. For example, if your front left row is all positive, you’d have the following in your code:
driveLF.setPower(drive + strafe + rotate)
Or, if they were all negative, you’d have this:
driveLF.setPower(-drive - strafe - rotate)
Set up all your motors and make sure your robot drives as you expect.