# Estimating the Position of the Arm

For this tutorial, our OpMode is named HelloRobot\_ArmEncoder!

## Estimating the Position of the Arm using Telemetry

Let's start by creating a simple program for moving our robot's arm. The one below will look very similar to the code created during [Part 2: Robot Control](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-2/arm-control-blocks)!

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FmhQ8jI60KIauhhzM3RA8%2Fimage.png?alt=media&#x26;token=7e10f6bc-29e0-4fb3-9594-f176a9a6bfaf" alt=""><figcaption><p>A simple program for moving the arm up and down</p></figcaption></figure>

### Adding Telemetry

Within the loop we'll add a <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWBJfpbTuatRMfbhnlO%2FTelemetry%20-%20key%20and%20number.svg?alt=media&#x26;token=d6f21b70-8197-4ead-954a-2604cfb72155" 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-MaUgx24O_aiERCHX2_C%2F-MaZ6jj3OgarZ9wIkHBe%2Fmotor%20-%20arm%20get%20current%20positon.svg?alt=media&#x26;token=aa7cf01d-1009-44bf-be0f-9eb829475334" alt="" data-size="original"> to the number portion of the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MWACJyDlnpPjKwQvDeW%2F-MWBJfpbTuatRMfbhnlO%2FTelemetry%20-%20key%20and%20number.svg?alt=media&#x26;token=d6f21b70-8197-4ead-954a-2604cfb72155" alt="" data-size="original"> block. Change the key string from the default `"key"` to `"Arm Test."`

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FdKi78pTQ7oBlPOafvlpR%2Fimage.png?alt=media&#x26;token=a520a96f-614f-4535-87d6-8f9bb01aaabf" alt=""><figcaption></figcaption></figure>

### Finding the Position with Telemetry

Save the OpMode and run it.&#x20;

Use the gamepad commands to move the arm to the 90 degree position. Once you have the arm properly positioned read the telemetry off the Driver Hub to determine the encoder count relative to the position of the arm.&#x20;

![The value shown is an estimate. Test with your robot for the best results!](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MadRPzxQDwEcPdg_MX-%2F-Mae0pJn3Pk9zTlgBL5x%2FClass%20Bot%20side%20view%20arm%20at%2090%20-%20with%20DS%20telemetry.svg?alt=media\&token=6bbd1870-e17a-407a-b7d3-77be74c46e2b)

{% hint style="warning" %}
Remember that the encoder position is set to 0 each time the Control Hub is turned on! This means that if your arm is in a position other than the starting position when the Control Hub is turned on, that position becomes zero instead of the starting position.&#x20;
{% endhint %}

### Adding RUN\_TO\_POSITION to the Program

To add the `RUN_TO_POSITION` code the `if/else` statement must first be edited back into an <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-M_2CE63M33f73tG7c96%2F-M_76Lu914wbfp2TPuaG%2FLogic%20-%20if%20elif.svg?alt=media&#x26;token=15492f0a-0762-4bdb-867c-c9d910f15775" alt="" data-size="original"> block, as shown in the code below.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FLrz9Lt5jGNqBPJ4XyuUL%2Fimage.png?alt=media&#x26;token=b28b25c8-c831-4698-be11-5235d31f9302" alt=""><figcaption></figcaption></figure>

When `DpadUp` is pressed, the arm should move to the the 90 degree position. When `DpadDown` is pressed the arm should move back to the starting position. To do this, set the first <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MadRPzxQDwEcPdg_MX-%2F-Mai71kIttY8M1PhNW8b%2Farm%20-%20set%20arm%20to%20targetPosition.svg?alt=media&#x26;token=a2dfe87f-acc6-44fd-ab81-4d003ba2dbdd" alt="" data-size="original"> equal to the number of ticks it took your arm to get to 90 degrees, for this example we will use 83 ticks.&#x20;

Since we want `DpadDown` to return the arm to the starting position, keeping the  <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MajyF-W7HKhicX7wGD3%2F-MaosWQi5hUAA9ONxsD6%2Farm%20-%20set%20arm%20to%20targetPosition.svg?alt=media&#x26;token=028e0f4f-20c7-4728-8804-dfaddbeaf77a" alt="" data-size="original">  block set to 0 will allow us to accomplish this. Set both <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-Ma9eNmmkAyF5RUbfPkd%2F-MaAmcRjToPMPrh6OqtT%2Farm%20-%20set%20power%20to%200.svg?alt=media&#x26;token=a3889b45-d9db-4059-a6b8-55774c2c71fd" alt="" data-size="original"> blocks equal to 0.5.&#x20;

Lastly, we can remove the final "else" of our statement for now since we are now using `RUN_TO_POSITION` mode.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F8sbhvnuuCM9jK3SRd57G%2Fimage.png?alt=media&#x26;token=5f8d1137-7cce-4503-be04-723402dacd69" alt=""><figcaption><p>Full program using an estimated position</p></figcaption></figure>

Despite our power being a positive value for both directions, the arm will move up or down based on the set position!

### Testing the Program

If you try running this code you may notice that the arm oscillates around the 90 degree position. When this behavior is present you should also notice the telemetry output for the encoder counts fluctuating.

Recall `RUN_TO_POSITION` is a **Closed Loop Control**, which means that if the arm does not perfectly reach the target position, the motor will continue to fluctuate until it does. When motors continue to oscillate and never quite reach the target position this may be a sign that the factors determining tolerances and other aspects of the closed loop are not tuned to this particular motor or mechanism.&#x20;

There are ways to tune the motor, or ways to have the [program exit once the position is reached](https://docs.revrobotics.com/duo-control/hello-robot-blocks/autonomous-navigation-blocks#setting-up-the-whileloop), but for now we want to focus on working with the arm and expanding on how limits and positions work with regards to the mechanism.&#x20;
