User Tools

Site Tools


marvin:lab10

This is an old revision of the document!


Lab report 10

Date: November 21st 2008
Duration of activity: 8-12
Participants: Kasper, Bent and Johnny

Project Goal

In this lab session we will investigate how a behaviour-based architecture1), has been implemented in the subsumption API of leJOS NXJ. We will investigate the interface lejos.subsumption.Behavior and the class lejos.subsumption.Arbitrator and try to make an alternative implementation of the Arbitrator.

lab10-1.jpg
Marvin

Plan

The plan for today is devided into two parts. First we will investigate and evaluate the behaviour of BumperCar2). And second we will change the behaviour of Arbitrator for BumberCar3)

Part One

This will include the following:

  • Behaviour of BumberCar4)
  • Investigate the source code for the Arbitrator5)
  • Implement a third behaviour called Exit that will react on Escape button
  • Implement a fourth behaviour called Playsound that will react on Enter button

Part Two

Implementation of an alternative Arbitrator that will make sure that takeControl of all behaviours are called each time through the loop. When the Arbitrator is working we will compare it to the lejos Arbitrator.

Execution

The program bumberCar.java is run in the NXT and we observe what happens when the touch sensor is kept pressed constantly. We expect the hitWall behaviour to be active making Marvin go backwards and turn over and over. This is not what happens though and the reason for this is found by inspection of the arbitrator within the Lejos system. The source code for the arbitrator is shown below.

The lejOS Arbitrator

public class Arbitrator {
 
   .
   .
   .
 
   public void start() {
      int totalBehaviors = behavior.length - 1;
 
      while(true) {
         // Check through all behavior.takeControl() starting at highest level behavior
         for(int i = totalBehaviors;i>=0;--i) {
            if(behavior[i].takeControl()) {
               // As soon as takeControl() is true, execute the currentBehavior.suppress()
               //if(behavior[i] != currentBehavior) {
               if(i != currentBehavior) { // Prevents program from running same action over and over again
                  if (currentBehavior != NONE) {
                     if(currentBehavior >= i) // If higher level thread, wait to complete..
                        while(!actionThread.done) {Thread.yield();}
                     behavior[currentBehavior].suppress();
                  }
                  // Make currentBehavior this one
                  currentBehavior = i;
 
                  // Run the currentBehavior.behaviorAction()
                  actionThread.execute(i);
                  Thread.yield();
               }  
               break; // Breaks out of for() loop
            }
         }
      }
   }
 
   .
   .
   .
 
   private class BehaviorAction extends Thread {
      public boolean done = true;
      int current = NONE;
 
      public void run() {
         while(true) {
            	synchronized(this)
         	{
            	if(current != NONE) {
               		done = false;
               		behavior[current].action();
               		current = NONE;
               		done = true;
            	}
            }
            Thread.yield();
         }
      }
 
      public synchronized void execute(int index) {
         current = index;
      }
   }
}

Observe the test if(i != currentBehavior). This condition ensures that the same behaviour is not detected over and over, which is in fact the reason why HitWall() is not run continuously when the touch sensor is held pressed. Both DriveForward and HitWall contains the method takeControl and again by inspection of the arbitrator we observe that takeControl of DriveForward is called when HitWall is active, but because HitWall has a higher priority, the arbitrator waits until HitWall is done.
In order to further investigate the arbitrator we now implement a new behaviour called Exit, which calls System.exit(0) when the button escape is pressed. This new behaviour has the highest priority, which means that the priorities listed in descending order is Exit, HitWall and DriveForward. We now investigate what happens when pushing the escape button while HitWall is active. If Exit was an alarm indicator we would prefer that the current behaviour, HitWall, was interrupted when Exit is called. To our surprise, the result of this test is, that when Exit is called via the escape button the arbitrator allows HitWall to finish and further more it chooses DriveForward to be activated before the Exit. After some tests we realize that the arbitrator does not notice the escape button being pressed while HitWall is active. Therefore it activates DriveForward, which is called once by the arbitrator and only then the arbitrator will notice the Exit call. The reason that the arbitrator does not react to the Exit behaviour (even though it has the highest priority) when HitWall is active is, that the program is stuck in the while loop

while(!actionThread.done) {Thread.yield();}

The conclusion is that the arbitrator is working improperly in terms of a desired interrupt for the Exit behaviour. There are no reasons for further testing of this arbitrator as we may as well start to modify the arbitrator. One could of course write a new arbitrator from the ground off, but we believe that the arbitrator can be properly modified towards our desired goal. If we omit the line (i != currentBehavior) it is possible to run the same behaviour over and over, which could be important for an alarm sensor, which may have reason for re-activating itself. We may add another condition to the while loop, which allows an interrupt from a higher priority behaviour, but a final decision has not been made. The remaining time of this lab session we decided to devote to Motivation functions instead of working on a new arbitrator.

Motivation Functions

The “normal” case is that when hitWall is active, Marvin will back out, turn a little bit an step out of that routine. But what if the hitWall routine is activated repeatedly. What happens then? In the group, we talked about the case where Marvin would hit a bouncing wall, e.g. a wall with a spring attached to the back of it, so that it would move towards Marvin when pushed once. In this case Marvin would react the normal way, that is, back out and turn, but the wall will then hit him again, and this will repeat itself. In this case, it would be convenient to implement some sort of mechanism that would help Marvin to get out of that situation. Lets could it a motivation to do something. Thiemo Krink6) has some proposals on this topic. Marvin could have some sort of motivation factor to get away from the wall on change direction. As is seen in the picture below we have drawn a simple graph to illustrate how we think Marvin should behave in the case where he hits the wall several times.


The Krink model

lab10-2.jpg
Motivation over time

The first time Marvin hits the wall his motivation to back out and turn should be major. If takeControl takes over to reactivate the routine the motivation should thereafter me minor because of e.g. the spring wall mentioned earlier. There after the motivation should again rise over some time (the time period is case not important as we only talk principles, but is of course important in the implementation) to improve the motivation to get away from the wall. It should be noted that the first time Marvin hits the wall, it would be the normal case and after some time the motivation should be greater than normal state. This may not be that well illustrated on the graph.

The implementation of this could be e.g. 10-20 seconds time interval from first hit to full motivation and could be an integer or percentage (floating point number) that will increase the speed Marvin backs up with and second the degree of turn that he makes.

Conclusion

The LejOS implementation of the Arbitrator seems very random, and in many cases it disqualifies as an arbitrator.
A new implementation of the arbitrator could be made using the Krink model.

marvin/lab10.1227859632.txt.gz · Last modified: 2008/11/28 09:07 by deva