# Distance sensor with Robot

Hello,

I want to connect a distance sensor with my UR 3. The UR is going to move over an object with the height x and then I want to use the distance sensor to get the distance y between robot and object. After that, the robot should move exactly 30 cm above the object. So the point is to always get the exact distance between robot and the objects (with different heights).
The Sensor is using an anloug outout to give out the distance. The relationship between analog output and distance is linear. How does the Code have to look? Can I programm the conversion from analog outout to distance on polyscope?

Thank you very much in advance

Yes, you can definitely do this in Polyscope. You just simply need to know the formula for converting from the mA/volts to the linear distance. Some user guides for the sensor will give this to you, others will give you what the distance at the limits of the input is (4-20mA or 0-10V) then you can simply solve for the equation offline and then create a function in polyscope that allows you to pass in the sensor reading and get back the output.

1 Like

I think using URScript it would be more or less like this:

``````# define your own maximun and minimun values here
global MAX_DIST = 1
global MIN_DIST = 0.1
global MAX_ANALOG = 1024
global MIN_ANALOG = 0
# where the sensor is plugged
global SENSOR_ID = 0
def getDistance(analog_sensor):
m = (MAX_DIST-MIN_DIST) / (MAX_ANALOG - MIN ANALOG)
n = MIN_DIST
result = m*get_standard_analog_in(analog_sensor) + n
# result = m*get_tool_analog_in(analog_sensor) + n
return result
end
# example:
distance = getDistance(SENSOR_ID)
``````

Remember to the voltage/current range of your sensor

2 Likes