Exception using UltrasonicSensor with Thread [SOLVED]

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

Moderators: 99jonathan, roger, imaqine

Exception using UltrasonicSensor with Thread [SOLVED]

Postby Luke » Fri Apr 06, 2012 10:24 am

Hi there,

I'm using three UltrasonicSensor-Sensors on my robot and all distance stuff is handled by one thread
(there are two other threads, one for movement with a DifferentialPilot and the other for calculating the path).
The three sensors are called one by one (in one thread) and not parallel.

My code works in 8 of 10 cases. But in those 2 cases I get an NegativeArraySizeException.
I couldn't isolate the circumstances that cause this error yet.

Could anyone please give me a hint what I'm doing wrong?

Stack trace:
Code: Select all
Exeption : 35
at: 142(17)
at: 76(7)
at: 61(75)
at: 60(10)
at: 64(2)

Class 35: java.lang.NegativeArraySizeException

Method 142: lejos.nxt.<rasonicSensor.getRanges() PC 15298 Signature id 472
Method 76: lejos.robotics.objectdetection.RangeFeatureDetector.scan() PC 12871 Signature id 476
Method 61: sensors.DistanceControl.getDistance(sensors.DistanceSeonsor) PC 12234 Signature id 343
Method 60: sensors.DistanceControl.lookAround() PC 12190 Signature id 342
Method 64: sensors.DistanceControl.run() PC 12524 Signature id 1


Here is the code I wrote for getting the distance:
Code: Select all
public class DistanceControl extends Thread
{
   //[...]
   private float lastDistance[] = new float[3];
   
   public DistanceControl(I2CPort ultrasonicSensorPortLeft,I2CPort ultrasonicSensorPortFront,I2CPort ultrasonicSensorPortRight)
   {
      //[...]
      sensorLeft = new UltrasonicSensor(ultrasonicSensorPortLeft);
      detectorLeft = new RangeFeatureDetector(sensorLeft,maxDistance,period);
      
      sensorFront = new UltrasonicSensor(ultrasonicSensorPortFront);
      detectorFront = new RangeFeatureDetector(sensorFront,maxDistance,period);
      
      sensorRight = new UltrasonicSensor(ultrasonicSensorPortRight);
      detectorRight = new RangeFeatureDetector(sensorRight,maxDistance,period);
   }
   
   public synchronized void lookAround()
   {      
      lastDistance[0] = getDistance(DistanceSeonsor.Left);
      lastDistance[1] = getDistance(DistanceSeonsor.Front);
      lastDistance[2] = getDistance(DistanceSeonsor.Right);
      
      fireNewDistanceValuesListener();
   }
   
   public synchronized float getDistance(DistanceSeonsor sensor)
   {      
      FeatureDetector detector;
      int sensorCount = 0;
            
      switch (sensor) {
      case Left:
         detector = detectorLeft;
         sensorCount = 0;
         break;
      case Right:
         detector = detectorRight;
         sensorCount = 1;
         break;
      case Front:
         detector = detectorFront;
         sensorCount = 2;
         break;
      default:
         detector = detectorFront;
         sensorCount = 2;
         break;
      }
      
      Feature result = detector.scan();
      if(result != null)
      {            
         float value=0; //the result of the whole measurement
         int count = 0; //counter for measurements
         float currentValue = 0; //current value of a measurement
         
         
         for(int i=0;i<4;i++)
         {
            currentValue = result.getRangeReading().getRange();
            //[...] some code to average and correct measurement errors -> saved in value
         }
         
         return value;
      }
   }
   
   //[...] Some other functions
}


ps: I'm quite new to leJOS...


Luke
Last edited by Luke on Thu Apr 12, 2012 9:42 am, edited 1 time in total.
Luke
New User
 
Posts: 3
Joined: Fri Apr 06, 2012 9:51 am

Re: Exception using UltrasonicSensor with Thread

Postby skoehler » Fri Apr 06, 2012 10:45 am

I believe the bug in UltrasonicSensor.getRanges was fixed on 2011-06-22 and is part of leJOS 0.9.1.
Are you still using leJOS 0.9.0?

As a workaround, use getDistances() instead. getRanges is basically calling getDistances(), but in the version you have, it doesn't check for a negative return value of getDistances which indicates an error.
skoehler
leJOS Team Member
 
Posts: 1405
Joined: Thu Oct 30, 2008 4:54 pm

Re: Exception using UltrasonicSensor with Thread

Postby Luke » Fri Apr 06, 2012 11:31 am

You are right: I'm still using leJOS 0.9.0 ...

Thank you very much for your help!

I'll try it out...
Luke
New User
 
Posts: 3
Joined: Fri Apr 06, 2012 9:51 am

Re: Exception using UltrasonicSensor with Thread

Postby skoehler » Fri Apr 06, 2012 11:58 am

Luke wrote:I'll try it out...


The workaround is a bit more complicated than I said: you actually need to write a class that implements the RangeFinder interface and then delegates the calls to getRanges() to the UltraSonic's getDistances().
skoehler
leJOS Team Member
 
Posts: 1405
Joined: Thu Oct 30, 2008 4:54 pm

Re: Exception using UltrasonicSensor with Thread

Postby Luke » Thu Apr 12, 2012 9:41 am

I decided to do the upgrade and now it looks fine. :D

Thank you very much for your help.
Luke
New User
 
Posts: 3
Joined: Fri Apr 06, 2012 9:51 am


Return to NXJ Software

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests

more stuff