Announcement

Collapse
No announcement yet.

URGENT: Encoder Problems

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

  • URGENT: Encoder Problems

    Hello Internet,

    I have written an autonomous program in a linear opmode which utilises encoders to:
    move forward
    turn 90 degrees anticlockwise
    move forward
    move lift up
    move front arm to deposit pre-loaded climbers
    move lift back down
    turn 180 degrees
    move forward
    turn 145 degrees clockwise
    move forward
    stop partially on the mountain low zone

    We tried it and the robot isn't moving... However, the lift does go up and the front arm does move. I would very much appreciate your help. Please take a look at my code and see if you can find out what is wrong. (also, it is indented properly in Android Studio, copy pasting it just messed it up).


    package com.qualcomm.ftcrobotcontroller.opmodes;

    //importing stuff
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.hardware.DcMotorController;
    import com.qualcomm.robotcore.hardware.Servo;

    /**
    * Created by MSRobotics on 1/4/2016.
    */




    public class AutonomousIB extends LinearOpMode {

    //diameter of wheel
    float wheelDiameter = 4;

    //gear ratio
    float gearRatio = 2;

    //time (in milliseconds) the robot takes to make its first turn
    int count90 = 1328;

    //time (in milliseconds) the robot takes to make its second turn
    int count145 = (1328/90)*145;

    //first distance (in inches) robot travels
    float distance1 = 72;
    //encoder counts necessary for the above distance
    double count1 = 16509.55;

    //second distance (in inches) robot travels
    float distance2 = 45;
    //encoder counts necessary for the above distance
    double count2 = 10318.47;

    //fourth distance (in inches) robot travels
    float mountainDistance = 64;
    //encoder counts necessary for the above distance
    double mountainCount = 14675.16;

    //tread motors
    DcMotor leftMotor1;
    DcMotor leftMotor2;
    DcMotor rightMotor1;
    DcMotor rightMotor2;

    //lift motor
    DcMotor liftMotor;

    //front arm
    Servo frontArm;

    @Override
    public void runOpMode() throws InterruptedException {
    frontArm = hardwareMap.servo.get("front_arm");
    leftMotor1 = hardwareMap.dcMotor.get("left_motor_1");
    leftMotor2 = hardwareMap.dcMotor.get("left_motor_2");
    rightMotor1 = hardwareMap.dcMotor.get("right_motor_1");
    rightMotor2 = hardwareMap.dcMotor.get("right_motor_2");
    liftMotor = hardwareMap.dcMotor.get("lift_motor");

    //reverse motors
    rightMotor1.setDirection(DcMotor.Direction.REVERSE );
    rightMotor2.setDirection(DcMotor.Direction.REVERSE );
    liftMotor.setDirection(DcMotor.Direction.REVERSE);
    frontArm.setDirection(Servo.Direction.REVERSE);

    //wait for driver to press play
    waitForStart();

    //the following loop will break when it gets to the break statement or when the driver presses the stop button
    while(opModeIsActive()) {
    //first distance the robot travels
    //reset tread distance encoders
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RESET_ENCODERS);
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RESET_ENCODERS);
    waitOneFullHardwareCycle();
    //setting the desired number of counts the motor will travel, setting a target
    leftMotor2.setTargetPosition((int) count1);
    rightMotor2.setTargetPosition((int) count1);
    //the motors will go to the target position (above)
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RUN_TO_POSITION);
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RUN_TO_POSITION);
    //the motors will move at this speed
    leftMotor2.setPower(.4);
    rightMotor2.setPower(.4);

    while(leftMotor2.isBusy() && rightMotor2.isBusy()){
    leftMotor1.setPower(.4);
    rightMotor1.setPower(.4);
    }
    leftMotor2.setPower(0);
    rightMotor2.setPower(0);
    leftMotor1.setPower(0);
    rightMotor1.setPower(0);

    //the robot makes turn 90 degrees clockwise
    //reset tread distance encoders
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RESET_ENCODERS);
    waitOneFullHardwareCycle();
    //setting the desired number of counts the motor will travel, setting a target
    leftMotor2.setTargetPosition((int) count90);
    //the motors will go to the target position (above)
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RUN_TO_POSITION);
    //the motors will move at this speed

    leftMotor2.setPower(.4);

    while(leftMotor2.isBusy()){
    leftMotor1.setPower(.4);
    rightMotor1.setPower(-.4);
    rightMotor2.setPower(-.4);
    }
    leftMotor2.setPower(0);
    rightMotor2.setPower(0);
    leftMotor1.setPower(0);
    rightMotor1.setPower(0);

    //second distance the robot travels
    //reset tread distance encoders
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RESET_ENCODERS);
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RESET_ENCODERS);
    waitOneFullHardwareCycle();
    //setting the desired number of counts the motor will travel, setting a target
    leftMotor2.setTargetPosition((int) count2);
    rightMotor2.setTargetPosition((int) count2);
    //the motors will go to the target position (above)
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RUN_TO_POSITION);
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RUN_TO_POSITION);
    //the motors will move at this speed

    leftMotor2.setPower(.4);
    rightMotor2.setPower(.4);

    while(leftMotor2.isBusy() && rightMotor2.isBusy()){
    leftMotor1.setPower(.4);
    rightMotor1.setPower(.4);
    }
    leftMotor2.setPower(0);
    rightMotor2.setPower(0);
    leftMotor1.setPower(0);
    rightMotor1.setPower(0);

    //move lift up
    liftMotor.setPower(1);
    sleep(3300);
    liftMotor.setPower(0);

    //the robot moves front arm to deposit pre-loaded climbers
    for(int i = 3; i >=1; i--) {
    frontArm.setPosition(1);
    sleep(1500);
    frontArm.setPosition(0);
    sleep(1500);
    }

    //move lift down
    liftMotor.setPower(-1);
    sleep(3300);
    liftMotor.setPower(0);

    //the robot makes turn 180 degrees clockwise
    //reset tread distance encoders
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RESET_ENCODERS);
    waitOneFullHardwareCycle();
    //setting the desired number of counts the motor will travel, setting a target
    leftMotor2.setTargetPosition((int) count1);
    //the motors will go to the target position (above)
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RUN_TO_POSITION);
    //the motors will move at this speed

    leftMotor2.setPower(.4);

    while(leftMotor2.isBusy()){
    leftMotor1.setPower(.4);
    rightMotor1.setPower(-.4);
    rightMotor2.setPower(-.4);
    }
    leftMotor2.setPower(0);
    rightMotor2.setPower(0);
    leftMotor1.setPower(0);
    rightMotor1.setPower(0);

    //third distance the robot travels
    //reset tread distance encoders
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RESET_ENCODERS);
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RESET_ENCODERS);
    waitOneFullHardwareCycle();
    //setting the desired number of counts the motor will travel, setting a target
    leftMotor2.setTargetPosition((int) count2);
    rightMotor2.setTargetPosition((int) count2);
    //the motors will go to the target position (above)
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RUN_TO_POSITION);
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RUN_TO_POSITION);
    //the motors will move at this speed

    leftMotor2.setPower(.4);
    rightMotor2.setPower(.4);

    while(leftMotor2.isBusy() && rightMotor2.isBusy()){
    leftMotor1.setPower(.4);
    rightMotor1.setPower(.4);
    }
    leftMotor2.setPower(0);
    rightMotor2.setPower(0);
    leftMotor1.setPower(0);
    rightMotor1.setPower(0);


    //the robot makes 145 degrees turn anticlockwise
    //reset tread distance encoders
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RESET_ENCODERS);
    waitOneFullHardwareCycle();
    //setting the desired number of counts the motor will travel, setting a target
    rightMotor2.setTargetPosition((int) count145);
    //the motors will go to the target position (above)
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RUN_TO_POSITION);
    //the motors will move at this speed

    rightMotor2.setPower(.4);

    while(rightMotor2.isBusy()){
    leftMotor1.setPower(-.4);
    leftMotor2.setPower(-.4);
    rightMotor1.setPower(.4);
    }
    leftMotor2.setPower(0);
    rightMotor2.setPower(0);
    leftMotor1.setPower(0);
    rightMotor1.setPower(0);

    //final distance robot travels
    //reset tread distance encodersa\\
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RESET_ENCODERS);
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RESET_ENCODERS);
    waitOneFullHardwareCycle();
    //setting the desired number of counts the motor will travel, setting a target
    leftMotor2.setTargetPosition((int) mountainCount);
    rightMotor2.setTargetPosition((int) mountainCount);
    //the motors will go to the target position (above)
    leftMotor2.setChannelMode(DcMotorController.RunMod e.RUN_TO_POSITION);
    rightMotor2.setChannelMode(DcMotorController.RunMo de.RUN_TO_POSITION);
    //the motors will move at this speed

    leftMotor2.setPower(.4);
    rightMotor2.setPower(.4);
    //[email protected]

    while(leftMotor2.isBusy() && rightMotor2.isBusy()){
    leftMotor1.setPower(.4);
    rightMotor1.setPower(.4);
    }
    leftMotor2.setPower(0);
    rightMotor2.setPower(0);
    leftMotor1.setPower(0);
    rightMotor1.setPower(0);

    break;
    }

    //turn off tread motors
    leftMotor1.setPower(0);
    leftMotor2.setPower(0);
    rightMotor1.setPower(0);
    rightMotor2.setPower(0);
    }

    }

  • #2
    Originally posted by FTC7155 View Post
    Hello Internet,

    I have written an autonomous program in a linear opmode which utilises encoders to:
    move forward
    turn 90 degrees anticlockwise
    move forward
    move lift up
    move front arm to deposit pre-loaded climbers
    move lift back down
    turn 180 degrees
    move forward
    turn 145 degrees clockwise
    move forward
    stop partially on the mountain low zone

    We tried it and the robot isn't moving... However, the lift does go up and the front arm does move. I would very much appreciate your help. Please take a look at my code and see if you can find out what is wrong. (also, it is indented properly in Android Studio, copy pasting it just messed it up).

    Code:
    package com.qualcomm.ftcrobotcontroller.opmodes;
    
    //importing stuff
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.hardware.DcMotorController;
    import com.qualcomm.robotcore.hardware.Servo;
    
    /**
     * Created by MSRobotics on 1/4/2016.
     */
    
    
    
    
    public class AutonomousIB extends LinearOpMode {
    
        //diameter of wheel
        float wheelDiameter = 4;
    
        //gear ratio
        float gearRatio = 2;
    
        //time (in milliseconds) the robot takes to make its first turn
        int count90 = 1328;
    
        //time (in milliseconds) the robot takes to make its second turn
        int count145 = (1328/90)*145;
    
        //first distance (in inches) robot travels
        float distance1 = 72;
        //encoder counts necessary for the above distance
        double count1 = 16509.55;
    
        //second distance (in inches) robot travels
        float distance2 = 45;
        //encoder counts necessary for the above distance
        double count2 = 10318.47;
    
        //fourth distance (in inches) robot travels
        float mountainDistance = 64;
        //encoder counts necessary for the above distance
        double mountainCount = 14675.16;
    
        //tread motors
        DcMotor leftMotor1;
        DcMotor leftMotor2;
        DcMotor rightMotor1;
        DcMotor rightMotor2;
    
        //lift motor
        DcMotor liftMotor;
    
        //front arm
        Servo frontArm;
    
        @Override
        public void runOpMode() throws InterruptedException {
            frontArm = hardwareMap.servo.get("front_arm");
            leftMotor1 = hardwareMap.dcMotor.get("left_motor_1");
            leftMotor2 = hardwareMap.dcMotor.get("left_motor_2");
            rightMotor1 = hardwareMap.dcMotor.get("right_motor_1");
            rightMotor2 = hardwareMap.dcMotor.get("right_motor_2");
            liftMotor = hardwareMap.dcMotor.get("lift_motor");
    
            //reverse motors
            rightMotor1.setDirection(DcMotor.Direction.REVERSE);
            rightMotor2.setDirection(DcMotor.Direction.REVERSE);
            liftMotor.setDirection(DcMotor.Direction.REVERSE);
            frontArm.setDirection(Servo.Direction.REVERSE);
    
            //wait for driver to press play
            waitForStart();
    
            //the following loop will break when it gets to the break statement or when the driver presses the stop button
            while(opModeIsActive()) {
                //first distance the robot travels
                //reset tread distance encoders
                leftMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                rightMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                waitOneFullHardwareCycle();
                //setting the desired number of counts the motor will travel, setting a target
                leftMotor2.setTargetPosition((int) count1);
                rightMotor2.setTargetPosition((int) count1);
                //the motors will go to the target position (above)
                leftMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                rightMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                //the motors will move at this speed
                leftMotor2.setPower(.4);
                rightMotor2.setPower(.4);
    
                while(leftMotor2.isBusy() && rightMotor2.isBusy()){
                    leftMotor1.setPower(.4);
                    rightMotor1.setPower(.4);
                }
                leftMotor2.setPower(0);
                rightMotor2.setPower(0);
                leftMotor1.setPower(0);
                rightMotor1.setPower(0);
    
                //the robot makes turn 90 degrees clockwise
                //reset tread distance encoders
                leftMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                waitOneFullHardwareCycle();
                //setting the desired number of counts the motor will travel, setting a target
                leftMotor2.setTargetPosition((int) count90);
                //the motors will go to the target position (above)
                leftMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                //the motors will move at this speed
    
                leftMotor2.setPower(.4);
    
                while(leftMotor2.isBusy()){
                    leftMotor1.setPower(.4);
                    rightMotor1.setPower(-.4);
                    rightMotor2.setPower(-.4);
                }
                leftMotor2.setPower(0);
                rightMotor2.setPower(0);
                leftMotor1.setPower(0);
                rightMotor1.setPower(0);
    
                //second distance the robot travels
                //reset tread distance encoders
                leftMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                rightMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                waitOneFullHardwareCycle();
                //setting the desired number of counts the motor will travel, setting a target
                leftMotor2.setTargetPosition((int) count2);
                rightMotor2.setTargetPosition((int) count2);
                //the motors will go to the target position (above)
                leftMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                rightMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                //the motors will move at this speed
    
                leftMotor2.setPower(.4);
                rightMotor2.setPower(.4);
    
                while(leftMotor2.isBusy() && rightMotor2.isBusy()){
                    leftMotor1.setPower(.4);
                    rightMotor1.setPower(.4);
                }
                leftMotor2.setPower(0);
                rightMotor2.setPower(0);
                leftMotor1.setPower(0);
                rightMotor1.setPower(0);
    
                //move lift up
                liftMotor.setPower(1);
                sleep(3300);
                liftMotor.setPower(0);
    
                //the robot moves front arm to deposit pre-loaded climbers
                for(int i = 3; i >=1; i--) {
                    frontArm.setPosition(1);
                    sleep(1500);
                    frontArm.setPosition(0);
                    sleep(1500);
                }
    
                //move lift down
                liftMotor.setPower(-1);
                sleep(3300);
                liftMotor.setPower(0);
    
                //the robot makes turn 180 degrees clockwise
                //reset tread distance encoders
                leftMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                waitOneFullHardwareCycle();
                //setting the desired number of counts the motor will travel, setting a target
                leftMotor2.setTargetPosition((int) count1);
                //the motors will go to the target position (above)
                leftMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                //the motors will move at this speed
    
                leftMotor2.setPower(.4);
    
                while(leftMotor2.isBusy()){
                    leftMotor1.setPower(.4);
                    rightMotor1.setPower(-.4);
                    rightMotor2.setPower(-.4);
                }
                leftMotor2.setPower(0);
                rightMotor2.setPower(0);
                leftMotor1.setPower(0);
                rightMotor1.setPower(0);
    
                //third distance the robot travels
                //reset tread distance encoders
                leftMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                rightMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                waitOneFullHardwareCycle();
                //setting the desired number of counts the motor will travel, setting a target
                leftMotor2.setTargetPosition((int) count2);
                rightMotor2.setTargetPosition((int) count2);
                //the motors will go to the target position (above)
                leftMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                rightMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                //the motors will move at this speed
    
                leftMotor2.setPower(.4);
                rightMotor2.setPower(.4);
    
                while(leftMotor2.isBusy() && rightMotor2.isBusy()){
                    leftMotor1.setPower(.4);
                    rightMotor1.setPower(.4);
                }
                leftMotor2.setPower(0);
                rightMotor2.setPower(0);
                leftMotor1.setPower(0);
                rightMotor1.setPower(0);
    
    
                //the robot makes 145 degrees turn anticlockwise
                //reset tread distance encoders
                rightMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                waitOneFullHardwareCycle();
                //setting the desired number of counts the motor will travel, setting a target
                rightMotor2.setTargetPosition((int) count145);
                //the motors will go to the target position (above)
                rightMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                //the motors will move at this speed
    
                rightMotor2.setPower(.4);
    
                while(rightMotor2.isBusy()){
                    leftMotor1.setPower(-.4);
                    leftMotor2.setPower(-.4);
                    rightMotor1.setPower(.4);
                }
                leftMotor2.setPower(0);
                rightMotor2.setPower(0);
                leftMotor1.setPower(0);
                rightMotor1.setPower(0);
    
                //final distance robot travels
                //reset tread distance encodersa\\
                leftMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                rightMotor2.setChannelMode(DcMotorController.RunMode.RESET_ENCODERS);
                waitOneFullHardwareCycle();
                //setting the desired number of counts the motor will travel, setting a target
                leftMotor2.setTargetPosition((int) mountainCount);
                rightMotor2.setTargetPosition((int) mountainCount);
                //the motors will go to the target position (above)
                leftMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                rightMotor2.setChannelMode(DcMotorController.RunMode.RUN_TO_POSITION);
                //the motors will move at this speed
    
                leftMotor2.setPower(.4);
                rightMotor2.setPower(.4);
                //[email protected]
    
                while(leftMotor2.isBusy() && rightMotor2.isBusy()){
                    leftMotor1.setPower(.4);
                    rightMotor1.setPower(.4);
                }
                leftMotor2.setPower(0);
                rightMotor2.setPower(0);
                leftMotor1.setPower(0);
                rightMotor1.setPower(0);
    
                break;
            }
    
            //turn off tread motors
            leftMotor1.setPower(0);
            leftMotor2.setPower(0);
            rightMotor1.setPower(0);
            rightMotor2.setPower(0);
        }
    
    }
    A few comments:
    1. The motors will not always reset in one hardware cycle. You should instead wait for the encoder value to reach 0 before continuing on.
    2. You don't need a while(opModeIsActive()) loop. Your code is intended only to run once, so it can be outside the loop.
    3. Your comment says that count1 and count145 store "time (in milliseconds)", but you use them as encoder targets.

    Comment


    • #3
      A few comments:
      1. The motors will not always reset in one hardware cycle. You should instead wait for the encoder value to reach 0 before continuing on.
      2. You don't need a while(opModeIsActive()) loop. Your code is intended only to run once, so it can be outside the loop.
      3. Your comment says that count1 and count145 store "time (in milliseconds)", but you use them as encoder targets.
      1. What is the syntax for that?
      2. Because only two of the motors have encoders, I used the loop to make sure that all of the motors are moving at the same time.
      3. Oops. Replace "time (in milliseconds)" with encoder counts

      Comment


      • #4
        Originally posted by dreiter1 View Post
        1. What is the syntax for that?
        2. Because only two of the motors have encoders, I used the loop to make sure that all of the motors are moving at the same time.
        1.
        Code:
        this.motor.setMode(DcMotorController.RunMode.RESET_ENCODERS);
        while (this.motor.getCurrentPosition() != 0) this.waitForNextHardwareCycle();
        this.motor.setMode(DcMotorController.RunMode.RUN_TO_POSITION);
        2. I am not referring to the while(motor.isBusy()) loops. The code you have currently written will cause the entire program to start over again every time it ends.

        Comment


        • #5
          Originally posted by GearTicks View Post
          The code you have currently written will cause the entire program to start over again every time it ends.
          It looks to me like the very last statement of the while(opModeIsActive()) loop is a break so it's shouldn't start over. It just means that having the loop is useless.

          Comment

          Working...
          X