# Convert Raw Sonar to inches?

Does anyone know the algorithm to convert raw sonar data to inches. I’m using vex2 + robotc. Any help is appreciated.

By the way, I know it has a feature to do this automatically but I don’t want to do that.

just take some data.

Measure something 3 inches away and then something 4 inches away, subtract out on paper the first reading with the second. When you want to convert your readings to inches, just multiply your reading by that answer. You can do the same thing with just about any unit you want. (like field tiles or centimeters etc.)

Speed of sound is 342m/S.

The ultrasonic sensor sends a 40KHz “ping” that has to travel to the object to be detected and back again. So time to travel to an object 1cm away is.

2 x 1/( 342 x 100 ) = 58.47uS

To convert raw ultrasonic data in uS to distance.

distance (in cm) = time (in uS) / 58
distance (in inches) = time (in uS) / 148

Here is a code snippet from ConVEX where I do this.

``````
// calculate echo time
vexSonars[nextSonar].time = vexSonars[nextSonar].time_f - vexSonars[nextSonar].time_r;

// was the time too great ?
if( vexSonars[nextSonar].time > 35000 )
vexSonars[nextSonar].time = -1;

// if we have a valid time calculate real distance
if( vexSonars[nextSonar].time != -1 )
{
vexSonars[nextSonar].distance_cm = vexSonars[nextSonar].time   / 58;
vexSonars[nextSonar].distance_inch = vexSonars[nextSonar].time / 148;
}
else
{
vexSonars[nextSonar].distance_cm = -1;
vexSonars[nextSonar].distance_inch = -1;
}
``````

Obviously if the raw data is not in uS then adjust accordingly.

Values which are too small or too large (>35000) should be ignored.