Java 机器人障碍记录/避障
我试着让这个机器人朝着随机的方向走,直到它碰到障碍物为止。然后应记录该障碍物(障碍物=1,2,3等)和切换方向。这应该一直持续到计时器过期Java 机器人障碍记录/避障,java,while-loop,robot,Java,While Loop,Robot,我试着让这个机器人朝着随机的方向走,直到它碰到障碍物为止。然后应记录该障碍物(障碍物=1,2,3等)和切换方向。这应该一直持续到计时器过期 public static void main(String args[]) throws Exception{ Robot therobot = new Robot(); int x = 10000; int obstacles = 0; Random rand = new Random(); int r1 =
public static void main(String args[]) throws Exception{
Robot therobot = new Robot();
int x = 10000;
int obstacles = 0;
Random rand = new Random();
int r1 = rand.nextInt(255) + 1;
int r2 = rand.nextInt(255) + 1;
therobot.setWheelVelocities(100,100);
long before = System.currentTimeMillis();
while (System.currentTimeMillis() - before < x){
Thread.sleep(x);
if( therobot.isObstacle() ==true || therobot.isTapped() == true)
{
r1 = rand.nextInt(255) - 255;
r2 = rand.nextInt(255) - 255;
obstacles = obstacles++;
therobot.setWheelVelocities(r1, r2);
}
}
System.out.println(obstacles);
therobot.stopWheels();
therobot.quit();
}
publicstaticvoidmain(字符串args[])引发异常{
机器人机器人=新机器人();
int x=10000;
int=0;
Random rand=新的Random();
int r1=rand.nextInt(255)+1;
int r2=rand.nextInt(255)+1;
机器人的摆轮速度(100100);
很久以前=System.currentTimeMillis();
while(System.currentTimeMillis()-before
但这似乎不起作用。它会一直持续到计时器过期,但不会停止或记录任何内容
我错过了什么 有
int x = 10000;
long before = System.currentTimeMillis();
while (System.currentTimeMillis() - before < x){
Thread.sleep(x);
// Processing
}
intx=10000;
很久以前=System.currentTimeMillis();
while(System.currentTimeMillis()-before
由于Thread.sleep(10000)
,while循环的第一次迭代耗时10秒
睡眠时间应该明显少于总持续时间。我建议测试时不要每次迭代使用10秒。。。如果我们不知道isObstacle和isTapped的行为,我们也帮不了你。还有,为什么在while循环结束时调用robot.quit()?当您脱离while循环时,不应该调用它吗?只要根据名称猜测,如果左侧或右侧障碍物传感器检测到障碍物,等温线将返回true。isTapped的情况也是一样,如果有东西碰到它,它应该停止移动。此外,它们是布尔型的。Robot.quit()和stopWheels()是什么意思?它在锡罐上写的就是这个意思。止动轮将阻止车轮移动,因此机器人。therobot.quit将正确关闭与robot的连接并重置robot,以便它立即准备好由后续程序控制。您的位置是否正在另一个线程中更新?因为你要求这个线程在10秒钟内什么都不做,最后检查一次障碍物。没错,我把它们移到了循环之外。问题仍然存在,就是这样!现在可以了。我刚把线拿出来。睡个好觉。它做了我现在想要它做的事。谢谢你,巴德!很高兴知道它有帮助。完全取消
睡眠
可能会因为环路太紧而提高CPU使用率。