Velocity is a closed loop control within the SDK that uses the encoder counts to determine the approximate power/speed the motors need to go in order to meet the set velocity.
To set a velocity, its important to understand the maximum velocity in RPM your motor is capable of. For the Class Bot V2 the motors are capable of a maximum RPM of 300. With a drivetrain, you are likely to get better control by setting velocity lower than the maximum. In this case, lets set the velocity to 175 RPM!
Since RPM is the amount of revolutions per minute, a conversion needs to be made from RPM to ticks per second (TPS). To do this, divide the RPM by 60 to get the amount of rotations per second.
Rotations per second can then be multiplied by COUNTS_PER_WHEEL_REV, to get the amount of ticks per second.
Create a new double variable called TPS. AddTPS the to the OpMode under where rightTarget is defined:
Exchange the setPower();functions for setVelocity();
Add TPS as the parameter for setVelocity();
With the velocity set, let's give our program a test run after building!
With the current state of the code you are likely to get errors similar to the ones pictured below:
This is because the setVelocity();function is a function of theDcMotorEx Interface. The DcMotorEx Interface is an extension of the DcMotor Interface, that provides enhanced motor functionality, such as access to closed loop control functions.
To use setVelocity(); the motor variables need to be changed to DcMotorEx as seen below:
Often times, like in the program created during Part 2: Robot Control, we use setPower(); to set the drivetrain motors to a set power or power based on a joystick's inputs. The combined power going to both motors help to determine the direction the robot moves or turns.
However, in RUN_TO_POSITION mode the encoder counts are used instead to dictate directionality of the motor.
Since speed an directionality impacts how a robot turns, target position and velocity need to be edited to get the robot to turn. Consider the following code:
The rightTarget has been changed to be a negative target position. Assuming that the encoder starts at zero due to STOP_AND_RESET_ENCODER this causes the robot to turn to the right.
Notice the velocity is the same for both motors. If you try running this code, you can see that the robot pivots along its center of rotation.
To get a wider turn, try changing the velocity so that the right motor is running at a lower velocity than the left motor. Adjust the velocity and target position as needed to get the turn you need.

