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?

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.jar2. 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

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.

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.

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=3353But 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.

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...

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 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.