[英]How do I end a method in Java after a certain amount of time?
我想强制某个方法在一定时间后结束,即使该方法尚未完成其任务。 我将如何去做呢?
编辑(添加说明和代码):我正在使用Android Studio为FTC(首次技术挑战)机器人比赛设计机器人。 为了控制机器人,我使用了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
}
我相信这就是你的意思。
如果需要,请进行一些澄清:“开始”是当前时间(以毫秒为单位)。 结束显然是在结束时。 (开始时间加延迟)尽管时间仍小于设置的结束时间,但代码仍在继续。
我知道这个问题有点老了,但是在最新的ftc_app Android SDK中,建议团队使用ElapsedTime类来了解时间,这些方法和过程也很重要。
暂停操作模式时要考虑的最重要的事情是,即使在驱动程序工作站应用程序上按下停止按钮,您仍然可以将其关闭。 您可以通过在while条件中包含opModeIsActive()方法来确保这一点
在我们的团队中,我们有一种暂停OpMode的方法,如下所示。 我们在用于库目的的单独类中声明了此代码。
public static void pauseOpMode(LinearOpmode op, ElapsedTime et, double waitTime){
double startTime = et.milliseconds();
while (op.opModeIsActive() && et.milliseconds() < startTime + waitTime){}
}
现在存在此方法,在我们的OpMode中,我们可以创建一个Elapsed time Object并将necessarry参数传递给暂停OpMode所需的函数
class someOpMode extends LinearOpMode{
ElapsedTime gameTimer = new ElapsedTime();
@Override
public void RunOpMode(){
//pause program for 5 seconds
pauseOpMode(this,gameTimer,5000);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.