# Getting robot position using RTDE and Python 3.x

**URL:** <https://forum.universal-robots.com/t/getting-robot-position-using-rtde-and-python-3-x/3411>\
**Category:** Robot Communication\
**Created:** [January 8, 2019, 10:54pm UTC](https://forum.universal-robots.com/t/getting-robot-position-using-rtde-and-python-3-x/3411 "2019-01-08T22:54:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon79795599](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@anon79795599](https://forum.universal-robots.com/u/anon79795599)\
**Post date:** [January 8, 2019, 10:54pm UTC](https://forum.universal-robots.com/t/getting-robot-position-using-rtde-and-python-3-x/3411/1 "2019-01-08T22:54:15Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![anon79795599](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@anon79795599](https://forum.universal-robots.com/u/anon79795599)\
**Post date:** [January 9, 2019, 6:51pm UTC](https://forum.universal-robots.com/t/getting-robot-position-using-rtde-and-python-3-x/3411/2 "2019-01-09T18:51:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon10555880](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@anon10555880](https://forum.universal-robots.com/u/anon10555880)\
**Post date:** [January 14, 2019, 7:24am UTC](https://forum.universal-robots.com/t/getting-robot-position-using-rtde-and-python-3-x/3411/3 "2019-01-14T07:24:17Z")

</div>

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/](https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/remote-control-via-tcpip-16496/)

---

<div class="post-metadata">

**Author:** ![anon26177842](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon26177842](https://forum.universal-robots.com/u/anon26177842)\
**Post date:** [January 15, 2019, 5:36am UTC](https://forum.universal-robots.com/t/getting-robot-position-using-rtde-and-python-3-x/3411/4 "2019-01-15T05:36:08Z")

</div>

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](https://forum.universal-robots.com/t/python-3-library-to-communicate-with-robot/461)

That might help you, too.

Regards,  
Flo

---

<div class="post-metadata">

**Author:** ![zahra.dehnavi](https://avatars.discourse-cdn.com/v4/letter/z/858c86/32.png) [@zahra.dehnavi](https://forum.universal-robots.com/u/zahra.dehnavi)\
**Post date:** [May 19, 2023, 11:57am UTC](https://forum.universal-robots.com/t/getting-robot-position-using-rtde-and-python-3-x/3411/5 "2023-05-19T11:57:34Z")

</div>

Could you please share the link to Zacobria website regarding the decoding of data streams?
