Announcement

Collapse
No announcement yet.

Encoders not working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Encoders not working

    Hi guys, I am trying to program autonomous with encoders, but either the code or the encoders aren't working. Here is my code:
    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);
       }
    }
    I am sure the encoders are plugged in right, and I am not sure what else the problem could be. Any help welcome.

  • #2
    You haven't converted a desired distance in inches to distance in encoder counts. You call setTargetPosition with a value of 2. For NeveRest 40s, this would be attempting to move an almost imperceptible 2/1120 of a wheel (really motor) revolution.
    The motor controller PID won't be able to actually make the motors move this small distance, because the power that it sets will not be enough to overcome friction.
    For converting between a distance in inches and encoder counts, check some of the FTC SDK examples.

    Comment


    • #3
      Ok, thanks for the quick reply. I will try a greater number.

      Comment


      • #4
        If you are using NeverRest 40s and direct gearing, a target of 1120 should make your bot move forward 1 full wheel rotation.

        Comment

        Working...
        X