In this blog post, I will take you through how to use Python programming language to control the UR e-Series cobot. In all the below examples, we have used UR3e and the motion/path planning are based on UR3e. If you have a different version, then it’s recommended that you verify the given waypoints before executing the commands.
Host computer (PC or Laptop)
Python 3.X installed on Host computer and understanding of python programming
Ethernet cable connected from UR controller to Host computer
And of course, a Universal Robots cobot
Once python and the necessary text editor are installed in the host computer, we need to setup the network settings on the Teach pendant of UR and the computer.
First, connect the ethernet cable between the UR controller and the host computer. Now, navigate to Settings through the burger icon on the top right corner, then go to Network settings. In this case the cobot is set with the below IP address; remember to make sure the subnet mask is the same for both the cobot and the host computer!
Once the network on the Host computer and UR cobot is setup, you should see the green tick with a ”Network is connected” status as shown above.
Now, on the top right corner of the teach pendant, you should see an icon with “Local” which defines that the teach pendant has full control. Once you click on “Local”, there should be a dropdown menu. Select Remote control – now your cobot is connected to your Host computer!
Up next comes the fun part – programming on python!
To start, we need to import the necessary library packages. Socket is the first package – it is for creating the connection between the computer and the Universal robot. The second package is time, which is used for adding delays between each process.
Next, we need to define the “Host”, which in this case is the cobot. The “host” is the IP address of the cobot which we defined in the UR Network settings previously. The “Port” is the open port on the UR which listens for the commands sent from the computer. The required port for sending the information is “30002”.
HOST = “192.168.1.100”
PORT = 30002
Then, the code to define the socket and make the connections are as below:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
The “s.send” command sends the cobot script code, and can move waypoints, Digital/Analog I/Os or functions which gets executed at the robot side. Here’s an example:
The “\n” is the newline which has to be added after the script code, because the UR needs a newline after each command sent. In this case, for socket communication, the strings being sent need to be encoded in “utf8” which is character encoding. Python 3 does not support Unicode() function and all strings by default are unicode.
The socket connection has to be closed after the command is sent with s.close() command.
Now that your python code is ready, make sure you save the file with “.py” extension and note the location or path where the file has been saved. The sample codes are in a zip file which can be used as reference (contact us to obtain the .zip file).
To run the python file, we need to open the command line. You can go to Windows Start Menu and type “cmd” or “Win logo”+ “r” to open run command and type “cmd” and hit enter.
Now, you need to navigate to the path where you saved the python file. On the command line, type “cd”, then type the path or copy and paste the location. This can be done from sublime. Right-click on the code where you saved and click ”copy file path”.
It should look something like this shown below:
When you hit enter, that should execute the commands. You should be able to see the change in the I/O tabs on th