Software > Software

(1/2) > >>

Brandon121233:
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.

sdk32285:
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
.....
.....

hazzer123:
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

nanob0t:
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.

Brandon121233:
Are there any functions like somehting that would arrange everything in numerical order, and a function to find the mean, mode, and median?