Hi Jakob,

I think the reason this happens is that button listeners run in the Listener 
thread which runs at the highest priority. When the Pilot class runs in this 
thread and Thread.yield() is called, the listener thread does not yield but 
continues to run as it is higher priority than any other thread. (There are 
comments in Thread.java that explain the priority system and how it affects 
thread scheduling). This means that the two Motor regulator threads that 
implement the rotate method do not get to run, and the motors run forever. 
If you sleep instead of yield, the regulator threads get a chance to run.

I think this means that you should not run classes like Pilot in button or 
sensor port listeners, but instead should set variables that the main (or 
another) thread tests, and makes the appropriate call to Pilot.

I will look into this to see if there is a better solution.

There are other problems with buttons in leJOS NXJ which make button 
listeners rather unreliable. You really need to wait a few milliseconds 
after a button is first detected to allow it to stabilize or you will get 
false readings. Classes like TextMenu do this, but it is not done for button 
listeners. This means that the wrong button press or release is recorded 
some of the time. Detecting a RIGHT button press as a LEFT button press 
seems fairly common. We will see if we can do something about this for the 
next release.

Lawrie


----- Original Message ----- 
From: "Jakob Keres" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Tuesday, January 29, 2008 8:53 AM
Subject: [Lejos-discussion] Serious problems with Pilot and yield()


> Hi,
>
> I have problems to control a NXT with the Pilot. Following programm causes 
> the
> robot to move forward until the right button is pressed, then it stops and
> starts rotating endlessly. After start of rotating is not possible to quit 
> the
> program with the left button.
>
> package nxj;
>
> import lejos.nxt.*;
> import lejos.navigation.*;
>
> public class Main implements ButtonListener{
>
>    private Pilot pilot;
>
>    public static void main(String[] arg){
>       Main m = new Main();
>       m.doTask();
>    }
>
>    public void doTask(){
>       pilot = new Pilot(5.6f, 15.5f, Motor.A, Motor.C, true);
>
>       Button.RIGHT.addButtonListener(this);
>       startAction();
>
>       try {
>          Button.LEFT.waitForPressAndRelease();
>       } catch(Exception e){
>       }
>    } // doTask
>
>    public void startAction() {
>       pilot.forward();
>    }
>
>    public void buttonAction() {
>       pilot.stop();
>       pilot.rotate(60);
>    }
>
>    public void buttonPressed(Button arg0) {
>    }
>
>    public void buttonReleased(Button arg0) {
>       buttonAction();
>    }
>
> } // Main
>
> I recognized that everything is working as expected, when I substitute
> Thread.yield() with Thread.sleep(80) in the Pilot class.
> I also tried several threadsafe variations of this programm with 
> sychronized and
> wait() but it is always the same. Therefore, it is hard to believe that 
> anyone
> uses this class in a productive way.
>
> I also posted this problem in the forum, but got no response so far.
>
> Any ideas what's wrong with my code?
>
> Regards,
> Jakob
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________
> Lejos-discussion mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/lejos-discussion
>
>
> -- 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.516 / Virus Database: 269.19.15/1248 - Release Date: 
> 28/01/2008 21:32
>
> 


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Lejos-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/lejos-discussion

Reply via email to