| 
								 | 
							
								
  | 
							
								
  | 
						
								
  | 
						
| 
			
			
			
			 Slackerous Maximus 
			
			
		
			
				
			
			
			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 06:15 PM..  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 Registered 
			
			
		
			
				
			
			
			Join Date: Dec 2001 
				Location: Cambridge, MA 
				
				
					Posts: 44,479
				 
                
				
				
				
				 | 
	
	
	
		
		
		
		
		
		 
			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  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 Slackerous Maximus 
			
			
		
			
				
			
			
			Join Date: Apr 2005 
				Location: Columbus, OH 
				
				
					Posts: 18,206
				 
                
				
				
				
				 | 
	
	
	
		
		
		
		
		
		 Quote: 
	
 
				__________________ 
		
		
		
		
		
	
	2022 Royal Enfield Interceptor. 2012 Harley Davidson Road King 2014 Triumph Bonneville T100. 2014 Cayman S, PDK. Mercedes E350 family truckster.  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 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  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 Max Sluiter 
			
			
		
			
				
			
			
								
		
	 | 
	
	
	
		
		
		
		
		
		 
			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  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 Targa, Panamera Turbo 
			
			
		
			
				
			
			
			Join Date: Aug 2004 
				Location: Houston TX 
				
				
					Posts: 22,366
				 
                
				
				
				
				 | 
	
	
	
		
		
		
		
		
		
		
	
		
	
			
			
		
		
		
		
		
			 
				__________________ 
		
		
		
		
		
	
	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  | 
||
| 		
			
			 | 
	
	
  | 
 
 | 
| 
			
			
			
			 Registered 
			
			
		
			
				
			
			
			Join Date: Mar 2003 
				
				
				
					Posts: 10,365
				 
                
				
				
				
				 | 
	
	
	
		
		
		
		
		
		 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? 
				__________________ 
		
		
		
		
		
	
	“IN MY EXPERIENCE, SUSAN, WITHIN THEIR HEADS TOO MANY HUMANS SPEND A LOT OF TIME IN THE MIDDLE OF WARS THAT HAPPENED CENTURIES AGO.”  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 Registered 
			
			
		
			
				
			
			
								
		
	 | 
	
	
	
		
		
		
		
		
		 
			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  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 Registered 
			
			
		
			
				
			
			
			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)  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 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  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 Platinum Member 
			
			
		
			
				
			
			
			Join Date: Jul 2001 
				Location: Leave the gun. Take the cannoli. 
				
				
					Posts: 21,131
				 
                
				
				
				
				 | 
	
	
	
		
		
		
		
		
		 
			
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.  | 
||
| 		
			
			 | 
	
	
  | 
| 
			
			
			
			 Slackerous Maximus 
			
			
		
			
				
			
			
			Join Date: Apr 2005 
				Location: Columbus, OH 
				
				
					Posts: 18,206
				 
                
				
				
				
				 | 
	
	
	
		
		
		
		
		
		 
			Thank you! 
		
	
		
	
			
			
				
					Quote: 
	
  | 
||
| 		
			
			 | 
	
	
  | 
 
 | 
![]()  | 
	
	
| Thread Tools | |
| Rate This Thread | |
| 
		 |