Announcement

Collapse
No announcement yet.

combining op modes

Collapse
X
  • 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
    robot.leftMotor.setPower(.9);
    robot.rightMotor.setPower(.9);
    runtime.reset();

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

    //reset motors
    robot.leftMotor.setPower(0);
    robot.rightMotor.setPower(0);

    // Start the robot moving forward, and then begin looking for a white line.
    robot.leftMotor.setPower(APPROACH_SPEED);
    robot.rightMotor.setPower(APPROACH_SPEED);

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

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

    Comment


    • #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.

      Comment


      • #4
        ok so here is what we came up with:
        robot.leftMotor.setPower(.9);
        robot.rightMotor.setPower(.9);
        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
        robot.leftMotor.setPower(0);
        robot.rightMotor.setPower(0);

        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?

        Comment


        • #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.

          Comment


          • #6
            Thank You! We got this now!

            Comment

            Working...
            X