# FTC:Software:Mecanum Drivetrain

Mecanum Drivetrains are one of the most popular drivetrains in FIRST Tech Challenge. It is a 4 wheel drivetrain that uses diagonal rollers to give the robot the freedom to strafe left and right. When programming a Mecanum Drivetrain, it is best to program controls that make it easy to take advantage of the benefits of a Mecanum drive while keeping it relatively simple to use. The way we program these is typically with the left joystick handling all translations, i.e. forwards/backwards and left/right, and letting the right joystick control rotation.

Refer to the image above as an example of how a robot could be layed out. Notice that the rollers on top of the robot are facing inwards, forming a sort of X shape. On our robot here we have 4 wheels: leftFront, leftBack, rightFront, and rightBack. Each of these will need a slightly different algorithm to determine which direction to travel during specific movements.

The way to figure out the algorithm is to think about which direction each motor would have to travel during each specific movement. For example, to travel forwards what powers do the motors need? All four of them need to move in the same direction to move the robot forwards so we just need to consider the motor powers to make them all move forwards. Below is a diagram showing the powers.

The two wheels on the right move positively while the left two wheels would move negatively. This is because the two motors on the left are facing the opposite direction so we want those motors to run counterclockwise. This will make all four wheels move forwards.

Knowing this we can add it to our algorithm.

So, for moving forwards, here are our powers:

 leftFront Negative (counter clockwise) leftBack Negative (counter clockwise) rightFront Positive (clockwise) rightBack Positive (clockwise)

We need to do this for the other two directions now- left/right, and rotation.

For strafing, to drive the robot to the right the two wheels have to be going towards each other while the left wheels need to go away from each other. This takes advantage of the angled rollers on the wheels. If this is backwards for you, just reverse it so that left and right sides are swapped.

So, for strafing, here is our graph and image:

 leftFront Negative (counter clockwise) leftBack Positive (clockwise) rightFront Negative (counter clockwise) rightBack Positive (clockwise)

And finally we just need it for rotations, shown here.

 leftFront Negative (counter clockwise) leftBack Negative (counter clockwise) rightFront Negative (counter clockwise) rightBack Negative (counter clockwise)

So we have our components for the algorithm now! All we need to do is essentially add or subtract the values for each motor based on the info we now have above. For the below motor’s power calculation the variables represent the following:

```x = left joystick x input
y = left joystick y input
r = right joystick x input
```

Th motor calculations are then:

```leftFrontPower = -x - y - r
leftBackPower = x - y - r
rightFrontPower = -x + y - r
rightBackPower = x + y - r
```

Ta daa! Now we just run the robot with those powers and it should work exactly as planned. It might take some messing around and some getting used to, especially if you’re coming from a tank drive or something similar, but this control layout can give you a lot of advantages in a game.

It’s recommended you also try doing things like having a button to cut the speed in half and other adjustments to improve accuracy as it’s difficult to move slowly and do precise movements while using a Mecanum Drivetrain.