UltrasonicSensor hangs in some cases

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

Moderators: 99jonathan, roger, imaqine

UltrasonicSensor hangs in some cases

Postby unroller » Thu Dec 08, 2011 8:59 pm

Hi,

I work on Segway-like robot and have found that UltrasonicSensor sometimes hangs. I cannot reproduce it by using some simple code, but I found that the problem occurs more frequently when I use remote control via Bluetooth.

I found that the execution hangs on the line
Code: Select all
i2cEvent.waitEvent(I2C_IO_COMPLETE << iPortId, NXTEvent.WAIT_FOREVER);

in i2cTransaction method of SensorPort.
It seems that the problem is in VM code.

I use two i2c sensors in my project but only UltrasonicSensor sensor hangs. LeJOS revision is 5926.

The source code can be found in http://dl.dropbox.com/u/13076168/NXJWay.zip. As a remote controller I use a game pad with the NXTGamePad program from nxtOSEK http://lejos-osek.sourceforge.net/nxtgamepad.htm.
unroller
Novice
 
Posts: 41
Joined: Sat Sep 25, 2010 6:10 am

Re: UltrasonicSensor hangs in some cases

Postby gloomyandy » Fri Dec 09, 2011 10:44 am

Hi,
I'm sorry but this test case is just way to complex for me to try and reproduce your problem. You will either need to reproduce the issue in a simpler test case or debug things further yourself. A few suggestions...

There are many reasons why code may appear to hang in a wait state, have you got say a high priority thread in some other part of your program that is spinning?
Does the completion bits for the event that you are waiting on ever get set? You should be able to write some code that dumps the state of the various completion bits to the screen.
Are you sure that only a single thread is reading the sensor? The sensor classes are not thread safe.
What happens if you unplug the sensor from the port when in the locked up state?
when you think the sensor is locked up try making a call to the i2cStatus call for the port and see what state the i2c code is in...

Good luck

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

Re: UltrasonicSensor hangs in some cases

Postby unroller » Fri Dec 09, 2011 4:26 pm

Hi,

1. Yes, the main control task runs as a high priority thread.
2. How can I get values of completion bits?
3. Only one thread reads the US sensor, but it is created in the main thread. There is some thread-local information associated with ports?
unroller
Novice
 
Posts: 41
Joined: Sat Sep 25, 2010 6:10 am

Re: UltrasonicSensor hangs in some cases

Postby skoehler » Fri Dec 09, 2011 4:54 pm

unroller wrote:Hi,

1. Yes, the main control task runs as a high priority thread.


high priority on leJOS is pretty much like "real time" on windows. It mean that as long as the thread is ready to execute, no other non-high priority thread will ever execute.

unroller wrote:3. Only one thread reads the US sensor, but it is created in the main thread. There is some thread-local information associated with ports?


That is not a problem. You can create the sensor on one thread, pass it on to another, or even multiple other threads. As long as no two threads call methods of the sensor concurrently, you're fine.
skoehler
leJOS Team Member
 
Posts: 1410
Joined: Thu Oct 30, 2008 4:54 pm

Re: UltrasonicSensor hangs in some cases

Postby gloomyandy » Fri Dec 09, 2011 5:42 pm

Hi you can get to the eventData field using getEventData, but the more useful userEvents can only accessed either by changing the code and rebuilding or using the VM class and the VMFields method to "peek" at the data...

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

Re: UltrasonicSensor hangs in some cases

Postby unroller » Sat Dec 10, 2011 8:32 am

I found that that eventData field has zero value when the US sensor hangs. When it works the eventData field has value 2 (my monitor thread pools the state every second).
unroller
Novice
 
Posts: 41
Joined: Sat Sep 25, 2010 6:10 am

Re: UltrasonicSensor hangs in some cases

Postby gloomyandy » Sat Dec 10, 2011 9:38 am

And what about the i2c status?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3992
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: UltrasonicSensor hangs in some cases

Postby gloomyandy » Sat Dec 10, 2011 9:40 am

And what happens if you unplug the sensor after it has hung?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3992
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: UltrasonicSensor hangs in some cases

Postby unroller » Sat Dec 10, 2011 9:59 am

