Announcement

Collapse
No announcement yet.

Issues With Stopping in Autonomous

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

  • Issues With Stopping in Autonomous

    Hello everyone-
    So I've been having this issue in the very newest version of the app as of 10/15/16. When I press the stop button on the drivers station phone, the robot controller app closes, and reopens. When this happens, the robot is still continually moving, which is causing some annoyances. I assumed it had something to do with the idle(); change in the newest update, but I was unsure what needed to change. Is the new solution to just remove all idle(); calls? Code:
    Code:
    package org.firstinspires.ftc.teamcode;
    
    import com.qualcomm.hardware.modernrobotics.ModernRoboticsI2cRangeSensor;
    import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
    import com.qualcomm.robotcore.hardware.ColorSensor;
    import com.qualcomm.robotcore.hardware.DcMotor;
    import com.qualcomm.robotcore.hardware.GyroSensor;
    import com.qualcomm.robotcore.hardware.I2cAddr;
    import com.qualcomm.robotcore.hardware.Servo;
    
    @com.qualcomm.robotcore.eventloop.opmode.Autonomous(name = "RedMidAuto", group = "What is this")
    public class RedMidAuto extends LinearOpMode {
        DcMotor leftWheel;
        DcMotor rightWheel;
    
        Servo buttonPusher;
    
        GyroSensor gyro;
        ModernRoboticsI2cRangeSensor frontRange;
    
        ColorSensor floor;
        ColorSensor beacon;
    
        int i = 1;
        int i1 = 1;
        int i2 = 1;
        int i3 = 1;
        int i4 = 1;
        int i5 = 1;
        int i6 = 1;
        int i7 = 1;
        int i8 = 1;
        int i9 = 1;
        int i10 = 1;
        int i11 = 1;
    
        int state1 = 0;
    
        int state2 = 0;
        @Override
        public void runOpMode() throws InterruptedException {
            leftWheel = hardwareMap.dcMotor.get("lw");
            rightWheel = hardwareMap.dcMotor.get("rw");
            leftWheel.setDirection(DcMotor.Direction.REVERSE);
    
            buttonPusher = hardwareMap.servo.get("b");
    
            buttonPusher.setPosition(.5);
    
            gyro = hardwareMap.gyroSensor.get("g");
            gyro.calibrate();
    
            frontRange = hardwareMap.get(ModernRoboticsI2cRangeSensor.class, "range2");
    
            floor = hardwareMap.colorSensor.get("c");
            floor.setI2cAddress(I2cAddr.create8bit(0x3c));
    
            beacon = hardwareMap.colorSensor.get("c2");
            beacon.setI2cAddress(I2cAddr.create8bit(0x6c));
    
            waitForStart();
    
            //backUp(); //1
            //sleep(1000);
            turnLeft45(); //2
            sleep(1000);
            straightUntilWhite(); //3
            sleep(1000);
            turnLeftPivot45(); //4
            sleep(1000);
            parkBeacon(); //5
            sleep(1000);
            redBeacon();
            sleep(1000);
            backUp2();
            sleep(1000);
            turnRight90(); //8
            sleep(1000);
            straightUntilWhite(); //9
            sleep(1000);
            turnLeft90(); //10
            sleep(1000);
            parkBeacon(); //11
            sleep(1000);
            redBeacon(); //12
            sleep(1000);
            backUp2();
    
        }
    
        public void turnLeft90() throws InterruptedException {
            while (i == 1) {
                if (gyro.getHeading() < 285 && gyro.getHeading() > 10) {
                    leftWheel.setPower(0);
                    rightWheel.setPower(0);
                    i--;
                } else {
                    leftWheel.setPower(-.1);
                }
                idle();
            }
            i++;
            gyro.resetZAxisIntegrator();
        }
    
        public void turnRight90() throws InterruptedException {
            while (i1 == 1) {
                if (gyro.getHeading() > 75 && gyro.getHeading() < 350) {
                    leftWheel.setPower(0);
                    rightWheel.setPower(0);
                    i1--;
                } else {
                    rightWheel.setPower(-.1);
                }
                idle();
            }
            i1++;
            gyro.resetZAxisIntegrator();
        }
    
        public void turnLeftPivot45() throws InterruptedException {
            while (i3 == 1) {
                if (gyro.getHeading() < 328 && gyro.getHeading() > 10) {
                    leftWheel.setPower(0);
                    rightWheel.setPower(0);
                    i3--;
                } else {
                    rightWheel.setPower(-.1);
                    leftWheel.setPower(.1);
                }
                idle();
            }
            i3++;
            gyro.resetZAxisIntegrator();
        }
    
        public void turnLeft45() throws InterruptedException {
            while (i5 == 1) {
                if (gyro.getHeading() < 325 && gyro.getHeading() > 10) {
                    leftWheel.setPower(0);
                    rightWheel.setPower(0);
                    i5--;
                } else {
                    rightWheel.setPower(-.1);
                }
                idle();
            }
            i5++;
            gyro.resetZAxisIntegrator();
        }
    
        public void turnRight45() throws InterruptedException {
            while (i6 == 1) {
                if (gyro.getHeading() > 35 && gyro.getHeading() < 350) {
                    leftWheel.setPower(0);
                    rightWheel.setPower(0);
                    i6--;
                } else {
                    leftWheel.setPower(-.1);
                }
                idle();
            }
            i6++;
            gyro.resetZAxisIntegrator();
        }
    
       /* public void backUp() throws InterruptedException {
            while (i4 == 1) {
                if (rangeSensor.cmUltrasonic() < 10) {
                    leftWheel.setPower(-.1);
                    rightWheel.setPower(-.1);
                } else {
                    leftWheel.setPower(0);
                    rightWheel.setPower(0);
                    i4--;
                }
                idle();
            }
            i4++;
        }
        */
    
        public void straightUntilWhite() throws InterruptedException {
            while (i10 == 1) {
                if (floor.green() > 15) {
                    i10--;
                } else {
                    if(gyro.getHeading() > 1 && gyro.getHeading() < 180) {
                        leftWheel.setPower(-.07);
                        rightWheel.setPower(-.25);
                    } else if(gyro.getHeading() < 355 && gyro.getHeading() < 179) {
                        leftWheel.setPower(-.25);
                        rightWheel.setPower(-.07);
                    } else {
                        leftWheel.setPower(-.07);
                        rightWheel.setPower(-.07);
                    }
                }
                idle();
            }
            leftWheel.setPower(0);
            rightWheel.setPower(0);
            i10++;
            gyro.resetZAxisIntegrator();
        }
    
        public void blueBeacon() throws InterruptedException {
            if(beacon.blue() > 3) {
    
                buttonPusher.setPosition(.75);
    
                sleep(250);
    
                buttonPusher.setPosition(.5);
            } else {
                buttonPusher.setPosition(.25);
    
                sleep(250);
    
                buttonPusher.setPosition(.5);
            }
        }
    
        public void redBeacon() throws InterruptedException {
            if(beacon.red() > 3) {
                buttonPusher.setPosition(.75);
    
                sleep(1000);
    
                buttonPusher.setPosition(.5);
    
                sleep(500);
            } else {
                buttonPusher.setPosition(.25);
    
                sleep(1000);
    
                buttonPusher.setPosition(.5);
    
                sleep(500);
            }
        }
    
        public void backUp2() throws InterruptedException {
            while (i4 == 1) {
                if (frontRange.cmUltrasonic() < 10) {
                    leftWheel.setPower(-.1);
                    rightWheel.setPower(-.1);
                } else {
                    leftWheel.setPower(0);
                    rightWheel.setPower(0);
                    i4--;
                }
                idle();
            }
            i4++;
        }
    
        public void parkBeacon() throws InterruptedException {
            while(frontRange.cmUltrasonic() > 10) {
                if(floor.green() > 7) {
                    leftWheel.setPower(-.1);
                    rightWheel.setPower(0);
                } else {
                    leftWheel.setPower(0);
                    rightWheel.setPower(-.1);
                }
            }
            leftWheel.setPower(0);
            rightWheel.setPower(0);
        }
    }

  • #2
    Yes, robots not stopping on command is annoying

    The Cause:
    If you have a long wait in your code (without checking for a stop condition), when you press stop, the app will give your code 2 seconds to exit gracefully, and if that doesn't happen it will flag the Stuck Stop() exception and reboot. (If you watch your RC and DS screens you should see this happen)

    The App should be killing all the motors etc when it reboots, so I'm not sure if/why it's not doing it in this case.
    It will take 2 seconds to detect the problem, and another couple to reboot the app. So if it's moving on for 3-4 seconds, then that's the likely cause.

    So the real issue to address here is that you are not checking for a stop condition:

    You should not let you code get into a loop which may run forever (eg: waiting for a turn to complete.... what if the robot gets stuck)
    So all your while loops MUST have a test that will let them exit when stop is pressed... eg:

    either opModeIsActive() or maybe isStopRequested()

    eg:

    public void turnLeft90() throws InterruptedException {
    while (opModeIsActive() && (i == 1)) {
    if (gyro.getHeading() < 285 && gyro.getHeading() > 10) {
    leftWheel.setPower(0);
    rightWheel.setPower(0);
    i--;
    } else {
    leftWheel.setPower(-.1);
    }
    idle();
    }
    i++;
    gyro.resetZAxisIntegrator();
    }

    Or possibly

    public void turnLeftPivot45() throws InterruptedException {
    while ((i3 == 1) && !isStopRequested()) {
    if (gyro.getHeading() < 328 && gyro.getHeading() > 10) {
    leftWheel.setPower(0);
    rightWheel.setPower(0);
    i3--;
    } else {
    rightWheel.setPower(-.1);
    leftWheel.setPower(.1);
    }
    idle();
    }
    i3++;
    gyro.resetZAxisIntegrator();
    }


    All that being said, it looks like this was actually originally written to be a state machine (with repeated calls),
    but now you've switched to linearOpMode this could be much simpler.

    Much of the complexity with all the i, i1,i2,i3 variables looks like it could be removed...

    public void turnLeft90() throws InterruptedException {

    // Start Rotating
    leftWheel.setPower(-.1);

    // Keep rotating until desired heading is reached.
    while (opModeIsActive() && (gyro.getHeading() > 285 || gyro.getHeading() < 10)) {
    idle();
    }

    // Stop Rotating and reset gyro
    leftWheel.setPower(0);
    rightWheel.setPower(0);
    gyro.resetZAxisIntegrator();
    }

    Comment


    • #3
      Thanks for the help! We updated our code with the opModeIsActive() calls, but it is still giving me that weird delay at the end. I must be missing something. Here's the revised code, if you have any other ideas:
      Code:
      package org.firstinspires.ftc.teamcode;
      
      import com.qualcomm.hardware.modernrobotics.ModernRoboticsI2cRangeSensor;
              import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
              import com.qualcomm.robotcore.hardware.ColorSensor;
              import com.qualcomm.robotcore.hardware.DcMotor;
              import com.qualcomm.robotcore.hardware.GyroSensor;
              import com.qualcomm.robotcore.hardware.I2cAddr;
              import com.qualcomm.robotcore.hardware.Servo;
      
      @com.qualcomm.robotcore.eventloop.opmode.Autonomous(name = "RedMidAuto", group = "What is this")
      public class RedMidAuto extends LinearOpMode {
          DcMotor leftWheel;
          DcMotor rightWheel;
      
          Servo buttonPusher;
      
          GyroSensor gyro;
          ModernRoboticsI2cRangeSensor frontRange;
      
          ColorSensor floor;
          ColorSensor beacon;
      
          int i = 1;
          int i1 = 1;
          int i2 = 1;
          int i3 = 1;
          int i4 = 1;
          int i5 = 1;
          int i6 = 1;
          int i7 = 1;
          int i8 = 1;
          int i9 = 1;
          int i10 = 1;
          int i11 = 1;
      
          @Override
          public void runOpMode() throws InterruptedException {
              leftWheel = hardwareMap.dcMotor.get("lw");
              rightWheel = hardwareMap.dcMotor.get("rw");
              leftWheel.setDirection(DcMotor.Direction.REVERSE);
      
              buttonPusher = hardwareMap.servo.get("b");
      
              buttonPusher.setPosition(.5);
      
              gyro = hardwareMap.gyroSensor.get("g");
              gyro.calibrate();
      
              frontRange = hardwareMap.get(ModernRoboticsI2cRangeSensor.class, "range2");
      
              floor = hardwareMap.colorSensor.get("c");
              floor.setI2cAddress(I2cAddr.create8bit(0x3c));
      
              beacon = hardwareMap.colorSensor.get("c2");
              beacon.setI2cAddress(I2cAddr.create8bit(0x6c));
      
              waitForStart();
      
              //backUp(); //1
              //sleep(1000);
              turnLeft45(); //2
              sleep(1000);
              straightUntilWhite(); //3
              sleep(1000);
              turnLeftPivot45(); //4
              sleep(1000);
              parkBeacon(); //5
              sleep(1000);
              redBeacon(); //good until this point
              sleep(1000);
              backUp2();
              sleep(1000);
              turnRight90(); //8
              sleep(1000);
              straightUntilWhite(); //9
              sleep(1000);
              turnLeft90(); //10
              sleep(1000);
              parkBeacon(); //11
              sleep(1000);
              redBeacon(); //12
              sleep(1000);
              backUp2();
          }
      
          public void turnLeft90() throws InterruptedException {
              while (opModeIsActive() && (i == 1)) {
                  if (gyro.getHeading() < 285 && gyro.getHeading() > 10) {
                      leftWheel.setPower(0);
                      rightWheel.setPower(0);
                      i--;
                  } else {
                      leftWheel.setPower(-.1);
                  }
                  idle();
              }
              i++;
              gyro.resetZAxisIntegrator();
          }
      
          public void turnRight90() throws InterruptedException {
              while ((opModeIsActive() && i1 == 1)) {
                  if (gyro.getHeading() > 75 && gyro.getHeading() < 350) {
                      leftWheel.setPower(0);
                      rightWheel.setPower(0);
                      i1--;
                  } else {
                      rightWheel.setPower(-.1);
                  }
                  idle();
              }
              i1++;
              gyro.resetZAxisIntegrator();
          }
      
          public void turnLeftPivot45() throws InterruptedException {
              while (opModeIsActive() && (i3 == 1)) {
                  if (gyro.getHeading() < 328 && gyro.getHeading() > 10) {
                      leftWheel.setPower(0);
                      rightWheel.setPower(0);
                      i3--;
                  } else {
                      rightWheel.setPower(-.1);
                      leftWheel.setPower(.1);
                  }
                  idle();
              }
              i3++;
              gyro.resetZAxisIntegrator();
          }
      
          public void turnLeft45() throws InterruptedException {
              while (opModeIsActive() && (i5 == 1)) {
                  if (gyro.getHeading() < 325 && gyro.getHeading() > 10) {
                      leftWheel.setPower(0);
                      rightWheel.setPower(0);
                      i5--;
                  } else {
                      rightWheel.setPower(-.1);
                  }
                  idle();
              }
              i5++;
              gyro.resetZAxisIntegrator();
          }
      
          public void turnRight45() throws InterruptedException {
              while (opModeIsActive() && (i6 == 1)) {
                  if (gyro.getHeading() > 35 && gyro.getHeading() < 350) {
                      leftWheel.setPower(0);
                      rightWheel.setPower(0);
                      i6--;
                  } else {
                      leftWheel.setPower(-.1);
                  }
                  idle();
              }
              i6++;
              gyro.resetZAxisIntegrator();
          }
      
         /* public void backUp() throws InterruptedException {
              while (opModeIsActive() && (i4 == 1)) {
                  if (rangeSensor.cmUltrasonic() < 10) {
                      leftWheel.setPower(-.1);
                      rightWheel.setPower(-.1);
                  } else {
                      leftWheel.setPower(0);
                      rightWheel.setPower(0);
                      i4--;
                  }
                  idle();
              }
              i4++;
          }
          */
      
          public void straightUntilWhite() throws InterruptedException {
              while (opModeIsActive() && (i10 == 1)) {
                  if (floor.green() > 15) {
                      i10--;
                  } else {
                      /*
                      if(gyro.getHeading() > 1 && gyro.getHeading() < 180) {
                          leftWheel.setPower(-.07);
                          rightWheel.setPower(-.25);
                      } else if(gyro.getHeading() < 355 && gyro.getHeading() < 179) {
                          leftWheel.setPower(-.25);
                          rightWheel.setPower(-.07);
                      } else {
                          leftWheel.setPower(-.07);
                          rightWheel.setPower(-.07);
                      }
                      */
                      leftWheel.setPower(-.07);
                      rightWheel.setPower(-.07);
                  }
                  idle();
              }
              leftWheel.setPower(0);
              rightWheel.setPower(0);
              i10++;
              gyro.resetZAxisIntegrator();
          }
      
          public void blueBeacon() throws InterruptedException {
              if(beacon.blue() > 3) {
      
                  buttonPusher.setPosition(1);
      
                  sleep(2000);
      
                  buttonPusher.setPosition(.5);
              } else {
                  buttonPusher.setPosition(0);
      
                  sleep(2000);
      
                  buttonPusher.setPosition(.5);
                  sleep(500);
              }
          }
      
          public void redBeacon() throws InterruptedException {
              if(beacon.red() > 3) {
                  buttonPusher.setPosition(1);
      
                  sleep(2000);
      
                  buttonPusher.setPosition(.5);
      
                  sleep(500);
              } else {
                  buttonPusher.setPosition(0);
      
                  sleep(2000);
      
                  buttonPusher.setPosition(.5);
      
                  sleep(500);
              }
          }
      
          public void backUp2() throws InterruptedException {
              while (opModeIsActive() && (i4 == 1)) {
                  if (frontRange.cmUltrasonic() < 10) {
                      leftWheel.setPower(.1);
                      rightWheel.setPower(.1);
                  } else {
                      leftWheel.setPower(0);
                      rightWheel.setPower(0);
                      i4--;
                  }
                  idle();
              }
              i4++;
          }
      
          public void parkBeacon() throws InterruptedException {
              while(opModeIsActive() && frontRange.cmUltrasonic() > 10) {
                  if(floor.green() > 7) {
                      leftWheel.setPower(-.1);
                      rightWheel.setPower(0);
                  } else {
                      leftWheel.setPower(0);
                      rightWheel.setPower(-.1);
                  }
              }
              leftWheel.setPower(0);
              rightWheel.setPower(0);
          }
      }

      Comment


      • #4
        Originally posted by Michael8461 View Post
        Thanks for the help! We updated our code with the opModeIsActive() calls, but it is still giving me that weird delay at the end. I must be missing something. Here's the revised code, if you have any other ideas:
        Hi

        Is it just a delay, or is there the stuck stop() error as well?

        You still have all those sleep commands in your code. They are also adding delays.

        Question: Are you running the latest SDK (released a couple of weeks back?) It builds a Rev 2.35 app.
        This code has better termination processing, but it does require a minot change in the opnode...

        To perform a clean exit, you should adopt the new interrupt style, whereby runOpMode is not interruptable.

        that is: change

        public void runOpMode() throws InterruptedException {

        to

        public void runOpMode() {

        And you can take out all the idle() calls, because they are now built into the opModeIsActive() call.

        When stop is pressed, it should truncate the sleep calls, and opModeIsActive() will also return false, so it should exit immediately.

        Comment

        Working...
        X