# Converting Encoder Ticks to a Distance

In the previous section, the basic structure needed to use `RUN_TO_POSITION`was created. The placement of<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">within the code, set the target position to 1000 ticks.&#x20;

But how far is a tick and how can we use them to help our robot navigate an area? We could attempt to estimate the distance the robot moves per tick or we can convert the amount of ticks per revolution of the encoder into a unit like millimeters or inches! For instance, if you work through the conversion process and find out that a drivetrain takes 700 ticks to move an inch, this can be used to find the total number of ticks need to move the robot 24 inches.

{% hint style="warning" %}
Reminder that the basis for this guide is the [Class Bot V2](https://docs.revrobotics.com/duo-build/ftc-starter-kit-class-bot). The REV DUO Build System is a metric system. Since part of the conversion process references the diameter of the wheels, this section will convert to ticks per mm.
{% endhint %}

## What's Needed for the Conversion&#x20;

This process will take a bit of math to achieve so let's break it down.

When using encoders built into motors, converting from ticks per revolution to ticks per unit of measure moved requires the following information:

* [x] Ticks per revolution of the encoder shaft
* [x] Total gear reduction on the motor
  * Including gearboxes and motion transmission components like gears, sprockets and chain, or belts and pulleys
* [x] Circumference of the driven wheels

### Ticks per Revolution

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.&#x20;

For HD Hex Motors the encoder counts 28 ticks per revolution of the motor shaft.&#x20;

{% 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 %}

### Total Gear Reduction

Since ticks per revolution of the encoder shaft is before any gear reduction calculating the total gear reduction is needed. This includes the gearbox and any addition reduction from motion transmission components. To find the total gear reduction use the [Compound Gearing formula](https://docs.revrobotics.com/duo-build/actuators/gears/gears-advanced#compound-gearing).&#x20;

For the Class Bot V2 there are two UltraPlanetary Cartridges, 4:1 and 5:1, and an additional gear reduction from the UltraPlanetary Output to the wheels, 72T:45T ratio.

{% hint style="info" %}
The UltraPlanetary Cartridges use the nominal gear ratio as a descriptor. The actual gear ratios can be found in the [UltraPlanetary Users Manual's Cartridge Details](https://docs.revrobotics.com/ultraplanetary/cartridge-details#actual-cartridge-gear-ratios).&#x20;
{% endhint %}

Using the compound gearing formula for the Class Bot V2 the total gear reduction is:

$$
\frac{3.61}{1} \* \frac{5.23}{1} \* \frac{72}{45} = 30.21
$$

{% hint style="info" %}
Unlike the spur gears used to transfer motion to the wheels, the UltraPlanetary Gearbox Cartridges are planetary gear systems. To make calculations easier the gear ratios for the Cartridges are already reduced.&#x20;
{% endhint %}

### Circumference of the Wheel

The Class Bot V2 uses the 90mm Traction Wheels. 90mm is the diameter of the wheel. To get the appropriate circumference use the following formula&#x20;

$$
circumference = diameter \* \pi
$$

You can calculate this by hand, but for the purpose of this guide, this can be calculated within the code.&#x20;

{% hint style="info" %}
Due to wear and manufacturing tolerances, the diameter of some wheels may be nominally different. For the most accurate results consider measuring your wheel to confirm that the diameter is accurate.&#x20;
{% endhint %}

### Quick Summary

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

| Ticks per revolution       | 28 ticks        |
| -------------------------- | --------------- |
| Total gear reduction       | 30.21           |
| Circumference of the wheel | $$90mm \* \pi$$ |

## Translating the Conversion to Code

### Setting up Variables

Each of these pieces of information will be used to find the number of encoder ticks (or counts) per mm that the wheel moves. Rather than worry about calculating this information by hand, these values can be added to the code as constant variables. To do this create three variables:

* `COUNTS_PER_MOTOR_REV`
* `DRIVE_GEAR_REDUCTION`
* `WHEEL_CIRCUMFERENCE_MM`

{% hint style="info" %}
The common naming convention for constant variables is known as CONSTANT\_CASE, where the variable name is in all caps and words are separated by and underscore.&#x20;
{% endhint %}

We'll add the [variables](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-2/arcade-style-teleop-blocks/establishing-variables-in-blocks) to the initialization section of the OpMode:&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Ft3sVgKaPx36meMBICCIm%2Fimage.png?alt=media&#x26;token=545bcd1a-a188-4563-9836-1d9fad0df595" alt=""><figcaption><p>Adding our new variables to initialization</p></figcaption></figure>

Once the variables are created and added to the OpMode, 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 variables to the respective values.&#x20;

For `WHEEL_CIRCUMFERENCE_MM`  a combination of the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYQpFaVyN6OAN2wLm6r%2F-MYRS32ax0_5fhPWNou9%2FMath%20-%20multiplication.svg?alt=media&#x26;token=76bfe431-53f9-4310-9164-47faeef5a26b" alt="" data-size="original"> , <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"> , and<img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYQpFaVyN6OAN2wLm6r%2F-MYRSB17mEeOLwbUp2y2%2FMath%20-%20pi%20block.svg?alt=media&#x26;token=4cb365a7-a4b0-4fe7-a0cb-d1727929b12e" alt="" data-size="original"> blocks will be used to get the circumference of the wheel.&#x20;

![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYQpFaVyN6OAN2wLm6r%2F-MYRP6ZUXJNSFde2UzFX%2FEncoder%20-%20adding%20numbers%20to%20the%20first%20three.svg?alt=media\&token=c2901d94-6d68-4db3-a5b5-1ae1a15d30ef)

Now that these three variables have been defined, we can use them to calculate two other variables: the **amount of encoder counts per rotation of the wheel** and **the number of counts per mm that the wheel moves**.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F8Fex3P3W6gZWuYoh1r7h%2Fimage.png?alt=media&#x26;token=baaf5112-37f6-4341-9440-1fac2ab4412e" alt=""><figcaption></figcaption></figure>

### Calculating COUNTS\_PER\_WHEEL\_REV

To calculate counts per wheel revolution multiply `COUNTS_PER_MOTOR_REV` by `DRIVE_GEAR_REDUCTION` Use the following formula:

$$
y = a
\*b
$$

Where:&#x20;

* $$a$$ = `COUNTS_PER_MOTOR_REV`
* $$b$$ = `DRIVE_GEAR_REDUCTION`&#x20;
* $$y$$ = `COUNTS_PER_WHEEL_REV`

Again math blocks need to be used to define these variables. Lets start with the `COUNTS_PER_WHEEL_REV`  variable. Add a <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYQpFaVyN6OAN2wLm6r%2F-MYRS32ax0_5fhPWNou9%2FMath%20-%20multiplication.svg?alt=media&#x26;token=76bfe431-53f9-4310-9164-47faeef5a26b" alt="" data-size="original"> to the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYQpFaVyN6OAN2wLm6r%2F-MYRfuLihTonvd67Sk4b%2FVariavble%20-%20COUNT_PER%20WHEEL%20REV.svg?alt=media&#x26;token=c0e85fc9-8dc2-4fe0-a0e4-8d32e1f311ad" 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-MYRgR43ABOUSGb1f8WZ%2F-MYeigphTtcNkGuZlxOg%2FVariable%20-%20counts%20per%20motor%20rev.svg?alt=media&#x26;token=72582836-0ff4-4093-819a-0cc7718025d4" alt="" data-size="original"> and <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYRgR43ABOUSGb1f8WZ%2F-MYeikxKcR2ab3l-siE2%2FVariable%20-%20drive%20gear%20reduction.svg?alt=media&#x26;token=c52353c7-2a92-48ea-92a2-4e407170b421" alt="" data-size="original">blocks to either side of the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYQpFaVyN6OAN2wLm6r%2F-MYRS32ax0_5fhPWNou9%2FMath%20-%20multiplication.svg?alt=media&#x26;token=76bfe431-53f9-4310-9164-47faeef5a26b" 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%2FGyPGBpfPupNS7N9jTLpt%2Fimage.png?alt=media&#x26;token=0f392be0-6a1e-4788-b966-a2a429ec144c" alt=""><figcaption></figcaption></figure>

### Calculating COUNTS\_PER\_MM

Once the `COUNTS_PER_WHEEL_REV` is calculated, it can be used to calculate the counts per mm that the wheel moves. To do this divide the `COUNTS_PER_WHEEL_REV` by the `WHEEL_CIRCUMFERENCE_MM`. Use the following formula.

$$
x = \frac{(a\*b)}{c} = \frac{y}{c}
$$

Where,

* $$a$$ = `COUNTS_PER_MOTOR_REV`
* $$b$$ = `DRIVE_GEAR_REDUCTION`
* $$c$$ = `WHEEL_CIRCUMFERENCE_MM`
* $$y$$ = `COUNTS_PER_WHEEL_REV`
* $$x$$ = `COUNTS_PER_MM`

{% hint style="warning" %}
`COUNTS_PER_WHEEL_REV`will be created as a separate variable from `COUNTS_PER_MM` as it is used in calculating a target velocity.&#x20;
{% endhint %}

Since `COUNTS_PER_WHEEL_REV`  has been calculated it can be used to calculate `COUNTS_PER_MM` add the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYRgR43ABOUSGb1f8WZ%2F-MYepPimZMwCPPARot8P%2FMath%20-%20division.svg?alt=media&#x26;token=53cb0fdc-cdf6-4ce0-abbd-cd3ab3f9d30d" alt="" data-size="original"> to the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYRgR43ABOUSGb1f8WZ%2F-MYeqNUzRqrbAl-CypnS%2FVariable%20-%20set%20counts%20per%20mm.svg?alt=media&#x26;token=c0e14153-93f2-4781-8583-840f4527bc70" alt="" data-size="original">. 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-MYRgR43ABOUSGb1f8WZ%2F-MYepPimZMwCPPARot8P%2FMath%20-%20division.svg?alt=media&#x26;token=53cb0fdc-cdf6-4ce0-abbd-cd3ab3f9d30d" alt="" data-size="original"> add the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYRgR43ABOUSGb1f8WZ%2F-MYey5fLzndMDfYs7HdW%2FVariable%20-counts%20per%20wheel.svg?alt=media&#x26;token=1ff59bbd-6e23-404f-a0bd-15b37501cec6" alt="" data-size="original"> block. 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-MYRgR43ABOUSGb1f8WZ%2F-MYepPimZMwCPPARot8P%2FMath%20-%20division.svg?alt=media&#x26;token=53cb0fdc-cdf6-4ce0-abbd-cd3ab3f9d30d" alt="" data-size="original"> add the <img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYeyIdN23s6PuxYRjxx%2F-MYf-5tkdwxOZ7wRXrM-%2FVariable%20-circumferenc.svg?alt=media&#x26;token=296ae9f7-5119-475f-b9a3-7eeba4fe8aeb" alt="" data-size="original"> .

![](https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M4_pJHI8HTuZFQTNfcy%2F-MYeyIdN23s6PuxYRjxx%2F-MYf7Q7ov7Ll3ETClAK_%2FVariable%20-%20set%20counts%20per%20mm%20to%20vairables.svg?alt=media\&token=8198265a-2ddd-4cb8-b48e-6476ebc1aecb)

### Final Variables

Once `COUNTS_PER_WHEEL_MM` is set, this completes the conversion process, and all constant variables are set.&#x20;

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FmxGAtXSl8eOncvqZsryw%2Fimage.png?alt=media&#x26;token=8442c7d3-9afa-4df4-8fd5-5589f7fe415a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FV9Cz1x3GJa5ZkJTiG8df%2Fimage.png?alt=media&#x26;token=e21b9d04-d308-4b00-abb2-833200352574" alt=""><figcaption></figcaption></figure>

Make sure to save your OpMode here to prevent any progress being lost in the event of a disconnect!
