![]() |
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; } |
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! |
Quote:
|
What language, and what is the hardware you are programming?
|
If you are looking to calm noise you want to integrate the signal and divide by the sample time.
|
Quote:
|
Quote:
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? |
you could always add a low pass filter on the analog input ..
|
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. |
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 |
Quote:
Arduino Playground - FunctionLibrary Arduino - Smoothing Instead of the delay, loop for multiple inputs, filter or average, then output to servo once every 'X' loops. |
Thank you!
Quote:
|
| 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