UltraSonic Sensor issue

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

Moderators: 99jonathan, roger, imaqine

UltraSonic Sensor issue

Postby nick2price » Sun Dec 27, 2009 2:31 am

Hi. Hopefully this is quite a simple problem. Basically, I have the code
Code: Select all
public class ObjectDistance {
public static void main(String[] args) throws InterruptedException{
   UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1);
   while(true){
      int value = us.getDistance();
      LCD.clear();
      LCD.drawInt(value, 0, 3);
      LCD.refresh();
      Thread.sleep(500);
   }
}
}


I was hoping for the distnace to be updated all the time, so that I could move the sensor around and keep getting different values. However, with the above, the only value it outputs is 255, no matter if I move the sensor about. Would there be any reason for this?

cheers
nick2price
New User
 
Posts: 9
Joined: Thu Jul 16, 2009 6:08 pm

Postby gloomyandy » Sun Dec 27, 2009 8:35 am

Hi,
What version of leJOS are you using? This code should work fine. I assume you have the sensor plugged into the correct port?

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

Postby gloomyandy » Sun Dec 27, 2009 10:58 am

Hi,
I've just switched back to a clean install of 0.85 and can't see any problem. I tried 3 different Ultrasonic sensors and they all worked fine. There was a bug with sensor ports in 0.85 that prevented the Ultrasonic sensor from working on port 4. Can I suggest that you try the following....
1. Open up a command shell.
2. cd into the leJOSNXJProjects\samples\SonicTest directory.
3. Compile it with nxjc SonicTest.java
4. Turn on your NXT and connect it using USB
5. Plug the Ultrasonic sensor into port 1
6. Run the test program using nxj -r SonicTest
7. Point the sensor at a flat surface (like a computer screen) approx 30cm away.
8. Check the display, what do you get? You should see details of the sensor version and type, followed by the distance to the screen.

If the above does not work you may wish to try the following...
1. Rename your existing leJOS classes.jar file as classes.rel. On a Windows PC this is located in c:\program files\lejosNXJ\lib
2. Download the updated classes.jar file from:
www.gloomy-place.net/lejos/classes.jar
2. Copy this file to your leJOS install (c:\program files\lejosNXJ\lib\clases.jar).
3. Recompile your test program with nxjc SonicTest.java
4. Turn on your NXT
5. Run the program nxj -r SonicTest
6. Test again as above?

Let me know what happens...

If the new classes.jar seems to work, you should probably replace the one in the directory in your home dir:
leJOSNXJProjects\classes\lib\classes.jar

Hope this helps

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

Postby nick2price » Sun Dec 27, 2009 11:04 pm

Hi. I am using the second latest version of LeJOS, think it is 0.8. Strangly enough, when I run sonic test, it works fine. And the only real difference I can see from that to my code is that they make a direct call to getDistance inside the drawInt method, whereas I assign it to a variable first. I tried my one again with the same result, so not sure what would be causing this. However, seeing that SonicTest is working, it doesnt really matter anymore.

cheers
nick2price
New User
 
Posts: 9
Joined: Thu Jul 16, 2009 6:08 pm

Re: UltraSonic Sensor issue

Postby hawesie » Tue Mar 15, 2011 10:02 am

Hi Andy,

Please could you post a link to the source (or svn revision) for the updated UltrasonicSenor code? I have an install of 0.85 which I'm currently using for teaching and I'd like to fix this bug without altering the rest of the source.

thanks,

Nick
hawesie
New User
 
Posts: 12
Joined: Thu Sep 10, 2009 8:09 am

Re: UltraSonic Sensor issue

Postby gloomyandy » Tue Mar 15, 2011 11:21 am