int leftTarget = (int)(610 * COUNTS_PER_MM);
int rightTarget = (int)(-610 * COUNTS_PER_MM);
double TPS = (100/ 60) * COUNTS_PER_WHEEL_REV;
waitForStart();
leftmotor.setTargetPosition(leftTarget);
rightmotor.setTargetPosition(rightTarget);
leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftmotor.setVelocity(TPS);
rightmotor.setVelocity(TPS);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)(610 * COUNTS_PER_MM);
int rightTarget = (int)(610 * COUNTS_PER_MM);
double TPS = (175/60) * COUNTS_PER_WHEEL_REV;
waitForStart();waitForStart();
leftmotor.setTargetPosition(leftTarget);
rightmotor.setTargetPosition(rightTarget);
leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftmotor.setVelocity(TPS);
rightmotor.setVelocity(TPS);
while (opModeIsActive() && (leftmotor.isBusy() && rightmotor.isBusy())){
}public class HelloRobot_Encoder extends LinearOpMode {
private DcMotorEx leftmotor;
private DcMotorEx rightmotor;public void runOpMode() {
leftmotor = hardwareMap.get(DcMotorEx.class, "leftmotor");
rightmotor = hardwareMap.get(DcMotorEx.class, "rightmotor");@Autonomous
public class HelloRobot_Encoder extends LinearOpMode {
private DcMotorEx leftmotor;
private DcMotorEx 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;
@Override
public void runOpMode() {
leftmotor = hardwareMap.get(DcMotorEx.class, "leftmotor");
rightmotor = hardwareMap.get(DcMotorEx.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)(610 * COUNTS_PER_MM);
int rightTarget = (int)(610 * COUNTS_PER_MM);
double TPS = (175/ 60) * COUNTS_PER_WHEEL_REV;
waitForStart();
leftmotor.setTargetPosition(leftTarget);
rightmotor.setTargetPosition(rightTarget);
leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftmotor.setVelocity(TPS);
rightmotor.setVelocity(TPS);
while (opModeIsActive() && (leftmotor.isBusy() && rightmotor.isBusy())) {
}
}
}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_MM will 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 OpMode but above the waitForStart(); command.
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:
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.
Next, multiply 610 millimeters by the COUNTS_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 the COUNTS_PER_MM needs 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_MM is part of an equation it is recommended that you convert to an integer AFTER the result of the equation is found.
Lastly, edit the setTargetPosition(); lines so that both motors are set to the appropriate target position. To do this add the leftTarget and rightTarget variables to their respective motor:
Moving the motors to a specific position, using the encoders, removes any potential inaccuracies or inconsistencies from using Elapsed Time. The focus of this section is to move the robot to a target position using encoders.
The OpMode structure below is simplified and only includes the necessary components needed to create the encoder based code.
Before diving in too far, recall that for certain , like the , one of the motors needs to be reversed as the motors are mirrored.
In our example, we are addingrightmotor.setDirection(DcMotor.Direction.REVERSE); to the code as seen below:
As introduced in , using RUN_TO_POSITION mode requires a three step process.
The first step is setting target position. To do so, add the lines leftmotor.setTargetPosition(1000);
int leftTarget = (int)(610 * COUNTS_PER_MM);
int rightTarget = (int)(610 * COUNTS_PER_MM);leftmotor.setTargetPosition(leftTarget);
rightmotor.setTargetPosition(rightTarget);rightmotor.setTargetPosition(1000); waitForStart();If we want our robot to travel a specific distance we will need to do a bit of math beforehand to calculate the TargetPosition. But for now let's start simple by setting the target position to 1000 ticks.
The next step is to set both motors to the RUN_TO_POSITIONmode. Add the lines leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);and rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);to your code, beneath the setTargetPosition code lines.
As mentioned, normally there would be more math involved to help determine how fast the motors should move to reach the desired position. But for testing purposes, we are going to start by keeping it simple! Since the setPower function was covered in and will communicate to the system what relative speed (or in this case duty cycle) is needed to get to the target, this can be used in the place of setVelocity for now.
Add the lines to set the power of both motors to 80% of duty cycle.
Build your OpMode and give it a test. What happens once you press play? What happens if you stop the program then start it again?
For our demo code we will want to request our motors reset their encoders during the initialization process of the program.
Right before waitForStart(); we can add leftmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); and rightmotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); to our OpMode.
Let's say we want our program to run only for however long it takes for the motors to reach designated position. Or maybe we intend for the robot to do something else after reaching the destination. For this we will need to edit our whileLoop block!
To the whileLoop let's add the leftmotor.isBusy() and righmotor.isBusy()functions. This will check if the left motor and right motor are busy running to a target position. Once either motor reaches the target position the program will stop.
Build your OpMode and give it a try!
As soon as the motors hit the desired position the program will end instead of continuously run in the event they do not perfectly hit the position.
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
@Autonomous //sets the op mode as an autonomous op mode
public class HelloRobot_Encoder extends LinearOpMode {
private DcMotor leftmotor;
private DcMotor rightmotor;
@Override
public void runOpMode() {
leftmotor = hardwareMap.get(DcMotor.class, "leftmotor");
rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");
// Wait for the game to start (driver presses PLAY)
waitForStart();
// run until the end of the match (driver presses STOP)
while (opModeIsActive()){
}
}
}
public void runOpMode() {
leftmotor = hardwareMap.get(DcMotor.class, "leftmotor");
rightmotor = hardwareMap.get(DcMotor.class, "rightmotor");
rightmotor.setDirection(DcMotor.Direction.REVERSE);
waitForStart();waitForStart();
leftmotor.setTargetPosition(1000);
rightmotor.setTargetPosition(1000);
while (opModeIsActive()){
}waitForStart();
leftmotor.setTargetPosition(1000);
rightmotor.setTargetPosition(1000);
leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
while (opModeIsActive()){
}waitForStart();
leftmotor.setTargetPosition(1000);
rightmotor.setTargetPosition(1000);
leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftmotor.setPower(0.8);
rightmotor.setPower(0.8);
while (opModeIsActive()){
}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);
waitForStart();while (opModeIsActive() && (leftmotor.isBusy() && rightmotor.isBusy())) {
}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.
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.
Reminder that the basis for this guide is the . 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.
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:
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.
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 .
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.
Using the compound gearing formula for the Class Bot V2 the total gear reduction is:
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
You can calculate this by hand, but for the purpose of this guide, this can be calculated within the code.
To summarize, for the Class Bot V2 the following information is true:
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
We'll add the to the initialization section of the OpMode:
To ensure variables are referenceable they are set as static final double variables. Static allows references to the variables anywhere within the class. 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.
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.
To calculate counts per wheel revolution multiple COUNTS_PER_MOTOR_REV by DRIVE_GEAR_REDUCTION Use the following formula:
Where:
= COUNTS_PER_MOTOR_REV
= DRIVE_GEAR_REDUCTION
= COUNTS_PER_WHEEL_REV
Once COUNTS_PER_WHEEL_REV is calculated, use it 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.
Where,
= COUNTS_PER_MOTOR_REV
= DRIVE_GEAR_REDUCTION
= WHEEL_CIRCUMFERENCE_MM
= COUNTS_PER_WHEEL_REV
= COUNTS_PER_MM
Ticks per revolution
28 ticks
Total gear reduction
30.21
Circumference of the wheel
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;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 =
static final double COUNTS_PER_MM =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 = 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;package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
@Autonomous //sets the op mode as an autonomous op mode
public class HelloWorld_Encoder 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;
@Override
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);
// Wait for the game to start (driver presses PLAY)
waitForStart();
leftmotor.setTargetPosition(1000);
rightmotor.setTargetPosition(1000);
leftmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightmotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftmotor.setPower(0.8);
rightmotor.setPower(0.8);
// run until the end of the match (driver presses STOP)
while (opModeIsActive() && (leftmotor.isBusy() && rightmotor.isBusy())) {
}
}
}COUNTS_PER_WHEEL_REVwill be created as a separate variable fromCOUNTS_PER_MM as it is used in calculating a target velocity.