# Programming Drivetrain Motors

In [Part 1](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-1) we learned how to control a single motor by giving it power or input from a joystick. For controlling a drivetrain, we need to be able to control two motors simultaneously to help the robot move. While we could try adding each motor individually, Blocks has a **dual** motor block available already for just this purpose.

To access the **dual** block you will need to select the actuators dropdown menu:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Fk5Z4jQQzJFTYHLsgNu4d%2Fimage.png?alt=media&#x26;token=20e720d9-add2-4501-b186-93bbd782f0f6" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Not seeing DcMotor under the actuators menu? Make sure your[ configuration](https://docs.revrobotics.com/duo-control/hello-robot-blocks/configuration) has been properly set up and activated before getting started!
{% endhint %}

## Programming Drivetrain Motors

Any code from Part 1 should be moved to the side of the workspace or deleted before continuing this section. Alternatively, you may choose to create a new program.

***

Add the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MVIIB5oT9i9P2l29VHj%2F-MVNpboWEAOhTj8EGLe4%2FDual%20Motor%20-%20Arm%20and%20Left%20(inline).svg?alt=media&#x26;token=fae3f82c-5dd1-4e3b-9590-138cbe778e47" alt="" data-size="original"> block to op mode while loop.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FeJhnMvPTgBHmUASl7vMM%2Fpowerblock.PNG?alt=media&#x26;token=0b066abd-c206-4409-8744-5bbbb337271e" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
When there are multiple of the same type of variable (such as multiple DcMotors) the variable specific blocks will choose a default variable based on alphabetical order. For this example, OpMode DcMotor blocks will default to the **arm** variable.&#x20;
{% endhint %}

Use the variable drop down menu on the block to change from arm to rightmotor.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Fct0RvfXVoEGU60R9dCNK%2Fimage.png?alt=media&#x26;token=27e522ef-5277-4c24-a490-69a1da482ead" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FTYwWGct9jOW1Za8IKJzV%2Frightleftmotor.png?alt=media&#x26;token=24d6eec3-2c4c-41f8-aad6-ef3864b7fce8" alt=""><figcaption></figcaption></figure>

### Quick Check!

Before running your code for the first time, pause and think about the following:

* What do you expect your robot to do once the program is activated?&#x20;

Now save your OpMode using the button in the upper lefthand corner and give your program a go!

<details>

<summary>Did the robot move as you expected? </summary>

You may have expected your robot to move in a straight line forwards or backwards. Instead, your robot likely spun in a circle.&#x20;

When motors run at different speeds they spin along their center pivot point. But the motors are both set to a power of 1 here so what else could be the cause?

</details>

{% hint style="danger" %}
Always keep the Driver Hub within reach in the case of the event that a robot does not perform as expected. When in doubt, disable your robot to keep you and it safe.&#x20;
{% endhint %}

## Mirroring Motors

DC Motors are capable of spinning in two different directions depending on the current flow provided. When a positive power value is applied the motors will spin in a clockwise direction. The opposite will happen when using a negative power value, meaning the motors will spin in a counter clockwise direction.

But how does that help with our current spinning robot? Let's take a closer look at our physical robot to find out:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FicRikwanxjJD9HMzNlnv%2Fimage.png?alt=media&#x26;token=f026f369-f46f-4ebc-aa0f-7fb3b3b0c527" alt=""><figcaption><p>Top down view of the Class Bot V2</p></figcaption></figure>

Notice how the motors on your robot are currently mirrored from each other as part of the drivetrain.  Now think about how we learned that when giving the motors a positive value they should turn clockwise. This is still how, however while they may both be rotating clockwise, the direction they know to be as clockwise is opposite.

![Motors running the same direction, but facing opposite ways!](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MVrZYOdMO43YdZ0zxXy%2F-MVrsHYT6qr4y1KLl-RD%2FDrivetrains-%20motors%20relation%20to%20turning.svg?alt=media\&token=bdddd2dd-dbbd-4204-a772-16dbca06832f)

Try activating your robot's code again, but this time watching which direction the wheels turn. You may consider supporting the robot's frame so the wheels are suspended to make this easier to see.

## Reversing a Motor:

There are a couple ways we could adjust our program to help our robot not to be a spinning top. For example, we could make sure the power is set to a negative value whenever one of our motors is called. Or we could simple reverse our motor's direction during initialization.&#x20;

***

Add <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MVIIB5oT9i9P2l29VHj%2F-MVOBZ-rzSPjT4Aje-Ub%2FMotor%20-%20rightmotorDirection.svg?alt=media&#x26;token=82cbecae-83d2-41a4-b7ee-eda7a029ed9c" alt="" data-size="original"> to your program, under the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MVIIB5oT9i9P2l29VHj%2F-MVRzzz2RWu0i_oNzqo3%2FComment%20-%20Put%20intialization%20blocks.svg?alt=media&#x26;token=5b100e29-9c67-4bb3-be74-0d35d452cb76" alt="" data-size="original"> comment block. If you recall, blocks placed in this section will run AFTER the intilizal button is pressed on the Driver Hub, but BEFORE the play button is clicked.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FeFhO4eGnP0thYk0Fcukz%2Freversedirection.PNG?alt=media&#x26;token=137a3a86-2eb0-406a-ad03-a728b6d90ba3" alt=""><figcaption></figcaption></figure>

Now with the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MVIIB5oT9i9P2l29VHj%2F-MVOBZ-rzSPjT4Aje-Ub%2FMotor%20-%20rightmotorDirection.svg?alt=media&#x26;token=82cbecae-83d2-41a4-b7ee-eda7a029ed9c" alt="" data-size="line"> block added the direction for the rightmotor will always be reversed for this program. Our power values do not need to be changed.

Go ahead and give it a try!
