# Drivetrain Encoders - Blocks

Moving the motors to a specific position, using the encoders, removes any potential inaccuracies or inconsistencies from using Elapsed Time. The focus of this section is to move the robot to a target position using encoders.&#x20;

## Setting up the Drivetrain Encoders

{% hint style="info" %}
For this tutorial, our OpMode is named HelloRobot\_Encoder!
{% endhint %}

Before diving in too far, recall that for certain [drivetrains](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-2), like the [Class Bot V2](https://docs.revrobotics.com/duo-build/ftc-starter-kit-class-bot), one of the motors needs to be reversed as the motors are mirrored. In our example, we are adding the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MW6-jSLF6Z7O0wWozgO%2F-MW6L1g4Y8GSBICwwFHF%2FMotor%20-%20rightmotorDirection.svg?alt=media&#x26;token=67d93d39-a4fd-441f-ae5d-6d323d657c6c" alt="" data-size="original"> block 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">.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FqVfH0w6uv9qwa5l4hIQQ%2Fimage.png?alt=media&#x26;token=68480ad9-d130-498a-be91-a4cec4404fd0" alt=""><figcaption><p>Setting the rightmotor to run in reverse</p></figcaption></figure>

### RUN\_TO\_POSITION

As introduced in [Using Encoders](https://docs.revrobotics.com/duo-control/hello-robot-blocks/using-encoder#using-run_to_position), using `RUN_TO_POSITION` mode requires a three step process.&#x20;

The **first step** is setting target position. To do so, grab the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYCCC1UjXUUSq0ujDmk%2F-MYGpj87ESnvB0Mbaqbo%2FDual%20Motoer%20-%20set%20position.svg?alt=media&#x26;token=8e0a9be7-426d-4d85-8a69-cc1682dd1d44" alt="" data-size="original">  block and add it to under the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWBfZTWW7pkkhMBnEix%2F-MWPfpmWuWVqh1EweC06%2FComment%20-%20Put%20loop%20blocks.svg?alt=media&#x26;token=f8741193-0083-4e16-8d7c-0ca7277c2dde" alt="" data-size="original"> comment. For this example, we are setting our position after pressing Initialize, but before we hit Play on the Driver Hub.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FduM7aIx27EhYtfxXKFAl%2Fimage.png?alt=media&#x26;token=23880bab-42bd-424d-852b-f5c5e48f560f" alt=""><figcaption></figcaption></figure>

If we want our robot to travel a specific distance we will need to do a bit of math beforehand to calculate the TargetPosition. But for now let's start simple by setting the target position to 1000 ticks.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FuVmCR6eDfzCkbJ1wjHvX%2Fimage.png?alt=media&#x26;token=a0766ebb-5b2b-4bc1-bc00-9e42d4236029" alt=""><figcaption><p>Adding TargetPosition for the drivetrain motors</p></figcaption></figure>

The next step is to set both motors to the `RUN_TO_POSITION` mode. Place the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYCCC1UjXUUSq0ujDmk%2F-MYGpV5c0A6R6UP0YKgC%2FDual%20Motor%20-%20set%20mode%20to%20RUNTO.svg?alt=media&#x26;token=d69c8c3e-35f2-4f64-8698-2fccba89ee01" alt="" data-size="original"> block beneath the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYCCC1UjXUUSq0ujDmk%2F-MYGq13GHps2CyeLWbZt%2FDual%20Motor%20-%20target%20position%20to%201000.svg?alt=media&#x26;token=6c977583-8da8-43a2-848e-0b09583f63d4" alt="" data-size="original"> block.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FGKeAh7LZLDBtsxExb1Te%2Fimage.png?alt=media&#x26;token=27d1cd4f-dba8-4218-9828-dd254e352b00" alt=""><figcaption><p>Changing the motors to RUN_TO_POSITION</p></figcaption></figure>

{% hint style="info" %}
Order matters! The TargetPosition block must come before RUN\_TO\_POSITION mode is set or it will result in an error.&#x20;
{% endhint %}

As mentioned, normally there would be more math involved to help determine how fast the motors should move to reach the desired position. But for testing purposes, we are going to start by keeping it simple!

&#x20;Add the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MVrZYOdMO43YdZ0zxXy%2F-MVwRA9auYOOqnGmzAX7%2FDual%20Motor%20-%20set%20to%20positive.svg?alt=media&#x26;token=ed171c16-63c4-4531-89f3-ed93ce36dbae" alt="" data-size="original"> block beneath the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYCCC1UjXUUSq0ujDmk%2F-MYGpV5c0A6R6UP0YKgC%2FDual%20Motor%20-%20set%20mode%20to%20RUNTO.svg?alt=media&#x26;token=d69c8c3e-35f2-4f64-8698-2fccba89ee01" alt="" data-size="original"> block. Let's go ahead and change the duty cycle (or power) of both motors to 0.8, instead of 1.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FcdV8DUmEM1HobVzysZyT%2Fimage.png?alt=media&#x26;token=5534bf21-aed9-4c6c-90a1-af56daca6f5b" alt=""><figcaption><p>Setting the power for the motors</p></figcaption></figure>

### Quick Check!

Save your OpMode and give it a test. What happens once you press play? What happens if you stop the program then start it again?&#x20;

<details>

<summary>What happens when testing?</summary>

Likely your motors turned on when testing out the code to spin until they've reached the set position.&#x20;

Some may have turned off once the position was reached, but you may also experience the motors twitching or making small adjustments in an attempt to reach the position. Then when starting the code again, the motor either continued twitching or did not move at all.

Recall we may need to reset our encoder to zero before running a program! The motor will continuously try to adjust until it hits the set position, but if it's already there it won't move!

Adjusting the power may help prevent the motor from overshooting the position and needing to repeatedly adjust.&#x20;

</details>

### STOP\_AND\_RESET\_ENCODERS

For our demo code we will want to request our motors reset their encoders during the initialization process of the program.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FP6InAgI8cfbHNl7ua2Xx%2Fimage.png?alt=media&#x26;token=5d019fbb-a670-4ff2-9375-4f3e2c1b00a7" alt=""><figcaption><p>Adding a block to STOP_AND_RESET_ENCODER</p></figcaption></figure>

## Setting up the whileLoop

Let's say we want our program to run only for however long it takes for the motors to reach designated position. Or maybe we intend for the robot to do something else after reaching the destination. For this we will need to edit our whileLoop block!

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F9KMAtrguAi0zdt1msn7q%2Fimage.png?alt=media&#x26;token=413f7f1e-167d-4aea-ab81-0ab505716c32" alt=""><figcaption><p>In this section we will edit our whileLoop</p></figcaption></figure>

{% hint style="info" %}
Even though we are ending a new exit case for our loop, we must always have our call to check opModeIsActive or our program will instantly timeout!
{% endhint %}

Grab an <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MW6-jSLF6Z7O0wWozgO%2F-MW9yJdVdzRolyWei1gK%2FLogic%20-%20and.svg?alt=media&#x26;token=129d69e5-1d08-4d91-8eb6-77ee65a3458b" alt="" data-size="original"> block from the logic menu and add it to the while loop. On the left side of the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MW6-jSLF6Z7O0wWozgO%2F-MW9yJdVdzRolyWei1gK%2FLogic%20-%20and.svg?alt=media&#x26;token=129d69e5-1d08-4d91-8eb6-77ee65a3458b" alt="" data-size="original"> block add the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYCCC1UjXUUSq0ujDmk%2F-MYLqJ0o25l0qXexhQAN%2FMotor%20-%20is%20busy.svg?alt=media&#x26;token=711996ba-e85c-428c-8661-c63dff1b3bde" alt="" data-size="original">block. On the right side add the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYpSPN_7dbYCVxvOxEL%2F-MYprDjxWilLiLPAqPut%2FMotor%20-%20roghtmotor%20is%20busy.svg?alt=media&#x26;token=fb46f071-0f9c-4447-9ef9-af50dd1e4182" alt="" data-size="original"> block.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FALsNYsqdmEIDiCaU4epV%2Fimage.png?alt=media&#x26;token=134a6ab1-df18-4fbd-a7a2-072da910c289" alt=""><figcaption><p>The call motor block is under the DcMotor menu</p></figcaption></figure>

Embed the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYpSPN_7dbYCVxvOxEL%2F-MYprs4f0C8BmAp0-ukL%2FCombo%20-%20left%20and%20right%20are%20busy.svg?alt=media&#x26;token=04d747f4-c4f7-40e9-a4bb-da9e574ebb6f" alt="" data-size="original"> in another <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MW6-jSLF6Z7O0wWozgO%2F-MW9yJdVdzRolyWei1gK%2FLogic%20-%20and.svg?alt=media&#x26;token=129d69e5-1d08-4d91-8eb6-77ee65a3458b" alt="" data-size="original"> block. Place the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYpSPN_7dbYCVxvOxEL%2F-MYprs4f0C8BmAp0-ukL%2FCombo%20-%20left%20and%20right%20are%20busy.svg?alt=media&#x26;token=04d747f4-c4f7-40e9-a4bb-da9e574ebb6f" alt="" data-size="original"> on the right side of the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MW6-jSLF6Z7O0wWozgO%2F-MW9yJdVdzRolyWei1gK%2FLogic%20-%20and.svg?alt=media&#x26;token=129d69e5-1d08-4d91-8eb6-77ee65a3458b" alt="" data-size="original"> block. Our call for the OpMode will go in the lefthand side slot.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FQd53688DBHaF5hEOW06J%2Fimage.png?alt=media&#x26;token=42e1619c-ccc6-4750-8b65-2f7ea5f2e36c" alt=""><figcaption><p>Full logic statement for the whileLoop</p></figcaption></figure>

Save your OpMode and give it a try!&#x20;

As soon as the motors hit the desired position the program will end instead of continuously run in the event they do not perfectly hit the position.&#x20;

{% hint style="info" %}
Right now the while loop is waiting for the right and left motors to reach their respective targets. There may be occasions when you want to wait for both motors to reach their target position, in this case the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWB9-FbRpvUKsRz3Tbi%2Flogic%20-%20or%20block.svg?alt=media&#x26;token=28de91ba-b844-4003-9c38-0a19d6e70e17" alt="" data-size="original"> can be used such as:  <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYpSPN_7dbYCVxvOxEL%2F-MYpwslGYPymyht-7Rwu%2Fcombo%20-%20right%20left%20are%20busy.svg?alt=media&#x26;token=14b6604f-c1c3-4857-a711-1cd7e24bdf75" alt="" data-size="original">
{% endhint %}
