Hi guys, I am trying to program autonomous with encoders, but either the code or the encoders aren't working. Here is my code:
I am sure the encoders are plugged in right, and I am not sure what else the problem could be. Any help welcome.
Code:
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.DcMotorSimple; public class EncoderTest extends LinearOpMode { DcMotor motorRight; DcMotor motorLeft; public void runOpMode() throws InterruptedException { motorRight = hardwareMap.dcMotor.get("right"); motorLeft = hardwareMap.dcMotor.get("left"); motorLeft.setDirection(DcMotorSimple.Direction.REVERSE); motorLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER); motorRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER); waitForStart(); DriveForwardDistance(1, 2); turnLeft(1); time(300); turnRight(1); time(300); driveForward(1); time(2000); } public void DriveForwardDistance(double power, int distance){ motorLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); motorRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); //Set target position motorLeft.setTargetPosition(distance); motorRight.setTargetPosition(distance); motorLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION); motorRight.setMode(DcMotor.RunMode.RUN_TO_POSITION); //set Drive Power driveForward(power); while(motorLeft.isBusy() && motorRight.isBusy()){ } stopMotors(); motorLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER); motorRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER); } public void driveForward(double power) { motorLeft.setPower(power); motorRight.setPower(power); } public void driveBack(double power) { motorLeft.setPower(-power); motorRight.setPower(-power); } public void turnLeft(double power) { motorLeft.setPower(-power); motorRight.setPower(power); } public void turnRight(double power) { motorLeft.setPower(power); motorRight.setPower(-power); } public void stopMotors() { driveForward(0); } public void time(long Time) throws InterruptedException { sleep(Time); } }
Comment