mandag den 8. november 2010

NXT Programming lesson 8

Lesson 8 description

Date: 4. November 2010

Duration of activity: 13 - 17

Group members participating: Nikki & Knud


GOALS for lesson 8

To implement several observable behaviors on the NXT controlling the 9797 LEGO car, with an ultrasonic sensor mounted.
The idea is to understand how to prioritize the different behaviors, and how they suppress each other.

Plan for achieving the goals:
The plan is to try SoundCar.java program as described in the lesson description with the 3 objects that each represent a single behavior. RandomDrive, AvoidFront and PlaySounds.

After this is done, we will try to add a behavior. The “drive towards light” or “Lightfinder”, as we called it, behavior.

Results:
We went back to lesson 7 and improved the light finder, with better sensors and better normalization. Our robot was now able to find its way out of a dark room and into the light through an open door.

The code that was given in the lesson 8 description was tested and confirmed that the behaviors implemented would also work as the prioritized order defined in the code which is:

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

This means that the levels are in the order of:
level 0: RandomDrive
level 1: AvoidFront
level 2: PlaySounds
And in practice that meant that the robot would start of driving around randomly as programmed in the RandomDrive class then each 10 seconds it would play a weird tune as described in the playsounds class however if the robot encountered an obstacle in front of it then the it would stop and back a little though not while playing a tune since the playsounds behavior has the highest priority. And it was important to understand that each behavior would suppress all behaviors at lower levels, when the suppress() method call is made because the suppressCount is incremented so it would jump to the next level or the behavior with higher priority.

We then added a behavior, the “Lightfinder” behavior or substituted it with the randomdrive behavior so to speak, and we changed the order a little so the avoidfront behavior became highest priority.
We tested it in the kitchen room in the zuse building with the lights turned off but with the door still open and it was possible for it to find and drive through the door itself from a random spot in the dark kitchen room. Sometimes it would drive against obstacles, but occurrences of that was greatly reduced when the lightfinder behavior could be suppressed by the AvoidFront behavior at all times.

So the order was:
lf = new Lightfinder("Findlight ",4,null);
ps = new PlaySounds ("Play ",3,lf);
af = new AvoidFront ("Avoid ",2,ps);

Or in term of priority or levels:
level 0: Lightfinder
level 1: PlaySounds
level 2: AvoidFront

A picture of all the classes in the eclipse project can be seen here.

It is important as well to understand that all these classes are running concurrently. For example, the avoidfront behavior is constantly checking, with the ultrasonic sensor, whether the distance to obstacles in front of the robot is bigger than a predefined threshold, if its not then it would suppress the behaviors with lower priority and back the robot a little while and then make a turn to avoid the obstacle.

Video of improved LightFinder behavior:


Lightfinder code:
import lejos.nxt.*;

public class Lightfinder extends Behavior {

static int normalize(int light){
int MAX_LIGHT = 500;
int MIN_LIGHT = 140;
int output = 100 - ((light - MAX_LIGHT)*100)/(MIN_LIGHT - MAX_LIGHT);
if (output < output =" 0;"> 100)
{
output = 100;
}
return output;
}

public Lightfinder(String name, int LCDrow, Behavior subsumedBehavior) {
super(name, LCDrow, subsumedBehavior);
}
public void run()
{

LightSensor LeftLight = new LightSensor(SensorPort.S4);
LightSensor Rightlight = new LightSensor(SensorPort.S1);

Rightlight.setFloodlight(false);
LeftLight.setFloodlight(false);

//power for disco lights on top of vehicle
MotorPort.A.controlMotor(100, 1);

while (true){
suppress();

int normright = normalize(Rightlight.getNormalizedLightValue());
int normleft = normalize(LeftLight.getNormalizedLightValue());
int leftinh = normleft;
int rightinh = normright;

forward((normright+40)-(leftinh-40),(normleft+40)-(rightinh-40));

release();

}
}
}

Ingen kommentarer:

Send en kommentar