If I unplug the sensor nothing is changed.
When the sensor hangs the i2c status is -2 (result of I2CPort.i2cStatus).

Also, I found that this issue occurs if following factors are present: two threads read i2c sensors (US and HT Accel sensors), there is a high priority thread that reads BT packets using readPacket method and performs some calculations (it called every 4 ms and each calculation cycle spends 0.6 ms). If the program reads only one i2c sensor or does not use BT, this issue occurs very rarely (but I cannot say that it does not occur).
unroller
Novice
 
Posts: 41
Joined: Sat Sep 25, 2010 6:10 am

Re: UltrasonicSensor hangs in some cases

Postby gloomyandy » Sat Dec 10, 2011 5:04 pm

-2 indicates that the device is still busy, which is why it is still waiting. Not sure why it is still busy though. Can you update to the latest snapshot and firmware and try that. Once you are up to date I can build you a test version of the firmware with debug in it... note that you will need to update everything not just the firmware...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3992
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: UltrasonicSensor hangs in some cases

Postby skoehler » Sat Dec 10, 2011 7:07 pm

@Andy: Hi, I have the feeling that the I2C transfer should eventually timeout such that the I2C transaction completes with an error code. That is not the case right now?
@unroller: Are there any other sensors attached to the same sensorport as the ultrasonic? Did you try another cable?
skoehler
leJOS Team Member
 
Posts: 1410
Joined: Thu Oct 30, 2008 4:54 pm

Re: UltrasonicSensor hangs in some cases

Postby unroller » Sat Dec 10, 2011 7:42 pm

I use the latest firmware.

I tried Lego and Mindsensors cables with the same effect.

I looked into i2c implementation in i2c.c and suspected that build_active_list function is not thread-safe. I checked it using Relacy Race Detector, and it found a data race in the line
Code: Select all
  i2c_port **new_list = (active_list == i2c_active[0] ? i2c_active[1] : i2c_active[0]);


I modified the SensorPort class to use class-level lock on i2cStartById and i2cCompleteById but it did not help.
Last edited by unroller on Sat Dec 10, 2011 7:53 pm, edited 1 time in total.
unroller
Novice
 
Posts: 41
Joined: Sat Sep 25, 2010 6:10 am

Re: UltrasonicSensor hangs in some cases

Postby skoehler » Sat Dec 10, 2011 7:50 pm

unroller wrote:Also, I found that this issue occurs if following factors are present: two threads read i2c sensors (US and HT Accel sensors), there is a high priority thread that reads BT packets using readPacket method and performs some calculations (it called every 4 ms and each calculation cycle spends 0.6 ms). If the program reads only one i2c sensor or does not use BT, this issue occurs very rarely (but I cannot say that it does not occur).

I think an I2C transaction is divided into two parts: sending the read request to the sensor, actually receiving the returned data. Both phases have to be initiated by Java code calling a native method. Is it possible, that the Ultrasonic doesn't like it, if the time between the two phases is too large? I'm particularly worried by unroller's statement, that the high priority thread sometimes performs calculations. Are those calculations taking a long time? Is the high priority thread consuming a long burst of CPU time sometimes? Maybe, this issue occurs if the calculations happen to happen inbetween the two phases?
skoehler
leJOS Team Member
 
Posts: 1410
Joined: Thu Oct 30, 2008 4:54 pm

Re: UltrasonicSensor hangs in some cases

Postby unroller » Sat Dec 10, 2011 8:21 pm

I tried to use normal thread priority for the controller task. In this case the problem also occurs, but requires more time to reproduce.
unroller
Novice
 
Posts: 41
Joined: Sat Sep 25, 2010 6:10 am

Re: UltrasonicSensor hangs in some cases

Postby skoehler » Sat Dec 10, 2011 8:22 pm

unroller wrote:I tried to use normal thread priority for the controller task. In this case the problem also occurs, but requires more time to reproduce.

OK, doesn't sound like the problem I suspected.
skoehler
leJOS Team Member
 
Posts: 1410
Joined: Thu Oct 30, 2008 4:54 pm

Next

Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 4 guests

cron
more stuff