# Moving to a Target Distance

Now that you have created the constant variables needed to calculate the amount of ticks per mm moved, you can use this to set a target distance. For instance, if you would like to have the robot move forward two feet, converting from feet to millimeters and multiplying by the COUNTS_PER_MMwill give you the amount of counts (or ticks) needed to reach that distance.

Create two more variables called leftTarget andrightTarget. These variables can be fluctuated and edited in your code to tell the motors what positions to go to, rather than place them with the constant variables, create these variables within the op mode but above the waitForStart(); command.

The setTargetPosition();function takes in a integer (or int) data type as its parameter, rather than a double. Since both the leftTarget and rightTargetwill be used to set the target position, create both variables as int variables.

public void runOpMode() {
leftmotor = hardwareMap.get(DcMotor.class, "leftmotor");
rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");

rightmotor.setDirection(DcMotor.Direction.REVERSE);

leftmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);

int leftTarget;
int rightTarget;

waitForStart();

Right now the main distance factor is COUNTS_PER_MM , however you may want to go a distance that is in the imperial system, such as 2 feet (or 24 inches). The target distance in this case will need to be converted to mm. To convert from feet to millimeters use the following formula:

$d_{(mm)} = d_{(ft)} × 304.8$

If you convert 2 feet to millimeters, it comes out the be 609.6 millimeters. For the purpose of this guide, lets go ahead an round this to be 610 millimeters. Multiply 610 millimeters by theCOUNTS_PER_MM variable to get the number of ticks needed to move the robot 2 feet. Since the intent is to have the robot move in a straight line, set both the leftTarget and rightTarget, to be equal to 610 * COUNTS_PER_MM

As previously mentioned the setTargetPosition(); function requires that its parameter must be an integer data type. The leftTargetand rightTargetvariables have been set to be integers, however theCOUNTS_PER_MM variable is a double. Since these are two different data types, a conversion of data types needs to be done.

In this case theCOUNTS_PER_MMneeds to be converted to an integer. This is as simple as adding the line (int) in front of the double variable. However, you need to be cautious of potential rounding errors. Since COUNTS_PER_MMis part of an equation it is recommended that you convert to an integer after the result of the equation is found. The example of how to do this is exhibited below.

int leftTarget = (int)(610 * COUNTS_PER_MM);
int rightTarget = (int)(610 * COUNTS_PER_MM);

Edit the setTargetPosition(); lines so that both motors are set to the appropriate target position. To do this add the leftTargetand rightTarget variables to their respective motor.

leftmotor.setTargetPosition(leftTarget);
rightmotor.setTargetPosition(rightTarget);

Try running the code and observing the behavior of the robot. Consider some of the following

• Is the robot moving forward by two feet?

• Does the robot seem to be moving in straight line?

• Is the code running without error?

Last updated