Announcement

Collapse
No announcement yet.

Encoder Help! (Android Studio)

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

  • Encoder Help! (Android Studio)

    So, I'm needing help with my encoders, so far what this does is move forward but will go entirely off track every now and then and I have no idea why, I've referred to the external example for help but I'm unable to figure it out, any help would be appreciated!


    package org.firstinspires.ftc.teamcode;

    import com.qualcomm.robotcore.eventloop.opmode.Autonomous ;
    import com.qualcomm.robotcore.hardware.HardwareMap;
    import com.qualcomm.robotcore.hardware.Servo;
    import com.qualcomm.robotcore.eventloop.opmode.Disabled;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMo de;
    import com.qualcomm.robotcore.hardware.DcMotorController;
    import com.qualcomm.robotcore.hardware.DcMotorSimple;
    import com.qualcomm.robotcore.util.ElapsedTime;

    @Autonomous(name = "AutoBot")

    public class AutoBot extends LinearOpMode
    {

    private DcMotor leftDrive;
    private DcMotor rightDrive;
    private DcMotor lift;
    private DcMotor flipArm;
    private DcMotor intake;
    private Servo hook;
    public ElapsedTime runTime = new ElapsedTime();

    @Override
    public void runOpMode() throws InterruptedException
    {
    leftDrive = hardwareMap.get(DcMotor.class, "leftDrive");
    rightDrive = hardwareMap.get(DcMotor.class, "rightDrive");
    lift = hardwareMap.get(DcMotor.class, "lift");
    intake = hardwareMap.get(DcMotor.class, "intake");
    flipArm = hardwareMap.get(DcMotor.class, "flipArm");
    hook = hardwareMap.get(Servo.class, "hook");

    //rightDrive.setDirection(DcMotor.Direction.FORWARD) ;
    //leftDrive.setDirection(DcMotor.Direction.REVERSE);
    flipArm.setDirection(DcMotor.Direction.FORWARD);
    intake.setDirection(DcMotor.Direction.FORWARD);
    lift.setDirection(DcMotor.Direction.REVERSE);

    leftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_E NCODER);
    rightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ ENCODER);
    lift.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODE R);


    leftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODE R);
    rightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);
    lift.setMode(DcMotor.RunMode.RUN_USING_ENCODER);


    waitForStart();

    telemetry.addData("Mode", "running");
    telemetry.update();

    setTarget(774);
    setMotorPower(1);
    runTo();
    motorBusy();
    resetEncoders();



    }

    public void setTarget(int a)
    {
    leftDrive.setTargetPosition(a);
    rightDrive.setTargetPosition(a);

    }

    public void setMotorPower(double a)
    {
    leftDrive.setPower(-a);
    rightDrive.setPower(a);
    }

    public void runTo()
    {
    leftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION) ;
    rightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION );
    }

    public void motorBusy()
    {
    while(leftDrive.isBusy() && rightDrive.isBusy() && opModeIsActive())
    {

    }
    }

    public void resetEncoders()
    {

    leftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_E NCODER);
    rightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ ENCODER);

    leftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODE R);
    rightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCOD ER);
    }
    }

  • #2
    From our programmer you should try the following change:
    Where you have your code

    while(leftDrive.isBusy() && rightDrive.isBusy() && opModeIsActive())

    change to

    while(opModeIsActive() && leftDrive.isBusy() || rightDrive.isBusy())

    This will stop the robot when either motor stops. This helps with encoder mismatches.


    Comment


    • #3
      It seems like you have played around with it a bit, but you should set the left motor to reverse. If you set a negative power I am pretty sure the encoder count is decrementing instead of incrementing.

      Comment


      • #4
        Originally posted by mjurisch2017 View Post
        From our programmer you should try the following change:
        Where you have your code

        while(leftDrive.isBusy() && rightDrive.isBusy() && opModeIsActive())

        change to

        while(opModeIsActive() && leftDrive.isBusy() || rightDrive.isBusy())

        This will stop the robot when either motor stops. This helps with encoder mismatches.

        I tried this and it actaully stopped the right motor when it was told but it looped my leftMotor until i pressed stop, sorry for the late response!

        Comment


        • #5
          Originally posted by Bullet245 View Post

          I tried this and it actaully stopped the right motor when it was told but it looped my leftMotor until i pressed stop, sorry for the late response!
          I am thinking because this encoder count is going negative because the power is negative.

          Comment


          • #6
            Yeah, I'll mess with some stuff and get both motors to be going in the same direction to see if that'll change anything. Thank you!!

            Comment


            • #7
              Originally posted by rvansmith View Post
              It seems like you have played around with it a bit, but you should set the left motor to reverse. If you set a negative power I am pretty sure the encoder count is decrementing instead of incrementing.
              Thank you for the feedback, and I apologize for the late responce, I appreciate it!

              Comment

              Working...
              X