Announcement

Collapse
No announcement yet.

Encoder Difficulties

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

  • Encoder Difficulties

    Hello, I am a programmer of Team 8866 and we are currently experiencing difficulties with our linear lift arm during the autonomous period. We are using encoders to calculate the number of rotations necessary to raise and then lower the lift arm. We have tested our code. However, outcomes are always inconsistent, with the lift arm sometimes working successfully, to occasionally jerking, to not working at all. Our current code is the following code:

    robot.lift1Motor.setMode(DcMotor.RunMode.RUN_USING _ENCODER);
    robot.lift1Motor.setPower(1);
    while(opModeIsActive()&&(robot.lift1Motor.getCurre ntPosition() < tgLift1Count * 40));
    robot.lift1Motor.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
    robot.lift1Motor.setPower(0);
    sleep(1500);

    robot.liftHand.setPosition(.7);
    sleep(1000);

    robot.lift1Motor.setMode(DcMotor.RunMode.RUN_USING _ENCODER);
    robot.lift1Motor.setPower(-1);
    while(opModeIsActive()&&(robot.lift1Motor.getCurre ntPosition() > tgLift1Count * -37));
    robot.lift1Motor.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
    robot.lift1Motor.setPower(0);
    sleep(1000);

    Any help or suggestions would be helpful and greatly appreciated.


  • #2
    Before the arm begins to raise, there is no guarantee that the encoder position is 0 (you don't call STOP_AND_RESET_ENCODER). Are you sure that the encoder on the arm motor is good?

    Comment


    • #3
      Yes, we have tested multiple motors and encoder cables, as well as a REV motor. In our code we have a statement of: robot.lift1Motor.setMode(DcMotor.RunMode.STOP_AND_ RESET-ENCODER); to set the encoder initial position to 0. It was an accident that I left it out. We just can't seem to find the source of this problem.

      Comment


      • #4
        Do you check you battery levels when you test? Maybe your robot is too low on power to lift the arm at times.

        Comment


        • #5
          it was a full charge.

          Comment


          • #6
            Have you tried using RUN_WITHOUT_ENCODER?

            Comment


            • #7
              We haven't tried that.

              Comment


              • #8
                Since you reset the encoder to zero after each move, the next move depends on the previous one.
                So.. Any error/anomily will just compound. Once it get's off, it will stay off.

                Without actually seeing the arm move it's hard to guess if this is a problem.
                It's always best to let the encoder run (without resetting after each move), and adjust the target position. This will provide more predicable results.

                What does tgLift1Count represent? multiplying it by 40 and -37 is a bit confusing.

                To test what is hapenning, First I'd add code in the loop to display the encoder position with a telemetry statement and then I'd slow down the motor command to give the arm a chance to respond slowly so you can see what's happenning.

                The other thing I try to do is make my code work in either direction by creating a position error, and then checking the absolute value of it, instead of switching between < and >

                This is what I'd do in your case... Note: this is untested.


                Code:
                    void doStuff() {
                
                        //  Reset the Encoder JUST ONCE
                        robot.lift1Motor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
                        robot.lift1Motor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
                
                        // Lift arm and grab block
                        gotoArmPosition(tgLift1Count * 40);
                        sleep(1500);
                        robot.liftHand.setPosition(.7);
                        sleep(1000);
                
                        // Lower block
                        gotoArmPosition(0);
                        sleep(1000);
                    }
                
                    // Create a method to go to ANY position and use it everywhere.
                    void gotoArmPosition(int target) {
                        int     CLOSE_ENOUGH = 20  ; // choose how close the arm should get before it stops.
                        double  MOTOR_SPEED  = 0.4 ; // choose how fast the arm should move
                        int     armPosition = 0;
                        int     armError    = 0;
                
                        // Determine what direction we are running the motor to get to target
                        armPosition = robot.lift1Motor.getCurrentPosition();
                        armError = target - armPosition;
                        robot.lift1Motor.setPower(Math.signum(armError) * MOTOR_SPEED); // sets +/- based on error sign
                
                        // Loop until we are near our target position.  Works in either direction.
                        while(opModeIsActive()&& (Math.abs(armError) > CLOSE_ENOUGH)) {
                            // Display Arm encoder data
                            telemetry.addData("Arm Pos:Error", "%d|%d", armPosition, armError);
                            telemetry.update();
                
                            // Get fresh data for next time around the loop.
                            armPosition = robot.lift1Motor.getCurrentPosition();
                            armError = target - armPosition;
                        }
                
                        // Shut down motor before leaving
                        robot.lift1Motor.setPower(0);
                    }

                Comment

                Working...
                X