nhaulrik created page: Lab7 authored by Nikolaj Cilleborg Haulrik's avatar Nikolaj Cilleborg Haulrik
...@@ -164,3 +164,80 @@ For the first experiment with only cruise active, we observed a very simple beha ...@@ -164,3 +164,80 @@ For the first experiment with only cruise active, we observed a very simple beha
As an addition we tested the robot with Avoid and Cruise active. Here the robot drove straight ahead in a slight curve, ignoring light conditions. When encountering an object with the ultrasonic sensor, the robot compared distances and turned towards the direction where the distance is longest. As an addition we tested the robot with Avoid and Cruise active. Here the robot drove straight ahead in a slight curve, ignoring light conditions. When encountering an object with the ultrasonic sensor, the robot compared distances and turned towards the direction where the distance is longest.
## Exercise 3: Add an escape behavior
#### Task:
In the Figure 9.9 the top priority behavior is Escape. An implementation of Escape in IC is given on page 305 of [2].
Implement the Escape behavior in the RobotFigure9_9.java program.
#### Plan:
We impelemented the escape behavior as suggested by Jones, Flynn & Seiger [3].
```
import lejos.nxt.*;
import lejos.util.Delay;
class Escape extends Thread
{
private SharedCar car = new SharedCar();
private int power = 70, ms = 500;
TouchSensor touchLeft = new TouchSensor(SensorPort.S2);
TouchSensor touchRight = new TouchSensor(SensorPort.S3);
private boolean bumpLeft;
private boolean bumpRight;
public Escape(SharedCar car)
{
this.car = car;
}
public void bumpCheck()
{
bumpLeft = touchLeft.isPressed();
bumpRight = touchRight.isPressed();
}
public void run()
{
while(true)
{
bumpCheck();
if(bumpLeft && bumpRight)
{
car.backward(power, power);
Delay.msDelay(ms);
car.backward(0, power);
Delay.msDelay(ms);
}
else if(bumpLeft)
{
car.backward(power, power);
Delay.msDelay(ms);
car.backward(0, power);
Delay.msDelay(ms);
}
else if (bumpRight)
{
car.backward(power, power);
Delay.msDelay(ms);
car.backward(power, 0);
Delay.msDelay(ms);
} else
{
car.noCommand();
}
}
}
}
```