Thursday, November 6, 2008

Lesson 8



Date: 6. november 2008

Teacher: Ole Caprani

Made by: Henrik Hagen, Klaus Neumann and 
Martin Kristensen


Todays lesson
In this lesson we have to work on with the vehicles from lesson 7. They have to get several observable behaviors. We have to observe how it will react. See also the course homepage.

Observe several behaviors
Standard code (
We have to compile and run the code found at the course homepage. The car used is as instructed in the LEGO Mindstorms Education NXT Base Set 9797 manual, plus the ultra sonic sensor.

With the code the car drives randomly around. If the ultra sonic sensor is blocked it will drive backwards and turns. Every 10 seconds it plays a little tune. Look at the movie for example.

Only random drive
We expect the car to drive around randomly, if it hits anything it will keep on try driving "through" the obstacle.

The robot behaved exactly like we predicted. It ended up trying to drive through the wall.

With random drive and avoid
This time we again expect the car to drive around randomly, but this time it will avoid drive into the obstacles it sees on the way.

The robot drove around and avoided obstacles as predicted.

Out of these test we noticed the ultra sonic sensor on this model is placed to high.

A look into behavior class
Why is the behavior threads set as daemons?

If a thread is set as a daemon it will end when the program there started the thread end. If the thread not is set as a daemon you have to manual stop the thread, or else it will keep on running when the main program stop.

How is the suppress boolean used to suppress the other threads?

There's a method setSuppress witch is used by the others threads to suppress this thread. The forward, backward and stop methods, witch controls the motors, looks at the suppress boolean to see if its set or not.

Adding light sensor
By combining the class' from lesson 7 and lesson 8 we can make a "intelligent" vehicle. Which is driving towards the light but if its driving into anything it will prevent it. When its in a area with light enough it will drive around randomly. Course we are using the behavior  class it will play the stupid sound every 10 second, we could remove it but we won't.

For construction see picture, its the 9797 with extra RCX light sensor.

See the diagram for the layers of suppression

The code for the vehicle

import lejos.nxt.*;

public class findLight extends Behavior


   private RCXLightSensor lsr;

   private RCXLightSensor lsl;

   private final int enoughLightThreshold = 6;

       private Behavior b; // Behavior to suppress

      public findLight( String name, int LCDrow, Behavior b)


       super(name, LCDrow);

       lsr = new RCXLightSensor(SensorPort.S1);

       lsl = new RCXLightSensor(SensorPort.S2);

       this.b = b;


      public void run()




                        boolean showRightValue = true;

            int LeftLowstValue = 100;

            int RightLowstValue = 100;

                       while (true)


               int lightRight = lsr.readValue();

               int lightLeft = lsl.readValue();




                                      lightRight = lsr.readValue();

                   lightLeft = lsl.readValue();

                                      if(lightRight <>

                       RightLowstValue = lightRight;

                                      if(lightLeft <>

                       LeftLowstValue = lightLeft;

                                      if(showRightValue == true)


                       showRightValue = false;





                       showRightValue = true;



                   lightLeft = lightLeft - LeftLowstValue;

                   lightRight = lightRight - RightLowstValue;

               }while ( lightRight > enoughLightThreshold && lightLeft > enoughLightThreshold);


                              forward(100 - lightLeft * 3, 100 - lightRight * 3);






               drawString(" ");

                              b.setSuppress(false);                      }





No comments: