机器人障碍物记录/避障



我试图让这个机器人向随机方向移动,直到它到达障碍物。然后它应该记录该障碍物(障碍物= 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 秒的整个持续时间。

睡眠量应明显少于总持续时间。

最新更新