In some cases, it can be beneficial to be able to control a UR robot through python, if for instance you wish to control the robot based on a web interface or calculations outside of the polyscope interface.
If you want to control your robot through a python script, this can be done via the RTDE (Real Time Data Exchange) port of the robot controller. RTDE is running in the background of the robot and is not accessible through the polyscope interface. Through RTDE you are able to get information about the robot, including current TCP pose, input and output statuses etc. You are also able to control the robot through move or servo commands and control the IO of the robot controller and the end-effector.
Installation
To communicate with RTDE from python you need to install the ur_rtde library from SDU (University of Southern Denmark). To install the library, head to your python environment in the terminal and activate it. If you are using anaconda you can use this command:
conda activate <your_environment_name>
After activating your environment, run the following command to install the library to your environment:
pip install ur_rtde
The official installation guide can be found here: Installation — ur_rtde 1.6.0 documentation
To use the RTDE library in python you need to import it to your python script. This is done like this:
import rtde_control # For controlling the robot
import rtde_receive # For receiving data from the robot
import rtde_io # For robot IO
For easy use of the RTDE libraries you can definitions like this:
rtde_ctrl = rtde_control.RTDEControlInterface(robot_ip)
rtde_rec = rtde_receive.RTDEReceiveInterface(robot_ip)
rtde_inout = rtde_io.RTDEIOInterface(robot_ip)
robot_ip = “192.168.0.xxx”
To get the IP of the UR robot, click on the UR logo in the top left corner of the polyscope interface.
Use
The RTDE libraries approximately the same syntax as URscripts.
Here is a few examples of how to use the RTDE libraries:
Set a standard digital output
rtde_inout.setStandardDigitalOut(7, False)
This method can also be used to set safety, tool or configurable outputs.
Make a move
rtde_ctrl.moveJ([x, y, z, rx, ry, rz], speed, acceleration, blend radius, time)
Move commands also work for other types of movement like moveL
and moveP
.
Notice that the x, y and z coordinates
should be entered as [m] in RTDE commands, while the polyscope interface displays positions as [mm]. rx, ry and rz
should be entered as [rad], the same as in the polyscope interface.
import math
joint_rotation = [
math.radians(base rotation),
math.radians(shoulder rotation),
math.radians(elbow rotation),
math.radians(wrist 1 rotation),
math.radians(wrist 2 rotation),
math.radians(wrist 3 rotation)
]
rtde_ctrl.moveJ(joint_rotation)
The rotation values should be entered as [°] like displayed in the polyscope interface.
Get actual TCP pose
rtde_rec.getActualTCPPose()
Stop script
rtde_ctrl.stopScript()
This command makes sure the RTDE communication is stopped correctly, and not leaving the RTDE occupied.
Be aware that the RTDE libraries are designed for C++ but most functions have been translated to python.
It is also possible to compile and edit the C++ libraries yourself to add functionality. For information about that, see the examples page which also includes more examples of use in C++ and python: Examples — ur_rtde 1.6.0 documentation