Society of Robots - Robot Forum

Software => Software => Topic started by: Brandon121233 on April 22, 2007, 08:41:07 PM

Title: Filtering bogus readings
Post by: Brandon121233 on April 22, 2007, 08:41:07 PM
Hey everyone
If no one has noticed yet- my weakpoint in robotics is software (I only took a half year course in C++, and tried to reach myself the rest). My question regards making an Ultrasonic sensor more reliable. The sensor takes readings about 22 times a second, and since its going so fast ever so often it has a bogus reading. Right now to kinda smooth out the readings, I take and average the first 10 readings then convert that into my usable inches foramt. But sometimes for instance- it might take 9- 14inch readings and 1- 230inch reading, and that obviously throws off the average by a good amount, causing a false trigger, or no trigger when it should have.
I am wondering if there is an efficient way to analyze the first 10 values, then allow only like the ones that fall within say 80%-90% of the mean. I think I could do this on my own with a few hundred lines of repetitive- extraneous code, but does anyone know of a function that already exists that could do this, or a way to do it with as little effort as possible?
I am using the Arduino language, so if anyone could help in that format or a similar C++ format I can understand that would be terrific.
Thanks in advance- Brandon
Title: Re: Filtering bogus readings
Post by: Kohanbash on April 23, 2007, 03:31:38 AM
Hi
I am not sure what arduino is. but this should work assuming initial values are correct.

count=1
mean = (NewValue+OldValue) / count
if (NewValue >mean * .80) and (NewValue <mean * 1.20) then
      .....
      .....
     count++

else
     .....
     .....
     
Title: Re: Filtering bogus readings
Post by: hazzer123 on April 23, 2007, 09:54:13 AM
Maybe try calculating the mode or the median of the values instead?

Median - middle value when they are listed in order of size

Mode - most common value
Title: Re: Filtering bogus readings
Post by: nanob0t on April 23, 2007, 02:37:19 PM
Apply a bit more math, if you took a statistics class, you learned about outliers:

An outlier equals the means the third quartile minus the first quartile x 1.5 added to the first/third quartile. 

A quartile is the 4ths of a data set.  The median would be the middle.  For example:

1 3 5 7 9 11 13 15 17

Median = 10
1st Quartile = 6
2nd Quartile = 14
Outlier # = (14 - 6) x 1.5 = 12
Outliers = x > -6  x > 26

If you can represent that in code, then find the mean after finding outliers, you'll have a very precise code, but this will make it much larger.

Just a thought.  If you don't have any idea on how to represent this, just ask.
Title: Re: Filtering bogus readings
Post by: Brandon121233 on April 23, 2007, 04:33:15 PM
Are there any functions like somehting that would arrange everything in numerical order, and a function to find the mean, mode, and median?
Title: Re: Filtering bogus readings
Post by: hazzer123 on April 23, 2007, 04:48:14 PM
Heres a sorting function you probably could use.

http://www.devx.com/tips/Tip/12940 (http://www.devx.com/tips/Tip/12940)
Title: Re: Filtering bogus readings
Post by: Brandon121233 on April 23, 2007, 05:45:31 PM
I don't think I can import the necessary library into Arduino, if someone who has used the Arduino program could tell me how to be able to import and use the sort() function that would be spectacular.
Title: Re: Filtering bogus readings
Post by: nanob0t on April 23, 2007, 06:03:57 PM
Is there a summation function?  Represented by the weird Eish symbol.
Title: Re: Filtering bogus readings
Post by: Brandon121233 on April 23, 2007, 06:56:31 PM
I think I can use some of this code http://www.tigoe.net/pcomp/code/archives/arduino/000711.shtml (http://www.tigoe.net/pcomp/code/archives/arduino/000711.shtml) and then from there I know how to do the rest
Title: Re: Filtering bogus readings
Post by: Admin on May 13, 2007, 08:05:05 PM
(apologies for late reply, finishing up the last of the messages I missed resulting from my trip)

Firstly, you should figure out why your sonar is getting these weird values. I think your sonar is picking up echoes from a previous chirp. To correct for this, you would need to reduce your sonar frequency. 22Hz is pretty fast for sonar. Id recommend closer to 5Hz.

But if this isnt the problem, there are other methods to removing strange values than just averaging. The method is to compare your newest value to the average of previous values. If the new value is really whack, throw it away and use the average of the last few values. Dont average it in, just dump it out.