Control UR5e with Python/ Execute a .script file with Python

Hi, I have the end goal of trying to use the UR5e with my D435i Depth camera for pick and place tasks with object detection. I am trying to approach this step-by-step as I don’t have a lot of experience with the UR5e outside of using the Teach Pendant. I have just started exploring the TCP/IP Socket communication method and am able to make the cobot move to a particular position (using the moveL) command, but I’m trying to understand a few things:

  1. The coordinate system from the robot frame, world frame, camera frame, etc. What are the coordinates that I am passing into my moveL command, and also, would this take into account the joint limits of my robot?

  2. I have attached the Hand E Gripper, I asked the cobot to move to a particular pose and it was about to cause a collision with the gripper, so does the moveL command not take into account that the gripper is present? Should I be using a different movement control command so it takes into account joint limits + added geometry of the camera?

  3. I am currently embedding the URScript command inside a variable and calling it with the python script, but how do I send multiple movement commands? I tried adding a time.sleep step between each moveL command but it still causes issues with the movement - is there a better way to do this?

  4. Finally, I made a pick&place program on the TP and I got it out of the TP and onto my laptop with the SFTP method, I am now trying to invoke this program with a python script from my laptop - can this be done? If so, can someone provide some resources/code snippets that could help me?

Thanks a lot, super excited to learn more!

We’re also having the same exact problem! :smiley: Were you able to make any progress?

Hello @intelligent.autonomy, Let me explain your questions a little. I guess that @a.chamarthy do not publish the answers yet.
So:

  1. The most important coordinate system for a robot is its base. All movements that the robot makes are movements with respect to the base. Other systems, such as the camera system or its own created plane, are transformations of the base system. The whole thing is done using the pose_trans() function (check the script and supporting materials for more information about it). In your case, the pose will be determined by the camera, so with respect to the camera’s coordnate system. The key here will be to use pose_trans() to transform the camera’s coordinate system into the robot’s base coordinate system and then just go to this position.

  2. The moveL command always assumes a linear trajectory between two points - this is most important. When using moveL, it is up to us as programmers to ensure that there are no collisions or avoiding singularities

  3. The problem you are experiencing is due to UR receiving data. By sending two motion commands one after the other you will make the robot get the second command if it has not yet completed the first one. You should condition the sending of commands on the condition of reaching the position of the first one and only send the second one, or, as I will describe in the next section, send the whole file

  4. Of course, you can send the entire .script file directly to the UR control module. This is a very common method. Below I throw in a snippet of code in python to handle sending the script to the robot. Remember to enter the “remote control” mode on your robot.

Best regards!
WG

1 Like

Thank you so much for helping @wojciech.goral ! :smiley:

Right now, we are still trying to understand how to make the robot move (without the camera yet). Our Python script only has three lines for now:

import urx
rob = urx.Robot("172.22.22.2")
rob.movej([0.0,0,0,0,0,0], 0.5, 1.0)

Are you familiar with the URX library? When I run the code above (in the “remote control” mode) the robot sometimes move to the position and sometimes it doesn’t. I think I have a basic understanding of line 3: the values in the square bracket are the joint angles in radians, the 0.5 is supposed to be the acceleration, and 1.0 is supposed to be the maximum velocity.

However, I don’t understand why it sometimes work and sometimes it doesn’t. Are you able to provide any assistance on this? Thank you so much again for your time! :smiley:

@intelligent.autonomy, yes I am familiar with this library a little, and also I just tested your lines of code because I just had vscode open :smiley: Generally it should work, you have a properly written function and defined values.

Your problem looks like a problem with communication, not script.

What feedback are you getting from the debugger ? Is the connection being established correctly ?
Are you sure that you have w stable connection ?

2 Likes

Good morning @wojciech.goral,

Thank you for testing the code! :smiley: This reply is a bit long so if you want me to shorten it, I’ll be happy to! When I ran the above code, I get the following error:

runfile('C:/Users/Public/Documents/RTDE_ReadData/RTDE_Python_Client_Library-main/RTDE_Python_Client_Library-main/examples/test.py', wdir='C:/Users/Public/Documents/RTDE_ReadData/RTDE_Python_Client_Library-main/RTDE_Python_Client_Library-main/examples')
tried 11 times to find a packet in data, advertised packet size: -2, type: 3
Data length: 68
tried 11 times to find a packet in data, advertised packet size: -2, type: 3
Data length: 1092
tried 11 times to find a packet in data, advertised packet size: -2, type: 3
Data length: 1539
Traceback (most recent call last):

  File C:\ProgramData\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\public\documents\rtde_readdata\rtde_python_client_library-main\rtde_python_client_library-main\examples\test.py:10
    rob.movej([0.0,0,0,0,0,0], 0.5, 1.0)

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\urrobot.py:277 in movej
    self._wait_for_move(joints[:6], threshold=threshold, joints=True)

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\urrobot.py:217 in _wait_for_move
    raise RobotException("Robot stopped")

