I'm a beginner with Java and Lejos and I'm having some troubles with mixing threads and sensor.
My aim is to have a bumper I release to start the robot and, when I hit a second time, the robot will stop.
So I wrote the following code :
- Code: Select all
import lejos.nxt.*;
import lejos.robotics.navigation.*;
import lejos.util.Delay;
class Robot
{
Arret a;
DifferentialPilot pilot;
public static void main(String[] args)
{
(new Robot()).run();
}
Robot()
{
pilot = new DifferentialPilot(4.0f, 12.5f, Motor.A, Motor.C, true); //for further use
a=new Arret(pilot);
}
void run()
{
while(a.starting());
a.run();
boolean bool=true;
while(a.ok())
{
LCD.drawString("BOUCLE PRINCIPALE",0,1);
}
}
}
- Code: Select all
import lejos.util.*;
import lejos.nxt.*;
import lejos.robotics.navigation.*;
import lejos.util.Timer;
import java.lang.Thread;
class Arret extends Thread
{
boolean boolok;
TouchSensor bumper;
DifferentialPilot pilot;
Arret(DifferentialPilot p)
{
boolok=true;
pilot=p;
bumper= new TouchSensor(SensorPort.S1);
}
public void run()
{
LCD.drawString("THREAD",0,2);
if(bumper.isPressed())
{
stop();
LCD.drawString("PRESSED",0,3);
}
else
{
LCD.drawString("NOT PRESSED",0,4);
}
}
public void timedOut() //90s se sont écoulées.
{
stop();
}
void stop()
{
boolok=false;
pilot.stop();
}
boolean ok(){return boolok;}
boolean starting()
{
return bumper.isPressed();
}
}
I know it's a little messy but I think it is understandable.
When I run my software, the begining works fine (ie the robot starts when I release the bumper) but after I can clearly see on the screen THREAD and NOT PRESSED on the screen but the bumper doesn't "answer." (nothing happend when I hit it)
I'm using the latest version of lejos (0.9) with Fedora Core 16.
PS: I know i could avoid all that stuff by putting directly the bumper my loop's tests but I would like to understand WHY it's not working.
Thanks.
David.
