Pelican Parts Forums

Pelican Parts Forums (http://forums.pelicanparts.com/)
-   Off Topic Discussions (http://forums.pelicanparts.com/off-topic-discussions/)
-   -   Need some math help. 'Calming' results. (http://forums.pelicanparts.com/off-topic-discussions/776171-need-some-math-help-calming-results.html)

HardDrive 10-12-2013 04:32 PM

Need some math help. 'Calming' results.
 
Lets suppose I am getting feedback from a sensor. In this case, a sonic range finder. The results come back on a scale of 2-100cm. That output is then converted into an input value for a servo motor, and the servo has a needle attached, and sits in the middle of dial. A very simple range finder.

The problem I am having is that the range finder kicks out spurious results. Its right 95% of the time, but unless its sitting in a perfectly square environment, the less than perfect echos it gets back cause incorrect results. How could I calm the data so that the needle isn't hunting wildly?

Code is below. The servo operates on a scale of 100-600. The output from the range sensor is 2-100cm. The (rather crude) conversion is denoted by *************** in the code below.

Can anyone recommend a method of calming the results so that the spurious results don't cause the needle to hunt wildly?




#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN 100 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096)



// Pin number constants
const int triggerPin = 22;
const int echoPin = 31;
long int distanceServo = 100;
int servonum = 14;

void setup() {
// initialize serial communication:
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, feet, inches, cm;

// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(triggerPin, LOW);

// The echo pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
distanceServo = ((cm * 4) + 100);***************************************
updateServo(distanceServo);
delay(500);
}

void updateServo(int pulselen)
{

Serial.print("updateservo");
Serial.println();
pwm.setPWM(14, 0, pulselen);
Serial.print(pulselen);
Serial.println();
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

Shaun @ Tru6 10-12-2013 05:03 PM

Fish math problems are tough.

if a fish has 3254 scales, each having an average 7.6mm2 surface area, and is swimming in a school of 348 other fish against a 4 knot current, would it be better grilled with a peach-habanero butter or stuffed with mango-pork-fish sauce mousse with a little fresh cilantro sprinkled on top?

White burgundy or a nice Riesling?

Fish math is tough!

HardDrive 10-12-2013 05:31 PM

Quote:

Originally Posted by Shaun 84 Targa (Post 7702177)
Fish math problems are tough.

if a fish has 3254 scales, each having an average 7.6mm2 surface area, and is swimming in a school of 348 other fish against a 4 knot current, would it be better grilled with a peach-habanero butter or stuffed with mango-pork-fish sauce mousse with a little fresh cilantro sprinkled on top?

White burgundy or a nice Riesling?

Fish math is tough!

Pinot Gris. Duh!!!!

Red88Carrera 10-12-2013 06:08 PM

What language, and what is the hardware you are programming?

Flieger 10-12-2013 06:20 PM

If you are looking to calm noise you want to integrate the signal and divide by the sample time.

M.D. Holloway 10-12-2013 06:56 PM

Quote:

Originally Posted by HardDrive (Post 7702209)
Pinot Gris. Duh!!!!

negatory Ghost rider, Moscato!

id10t 10-12-2013 06:57 PM

Quote:

Originally Posted by Red88Carrera (Post 7702264)
What language, and what is the hardware you are programming?

Looks C++/C#-ish


If the sensor isn't exactly square and it causes the error then I'd say the issue is in hardware and alignment. Possibly differnet receiver set up? Would it be possible to have it in a fixture that guarantees alignment? Or some sort of calibration setup?

If it has to be fixed in code, are all results spurious when it isn't perfectly aligned? Would it be possible to run 10 pings and then get rid of results on extreme end, average remaining results, and be more consistent?

9dreizig 10-12-2013 07:59 PM

you could always add a low pass filter on the analog input ..

Rob Channell 10-12-2013 08:02 PM

Simplest method to me would be to take multiple samples and average them. You could also toss out any samples that varied widely from the average. Looks like id10T already made the same suggestion.

Since it is sonic you could also think about directional gain from the microphone. Think of the huge parabolic microphones used at sports events as an example.

Looks like Arduino code. It is fun stuff. Some pictures and description of your project would be nice to see sometime.

nota 10-12-2013 08:51 PM

real world survey instruments avg distance shots
best accuracy is throw out the high and low and average the middle numbers
then do two more sets and avg that result
newer machines do it internally and just report the final

dad911 10-13-2013 06:13 AM

Quote:

Originally Posted by 9dreizig (Post 7702384)
you could always add a low pass filter on the analog input ..

Filter in software:
Arduino Playground - FunctionLibrary
Arduino - Smoothing

Instead of the delay, loop for multiple inputs, filter or average, then output to servo once every 'X' loops.

HardDrive 10-13-2013 08:09 AM

Thank you!

Quote:

<div class="pre-quote">
Quote de <strong>9dreizig</strong>
</div>

<div class="post-quote">
<div style="font-style:italic">you could always add a low pass filter on the analog input ..</div>
</div>Filter in software: <br>
<a href="http://playground.arduino.cc/Main/FunctionLibrary" target="_blank">Arduino Playground - FunctionLibrary</a><br>
<a href="http://arduino.cc/en/Tutorial/Smoothing" target="_blank">Arduino - Smoothing</a><br>
<br>
Instead of the delay, loop for multiple inputs, filter or average, then output to servo once every 'X' loops.


All times are GMT -8. The time now is 05:16 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0
Copyright 2025 Pelican Parts, LLC - Posts may be archived for display on the Pelican Parts Website


DTO Garage Plus vBulletin Plugins by Drive Thru Online, Inc.