No announcement yet.

combining op modes

  • Filter
  • Time
  • Show
Clear All
new posts

  • combining op modes

    Does anyone have an example of an autonomous program that uses the drive using encoders program that transitions to the line following program? We have an autonomous program that uses drive by time then transitions to the line following but the drive by encoder program doesn't seem to work? Maybe there is a simpler version of the drive by encoders that we can find?

  • #2
    Alittle further clarification what we are trying to do: Our current code is - //Drive towards line for 2 sec at high speed

    while (opModeIsActive() && (runtime.seconds() < 2.0)) {
    idle(); }

    //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());
    Color.RGBToHSV( * 8, * 8, * 8, hsvValues);
    telemetry.addData("Red ",;
    telemetry.addData("Blue ",;
    telemetry.addData("Hue", hsvValues[0]);
    idle(); // Always call idle() at the bottom of your while(opModeIsActive()) loop

    We would like to run using encoder count versus the seconds.


    • #3
      Replace your time condition with an encoder count condition.
      You will first need to convert distance to encoder counts. This can be done by calculating CPI based on wheel circumference and moter CPR (counts per rev). If you are using Andy Mark Neverest motors, CPR = 1120.
      Next you will want to get the current encoder count for both motors.
      Something like (note - I am not by any code, so the calls are from memory):
      int initLeftCount = robot.leftMotor.getCurrentPosition())
      int tgLeftCount = initLeftCount + distance * CPI;
      Then your condition becomes while(opModeIsActive() && robot.leftMotor.getCurrentPosition() < tgtLeftCount)
      Naturally, you would have a right dependency as well.
      Note: You shouldn't need the idle calls if you are using SDK 2.35. They are now included as part of opModeIsActive().
      You could leave the timer condition is as a safeguard.


      • #4
        ok so here is what we came up with:
        int initLeftCount = robot.leftMotor.getCurrentPosition();
        int tgLeftCount = initLeftCount + 4 * 1120;
        int initRightCount = robot.rightMotor.getCurrentPosition();
        int tgRightCount = initLeftCount + 4 * 1120;

        while (opModeIsActive() && (robot.rightMotor.getCurrentPosition() < 6000)) {
        idle(); }

        //reset motors

        Where "4" is the radius of the wheel. But this is only measuring the right wheel. We think we can make this work but don't we need to reset the position each time?


        • #5
          Wrong calc for tgt*Count. You need the circumference to map to the number of counts per rev (CPR) of 1120. So for 1 wheel rotation, the encoder counts will go up by 1120 and the bot will travel 2*PI*radius inches forward. So counts per inch (CPI) = CPR/(2*PI*radius) = 1120/(8PI).
          And, tgtLeftCount = initLeftCount + CPI*(desiredDistance).
          You then have a magical "< 6000". This should be replaced by "<= tgtLeftCount".
          You do not need to reset until after the end of the while loop - and even then it is optional if you always offset by the currentPos at the beginning of a new segment. And you don't need the idle.


          • #6
            Thank You! We got this now!