The goal for this lab session is to make a sumo wrestling lego car.
The goal for this lab session is to make a sumo wrestling LEGO car.
## Plan
## Plan
The plan is to follow the plan described in [1], and to build a sumo wrestling lego car and decide on a control strategy to win the tournament.
The plan is to follow the plan described in [1], and to build a sumo wrestling LEGO car and decide on a control strategy to win the tournament.
## Bumper car
## Bumper car
When the touch sensor is pressed the "DetectWall" behavior is triggered which makes the car drive backwards and turn to the right. This will continue as long as the touch sensor is triggered.
An Exit behavior is added to the BumperCar in the following way.
```java
classExitimplementsBehavior
{
publicbooleantakeControl()
{
if(Button.ESCAPE.isDown())
returntrue;
returnfalse;
}
publicvoidsuppress()
{
//_suppressed = true;// standard practice for suppress methods
}
publicvoidaction()
{
System.exit(0);
}
}
```
The takeControl method waits for the ESCAPE button to be pressed and then returns true. This allows the Exit behavior to control the car with the action method. In this case the action is to Exit the program. The "Exit" behavior is activated immediately if the car is controlled by the "DriveForward" behavior due the implementation of the suppress method. In the "DetectWall" behavior this method is not implemented as therefore the "Exit" behavior is not allowed to overrule the current behavior.
In order to effectuate the "DetectWall" behavior a continuous reading of the ultra sonic sensor has been implemented using threading. The "DetectWall" class extends the "Thread" class as shown in the following code snippet.
```java
classDetectWallextendsThreadimplementsBehavior
```
The thread is started in the constructor of the "DetectWall behavior" and the reading performed using the runnable interface in the method `run()` as shown in the following code snippet.
```java
publicvoidrun()
{
while(true){
sonar.ping();
distance=sonar.getDistance();
}
}
```
The action method of the "DetectWall" behavior is now extended so that the car drives backwards for one second whenever the touch sensor is pressed or the sonar sensor detects an object in front. The implementation of the action method is shown in the following code snippet.