Threading and sensor

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Threading and sensor

Postby Davidbrcz » Thu Feb 09, 2012 8:32 pm

Hi all.

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.
French student in Supaero. See more there : http://www.isae.fr/en/isae_training_offer2/supaero_graduate_engineer.html
Davidbrcz
New User
 
Posts: 2
Joined: Mon Jan 30, 2012 4:35 pm

Re: Threading and sensor

Postby Rickz2020 » Thu Feb 09, 2012 9:07 pm

Ah sorry, misread.

Maybe you should have another while loop inside of your run, so that it continues forever?
Code: Select all
void run()
  {
  while(true){
       while(a.starting())){
               a.run();
               boolean bool=true;
        }
       while(a.ok()){
                LCD.drawString("BOUCLE PRINCIPALE",0,1);
              }
        }     
}

Regards,

Rich.
Rickz2020
Active User
 
Posts: 121
Joined: Thu Feb 10, 2011 5:47 pm
Location: London, Earth.


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

cron
more stuff