Universal Robots Forum

Getting robot position using RTDE and Python 3.x

I’m trying to get the current position from the robot using data on port 30003. I find the example RTDE Python code on the UR site to be complex and difficult to understand. I found some useful and more easily understood sample code on the Zacobria website that explain how to get the data stream and decode it.

See end of post for the complete code section.

The code appears to be Python 2.7 compliant and I’m having trouble doing the data conversions in Python 3. Here is an example of a position decode section:

packet_12 = s.recv(8)
packet_12 = packet_12.encode(“hex”) #convert the data from \x hex notation to plain hex
x = str(packet_12)
x = struct.unpack(’!d’, packet_12.decode(‘hex’))[0]
print "X = ", x * 1000

Any help on making this Python 3 friendly ?

Thanks in advance for any help you can provide.

Mark

Complete Zacobria sample code:

Echo client program

import socket
import time
import struct

HOST = “192.168.0.9” # The remote host
PORT_30003 = 30003

print “Starting Program”

count = 0
home_status = 0
program_run = 0

while (True):
if program_run == 0:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
s.connect((HOST, PORT_30003))
time.sleep(1.00)
print “”
packet_1 = s.recv(4)
packet_2 = s.recv(8)
packet_3 = s.recv(48)
packet_4 = s.recv(48)
packet_5 = s.recv(48)
packet_6 = s.recv(48)
packet_7 = s.recv(48)
packet_8 = s.recv(48)
packet_9 = s.recv(48)
packet_10 = s.recv(48)
packet_11 = s.recv(48)

packet_12 = s.recv(8)
packet_12 = packet_12.encode(“hex”) #convert the data from \x hex notation to plain hex
x = str(packet_12)
x = struct.unpack(’!d’, packet_12.decode(‘hex’))[0]
print "X = ", x * 1000

packet_13 = s.recv(8)
packet_13 = packet_13.encode(“hex”) #convert the data from \x hex notation to plain hex
y = str(packet_13)
y = struct.unpack(’!d’, packet_13.decode(‘hex’))[0]
print "Y = ", y * 1000

packet_14 = s.recv(8)
packet_14 = packet_14.encode(“hex”) #convert the data from \x hex notation to plain hex
z = str(packet_14)
z = struct.unpack(’!d’, packet_14.decode(‘hex’))[0]
print "Z = ", z * 1000

packet_15 = s.recv(8)
packet_15 = packet_15.encode(“hex”) #convert the data from \x hex notation to plain hex
Rx = str(packet_15)
Rx = struct.unpack(’!d’, packet_15.decode(‘hex’))[0]
print "Rx = ", Rx

packet_16 = s.recv(8)
packet_16 = packet_16.encode(“hex”) #convert the data from \x hex notation to plain hex
Ry = str(packet_16)
Ry = struct.unpack(’!d’, packet_16.decode(‘hex’))[0]
print "Ry = ", Ry

packet_17 = s.recv(8)
packet_17 = packet_17.encode(“hex”) #convert the data from \x hex notation to plain hex
Rz = str(packet_17)
Rz = struct.unpack(’!d’, packet_17.decode(‘hex’))[0]
print "Rz = ", Rz

home_status = 1
program_run = 0
s.close()
except socket.error as socketerror:
print("Error: ", socketerror)
print “Program finish”

With a little more research and enough hammering on it, I figured it out.

Here is the code for the X value that works in Python 3.6:

packet_12=s.recv(8)
x = struct.unpack(’!d’, packet_12)[0]
x = x * 1000
print('X= ', x)

This will convert the value to a float, convert it to millimeters, and print the value. This should match what is shown for the position of TCP (Base) on the move tab of polyscope.

Modify packets 13 through 17 in the same manner to get the remaining 5 elements of a pose.

Note - Rx,Ry,Rz are angular measurements and don’t need to be multiplied by 1000 to convert the units.

packet_15=s.recv(8)
Rx = struct.unpack(’!d’, packet_15)[0]
print('Rx= ', Rx)

Mark

1 Like

Note that the example code shown doesn’t seem to use the RTDE interface at all but the “Real-time client” interface at port 30003. The RTDE interface is more general purpose but also significantly more complex to use since its not just a stream of a predefined structure.
https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/remote-control-via-tcpip-16496/

Hi Mark,

I had the same problem some month ago. I started with the example from Zacobria but it was implemented with Python 2.7 and I needed Python 3.

After some Research I found a very cool and usefull API here in the UR Forum. This API uses all communication ports, also the RTDE. It is implemented with Python 3 and the API is powerful and easy to understand. You can use it to get all informations from the robot also the actual position.

You can find it here:
https://forum.universal-robots.com/t/python-3-library-to-communicate-with-robot/461

That might help you, too.

Regards,
Flo