RobotException: Robot stopped

Then, when I searched online, I saw someone modified the code to be the following. I’m not sure what the additional two inputs are supposed to mean.

rob.movej([0.0,0,0,0,0,0], 0.5, 1.0, 0, 0)

After doing that, I sometimes get one of two different error messages.
This is the first type:

runfile('C:/Users/Public/Documents/RTDE_ReadData/RTDE_Python_Client_Library-main/RTDE_Python_Client_Library-main/examples/test.py', wdir='C:/Users/Public/Documents/RTDE_ReadData/RTDE_Python_Client_Library-main/RTDE_Python_Client_Library-main/examples')
Exception in thread Thread-26:
Traceback (most recent call last):
  File "C:\ProgramData\anaconda3\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\anaconda3\Lib\site-packages\urx\ursecmon.py", line 286, in run
Traceback (most recent call last):

  File C:\ProgramData\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\public\documents\rtde_readdata\rtde_python_client_library-main\rtde_python_client_library-main\examples\test.py:6
    rob = urx.Robot("172.22.22.2")

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\robot.py:27 in __init__
    URRobot.__init__(self, host, use_rt)

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\urrobot.py:38 in __init__
    self.secmon = ursecmon.SecondaryMonitor(self.host)  # data from robot at 10Hz

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\ursecmon.py:252 in __init__
    self.wait()  # make sure we got some data before someone calls us

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\ursecmon.py:344 in wait
    raise TimeoutException("Did not receive a valid data packet from robot in {}".format(timeout))

TimeoutException: Did not receive a valid data packet from robot in 0.5

    data = self._get_data()
           ^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\urx\ursecmon.py", line 333, in _get_data
    tmp = self._s_secondary.recv(1024)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: timed out

and this is the second type:

runfile('C:/Users/Public/Documents/RTDE_ReadData/RTDE_Python_Client_Library-main/RTDE_Python_Client_Library-main/examples/test.py', wdir='C:/Users/Public/Documents/RTDE_ReadData/RTDE_Python_Client_Library-main/RTDE_Python_Client_Library-main/examples')
Exception in thread Thread-31:
Traceback (most recent call last):
  File "C:\ProgramData\anaconda3\Lib\threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\anaconda3\Lib\site-packages\urx\ursecmon.py", line 286, in run
    data = self._get_data()
           ^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\site-packages\urx\ursecmon.py", line 333, in _get_data
    tmp = self._s_secondary.recv(1024)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: timed out
Traceback (most recent call last):

  File C:\ProgramData\anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\public\documents\rtde_readdata\rtde_python_client_library-main\rtde_python_client_library-main\examples\test.py:6
    rob = urx.Robot("172.22.22.2")

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\robot.py:27 in __init__
    URRobot.__init__(self, host, use_rt)

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\urrobot.py:38 in __init__
    self.secmon = ursecmon.SecondaryMonitor(self.host)  # data from robot at 10Hz

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\ursecmon.py:252 in __init__
    self.wait()  # make sure we got some data before someone calls us

  File C:\ProgramData\anaconda3\Lib\site-packages\urx\ursecmon.py:344 in wait
    raise TimeoutException("Did not receive a valid data packet from robot in {}".format(timeout))

TimeoutException: Did not receive a valid data packet from robot in 0.5

Does this mean the connection is bad? I am using an Ethernet cable to connect from my Windows computer directly to the control box. On the Windows computer, I am using a USB-to-Ethernet adapter because the ethernet port is taken up by another ethernet connection. Thank you so much again for your time!

Hi @intelligent.autonomy,

So, it definetely looks like problem with communication. First time you received information that robot was stopped, so propably wasn’t in remote control mode or “play” wasn’t click properly.

Neverthless, the second command you mentiones is still move command but with more arguments which are not neccesary - see script manual for movej() command :
image

Second and third feedback from debugger is more meaningfull - your transfered packages do not transfer correctly, so you have to double check the Ip address, connection (try to use ping -t) command in cmd to see the connection continously, in an extreme case you can check the firewall. It will be good option to unplug other connection and try only with this one.

I don’t assume it’s a problem with the urx library, but just to be sure, you can go a lower and write the socket communication yourself like in my picture, and after opening the connection, send the same movej command:
#open socket like on the picture
message = “movej([0,0,0,0,0,0], 0.5, 0.5]\n”
communication.send(str.encode(message))
#close connection like on the picture

I think you’re close to find the cause. :wink:

BR,
Wojciech

Hello, i have a problem about import urx in vscode in ubuntu20.04, i have pip install urx with Python version–3.9,but i meet a problem. When i import urx, it reminds me no module urx.when i pip list, urx exist!and when i export the urx road. It gives me new error about numpy!wishes for your help!

I don’t know exactly about the steps how to import urx in vscode.maybe there’s some details that need to config!thanks!