简体   繁体   中英

How do I end a method in Java after a certain amount of time?

I would like to force a method to end after a certain amount of time even if it has not completed its task. How would I go about doing this?

Edit (added clarification and code): I am programming a robot using Android Studio for the FTC (First Tech Challenge) robotics competition. To control the robot, I am using the FTC SDK (see https://github.com/ftctechnh/ftc_app ).

The method works fine for going a particular distance and then stopping but after it stops by setting the power of all the motors to zero, it appears to get hung and no subsequent methods are invoked. Currently, it is only supposed to have the motors be stopped for one second before exiting but it appears to still get stuck on the first invocation of the method that sets the motor power to zero (setPower). For this reason, I would like to be able to terminate setPower after it has been running for a certain amount of time so that my method can exit and subsequent methods can be invoked.

Here is my method:

public void moveLine(DcMotor m1, DcMotor m2, DcMotor m3, DcMotor m4, double distance /* distance to move in meters */, double motorPower /* power to set the motors */) {

    final double SPROCKET_CIRCUMFRENCE = Math.PI * 0.0652; //calculates the circumference of the sprocket
    final int ENCODER_CPR_NR60 = 1680; //encoder counts for NeveRest 60
    //final static int ENCODER_CPR_NR40 = 1120; //encoder counts for NeveRest 40

    double amountOfRotationsCalc = distance / SPROCKET_CIRCUMFRENCE; //calculates the amount of rotations to move to reach the target distance

    double amountOfEncUnitsCalc = ENCODER_CPR_NR60 * amountOfRotationsCalc; //calculates the amount of encoder units to move

    //this gets the sum of the encoder positions of the drive motors
    int currentEncPosSum = m1.getCurrentPosition() + m2.getCurrentPosition() + m3.getCurrentPosition() + m4.getCurrentPosition();

    //this gets the average encoder position
    int currentEncPosAvg = currentEncPosSum / 4;

    //if the robot is supposed to be moving forward (positive distance), the motors will be set to positive values
    if (distance > 0) {

        //it may make sense to make this a while loop. Will this fix the issue?
        if (currentEncPosAvg < amountOfEncUnitsCalc) {
            m1.setPower(motorPower);
            m2.setPower(motorPower);
            m3.setPower(motorPower);
            m4.setPower(motorPower);
        } else {
            //these stop the robot. Without them, it continues to move.
            long start = System.currentTimeMillis();
            long end = start + 1000;
            while (System.currentTimeMillis() < end) {
                m1.setPower(0);
                m2.setPower(0);
                m3.setPower(0);
                m4.setPower(0);
            }
            return; //this is supposed to exit this method
        }

    } else {

        //this is essentially the opposite of the code for going forwards
        if (currentEncPosAvg > amountOfEncUnitsCalc) {
            m1.setPower(-motorPower);
            m2.setPower(-motorPower);
            m3.setPower(-motorPower);
            m4.setPower(-motorPower);
        } else {
            //these stop the robot. Without them, it continues to move.
            long start = System.currentTimeMillis();
            long end = start + 1000;
            while (System.currentTimeMillis() < end) {
                m1.setPower(0);
                m2.setPower(0);
                m3.setPower(0);
                m4.setPower(0);
            }
            return;
        }

    }

}
long beginning = System.currentTimeMillis();
long end=beginning + yourTimeInMilliseconds;
while (end > System.currentTimeMillis()){
    //your code here
}

I believe this is what you mean.

Some clarification, if you need any: beginning is the current time in milliseconds. end is obviously when it ends. (Start time plus delay) While the time is still less than the set end time, the code keeps going.

I know this question is a bit old, but in the latest ftc_app Android SDKs it is recommended that for time aware methods and procedures that teams use the ElapsedTime class.

The most important thing to consider when pausing an opmode is that you can still shut it down when the stop button is pressed on the driver station app. you can make sure of this by including the opModeIsActive() method in your while condition

On our team we have a method for pausing OpModes that looks something like this. we have this declared in a separate class used for library purposes.

public static void pauseOpMode(LinearOpmode op, ElapsedTime et, double waitTime){
    double startTime = et.milliseconds();
    while (op.opModeIsActive() && et.milliseconds() < startTime + waitTime){}
}

Now that this method exists, in our OpMode we can create an Elapsed time Object and pass the necessarry parameters to the function required to pause the OpMode

class someOpMode extends LinearOpMode{
    ElapsedTime gameTimer = new ElapsedTime();
    @Override
    public void RunOpMode(){
        //pause program for 5 seconds
        pauseOpMode(this,gameTimer,5000);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM