Monday, 9 April 2012

Legolab 7

Legolab 7
Date: 29-03-12
Duration: 3 hours
Group members attending: Tore, Troels & Kristian

Goal
Complete todays exercises concerning threading.

Plan
1. Mount the ultrasonic sensor on our car.
2. Try out the program SoundCar (*1), observe behavior, analyse the program.
3. Add behavior to the robot.

Execution
1. This was easily done, as shown below:


2. We transfered the SoundCar program to the robot. It appears the program runs 3 threads, one to drive, one to avoid obstacles and one to play a sound. The LCD displays an overview of the threads, whenever a thread has control, a “1” is displayed next to the thread of the level below.
The RandomDrive is level 0, the AvoidFront is level 1 and the PlaySounds is level 2.
Every tenth second the sound is played. The avoid-thread is triggered, when the distance measured is below 20, thus making the car drive backwards for a little while and then turn. The drive-thread has the control the rest of the time.
The RandomDrive is level 0, the AvoidFront is level 1 and the PlaySounds is level 2.
The purpose of making the daemon threads is to make sure that the whole program exits when we close the main program. The daemon threads works as supporting threads to the main program. When the main program exits there is no need for daemon threads and the interpreter will exit.

The suppressCount is used to check, whether a thread can take control or not. If e.g. the PlaySounds thread is in control, it uses the suppress() method to suppress AvoidFront and RandomDrive, making these threads unable to control the car. When the PlaySounds is done it releases control again with the realease() method. If a thread calls suppress(), it recursively suppresses all the levels below.
We could not find the article of Fred Martin mentioned in the exercises.

3. We modified todays testprogram with an extra thread level between the RandomDrive and the AvoidFront, to make the car drive towards the light. It was mostly copy-paste from last labsession and editing the suppress-mechanism. The light-sensors was mounted on the car like we did last time.

Condition
lightThreshold = 800;
    while (light1 < lightThreshold &&
              light2 < lightThreshold) {
          light1 = ls1.getNormalizedLightValue();
           light2 = ls2.getNormalizedLightValue();
           drawInt(ls1.getNormalizedLightValue());
       }

    Subsumption
    rd = new RandomDrive("Drive",1, null);
      tl = new TowardsLight("Light",2,rd);
      af = new AvoidFront ("Avoid",3,tl);
      ps = new PlaySounds ("Play ",4,af);

Status
The exercises today taught us the basics of multi-threading and how to manage thread control. We were impressed with how simple yet powerful the structure of the layers were. It’s very easy to add new layers and the structure will adapt automatically. This could be very useful later on.

references
(*1) http://legolab.cs.au.dk/DigitalControl.dir/NXT/Lesson7.dir/SoundCar.java

No comments:

Post a Comment