... | ... | @@ -24,17 +24,96 @@ The plan is to build a BumberCar to try out the different experiments with. Afte |
|
|
|
|
|
|
|
|
## Experiments with the BumberCar
|
|
|
Press the touch sensor and keep it pressed. What happens? Explain.
|
|
|
|
|
|
When conducting the first experiment with the bumbercar (Figure 1) we experienced that when the touch sensor was activated, the robot started to go through the detectWall class, which was driving firstly driving backwards and then turn a bit, still going backwards, after a couple of seconds it starts driving forward again to see if the coast is now clear.
|
|
|
|
|
|
![IMG_1760](http://gitlab.au.dk/uploads/u4099/legolabtimadala/f008cef598/IMG_1760.JPG)
|
|
|
##### Figure 1: BumberCars frontview
|
|
|
|
|
|
|
|
|
|
|
|
- Implement a third behavior, Exit. This behavior should react to the ESCAPE button and call System.Exit(0) if ESCAPE is pressed. Exit should be the highest priority behavior. Try to press ESCAPE both when DriveForward is active and when DetectWall is active. Is the Exit behavior activated immediately ?
|
|
|
|
|
|
After implementing the third behavior, Exit, we tried out the behavior and experienced that it was not triggered immediately when pushing the grey button very quickly. We then tried to push the button slower and then it work fine and thereby exits the program and stops driving.
|
|
|
|
|
|
|
|
|
|
|
|
- Both DriveForward and DetectWall have a method takeControl that are called in the Arbitrator. Investigate the source code for the Arbitrator and figure out if takeControl of DriveForward is called when the triggering condition of DetectWall is true.
|
|
|
|
|
|
When detecting a wall via the touch sensor, both the takeControl method for detectWall and DriveForward is set to true (fig 1) [7]. Even though, the takeControl method of drive forward is called, it is being suppressed since it has a lower priority than detect wall in the Arbitrator array. |
|
|
\ No newline at end of file |
|
|
When detecting a wall via the touch sensor, both the takeControl method for detectWall and DriveForward is set to true (fig 1) [7]. Even though, the takeControl method of drive forward is called, it is being suppressed since it has a lower priority than detect wall in the Arbitrator array.
|
|
|
|
|
|
![HigherLevelBehaviorSupressLowerLevelBehavior](http://gitlab.au.dk/uploads/u4099/legolabtimadala/6f644d9100/HigherLevelBehaviorSupressLowerLevelBehavior.JPG)
|
|
|
##### Figure 2: priority for executing methods
|
|
|
|
|
|
|
|
|
|
|
|
- The takeControl method of DetectWall contains a call to the ultrasonic sensor method getDistance that includes a delay. This means that the call of takeControl for the other behaviors is delayed and the reaction to an event is not immidiate. In [1] it is recommended that takeControl "should return quickly, not perform a long calculation." To avoid the pause in the takeControl method of DetectWall a local thread in DetectWall could be implemented that sample the ultrasonic sensor e.g. every 20 msec and stores the result in a variable distance accessible to takeControl. Try that.
|
|
|
|
|
|
To avoid the delay when the ultrasonic sensor calls the method getDistance, we have implemented the following code:
|
|
|
|
|
|
|
|
|
```
|
|
|
public DetectWall()
|
|
|
{
|
|
|
touch = new TouchSensor(SensorPort.S1);
|
|
|
sonar = new UltrasonicSensor(SensorPort.S3);
|
|
|
|
|
|
Thread t = new Thread(new Runnable() {
|
|
|
public void run() {
|
|
|
|
|
|
while (!Button.ESCAPE.isDown())
|
|
|
try {
|
|
|
|
|
|
sonarDistance = sonar.getDistance();
|
|
|
Thread.sleep(20);
|
|
|
} catch (InterruptedException e) {
|
|
|
// TODO Auto-generated catch block
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
t.start();
|
|
|
}
|
|
|
public boolean takeControl()
|
|
|
{
|
|
|
|
|
|
LCD.drawString("us vlaue", 2,2);
|
|
|
LCD.drawInt(sonarDistance, 3,3);
|
|
|
|
|
|
return touch.isPressed() || sonarDistance < 25;
|
|
|
}
|
|
|
```
|
|
|
##### Code Snippet 1
|
|
|
|
|
|
|
|
|
This code runs in a separate thread and updates the sonarDistance every 20 msec. This data is then used in the method takeControl int the detectWall class to know whether or not, this class’ action should be executed.
|
|
|
|
|
|
We were also asked to make the Legocar move backwards for one second before making the car turn. This behavior should be implemented when the Legocar detects a wall. To avoid using Thread.sleep or Delay the execution of the code, we implemented System.time and created a while-loop that makes the Legocar move backwards for one second while still being able to be suppressed by a click on the exit button.
|
|
|
|
|
|
```
|
|
|
public void action()
|
|
|
{
|
|
|
|
|
|
_suppressed = false;
|
|
|
int now = (int) System.currentTimeMillis();
|
|
|
|
|
|
while (!_suppressed && ((int)System.currentTimeMillis()< now + 1000) ){
|
|
|
BumperCar.leftMotor.backward();
|
|
|
BumperCar.rightMotor.backward();
|
|
|
}
|
|
|
|
|
|
Motor.B.rotate(-180, true);
|
|
|
Motor.C.rotate(-540, true);
|
|
|
while (!_suppressed && Motor.C.isMoving())
|
|
|
{
|
|
|
Thread.yield(); //don't exit till suppressed
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
##### Code Snippet 2
|
|
|
|
|
|
We also tried to implement a behavior that would make the detectWall action method restart if either the touch sensor or sonar triggered the detectWall while making a turn. We tried implementing this by creating a simple if-sentence within the action that should make the action restart. Although this did not work. the only thing it did was that, if the touch sensor or sonar was constantly activated then the wheels kept turning backwards.
|
|
|
|
|
|
We believe that a way to make this work could be done by creating a new behavior that could suppress the detectWall when triggered. This method could be called detectWall2. Although we know that this is not a viable solution since it would mean that we also should implement detectWall3, detectWall4 and so on, it could solve the problem.
|
|
|
|
|
|
When creating our wrestling robot, we used the alternated code Behavior.java, Arbitrator.java and BumperCar.java from [8]. When using this code the interruption worked, since we here were able to change the priority dynamically.
|
|
|
|