Use the Primary, Secondary, and Realtime Interface to send URScript commands
Universal Robots provides multiple interfaces for sending URScript commands to control the robot. These interfaces include the Primary Interface, Secondary Interface, and Realtime Interface. Each interface serves different purposes and is suitable for various levels of control and communication.
This tutorial will guide you through using these interfaces to send URScript commands to the UR controller.
Prerequisites
A Universal Robot (UR) with network connectivity
A computer with a network connection to the UR
Basic understanding of URScript programming
Understanding the Interfaces
-
Port: 30001
Frequency: 10 hz
Used for controlling the robot with high-level commands and receiving feedback
-
Port: 30002
Frequency: 10 hz
Similar to the Primary Interface but typically used for lower priority tasks and non-critical commands
-
Port: 30003
Frequency: 500 hz
Used for sendingcommands that require real-time execution and feedback, and receiving real-time data
Step-by-Step Guide
Step 1: Establish Network Connectivity
Connect the UR to the Network. Ensure the robot is connected to your local network via Ethernet, and that your computer can communicate with the robot. by pinging the robot IP address.
Step 2: Create a client to connect to the interface
Connect to the robot by creating a TCP client. You can use any programming language that supports TCP/IP sockets (e.g., Python, C++, Java). We are using Python in this tutorial. If you wish to run this example on your machine, you will need to have Python installed.
Python Client Code Example
import socket
# initialize variables
robotIP = "127.0.0.1"
PRIMARY_PORT = 30001
SECONDARY_PORT = 30002
REALTIME_PORT = 30003
# URScript command being sent to the robot
urscript_command = "set_digital_out(1, True)"
# Creates new line
new_line = "\n"
def send_urscript_command(command: str):
"""
This function takes the URScript command defined above,
connects to the robot server, and sends
the command to the specified port to be executed by the robot.
Args:
command (str): URScript command
Returns:
None
"""
try:
# Create a socket connection with the robot IP and port number defined above
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((robotIP, PRIMARY_PORT))
# Appends new line to the URScript command (the command will not execute without this)
command = command+new_line
# Send the command
s.sendall(command.encode('utf-8'))
# Close the connection
s.close()
except Exception as e:
print(f"An error occurred: {e}")
send_urscript_command(urscript_command)
Step 3: Send Commands to the robot
To send commands to the robot, set the robotIP variable to be your robot IP address. Then, set the urscript_command variable as the URScript command that you wish to send. Here are some example commands being set as the urscript_command variable:
Move the robot in joint space:
urscript_command = "movej([1.57, -1.57, 1.57, -1.57, 1.57, 0], a=1.4, v=1.05)"
Move the robot in Cartesian space:
urscript_command = "movel(p[0.2, 0.4, 0.3, 0, 3.14, 0], a=1.2, v=0.25)"
Set a digital output:
urscript_command = "set_digital_out(2, True)"
Now, run the Python script in your terminal, where
python3 <filename>.py
The robot should now execute the URScript command that was sent. Because this method sends the script directly to the robot controller, there is no need to play the program in PolyScope, and allows the user to control the robot remotely.
Conclusion
By using the Primary, Secondary, and Realtime Interfaces, you can effectively send URScript commands to control your Universal Robot. Each interface provides different capabilities and is suitable for various control tasks. Using the example Python code provided, you can easily connect to any of these interfaces and start sending commands to your UR, enabling sophisticated automation and control in your applications.