No announcement yet.

encoder question

  • Filter
  • Time
  • Show
Clear All
new posts

  • encoder question

    Is there a way or formula we can use to run using encoders without usingthe following method at each case?

    robot.left1.setMode(DcMotor.RunMode.STOP_AND_RESET _ENCODER);
    robot.left2.setMode(DcMotor.RunMode.STOP_AND_RESET _ENCODER);
    robot.right1.setMode(DcMotor.RunMode.STOP_AND_RESE T_ENCODER);
    robot.right2.setMode(DcMotor.RunMode.STOP_AND_RESE T_ENCODER);

    robot.left1.setMode(DcMotor.RunMode.RUN_USING_ENCO DER);
    robot.left2.setMode(DcMotor.RunMode.RUN_USING_ENCO DER);
    robot.right1.setMode(DcMotor.RunMode.RUN_USING_ENC ODER);
    robot.right2.setMode(DcMotor.RunMode.RUN_USING_ENC ODER);


    while (opModeIsActive() && (robot.left1.getCurrentPosition() > 23.5 * -7));
    while (opModeIsActive() && (robot.left2.getCurrentPosition() < 23.5 * 7)) ;
    while (opModeIsActive() && (robot.right1.getCurrentPosition()< 23.5 * 7)) ;
    while (opModeIsActive() && (robot.right2.getCurrentPosition() > 23.5 * -7));


    The "STOP_AND_RESET_ENCODER" and the "RUN_USING_ENCODER" take up alot of space and seem redundant.

  • #2
    You need to tell the robot when to set the motor power as well as setting the mode to RUN_TO_POSITION at the beginning. So in the while loops you have, you need to tell the program to set the power. and you can use one motor rather than all of them. It should look like this. It should replace the setPowers and the 4 while loops and you only need 1 of these loops. You also need to set the motors to run using encoders after it's done moving otherwise it will return an error which makes the setting mode necessary.

    while (opModeIsActive() && robot.motorname.getCurrentPosition() != "targetposition") {


    • #3
      While you can use Run to Position which works, to answer your specific question you can make methods to call in your class or separate classes. You can make a method with all four stop and reset commands and give it a name something like ResetAllMotors(). Then you just type ResetAllMotors() to have all 4 commands run . Or you could make a ResetAllMotorsToUsingEncoders.


      • #4
        Those lines are not really redundant, because you are accessing 4 different pieces of hardware, so you do need to give each of them the commands. I generally do not like to STOP_AND_RESET except at the very beginning, unless there is a particular reason for doing so. We just add/subtract to the current tick value to come up with the new target (desired stop count). So then if you have not changed your motor mode elsewhere in between moves, you would not need any of those 8 statements.

        You could also use RUN_TO_POSITION instead or extract that verbose code into a method as suggested above. However, I'm not sure your current logic is going to do exactly what you are expecting.

        Since you have 4 separate "while" loops, they are going to process sequentially. You send power to all motors & then wait for left1 to reach the target before starting the next loop to check for left2. Then that finishes before checking right1, then finally will check for right2. If your motors are well-balanced to each other, then you may not see much difference, because when left1 reaches its target, it probably just drops through the other 3 "while" loops. The better way might to a single "while" that checks all 4 counts - I would also experiment with "and" vs "or" for linking them together.