Monday, May 5, 2014

Lab Session 10

Date: 01 May 2014
Duration of activity: 3 hours
Group members participating:  Daniela, Helene, Lukas


Goals 
Do the BumperCar exercises to get insight into behaviour based architecture
 

Plan
  • Build the BumperCar
  • Observe the behaviours from the LejOS tutorial
  • Analyse LejOS Arbiter class
  • Implement behaviours
  • Explore motivation functions
Results

We started by building the BumperCar, based off of the instructions for the ExpressBot. The robot then looked like this:




BumperCar

The exercises then said to run the BumperCar.java program that can be found in the leJOS samples found here.

Running this program, the robot drives forward, until the pressure sensor is pressed, at which point the robot drives backwards and turns slightly and then resumes driving forward.

When we hold the pressure sensor down, the robot keeps repeating the pattern of driving backwards and turning to the right, until the button is released.

When reading through the source code of BumperCar.java, we note that the exhibited behaviour is defined by the DetectWall class, which turns the wheels backwards a set amount of degrees. It must be this behaviour, which is used repeatedly while the button is pressed.

The Exit Behaviour


We are asked to implement the Exit behaviour, which should shut down the program, when the Escape button is pressed. In order to do this correctly, we need to change the DetectWall behaviour, because it can not be suppressed at this moment.

We changed the DetectWall behaviour, so that it remembers via the suppress variable that suppress() was called. When the action() method then is executed by another thread, a while loop tests if suppress() was called and stops the motors and exits the behaviour. The code looks like this:




Then we implement a new Exit behaviour, which should take action when the Escape button is pressed, and exit the program, when its action() method is called. There is no point in letting another behaviour suppress the Exit behaviour, because the program should shut down anyway. The implementation looks like this:


Because the Exit behaviour should have the highest priority of all the behaviours, it needs to be the last behaviour in the list given to the arbiter. This is shown here:





While running this program, we pressed the Escape button to see if it would stop the program. If we held the button long enough, the program would exit as intended, but when we pressed the button only for a short moment, the wheels would stop turning shortly, and then resume driving, without the program quitting. It looked like the other two behaviours were being suppressed, which stopped the motors, but then activated immediately again, before the Exit behaviours action() method could be called.

We assume this happens because the Arbitrator chooses another behaviour to run, before there is time to call action() on the currently selected behaviour, which results in that other behaviours are suppressed, but the chosen behaviour not being used at this point in time.

There is only a single thread running on the NXT CPU at any given time, because the processor only has a single core. It might be the case that the time-slices assigned to threads are too large. With big time-slices, the thread deciding what behaviour to run, might run through its loop multiple times, without the thread calling action() being run in the meantime.

It did not matter, what other behaviour was active at the time, we tried pressing the button both when DetectWall was active and when DriveForward was active and the only thing that made a difference was how short the button was pressed down.

TakeControl


We are tasked to investigate, whether or not the takeControl() is called, when a behaviour with higher priority already returned true for takeControl().
When reading the Arbiter source code, we see that when one behaviour returns true for the takeControl() call, break is called, and the loop exits. This means that takeControl() is not called for the other behaviours that have a lower priority. The code in question looks like this:


Parallel Sensor Input


We want to speed up the takeControl() call of the DetectWall behaviour, by not calling the UltrasonicSensor's methods, which have a built-in delay.

To avoid this, we run another thread that calls the sensors methods periodically and stores the results, also called continuous sampling. We can then inside the DetectWall behaviour ask this thread, what the most recent result is, instead of waiting for results. We have implemented this thread as follows: [thread_code_1.png]
Then the DetectWall behaviour is changed slightly, it should not use the UltrasonicSensor directly, but simply start a new thread and supply a UltrasonicSensor to it, which looks like this:



There were no obvious changes in the robots behaviour when changing the code for the DetectWall behaviour. This might be because the delays were so small, that we could not notice them. The implementation is closer to what leJOS suggest with respect to the takeControl method and that it should take very little time, which might be important in the long run, for more complex programs and behaviours.

Extending the Detect Wall behaviour
We are asked to change the DetectWall behaviour to let the robot move backwards for one second, before turning.


To achieve this, we store the current time and the the wheels turn backward, until either a thousand milliseconds have passed, or the behaviour is suppressed.
Then before resuming the normal behaviour we test if this behaviour has been suppressed, in which case we simply stop the motors and exit the action() method. 

The code now looks like this:
 

And the robot now behaves like this:
 


Now we want to be able to restart the DetectWall behaviour when the touch sensor is pressed again. We chose to implement this in a rather naive fashion, e.g. testing if the touch sensor is pressed while inside the behaviour, and if so, simply call action() again, like this: 



Technically, this is not completely correct, we could also test for the sensor being pressed in the first while loop, where we let the robot drive backwards for a second. Also, we simply test if the sensor is pushed down right now, instead of testing if it ever has been released in the meantime. These details are easy to change, but what is more important, is that this way of implementing interruptible and resettable behaviours seems inflexible, because it requires us to change every loop inside a behaviour, which might be really annoying for complex behaviours.

The implementation works reasonable and the robots keeps reusing and resetting the DetectWall behaviour, as seen in this video:



Motivation Functions

We did not have enough time to implement an arbiter and behaviours, but we discussed motivation functions and the sample implementations.
The most important point we came across was, that compared to the leJOS arbitrator, this implementation allows behaviours to change their priority on the fly. This makes it possible to let behaviours reset themselves, without explicitly writing coding this in action(), like we did with DetectWall, but simply by changing the priority once while the behaviour is active.

Conclusion
We feel that we now have an idea on how the leJOS subsumption framework works , and how to use it to define different behaviours, to let the robot react to different input. We also might want to look more into motivation functions and consider using those for the final project of the course, instead of the standard Arbiter implementation.

References 
http://legolab.cs.au.dk/DigitalControl.dir/NXT/Lesson10.dir/Lesson.html