No announcement yet.

Encoder help!

  • Filter
  • Time
  • Show
Clear All
new posts

  • Encoder help!

    Can any one help us with resetting encoders? We have tried the method below and the robot just stops when it reaches :robot.leftMotor.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
    robot.rightMotor.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);

    Is there a better way or possibly a reason this isn't working? (This is only part of our program, we can add the whole thing if necessary.

    while (!isStarted()) {

    // Display the light level while we are waiting to start
    telemetry.addData("Light Level", lightSensor.getLightDetected());
    Color.RGBToHSV( * 8, * 8, * 8, hsvValues);

    // send the info back to driver station using telemetry function.

    telemetry.addData("Red ",;
    telemetry.addData("Blue ",;
    telemetry.addData("Hue", hsvValues[0]);

    int initLeftCount = robot.leftMotor.getCurrentPosition();
    int tgLeftCount = initLeftCount + 89;
    int initRightCount = robot.rightMotor.getCurrentPosition();
    int tgRightCount = initRightCount + 89;

    //Drive towards line for 24 inches at high speed

    while (opModeIsActive() && (robot.rightMotor.getCurrentPosition() < tgRightCount * 24));

    while (opModeIsActive() && (robot.leftMotor.getCurrentPosition() < tgLeftCount *24));

    robot.leftMotor.setMode(DcMotor.RunMode.STOP_AND_R ESET_ENCODER);
    robot.rightMotor.setMode(DcMotor.RunMode.STOP_AND_ RESET_ENCODER);
    //turn right

    while (opModeIsActive() && (robot.leftMotor.getCurrentPosition() < tgLeftCount *10));
    //reset motors

    // Start the robot moving forward, and then begin looking for a white line.

    // run until the white line is seen OR the driver presses STOP;
    while (opModeIsActive() && (lightSensor.getLightDetected() < WHITE_THRESHOLD)) {

    // Display the light level while we are looking for the line
    telemetry.addData("Light Level", lightSensor.getLightDetected());

  • #2
    After you switch modes to STOP_AND_RESET_ENCODER, you have to switch back to an active mode like RUN_WITHOUT_ENCODER or RUN_USING_ENCODER to get the motors to go again.


    • #3
      ok We'll give that at try


      • #4
        What Cheer4FTC said. You should specifically set the mode after a reset, as it is not clearly defined what mode it will be, and it may not be the one you want.
        In addition, there are other areas of the code that raise questions:
        What are the 89 and 24 and 10 hard coded literals values?
        Initially, the target pos is set as 89 counts beyond the initial position of the motors. 89 counts will typically not be very far in inches - unless you have some serious gearing. For reference: Our motor output shaft is directly connected to our wheel hub - no additional gearing. we have 6.5" diameter drive wheels and NeveRest 40 motors where 1120 counts = 1 rev of motor output shaft. This equates to ~55 counts per inch. Even with those big wheels, 89 counts would be just over 1.5".
        Then you test for 24*tgtPos. So you apply an 89 count offset to initPos and then multiply by 24. This could be a seriously big number if initPos was well off of 0 to begin with. The entire purpose of get the initial position is to insure that the routine works when it doesn't follow a reset.
        I assume that you are using either RUN_WITHOUT_ENCODERS or RUN_USING_ENCODERS, as there are no set_target calls. If this is the case, you will likely see significant overshoot, as you are running the motors at full speed, and waiting to see the encoder count reach the target and then stopping the motors. Inertia will be at work here.

        If 89 is your CPI, you should set it as a variable. Then if you want to go 24", your tgtPos would be initPos + 24 * CPI. You would then change the test in the while loop to simply be < tgtPos. You would not multiply by 24. Similar applies for the *10 test further down.