# Programming Mecanum - Simplified

{% hint style="info" %}
This example is a simplified form of a mecanum drivetrain code intended to review the basics of mecanum movement and is not recommended for a FTC robot.&#x20;

Check out [Programming Mecanum - Refined](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-4-going-beyond/programming-mecanum-refined)  for a competition ready example!
{% endhint %}

## Configuration <a href="#configuration" id="configuration"></a>

Before getting started with programming we needed to create a configuration file. Below is an overview of how the robot is configured for the TeleOp code to function as expected:

<table><thead><tr><th>Port Type</th><th width="160">Port Number</th><th>Device Type</th><th>Name</th></tr></thead><tbody><tr><td>Motor</td><td>0</td><td>REV Robotics Ultraplanetary HD Hex Motor</td><td>frontLeft</td></tr><tr><td>Motor</td><td>1</td><td>REV Robotics Ultraplanetary HD Hex Motor</td><td>frontRight</td></tr><tr><td>Motor</td><td>2</td><td>REV Robotics Ultraplanetary HD Hex Motor</td><td>backLeft</td></tr><tr><td>Motor</td><td>3</td><td>REV Robotics Ultraplanetary HD Hex Motor</td><td>backRight</td></tr></tbody></table>

### Example Program:

{% file src="<https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Fgtf846g9OgIvgvH0R4Ob%2FmecanumSimplified.blk?alt=media&token=6cfdd883-829f-4c44-9983-e3f7c6d956d4>" %}

## Gamepad Layout: <a href="#gamepad-layout" id="gamepad-layout"></a>

| Gamepad Input                              | Function                         |
| ------------------------------------------ | -------------------------------- |
| Left Joystick - Left/Right on X-Axis       | Strafe Left/Right                |
| Left Joystick - Forward/Backward on Y-Axis | Forward/Backward                 |
| Right Joystick - Left/Right on X-Axis      | Turn Counter-Clockwise/Clockwise |

## Programming Teleop - Blocks

### Initialization: <a href="#initialization" id="initialization"></a>

{% hint style="info" %}
Before diving into mecanum, double check the direction your motors and wheels are spinning. They may need to be reversed if you're experiencing jittering or inverted controls!

Adjust the ![](https://docs.revrobotics.com/~gitbook/image?url=https%3A%2F%2F268621232-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-MHCAE012xNfg1h3SM9v%252Fuploads%252Fsd50OrsprUDav0TPnrSt%252Fimage.png%3Falt%3Dmedia%26token%3Dca1e5857-bb4c-48c3-a2e2-54044e9fc94b\&width=300\&dpr=4\&quality=100\&sign=d3125b20\&sv=1) block to change the set direction during initialization.
{% endhint %}

For this program, we'll set the motors to RUN\_WITHOUT\_ENCODER along with their direction

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FaYRCrtqFWhqg1BLotaob%2Fimage.png?alt=media&#x26;token=fd5a099e-d4b6-419c-87b3-d62d41fd8923" alt="" width="534"><figcaption><p>Setting up the motors</p></figcaption></figure>

### Moving Forward and Backwards: <a href="#moving-forward-and-back" id="moving-forward-and-back"></a>

For a mecanum drivetrain all 4 motors will be given a command to follow when the left joystick is moved along the Y-axis of the joystick. For moving forward and back all wheels must turn the same direction.&#x20;

[Recall that the Y-axis on the gamepad must be inverted.](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-1/programming-motors/programming-a-motor-with-a-gamepad#adjusting-y-axis-direction)

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FdhPQdNonFfjkjzjSpMK1%2Fimage.png?alt=media&#x26;token=f6076e2c-6536-4185-ae59-27caa535944e" alt="" width="465"><figcaption><p>All four motors are set to move in the same direction</p></figcaption></figure>

### Strafing: <a href="#strafing" id="strafing"></a>

For this example, strafing is controlled by the left stick's X-axis allowing the robot to slide left and right. In order to achieve this movement, the motors move in diagonal pairs, so frontLeft and backRight will move the opposit direction of backLeft and frontRight, similar to the X shape the wheels make.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FiJat8lAgsQtSWPwryrp1%2Fimage.png?alt=media&#x26;token=f3fab545-d5a6-4321-9930-b297492cc7b6" alt="" width="470"><figcaption><p>Two motors will run in the opposite direction when strafing!</p></figcaption></figure>

### Turning: <a href="#turning" id="turning"></a>

Lastly, we have turning set by itself on the right joystick's X-axis. To turn our left and right pairs of wheels will spin in opposite directions.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FeSt4YNnf4HhLOesD8nVm%2Fimage.png?alt=media&#x26;token=8983a776-4759-4111-a9d4-4452b5494085" alt="" width="457"><figcaption><p>When turning the front and back motors rotate in opposite directions.</p></figcaption></figure>

{% hint style="info" %}
This version of the mecanum program does not account for diagonal movements of the joystick. Check out [Programing Mecanum - Refined](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-4-going-beyond/programming-mecanum-refined) to create a fully responsive mecanum drive!
{% endhint %}
