机器人障碍 Recording/Avoiding
Robot Obstacle Recording/Avoiding
我试图让这个机器人朝随机方向前进,直到它到达障碍物。然后它应该记录那个障碍物(Obstacle = 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 = 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();
}
但这似乎行不通。它只会持续到计时器到期,但不会停止或记录任何内容。
我错过了什么?
有
int x = 10000;
long before = System.currentTimeMillis();
while (System.currentTimeMillis() - before < x){
Thread.sleep(x);
// Processing
}
由于 Thread.sleep(10000)
.
,while 循环的第一次迭代需要 10 秒的整个持续时间
睡眠时间应明显少于总持续时间。
我试图让这个机器人朝随机方向前进,直到它到达障碍物。然后它应该记录那个障碍物(Obstacle = 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 = 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();
}
但这似乎行不通。它只会持续到计时器到期,但不会停止或记录任何内容。
我错过了什么?
有
int x = 10000;
long before = System.currentTimeMillis();
while (System.currentTimeMillis() - before < x){
Thread.sleep(x);
// Processing
}
由于 Thread.sleep(10000)
.
睡眠时间应明显少于总持续时间。