# Calculating Target Position

Now that we have an estimate for our target position let's see if we can refine it to be more precise using similar methods to what we covered during the [Drivetrain Encoders section](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-3/autonomous-navigation-blocks).&#x20;

## What's Needed for the Conversion

### Ticks per Revolution

Recall, that ticks per revolution of the encoder shaft is different than the ticks per revolution of the shaft that is controlling a mechanism, such as what we determined on our [Drivetrain](https://docs.revrobotics.com/duo-control/hello-robot-blocks/autonomous-navigation-blocks/converting-encoder-ticks-to-a-distance#whats-needed-for-the-conversion).

{% hint style="info" %}
For more information on the effect of motion transmission across a mechanism check out the [Compound Gearing](https://docs.revrobotics.com/duo-build/actuators/gears/gears-advanced#compound-gearing) section.&#x20;
{% endhint %}

The amount of ticks per revolution of the encoder shaft is dependent on the motor and encoder. Manufacturers of motors with built-in encoders will have information on the amount of ticks per revolution.

{% hint style="info" %}
Visit the manufacturers website for your motor or encoders for more information on encoder counts. For HD Hex Motors or Core Hex Motors visit the [Motor](https://docs.revrobotics.com/duo-build/actuators/motors) documentation.&#x20;
{% endhint %}

In the [Core Hex Motor specifications ](https://docs.revrobotics.com/duo-build/actuators/motors/core-hex-motor#product-specs)there are two different Encoder Counts per Revolution numbers:

* At the motor - 4 counts/revolution
* At the output - 288 counts/revolution

At the motor is the number of encoder counts on the shaft that encoder is on. This number is equivalent to the 28 counts per revolution we used for the HD Hex Motor. &#x20;

The 288 counts "at the output" accounts for the change in resolution after the motion is transmitted from the motor to the built in 72:1 gearbox.&#x20;

Lets use the **288 as ticks per revolution** so that we do not have to account for the gearbox in our total gear reduction variable.&#x20;

### Total Gear Reduction

Since we built the the gear reduction from the motor gearbox into the ticks per revolution the main focus of this section is calculating the gear reduction of the arm joint.&#x20;

The motor shaft drives a 45 tooth gear that transmits motion to a 125 tooth gear. The total gear ratio is 125T:45T. To calculate the gear reduction for this gear train, we can simply divide 125 by 45.&#x20;

$$
\frac{125}{45} = 2.777778
$$

### Quick Summary

To summarize, for the Class Bot V2 the following information is true:&#x20;

| Ticks per revolution | 288 ticks |
| -------------------- | --------- |
| Total gear reduction | 2.777778  |

## Adding Arm Encoders to the Program

### Establishing Initialization Variables

Now that we have this information lets create two constant variables:&#x20;

* `COUNTS_PER_MOTOR_REV`
* `GEAR_REDUCTION`

Add the variables `COUNTS_PER_MOTOR_REV` and `GEAR_REDUCTION` variables to the  initialization  section of the program.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Frzk5SpPzCew0AaVhlzhB%2Fimage.png?alt=media&#x26;token=54b3c960-e736-4ad7-9885-f92e10cfa510" alt=""><figcaption><p>Adding COUNTS_PER_MOTOR_REV and GEAR_REDUCTION</p></figcaption></figure>

Our `COUNTS_PER_MOTOR_REV` and `GEAR_REDUCTION` variables will be set to a value that will then be used to calculate our other two variables `COUNTS_PER_DEGREE` and `COUNTS_PER_GEAR_REV`. &#x20;

Let's go ahead and add these variables to our OpMode.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FzTc3REAMwKdr5iQ4Tsxu%2Fimage.png?alt=media&#x26;token=6c9fb1fe-192d-412d-9032-c37e03f63606" alt=""><figcaption><p>Full list of initialization variables</p></figcaption></figure>

### Adding Calculations to the Variables

Once the variables are created and in place, use the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYQpFaVyN6OAN2wLm6r%2F-MYRB7rWtI2jshN4W7LJ%2FMath%20-%200.svg?alt=media&#x26;token=99ad04a5-2f5e-4c97-a3f1-aa6471df73b2" alt="" data-size="original"> blocks to set the first variables to the respective [values](#quick-summary)

![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MajyF-W7HKhicX7wGD3%2F-ManrpfRGL9WbH0S_hlT%2FArm%20-%20the%20first%20two%20constants%20on%20their%20own.svg?alt=media\&token=8401fdda-ac1d-4e1b-bd47-c5d8d9bd224b)

Now that these two variables have been defined, we can use them to calculate two other variables: the amount of encoder counts per rotation of the 125T driven gear and the number of counts per degree moved.

Calculating counts per revolution of the 125T gear (or `COUNTS_PER_GEAR_REV`) is the same formula we used for [`COUNTS_PER_WHEEL_REV` variable on our drivetrain](https://docs.revrobotics.com/duo-control/hello-robot-blocks/autonomous-navigation-blocks/converting-encoder-ticks-to-a-distance#calculating-counts_per_wheel_rev), so to get this variable we can multiple `COUNTS_PER_MOTOR_REV` by `GEAR_REDUCTION`.

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FPJXledysR84B0gkl9696%2Fimage.png?alt=media&#x26;token=2cdb1e9c-09cd-4b74-8d44-a5af6940fb36" alt=""><figcaption></figcaption></figure>

To calculate the number of `COUNTS_PER_DEGREE` divide the `COUNTS_PER_GEAR_REV` variable by 360.&#x20;

![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MajyF-W7HKhicX7wGD3%2F-Mao6OjsQ1lyb5dBxPIQ%2FArm%20-%20counts%20per%20degree.svg?alt=media\&token=c2a5191c-ece1-4828-b4de-f7afee8b4653)

All together our variables will look like below:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FcqrCKZ8B8OrL79OvIUn4%2Fimage.png?alt=media&#x26;token=6a5d1506-8020-434a-96cd-979707b8742a" alt=""><figcaption><p>All of the initialization variables calculated</p></figcaption></figure>

### Adjusting our If/Else Statement

We need to create one more non-constant variable that will act as our position. This will be called `armPosition`.&#x20;

To get to the 90 degree position, the arm needs to move roughly 45 degrees therefore set arm position equal to `COUNTS_PER_DEGREE` times 45.&#x20;

![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MajyF-W7HKhicX7wGD3%2F-Maok2yFCAHOUh00l070%2Farm%20-%20setting%20arm%20position.svg?alt=media\&token=dceec55d-7e8a-45fd-a384-fa2353b7dc8f)

Add this variable to the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-M_2CE63M33f73tG7c96%2F-M_6pst95i16Ri14yZ4b%2FGamepad%20-%20d%20pad%20blocks.svg?alt=media&#x26;token=82f3553f-3e78-4940-a8b7-375a566b8ae6" alt="" data-size="original"> section of the <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"> statement, as this section dictates the 90 degree position. Add the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MajyF-W7HKhicX7wGD3%2F-MaotXo3dfKi8y9Cp7gE%2FArm%20-%20armPosition.svg?alt=media&#x26;token=fdd22974-3023-4463-bbbc-3c4d5eeb30e9" alt="" data-size="original"> block to 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.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FoeAE7xNs0xqoABfr6nEo%2Fimage.png?alt=media&#x26;token=3db696ee-a762-408a-97b1-6233fb3e9094" alt=""><figcaption><p>Adjusting the If/Else to include the armPosition</p></figcaption></figure>

Save your OpMode and give it a test!
