Encoder-Based Movement (OnBot Java)

intermediate50 min

Motor encoders

Built-in encoders count shaft rotation. Target counts give more repeatable moves than sleep() alone.

See Motor Encoders.

Encoder workflow

Reset with STOP_AND_RESET_ENCODER, set RUN_TO_POSITION, call setTargetPosition(), then setPower() and wait on isBusy().

Forward move example

@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);
    }
}

Helper methods

Wrap reset, target, wait, and stop in something like driveForward(int counts, double power).

driveForward helper

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

  • Names in code must match configuration; encoders must be wired.
  • Reset before each move.
  • Wrong distance usually means counts-per-tick or gear ratio math is off—log positions with telemetry.

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_POSITION mode and set appropriate power.
  • Monitor encoder values with telemetry.
  • Stop motors after the turn is complete.