Announcement

Collapse
No announcement yet.

Encoders Moving in a circle

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

  • Encoders Moving in a circle

    I was recently trying to use encoders but it just goes into an endless circle and the robot just keeps rotating. So I was wondering if anyone had an idea of what's going on.
    Attached Files

  • #2
    Since the left and right motors are mounted 180 degrees from each other giving both a power of 1 they will rotate in opposite directions.
    Run To Position does not use plus or minus in setPower, it will always rotate the motors in whatever direction it takes to get to the set Target Position. Typically just put in positive power values.

    Add setDirection to one half of the robot (Might do right instead, I can never remember which one makes positive numbers forward.) to get all the wheels rotating the same direction.
    backLeft.setDirection(DcMotorSimple.Direction.REVE RSE); With the stop() the motors ran to 1120 and stopped on my test bot.
    Without the stop() it will run forever with a pause at every 1120 because your main section of the program runs your EncoderDrive over and over again. Get rid of while (opModeActive)

    Also put in telemetry in the isBusy while loop. If you don't see the encoders incrementing, your encoders aren't getting the data to the program, so that is another way the program will run forever. Check your wiring in that case.

    Comment


    • #3
      Since the left and right motors are mounted 180 degrees from each other giving both a power of 1 they will rotate in opposite directions.
      Run To Position does not use plus or minus in setPower, it will always rotate the motors in whatever direction it takes to get to the set Target Position. Typically just put in positive power values.

      Add setDirection to one half of the robot (Might do right instead, I can never remember which one makes positive numbers forward.) to get all the wheels rotating the same direction.

      backLeft.setDirection(DcMotorSimple.Direction.REVE RSE);

      With the stop() the motors ran to 1120 and stopped on my test bot.
      Without the stop() it will run forever with a pause at every 1120 because your main section of the program runs your EncoderDrive over and over again. Get rid of while (opModeActive)

      Also put in telemetry in the isBusy while loop. If you don't see the encoders incrementing, your encoders aren't getting the data to the program, so that is another way the program will run forever. Check your wiring in that case.

      Comment


      • #4
        Originally posted by 3805Mentor View Post
        Since the left and right motors are mounted 180 degrees from each other giving both a power of 1 they will rotate in opposite directions.
        Run To Position does not use plus or minus in setPower, it will always rotate the motors in whatever direction it takes to get to the set Target Position. Typically just put in positive power values.

        Add setDirection to one half of the robot (Might do right instead, I can never remember which one makes positive numbers forward.) to get all the wheels rotating the same direction.

        backLeft.setDirection(DcMotorSimple.Direction.REVE RSE);

        With the stop() the motors ran to 1120 and stopped on my test bot.
        Without the stop() it will run forever with a pause at every 1120 because your main section of the program runs your EncoderDrive over and over again. Get rid of while (opModeActive)

        Also put in telemetry in the isBusy while loop. If you don't see the encoders incrementing, your encoders aren't getting the data to the program, so that is another way the program will run forever. Check your wiring in that case.
        Its moving straight now thank you but it isn't stopping and the encoder wires are all the way in but i'm not sure what the problem could be. (thanks for running it yourself)

        Comment


        • #5
          Originally posted by 8129_Aidan View Post

          Its moving straight now thank you but it isn't stopping and the encoder wires are all the way in but i'm not sure what the problem could be. (thanks for running it yourself)
          Follow the rest of 3805Mentor's advice and put telemetry statements in your motor waiting loop.
          It's likely that there is still a problem with the encoders, or your code.
          What motors are you using? If they are not REV motors you need to use the level converters... it's possible to plug these in the wrong way.

          Can you post your code here so we can see what may need to be corrected.

          Use the # button in the toolbar to wrap your code so it displays correctly.

          Comment


          • #6
            Originally posted by Philbot View Post

            Follow the rest of 3805Mentor's advice and put telemetry statements in your motor waiting loop.
            It's likely that there is still a problem with the encoders, or your code.
            What motors are you using? If they are not REV motors you need to use the level converters... it's possible to plug these in the wrong way.

            Can you post your code here so we can see what may need to be corrected.

            Use the # button in the toolbar to wrap your code so it displays correctly.
            I got it working but only 1 motor goes the correct amount of ticks and one of them jumps at the end.(there are 4 motors)

            Code:
            import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
            import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
            import com.qualcomm.robotcore.hardware.DcMotor;
            
            /**
             * Created by phs-robotics on 1/16/2018.
             */
            @Autonomous(name = "TestEncoder")
            public class EncoderMethod extends LinearOpMode {
            
                DcMotor frontLeft = null;
                DcMotor backLeft = null;
                DcMotor frontRight = null;
                DcMotor backRight = null;
            
                public void runOpMode() throws NullPointerException {
            
                    frontLeft = hardwareMap.dcMotor.get("frontLeftDrive");
                    backLeft = hardwareMap.dcMotor.get("backLeftDrive");
                    frontRight = hardwareMap.dcMotor.get("frontRightDrive");
                    backRight = hardwareMap.dcMotor.get("backRightDrive");
            
                    frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); //Resets encoders
                    backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
                    frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
                    backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
            
            
                    frontLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER); //Sets mode to run to postion
                    backLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);  // Encoders are enabled
                    frontRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
                    backRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
            
                    frontLeft.setDirection(DcMotor.Direction.REVERSE);
                    backLeft.setDirection(DcMotor.Direction.REVERSE);
            
                    telemetry.addData("Path", "Starting at :%7d :%7d", //Shows the position of the robot on the phone
                            frontLeft.getCurrentPosition(),
                            backLeft.getCurrentPosition(),
                            frontRight.getCurrentPosition(),
                            backRight.getCurrentPosition());
                    telemetry.update();
            
                    waitForStart();
            
                    EncoderDrive(0.5, 1120);
            
                }
            
            
                public void EncoderDrive(double power, int distance) {
            
                    frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); //Resets encoders
                    backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
                    frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
                    backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
            
                    frontLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION); //Sets mode to run to postion
                    backLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);  // Encoders are enabled
                    frontRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
                    backRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
            
                    frontLeft.setTargetPosition(distance);
                    backLeft.setTargetPosition(distance);
                    frontRight.setTargetPosition(distance);
                    backRight.setTargetPosition(distance);
            
                    frontLeft.setPower(power);
                    backLeft.setPower(power);
                    frontRight.setPower(power);
                    backRight.setPower(power);
            
                    while(opModeIsActive() && frontLeft.isBusy() && backLeft.isBusy() && frontRight.isBusy() && backRight.isBusy())
                    {
                        telemetry.addData("Path", "Starting at :%7d :%7d", //Shows the position of the robot on the phone
                            frontLeft.getCurrentPosition(),
                            backLeft.getCurrentPosition(),
                            frontRight.getCurrentPosition(),
                            backRight.getCurrentPosition());
                        telemetry.update();
                    }
            
                    stop();
            
                    frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); //Resets encoders
                    backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
                    frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
                    backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
            
                }
            
            }

            Comment


            • #7
              Originally posted by 8129_Aidan View Post

              I got it working but only 1 motor goes the correct amount of ticks and one of them jumps at the end.(there are 4 motors)

              Code:
              import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
              import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
              import com.qualcomm.robotcore.hardware.DcMotor;
              
              /**
              * Created by phs-robotics on 1/16/2018.
              */
              @Autonomous(name = "TestEncoder")
              public class EncoderMethod extends LinearOpMode {
              
              DcMotor frontLeft = null;
              DcMotor backLeft = null;
              DcMotor frontRight = null;
              DcMotor backRight = null;
              
              public void runOpMode() throws NullPointerException {
              
              frontLeft = hardwareMap.dcMotor.get("frontLeftDrive");
              backLeft = hardwareMap.dcMotor.get("backLeftDrive");
              frontRight = hardwareMap.dcMotor.get("frontRightDrive");
              backRight = hardwareMap.dcMotor.get("backRightDrive");
              
              frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); //Resets encoders
              backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              
              
              frontLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER); //Sets mode to run to postion
              backLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER); // Encoders are enabled
              frontRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
              backRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
              
              frontLeft.setDirection(DcMotor.Direction.REVERSE);
              backLeft.setDirection(DcMotor.Direction.REVERSE);
              
              telemetry.addData("Path", "Starting at :%7d :%7d", //Shows the position of the robot on the phone
              frontLeft.getCurrentPosition(),
              backLeft.getCurrentPosition(),
              frontRight.getCurrentPosition(),
              backRight.getCurrentPosition());
              telemetry.update();
              
              waitForStart();
              
              EncoderDrive(0.5, 1120);
              
              }
              
              
              public void EncoderDrive(double power, int distance) {
              
              frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); //Resets encoders
              backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              
              frontLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION); //Sets mode to run to postion
              backLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION); // Encoders are enabled
              frontRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
              backRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
              
              frontLeft.setTargetPosition(distance);
              backLeft.setTargetPosition(distance);
              frontRight.setTargetPosition(distance);
              backRight.setTargetPosition(distance);
              
              frontLeft.setPower(power);
              backLeft.setPower(power);
              frontRight.setPower(power);
              backRight.setPower(power);
              
              while(opModeIsActive() && frontLeft.isBusy() && backLeft.isBusy() && frontRight.isBusy() && backRight.isBusy())
              {
              telemetry.addData("Path", "Starting at :%7d :%7d", //Shows the position of the robot on the phone
              frontLeft.getCurrentPosition(),
              backLeft.getCurrentPosition(),
              frontRight.getCurrentPosition(),
              backRight.getCurrentPosition());
              telemetry.update();
              }
              
              stop();
              
              frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER); //Resets encoders
              backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
              
              }
              
              }
              Sorry 3 of the motors work properly.

              Comment


              • #8
                Not using this approach, but from a quick scan of your code, this is what I'd guess is happening:

                While (A && B && C && D) is only true as long as A is true AND B is true AND C is true AND D is true. If any one of these conditions ceases to be true, it's all stop.

                In this case, A, B, C, and D appear to be true only until the corresponding motor reaches it's position. If one of them hits early, all get kicked out.
                If one lags a little, the loop still ends when the other 3 are complete.
                Mentor, teams 8578 & 11959

                Comment


                • #9
                  Run to Position uses PID control on the motors. All 4 motors should be very close to the same encoder count. If one lags significantly it would have to have so much friction that full power couldn't keep it up to speed of the others. If one encoder wasn't working that motor would run much faster than the others, the encoder count would never reach target, so one of the other motors would have to stop the loop.

                  He could use or instead of and. Then the robot would stop when the last motor reached target instead of the first, but they should all still be close to the same count.

                  If he takes out the stop() and the stop and reset encoders at the end of the encoderDrive sub, then even after the isBusy loop is done the motors will continue to target and hold on that position. He will also need an infinite loop at the end of the main code to keep power on the motors. So after calling encoderDrive do a while (opModeIsActive) {telemetry read encoders} That way you can see if the encoders are working as the program won't shut down and since there is still setPower the motors will continue to hold target position.
                  If he puts setPowers to zero after returning from encoderDrive before the telemetry loop, it would show if a motor is lagging or leading as they'll be stopped in their incorrect positions with telemetry still reporting.

                  Comment

                  Working...
                  X