Calculating Target Position

In the initial introduction to run to position, you worked through the calculations needed to convert the ticks per rotation of a motor into ticks per mm moved. Now we want to focus on how to convert ticks per rotation of the motor to ticks per degree moved. From the previous section you should have a rough estimate of the amount of ticks you need to get to the 90 degree position. The goal of this section is to work through how to get a more exact position.

To start you will need some of the same variables we used in Encoder Navigation:

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. We saw this in the Encoder Navigation section when the ticks per revolution at the motor was different from the ticks per revolution of the wheel. As motion is transmitted from a motor to a mechanism, the resolution of the encoder ticks changes.

For more information on the effect of motion transmission across a mechanism check out the Compound Gearing section.

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.

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

In the Core Hex Motor specifications 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. 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. 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.

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

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

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

Ticks per revolution

288 ticks

Total gear reduction

2.777778

Now that we have this information lets create two constant variables:

  • COUNTS_PER_MOTOR_REV

  • GEAR_REDUCTION

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 COUNTS_PER_MOTOR_REV and GEAR_REDUCTIONvariables to the op mode beneath where the hardware variables are created.

public class HelloRobot_ArmControl extends LinearOpMode {
    private DcMotor arm;
    
    static final double     COUNTS_PER_MOTOR_REV    = 288; 
    static final double     GEAR_REDUCTION    = 2.7778;   

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 used in Converting Encoder Ticks for ourCOUNTS_PER_WHEEL_REVvariable. So to get this variable we can multipleCOUNTS_PER_MOTOR_REV by GEAR_REDUCTION.

static final double  COUNTS_PER_GEAR_REV    = COUNTS_PER_MOTOR_REV * GEAR_REDUCTION;

To calculate the number of counts per degree or moved or COUNTS_PER_DEGREEdivide the COUNTS_PER_GEAR_REVvariable by 360.

static final double     COUNTS_PER_DEGREE    = COUNTS_PER_GEAR_REV/360;

Add both these variables to the op mode.

public class HelloRobot_ArmControl extends LinearOpMode {
    private DcMotor arm;
    
    static final double     COUNTS_PER_MOTOR_REV    = 288; 
    static final double     GEAR_REDUCTION    = 2.7778;   
    static final double     COUNTS_PER_GEAR_REV    = COUNTS_PER_MOTOR_REV * GEAR_REDUCTION;
    static final double     COUNTS_PER_DEGREE    = COUNTS_PER_GEAR_REV/360;

Finally we need to create a non-constant variable that will act as our position. Create a variable called armPosition above the waitForStart(); command.

public void runOpMode() {
        arm = hardwareMap.get(DcMotor.class, "arm");
        
        int armPosition;
        
        waitForStart();

Add this variable to the if(gaempad1.dpad_up) section of the Target Position if/else ifstatement, as this section dictates the 90 degree position. To get to the 90 degree position, the arm needs to move roughly 45 degrees. Set arm position equal to COUNTS_PER_DEGREEtimes 45.

Recall that setTargetPosition()requires an integer to be its parameter. When defining armPosition remember to add 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.

armPosition = (int)(COUNTS_PER_DEGREE * 45);
while (opModeIsActive()) {

   if(gamepad1.dpad_up){
         armPosition = (int)(COUNTS_PER_DEGREE * 45);
         arm.setTargetPosition(83);
         arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
         arm.setPower(0.4);
                 }

Set target position to armPostion.

if(gamepad1.dpad_up){
     armPosition = (int)(COUNTS_PER_DEGREE * 45);
     arm.setTargetPosition(armPosition);
     arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
     arm.setPower(0.4);
            }
else if (gamepad1.dpad_down){
      arm.setTargetPosition(0);
      arm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
      arm.setPower(0.4);
            } 

We could change what armPosition is equal to in the gamepad1.dpad_down portion of the if/else if statement such as:

else if (gamepad1.dpad_down){
      armPosition = (int)(COUNTS_PER_DEGREE * 0);
      arm.setTargetPosition(armPosition);
      arm.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
      arm.setPower(0.4);
            } 

In this case we would consistently redefine armPosition to match the needs of whatever positions we want to create. Since our only two positions at the moment are our starting position and our 90 degree position it isn't necessary However, it is a good practice to create a variable in situations like this. If we want to add another position later, we can easily edit the variable to fit our needs.

Last updated