Encoder-Based Movement (OnBot Java)
What are Motor Encoders?
Motor encoders are sensors built into motors that measure rotation. In FTC, encoders allow you to move your robot a precise distance by counting the number of rotations or partial rotations of the wheels. This makes autonomous movement much more accurate than time-based methods. For more information on encoders, see Motor Encoders.
Using Encoders in FTC SDK
To use encoders in OnBot Java, you need to reset the encoders, set a target position, and run the motors to that position. The FTC SDK provides methods like setMode(), setTargetPosition(), and setPower() for this purpose.
Encoder-Based Movement Example
This example shows how to drive forward a set distance using encoders. Each step is explained in the comments.
@Autonomous(name="EncoderMove")
public class EncoderMove extends LinearOpMode {
private DcMotor leftDrive;
private DcMotor rightDrive;
@Override
public void runOpMode() {
leftDrive = hardwareMap.get(DcMotor.class, "left_drive");
rightDrive = hardwareMap.get(DcMotor.class, "right_drive");
// Reset encoders
leftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
// Set target position (e.g., 1000 encoder counts)
leftDrive.setTargetPosition(1000);
rightDrive.setTargetPosition(1000);
// Set to RUN_TO_POSITION mode
leftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// Start moving
leftDrive.setPower(0.5);
rightDrive.setPower(0.5);
// Wait until motors reach target
while (opModeIsActive() && (leftDrive.isBusy() || rightDrive.isBusy())) {
telemetry.addData("Left Position", leftDrive.getCurrentPosition());
telemetry.addData("Right Position", rightDrive.getCurrentPosition());
telemetry.update();
}
// Stop all motion
leftDrive.setPower(0);
rightDrive.setPower(0);
}
}
Writing Modular Encoder Movement Methods
To keep your code organized, write helper methods for common encoder-based movements. For example, create a driveForward(int counts, double power) method that handles all the encoder logic. This makes your code easier to read and reuse.
Modular Encoder Movement Method Example
This snippet shows how to write a helper method for driving forward a set distance using encoders.
public void driveForward(int counts, double power) {
leftDrive.setTargetPosition(leftDrive.getCurrentPosition() + counts);
rightDrive.setTargetPosition(rightDrive.getCurrentPosition() + counts);
leftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftDrive.setPower(power);
rightDrive.setPower(power);
while (opModeIsActive() && (leftDrive.isBusy() || rightDrive.isBusy())) {
telemetry.addData("Left Position", leftDrive.getCurrentPosition());
telemetry.addData("Right Position", rightDrive.getCurrentPosition());
telemetry.update();
}
leftDrive.setPower(0);
rightDrive.setPower(0);
}
Troubleshooting Encoder Issues
- If your robot doesn’t move as expected, check that your motors are properly mapped and encoders are connected.
- Make sure to reset encoders before starting a new movement.
- If the robot moves the wrong distance, verify your counts-per-revolution and gear ratio calculations.
- Use telemetry to monitor encoder values during testing.
Practice Exercise: Encoder-Based Turn
Write a helper method that turns the robot in place using encoders. Test it by turning 90 degrees. Use telemetry to display encoder values during the turn.
- Write a method to set opposite target positions for left and right motors.
- Use
RUN_TO_POSITIONmode and set appropriate power. - Monitor encoder values with telemetry.
- Stop motors after the turn is complete.