UltraSonic sensor doesn't work ??

This is where you talk about the NXJ hardware related topics such as the brick, sensors, LEGO pieces, etc.

Moderators: 99jonathan, roger, imaqine

UltraSonic sensor doesn't work ??

Postby eddynamique » Sun Oct 25, 2009 3:07 pm

Hello,

I've a problem with the UltraSonic sensor on the NXT brick. When I call the method getDistance(), it always return me "255". I tried with a test program but it doesn't work...
I give you the code :
Code: Select all

  public class testDeplace{

   public static void main(String [] args) throws Exception{
      
      UltrasonicSensor ultra = new UltrasonicSensor(SensorPort.S4);
      
      while(!Button.ESCAPE.isPressed()){
         LCD.drawString("Distance : "+ultra.getDistance(), 1, 1);
      }
   }
   
  }



Thanks to answer me.

eddynamique
eddynamique
New User
 
Posts: 5
Joined: Sun Oct 25, 2009 3:00 pm

Postby gloomyandy » Sun Oct 25, 2009 5:33 pm

Hi,
There is a bug in 0.85 that means that i2c sensors do not work on port 4. Try another port...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3629
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby eddynamique » Sun Oct 25, 2009 7:46 pm

Thanks Andy for your answer. I switched to the port 3 but I've got a new problem now... getDistance() returns an INT that start from 235 instead of 0 and I don't understand why...
I give you the code of my test program :
Code: Select all
public class testDeplace{

   public static void main(String [] args) throws Exception{
   
      while(!Button.ESCAPE.isPressed()){
         
         LCD.drawString("Distance : "+ultra.getDistance(), 0, 1);
         LCD.refresh();   
      }      
   }   
}


Thanks

eddynamique
eddynamique
New User
 
Posts: 5
Joined: Sun Oct 25, 2009 3:00 pm

Postby gloomyandy » Sun Oct 25, 2009 8:45 pm

Hi,
You may want to clear the screen before displaying new results, you are probably seeing old values and new values being combined.

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3629
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby eddynamique » Mon Oct 26, 2009 11:55 am

You're right Andy. And I notice that I must wait around two seconds before the distance be correct...

Thanks for yours answers

eddynamique
eddynamique
New User
 
Posts: 5
Joined: Sun Oct 25, 2009 3:00 pm

Postby gloomyandy » Mon Oct 26, 2009 12:32 pm

I don't think that is correct you should not have to wait any time for the distance to become correct (certainly not 2 seconds), so what happens if you don't wait? What sort of readings do you get, and what sort of thing are you doing to test these readings? You may get spurious readings from the sensor (welcome to the world of sensor handling), but you should be able to cleanup the data and ignore them...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3629
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby eddynamique » Mon Oct 26, 2009 8:43 pm

In fact, I noticed that I have to call getDistance() three times to get a correct value. The first and the second call return 255...
My code :
Code: Select all
public class testDeplace  {

   public static void main(String [] args) throws Exception{
   
      UltrasonicSensor ultra = new UltrasonicSensor(SensorPort.S3);
         
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 1);
      Thread.sleep(1000);
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 2);
           Thread.sleep(1000);
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 3);
      Thread.sleep(5000);
   }
}
eddynamique
New User
 
Posts: 5
Joined: Sun Oct 25, 2009 3:00 pm

Postby gloomyandy » Mon Oct 26, 2009 11:00 pm

Ah, ok so what I think you are seeing is the sensor powering on. What happens after the initial set of readings? You should not have to wait between readings to get stable data...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3629
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Postby eddynamique » Tue Oct 27, 2009 10:53 am

To test my code, there are 20cm between the wall and the sensor and I read the distance on the LCD display.

If I do that :
Code: Select all
public class testDeplace  {

   public static void main(String [] args) throws Exception{
   
      UltrasonicSensor ultra = new UltrasonicSensor(SensorPort.S3);
   
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 1);
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 2);
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 3);
           Thread.sleep(5000);
   }
}


I got this on the screen :
Code: Select all
     Distance : 255
     Distance : 255
     Distance : 255


And if I do this :

Code: Select all
public class testDeplace  {

   public static void main(String [] args) throws Exception{
   
      UltrasonicSensor ultra = new UltrasonicSensor(SensorPort.S3);
   
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 1);
                Thread.sleep(1000);
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 2);
                Thread.sleep(1000);
      LCD.drawString("Distance : "+ultra.getDistance(), 0, 3);
           Thread.sleep(5000);
   }
}


I got this :
Code: Select all
     Distance : 255
     Distance : 255
     Distance : 22



That's why I think I must wait few seconds to get a stable value... What do you think about my test?
eddynamique
New User
 
Posts: 5
Joined: Sun Oct 25, 2009 3:00 pm

Postby gloomyandy » Tue Oct 27, 2009 11:00 am

Hi,
The problem with your test is that it is only testing what happens when you first use the sensor (just after your program starts). It is normal for devices to take a short while to start up. The more interesting question is what happens after the device has started. Do you still have to wait between readings? I'm pretty sure that you should not have to do so...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3629
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Hardware

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff