Announcement

Collapse
No announcement yet.

running with encoders help!!

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

  • running with encoders help!!

    Below is our code, we are trying to run using encoders straight for 24 inches then turn right and then go forward at our "approach speed". Our robot goes forward 24 inches, then goes directly to approach speed and skips the right turn. Any help would be appreciated!

    int initLeftCount = robot.leftMotor.getCurrentPosition();
    int tgLeftCount = initLeftCount + 89;
    int initRightCount = robot.rightMotor.getCurrentPosition();
    int tgRightCount = initRightCount + 89;

    //Drive towards line for 24 inches at high speed
    robot.leftMotor.setPower(1.0);
    robot.rightMotor.setPower(1.0);

    while (opModeIsActive() && (robot.rightMotor.getCurrentPosition() < tgRightCount * 24));

    while (opModeIsActive() && (robot.leftMotor.getCurrentPosition() < tgLeftCount *24));


    //turn right
    robot.leftMotor.setPower(1.0);
    robot.rightMotor.setPower(0);

    while (opModeIsActive() && (robot.leftMotor.getCurrentPosition() < tgLeftCount *10));
    //reset motors
    robot.leftMotor.setPower(0);
    robot.rightMotor.setPower(0);

    // Start the robot moving forward, and then begin looking for a white line.
    robot.leftMotor.setPower(APPROACH_SPEED);
    robot.rightMotor.setPower(APPROACH_SPEED);

    // run until the white line is seen OR the driver presses STOP;
    while (opModeIsActive() && (lightSensor.getLightDetected() < WHITE_THRESHOLD)) {

    // Display the light level while we are looking for the line
    telemetry.addData("Light Level", lightSensor.getLightDetected());
    Color.RGBToHSV(colorSensor.red() * 8, colorSensor.green() * 8, colorSensor.blue() * 8, hsvValues);
    telemetry.addData("Red ", colorSensor.red());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);
    telemetry.update();
    }
    while (opModeIsActive() && (!touchSensor.isPressed()))
    {
    {

  • #2
    Originally posted by thensley View Post
    Below is our code, we are trying to run using encoders straight for 24 inches then turn right and then go forward at our "approach speed". Our robot goes forward 24 inches, then goes directly to approach speed and skips the right turn. Any help would be appreciated!

    int initLeftCount = robot.leftMotor.getCurrentPosition();
    int tgLeftCount = initLeftCount + 89;
    int initRightCount = robot.rightMotor.getCurrentPosition();
    int tgRightCount = initRightCount + 89;

    //Drive towards line for 24 inches at high speed
    robot.leftMotor.setPower(1.0);
    robot.rightMotor.setPower(1.0);

    while (opModeIsActive() && (robot.rightMotor.getCurrentPosition() < tgRightCount * 24));

    while (opModeIsActive() && (robot.leftMotor.getCurrentPosition() < tgLeftCount *24));


    //turn right
    robot.leftMotor.setPower(1.0);
    robot.rightMotor.setPower(0);

    while (opModeIsActive() && (robot.leftMotor.getCurrentPosition() < tgLeftCount *10));
    //reset motors
    robot.leftMotor.setPower(0);
    robot.rightMotor.setPower(0);

    // Start the robot moving forward, and then begin looking for a white line.
    robot.leftMotor.setPower(APPROACH_SPEED);
    robot.rightMotor.setPower(APPROACH_SPEED);

    // run until the white line is seen OR the driver presses STOP;
    while (opModeIsActive() && (lightSensor.getLightDetected() < WHITE_THRESHOLD)) {

    // Display the light level while we are looking for the line
    telemetry.addData("Light Level", lightSensor.getLightDetected());
    Color.RGBToHSV(colorSensor.red() * 8, colorSensor.green() * 8, colorSensor.blue() * 8, hsvValues);
    telemetry.addData("Red ", colorSensor.red());
    telemetry.addData("Blue ", colorSensor.blue());
    telemetry.addData("Hue", hsvValues[0]);
    telemetry.update();
    }
    while (opModeIsActive() && (!touchSensor.isPressed()))
    {
    {
    By the time you have driven forward 24 inches, your left motor is probably already > tgLeftCount *10, so it drops right through to the next command.

    I would add some telemetry data to see what encoder values you are getting.

    You also may want to combine your 2 while statements for driving forward. If your left motor reaches first, your current code may go a tiny bit further than you want. Or you could use RUN_TO_POSITION instead so it will ramp down as it approaches your target.

    Comment


    • #3
      is there an easy way to simply reset the encoders?

      Comment


      • #4
        Originally posted by thensley View Post
        is there an easy way to simply reset the encoders?
        Code:
        robot.leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
        robot.rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);

        Comment


        • #5
          We tried that and the robot simply stops after going forward 24 inches.

          int initLeftCount = robot.leftMotor.getCurrentPosition();
          int tgLeftCount = initLeftCount + 89;
          int initRightCount = robot.rightMotor.getCurrentPosition();
          int tgRightCount = initRightCount + 89;

          //Drive towards line for 24 inches at high speed
          robot.leftMotor.setPower(1.0);
          robot.rightMotor.setPower(1.0);

          while (opModeIsActive() && (robot.rightMotor.getCurrentPosition() < tgRightCount * 24));

          while (opModeIsActive() && (robot.leftMotor.getCurrentPosition() < tgLeftCount *24));

          robot.leftMotor.setMode(DcMotor.RunMode.STOP_AND_R ESET_ENCODER);
          robot.rightMotor.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
          //turn right
          robot.leftMotor.setPower(1.0);
          robot.rightMotor.setPower(0);

          while (opModeIsActive() && (robot.leftMotor.getCurrentPosition() < tgLeftCount *10));
          //reset motors
          robot.leftMotor.setPower(0);
          robot.rightMotor.setPower(0);

          // Start the robot moving forward, and then begin looking for a white line.
          robot.leftMotor.setPower(APPROACH_SPEED);
          robot.rightMotor.setPower(APPROACH_SPEED);

          // run until the white line is seen OR the driver presses STOP;
          while (opModeIsActive() && (lightSensor.getLightDetected() < WHITE_THRESHOLD)) {

          Comment


          • #6
            In general, I would advise against resetting encoders. It takes time to do so, while the alternative can be measured in nanoseconds. Instead of resetting the encoders, just offset the value you want to reach by the current position.

            Say you want to move 10 inches forward. It appears 89 encoder counts = 1 inch. Therefore, you would do something like this:
            Code:
            int targetEncoderValue = robot.leftMotor.getCurrentPosition() + (89 * 10) // 10 inches in encoder counts
            robot.leftMotor.setPower( 1.0 )
            while( opModeIsActive() && robot.leftMotor.getCurrentPosition < targetEncoderValue )); // wait for the motor to move
            If the robot previously moved to X encoder counts, you want to move to X + target distance in encoder counts.

            Comment

            Working...
            X