Hi,
The fix for this problem here not made to the Ultrasonic sensor driver, they were made to the SensorPort code. Also to re-create the jar I built you may also need some additional fixes to the color sensor code (assuming you use the color sensor), again this is in the SensorPort class. Unfortunately I don't think this code exists any more (or at least not in an easily accessible place, I may have it on a backup disk) and it has been a long time since then! The changes I made were intended as a quick test to establish what the problem was that some 0.8.5 users were having. I basically backported some fixes from the then current snapshot to the 0.8.5 source base. In general we do not tend to maintain older releases so this code was never checked back in as a branch to the leJOS svn repositories. In essence the change is shown by this diff:
http://lejos.svn.sourceforge.net/viewvc ... 23&r2=3353
But I'm not sure that this is how I applied the change. The key thing is that modifications should not be made to the sensor I/O pin state when changing the other settings associated with a port being used for i2c operations.

The changes needed for the color sensor are shown below...
http://lejos.svn.sourceforge.net/viewvc ... 26&r2=3423

Could you provide a little more background on how you are using leJOS? Do you normally build the classes.jar for your class from source? Do the students do this? If not then you can simply use the replacement classes.jar given above. If not then I suggest that you apply a change similar to the above diffs (or simply add an if statement testing for the two i2c sensor types, around the sensor I/O pin setting code) and test the code to ensure that it works for you...

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

Re: UltraSonic Sensor issue

Postby hawesie » Tue Mar 15, 2011 1:10 pm

Hi Andy,

Thanks for the info. I provide the students with the jar file (via a networked drive) which they then compile against. I've been using the jars that are packaged with 0.8.5. As the students are currently right in the middle of a major exercise, I don't want to change too much of the leJOS code and risk causing them problems. This is why I was interested in only the fixes needed for this issue. Does the above jar contain any other changes from 0.8.5 or is it just the SensorPort ones you've provided the diffs for? If it's just those then I will replace the current jar I use, otherwise I'll apply the patches to 0.8.5 myself.

thanks,

Nick
hawesie
New User
 
Posts: 12
Joined: Thu Sep 10, 2009 8:09 am

Re: UltraSonic Sensor issue

Postby skoehler » Tue Mar 15, 2011 1:48 pm

hawesie wrote:Hi Andy,

Thanks for the info. I provide the students with the jar file (via a networked drive) which they then compile against. I've been using the jars that are packaged with 0.8.5. As the students are currently right in the middle of a major exercise, I don't want to change too much of the leJOS code and risk causing them problems. This is why I was interested in only the fixes needed for this issue. Does the above jar contain any other changes from 0.8.5 or is it just the SensorPort ones you've provided the diffs for? If it's just those then I will replace the current jar I use, otherwise I'll apply the patches to 0.8.5 myself.


I believe, Andy has assembled this classes.jar as a drop-in replacement for the 0.8.5 version. That is, there are no API changes, only implementation changes.

There might be another UltraSonic sensor issue still present in Andy's JAR. Not sure, whether he fixed that one as well, but it's simple to fix: there must be a minimal delay of at least 50ms between individual calls to getDistance() or getDistances(). Otherwise, the Ultrasonic returns 255 even if there is an object in front of it.
skoehler
leJOS Team Member
 
Posts: 1397
Joined: Thu Oct 30, 2008 4:54 pm

Re: UltraSonic Sensor issue

Postby gloomyandy » Tue Mar 15, 2011 2:57 pm

Hi,
The jar contains only the fixes. There are no API changes. It does not contain any changes to UltrasonicSensor, so the the problems that Sven mentioned may still exist...

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

Re: UltraSonic Sensor issue

Postby hawesie » Tue Mar 15, 2011 8:02 pm

Thanks for the replies guys. I'll try it out first thing tomorrow and let you know how it goes.

cheers,

Nick
hawesie
New User
 
Posts: 12
Joined: Thu Sep 10, 2009 8:09 am

Re: UltraSonic Sensor issue

Postby hawesie » Wed Mar 16, 2011 9:06 pm

Seems to be working well. It also fixes the error that calling ping() wouldn't switch modes on the sensor.

cheers,

Nick
hawesie
New User
 
Posts: 12
Joined: Thu Sep 10, 2009 8:09 am


Return to NXJ Software

Who is online

Users browsing this forum: gloomyandy and 2 guests

more stuff