gpt4 book ai didi

java - 如何在 Java 中在一段时间后结束一个方法?

转载 作者:行者123 更新时间:2023-11-30 07:38:49 25 4
gpt4 key购买 nike

我想强制一个方法在一定时间后结束,即使它尚未完成其任务。我该如何去做呢?

编辑(添加说明和代码):我正在使用 Android Studio 为 FTC(First Tech Challenge)机器人竞赛编写一个机器人。为了控制机器人,我使用 FTC SDK(请参阅 https://github.com/ftctechnh/ftc_app )。

该方法对于行驶特定距离然后停止来说效果很好,但是在通过将所有电机的功率设置为零而停止后,它似乎挂起并且没有调用后续方法。目前,它只应该让电机在退出前停止一秒钟,但在第一次调用将电机功率设置为零 (setPower) 的方法时,它似乎仍然卡住。因此,我希望能够在 setPower 运行一定时间后终止它,以便我的方法可以退出并调用后续方法。

这是我的方法:

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
}

我相信这就是你的意思。

一些说明(如果您需要的话):begin 是当前时间(以毫秒为单位)。end 显然是结束的时候。 (开始时间加上延迟)虽然时间仍然小于设定的结束时间,但代码继续执行。

关于java - 如何在 Java 中在一段时间后结束一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34983788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com