about ultrasonic sensor calibration

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

Moderators: roger, imaqine, 99jonathan

clh20000
New User
Posts: 1
Joined: Wed Mar 05, 2008 10:01 pm

about ultrasonic sensor calibration

Postby clh20000 » Wed Mar 05, 2008 10:08 pm

I am trying to calibrate the ultrasonic sensor in NXT with Lejos,there seems to be a lot of API functions,and it seems to be related with I2C sensor interface,the readvalue i got always shows 255,i have no idea of the calibration and wonder anybody has tried it and could kind provide a short sample program of it,thanks

import lejos.nxt.*;
class ultrasonicsensor {
public ultrasonicsensor(){
};
public static void main(String[] args){
UltrasonicSensor us = new UltrasonicSensor(SensorPort.S2);
SensorPort.S2.activate();
int dis=us.getDistance();
LCD.drawInt(dis, 2, 2);
LCD.refresh();
}
};

roger
Moderator
Posts: 371
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Postby roger » Thu Mar 06, 2008 8:38 pm

Code: Select all

  public static void main(String[] args)
   {
     UltrasonicSensor  sonar = new UltrasonicSensor(SensorPort.S3);
     boolean more = true;
     while(more)
     {
        LCD.drawInt(sonar.getDistance(),4, 0, 0);
        more = Button.readButtons()==0;
     }
   }
This works best if the sensor is actually connected to port 3.I mention it because distance 255 often means s loose connection.
My ultrasonic sensor seems very accurate out of the box without any calibration.
Good luck,
Roger

Spymaster101
New User
Posts: 4
Joined: Thu Mar 13, 2008 2:46 am

Postby Spymaster101 » Fri Mar 14, 2008 2:42 am

If above doesn't work try:

Code: Select all

UltrasonicSensor uss = new UltrasonicSensor(SensorPort.S2);
     while(true){
          uss.ping();
          LCD.drawInt(uss.getDistance(), 2, 2);
     }


This will constantly redisplay the distance on your LCD. Make sure you are connected to Sensor Port 2!!!!
And i also agree with above that "255 often means s loose connection."
"Life is like a train, someday it's gonna hit you. So you can either try to run away from it, or you can sit down, crack open a beer and watch it come."

Spymaster101
New User
Posts: 4
Joined: Thu Mar 13, 2008 2:46 am

Postby Spymaster101 » Fri Mar 14, 2008 1:55 pm

On second thougth do add a

Code: Select all

LCD.refresh();


And sometimes when the USS is too far to see anything it also just gives 255
I just barely ran into this. Also try shortening your cable. When i got this i switched to a short cable, and it worked perfectly fine.
"Life is like a train, someday it's gonna hit you. So you can either try to run away from it, or you can sit down, crack open a beer and watch it come."

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

Postby gloomyandy » Fri Mar 14, 2008 3:09 pm

Hi
As of leJOS 0.5 you will not need the LCD.refresh(). We added auto refresh of the display. so unless you turn this off you should no longer need to call refresh() manually.

Andy


Return to “NXJ Software”

Who is online

Users browsing this forum: No registered users and 2 guests