Encoder Autonomous
Use motor feedback for measured movement.
Use motor feedback for measured movement.
In this lesson, you will:
Encoder autonomous improves timed movement by measuring motor rotation. It still does not prove field position, but it gives code a feedback signal.
Encoders tell how far shafts turned, not whether the robot perfectly moved on the field. Wheel slip and collisions still matter.
Reset or record starting ticks, compute a target, drive while error remains and timeout has not passed, then stop. Print current ticks and reason for exit.
EncoderAuto.javaJava
int start = leftMotor.getCurrentPosition();
int target = start + 900;
String exitReason = "stopped";
runtime.reset();
try {
while (opModeIsActive()) {
int error = target - leftMotor.getCurrentPosition();
if (error <= 0) {
exitReason = "target";
break;
}
if (runtime.seconds() >= 2.0) {
exitReason = "timeout";
break;
}
setDrivePower(0.25, 0.25);
telemetry.addData("target", target);
telemetry.addData("position", leftMotor.getCurrentPosition());
telemetry.addData("error", error);
telemetry.addData("elapsed s", runtime.seconds());
telemetry.update();
}
} finally {
stopDrive();
}
telemetry.addData("exit", exitReason);
telemetry.update();Wrong target signs, unreset encoders, and run-mode confusion are common. If ticks do not change, check wiring and mode before rewriting logic.
Drive to a target tick count with a timeout and report whether the target or timeout ended the step.
Check your understanding before moving on.
0 of 2 answered