Run to Position Example
public void encoderDrive(double speed, double Revolutions, double timeoutS) {
int newTarget;
// Ensure that the opmode is still active
if (opModeIsActive()) {
// Determine new target position, and pass to motor controller
newTarget = motor.getCurrentPosition() + (int)(Revolutions * COUNTS_PER_MOTOR_REV);
motor.setTargetPosition(newTarget);
// Turn On RUN_TO_POSITION
motor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
runtime.reset();
motor.setPower(Math.abs(speed));
while (opModeIsActive() && (runtime.seconds() < timeoutS) &&
(motor.isBusy())) {
}
// Stop all motion;
motor.setPower(0);
// Turn off RUN_TO_POSITION
motor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// sleep(250); // optional pause after each move
}
}
Last updated
Was this helpful?