Pelican Parts
Parts Catalog Accessories Catalog How To Articles Tech Forums
Call Pelican Parts at 888-280-7799
Shopping Cart Cart | Project List | Order Status | Help



Go Back   Pelican Parts Forums > Miscellaneous and Off Topic Forums > Off Topic Discussions


Reply
 
LinkBack Thread Tools Rate Thread
Author
Thread Post New Thread    Reply
Slackerous Maximus
 
HardDrive's Avatar
 
Join Date: Apr 2005
Location: Columbus, OH
Posts: 18,206
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
#include

// 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;
}

__________________
2022 Royal Enfield Interceptor.
2012 Harley Davidson Road King
2014 Triumph Bonneville T100.
2014 Cayman S, PDK.
Mercedes E350 family truckster.

Last edited by HardDrive; 10-12-2013 at 05:15 PM..
Old 10-12-2013, 04:32 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #1 (permalink)
Registered
 
Shaun @ Tru6's Avatar
 
Join Date: Dec 2001
Location: Cambridge, MA
Posts: 44,468
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!
__________________
Tru6 Restoration & Design
Old 10-12-2013, 05:03 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #2 (permalink)
Slackerous Maximus
 
HardDrive's Avatar
 
Join Date: Apr 2005
Location: Columbus, OH
Posts: 18,206
Quote:
Originally Posted by Shaun 84 Targa View Post
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!!!!
__________________
2022 Royal Enfield Interceptor.
2012 Harley Davidson Road King
2014 Triumph Bonneville T100.
2014 Cayman S, PDK.
Mercedes E350 family truckster.
Old 10-12-2013, 05:31 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #3 (permalink)
Un-Registered User
 
Join Date: Apr 2007
Location: Upstate New York
Posts: 902
What language, and what is the hardware you are programming?
__________________
Don
1988 Targa
Old 10-12-2013, 06:08 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #4 (permalink)
Max Sluiter
 
Flieger's Avatar
 
Join Date: Mar 2006
Location: So Cal
Posts: 19,644
Garage
If you are looking to calm noise you want to integrate the signal and divide by the sample time.
__________________
1971 911S, 2.7RS spec MFI engine, suspension mods, lightened
Suspension by Rebel Racing, Serviced by TLG Auto, Brakes by PMB Performance
Old 10-12-2013, 06:20 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #5 (permalink)
Targa, Panamera Turbo
 
M.D. Holloway's Avatar
 
Join Date: Aug 2004
Location: Houston TX
Posts: 22,366
Quote:
Originally Posted by HardDrive View Post
Pinot Gris. Duh!!!!
negatory Ghost rider, Moscato!
__________________
Michael D. Holloway
https://simple.m.wikipedia.org/wiki/Michael_D._Holloway
https://5thorderindustry.com/
https://www.amazon.com/s?k=michael+d+holloway&crid=3AWD8RUVY3E2F&sprefix= michael+d+holloway%2Caps%2C136&ref=nb_sb_noss_1
Old 10-12-2013, 06:56 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #6 (permalink)
 
Registered
 
id10t's Avatar
 
Join Date: Mar 2003
Posts: 10,364
Quote:
Originally Posted by Red88Carrera View Post
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?
__________________
“IN MY EXPERIENCE, SUSAN, WITHIN THEIR HEADS TOO MANY HUMANS SPEND A LOT OF TIME IN THE MIDDLE OF WARS THAT HAPPENED CENTURIES AGO.”
Old 10-12-2013, 06:57 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #7 (permalink)
Registered
 
9dreizig's Avatar
 
Join Date: Jul 2005
Location: Reno, NV
Posts: 5,941
Send a message via AIM to 9dreizig
you could always add a low pass filter on the analog input ..
__________________
"Todd"
98 Tahoe ,2007 Saturn Vue
86 930 black and stock, 80 930 blue tracdog
91 Spec Miata (yeah I race a chick car)
"life"ll kill ya" Warren Zevon
Old 10-12-2013, 07:59 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #8 (permalink)
Registered
 
Rob Channell's Avatar
 
Join Date: Feb 2004
Location: Decatur/Madison, Alabama
Posts: 1,192
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.
__________________
Rob Channell
One Way Motorsports
1979 911SC mostly stock
1972 911T Targa now with a good 2.7
1990 Miata (cheap 'n easy)
1993 C1500 Silverado (parts getter)
Old 10-12-2013, 08:02 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #9 (permalink)
Banned
 
Join Date: Jan 2005
Location: cutler bay
Posts: 15,136
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
Old 10-12-2013, 08:51 PM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #10 (permalink)
Platinum Member
 
dad911's Avatar
 
Join Date: Jul 2001
Location: Leave the gun. Take the cannoli.
Posts: 21,126
Quote:
Originally Posted by 9dreizig View Post
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.
Old 10-13-2013, 06:13 AM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #11 (permalink)
Slackerous Maximus
 
HardDrive's Avatar
 
Join Date: Apr 2005
Location: Columbus, OH
Posts: 18,206
Thank you!

Quote:

Quote de 9dreizig



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.

Old 10-13-2013, 08:09 AM
  Pelican Parts Catalog | Tech Articles | Promos & Specials    Reply With Quote #12 (permalink)
 
Reply


 


All times are GMT -8. The time now is 01:59 PM.


 
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 -    DMCA Registered Agent Contact Page
 

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