- Code: Select all
`// get a port instance`

Port port = LocalEV3.get().getPort("S2");

// Get an instance of the Ultrasonic EV3 sensor

SensorModes sensor = new EV3UltrasonicSensor(port);

// get an instance of this sensor in measurement mode

SampleProvider distance= sensor.getModeName("Distance");

// stack a filter on the sensor that gives the running average of the last 5 samples

SampleProvider average = new MeanFilter(distance, 5);

// initialise an array of floats for fetching samples

float[] sample = new float[average.sampleSize()];

// fetch a sample

average.fetchSample(sample, 0);

I found that once I got a Infinity value, I would always get Infinity or NaN instead of a meaningful number.

I investigated the source of MeanFIlter, and found the problem might be in SumFilter where the result is calculated by substracting from or add to sum. Once the sum became Infinity or NaN, the result would be the same forever.

- Code: Select all
`// substract oldest sample from sum`

getOldest(buf, 0);

for (int i = 0; i < sampleSize; i++) {

sum[i] -= buf[i];

}

super.fetchSample(buf, 0);

// add the newest sample to sum

for (int i = 0; i < sampleSize; i++) {

sum[i] += buf[i];

sample[i + off] = sum[i];

}

I am not sure whether this problem had been solved in 0.8.1 beta. If not, hope this could be solved in the future version.