# Converting Encoder Ticks to a Distance

In the previous section, the basic structure needed to use RUN_TO_POSITIONwas created. The placement of leftmotor.setTargetPosition(1000); and rightmotor.setTargetPosition(1000); within the code, set the target position to 1000 ticks. What is the distance from the starting point of the robot and the point the robot moves to after running this code?

Rather than attempt to measure, or estimate, the distance the robot moves, the encoder ticks can be converted from amount of ticks per revolution of the encoder to how many encoder ticks it takes to move the robot a unit of distance, like a millimeter or inch. Knowing the amount of ticks per a unit of measure allows you to set a specific distance. 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.

Reminder that the basis for this guide is the Class Bot V2. 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.

For the conversion process the following information is needed:

• Ticks per revolution of the encoder

• Total gear reduction on the motor

• Including gearboxes and motion transmission components like gears, sprockets and chain, or belts and pulleys

• 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. For HD Hex Motors the encoder counts 28 ticks per revolution of the motor shaft.

#### 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.

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.

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.

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$

Unlike the 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.

#### 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

$circumference = diameter * \pi$

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

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.

To summarize, for the Class Bot V2 the following information is true:

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

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

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.

Add the variables to op mode class, where the hardware variables are defined. Defining the variables within the bounds of the class but outside of the op mode, allows them to be referenced in other methods of functions within the class. To ensure variables are referenceable they are set as static final double variables. Static allows references to the variables anywhere within the class and final dictates that these variables are constant and unchanged elsewhere within the code. Since these variables are not integers they are classified as double variables.

Add the variables to op mode class, where the hardware variables are defined. Defining the variables within the bounds of the class but outside of the op mode, allows them to be referenced in other methods of functions within the class. To ensure variables are referenceable they are set as static final double variables. Static allows references to the variables anywhere within the class and final dictates that these variables are constant and unchanged elsewhere within the code. Since these variables are not integers they are classified as double variables.

public class HelloRobot_EncoderAuton extends LinearOpMode {
private DcMotor leftmotor;
private DcMotor rightmotor;

static final double     COUNTS_PER_MOTOR_REV    = 28.0;
static final double     DRIVE_GEAR_REDUCTION    = 30.21;
static final double     WHEEL_CIRCUMFERENCE_MM  = 90.0 * Math.PI;

Now that these three variables have been defined, they can be used 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.

To calculate counts per wheel revolution multipleCOUNTS_PER_MOTOR_REV by DRIVE_GEAR_REDUCTION Use the following formula:

$y = a *b$

Where,

• $a$ = COUNTS_PER_MOTOR_REV

• $b$ = DRIVE_GEAR_REDUCTION

• $y$ = COUNTS_PER_WHEEL_REV

Create the COUNTS_PER_WHEEL_REVvariable within the code. This will also be a static final double variable.

public class HelloRobot_EncoderAuton extends LinearOpMode {
private DcMotor leftmotor;
private DcMotor rightmotor;

static final double     COUNTS_PER_MOTOR_REV    = 28.0;
static final double     DRIVE_GEAR_REDUCTION    = 30.24;
static final double     WHEEL_CIRCUMFERENCE_MM  = 90.0 * 3.14;

static final double     COUNTS_PER_WHEEL_REV    = COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION;

Once COUNTS_PER_WHEEL_REV is calculated, use it to calculate the counts per mm that the wheel moves. To do this divide theCOUNTS_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

Create the COUNTS_PER_MMvariable within the code. This will also be a static final double variable.

public class HelloRobot_EncoderAuton extends LinearOpMode {
private DcMotor leftmotor;
private DcMotor rightmotor;

static final double     COUNTS_PER_MOTOR_REV    = 28.0;
static final double     DRIVE_GEAR_REDUCTION    = 30.24;
static final double     WHEEL_CIRCUMFERENCE_MM  = 90.0 * 3.14;

static final double     COUNTS_PER_WHEEL_REV    = COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION;
static final double     COUNTS_PER_MM           = COUNTS_PER_WHEEL_REV / WHEEL_CIRCUMFERENCE_MM;

COUNTS_PER_WHEEL_REVwill be created as a separate variable fromCOUNTS_PER_MM as it is used in calculating a target velocity.

Last updated