Society of Robots - Robot Forum

Software => Software => Topic started by: Yr0gErG on September 30, 2010, 03:18:18 AM

Title: Distance measurement: p18f4520 and PING))) Ultrasonic Sensor
Post by: Yr0gErG on September 30, 2010, 03:18:18 AM
Hi, I would like to make a C code for distance measurement using the C18 Compiler for MPLab. what would be the steps needed? would the following pseudocode do?

Make the I/O pin an Output
Bring LOW the  pin that the PING rangefinder is connected
Bring the  pin HIGH for 5 microseconds
Bring the  pin LOW
Make the I/O pin an Input
Wait until the pin goes HIGH
Use a timer to see how much longer it takes for the pin to become LOW
The time it took to become low is now our raw distance ( in microseconds)
Divide Raw Distance by two since it includes the time for a return trip of the sonar
Raw distance * 2257 is our distance in cm

Please advise!
Raw distance * 889 is our distance in inches
Title: Re: Distance measurement: p18f4520 and PING))) Ultrasonic Sensor
Post by: kidwidget on October 01, 2010, 05:15:39 PM
That's the basic algorithm. I didn't check your math for the conversion of cm and inches though.

RockON!
Title: Re: Distance measurement: p18f4520 and PING))) Ultrasonic Sensor
Post by: Yr0gErG on October 01, 2010, 11:02:19 PM
I'm trying to make it print the distance values on the debugger, any idea how to do that? im using a C18 compiler, i'm trying to stick with compiler for consistency purposes
Title: Re: Distance measurement: p18f4520 and PING))) Ultrasonic Sensor
Post by: Webbot on October 02, 2010, 05:44:32 AM
Cant comment on the compiler but the maths is wrong....

 Sound travels at 1130ft/sec or 13,560in/sec or 73.7463uS per inch

 Since we are detecting the echo then the sound needs to travel out and back so it will take twice  as long ie 147.4926254uS per inch or 58.06796274uS per cm.

 If you have measured the duration in microseconds (us) then:
 cm = us / 58.06796274
 inches = us / 147.4926254