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
