- This file demonstrates how to use transmission control protocol (TCP) sockets in Universal Robot's (UR) proprietary programming language URScript.
- This example uses the official UR simulator running a UR3e on Virtual Box; an actual cobot can be used instead.
- Critical points:
- A computer is used to instantiate a server using Python that is listening on its internet protocol address at port 8080.
- A URScript file is loaded into the cobot's programming interface; this script connects with the computer then sends a request message with payload "Hello World".
- Install Python3
- Install / setup UR simulator; ensure the network setting in step5 are correct.
-
Create a Python file and copy / paste this generic server script; no modifications should be necessary.
# server.py # This program starts a server, receives request message then displays data. import socket import sys # Used to safely terminate on keyboard interrupt. def handle_keyboard_interrupt(client_socket, server_socket): print("Received KeyboardInterrupt, shutting down the server...") client_socket.close() server_socket.close() sys.exit(0) # Used to receive the data in chunks and print it out. def receive_data(client_socket, server_socket): try: while True: data = client_socket.recv(1024) if not data: break print("Received data: {}".format(data.decode())) except KeyboardInterrupt: handle_keyboard_interrupt(client_socket, server_socket) # Opens TCP socket, listens, binds then receives and displays data. # Terminates safely when the client closes connection or keyboard interrupt. def main(): HOST_IP_ADDRESS = socket.gethostbyname(socket.gethostname()) PORT = 8080 # create a TCP/IP socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: server_socket.bind((HOST_IP_ADDRESS, PORT)) except socket.error as e: print("Error binding the socket: {}".format(e)) sys.exit(1) try: server_socket.listen() except socket.error as e: print("Error listening on the socket: {}".format(e)) sys.exit(1) print("Listening on {}:{}".format(HOST_IP_ADDRESS, PORT)) print("Waiting for a connection...") try: client_socket, client_address = server_socket.accept() except socket.error as e: print("Error accepting a connection: {}".format(e)) sys.exit(1) print("Accepted connection from {}".format(client_address)) receive_data(client_socket, server_socket) # close the connection client_socket.close() server_socket.close() if __name__ == '__main__': main()
-
Start the simulator, turn the robot on then add the following URScript with your computer's IP address into its program using these actions:
NOTE: If you start the above Python script, your IP address will be displayed in console.
socket_open("<ADD_YOUR_COMPUTERS_IP_ADDRESS>", 8080) socket_send_string("Hello World")
-
Start the server, activate the cobot's simulation mode then execute the script.
current = get_actual_joint_positions()
socket_open("<ADD_YOUR_COMPUTERS_IP_ADDRESS>", 8080)
socket_send_string(current)
Listening on IP address10.0.0.25, port 8080
Waiting for a connection...
Accepted connection from ('10.0.0.133', 48407)
Received data: [-1.6007,-1.7271,-2.203,-0.808,1.5951,-0.031]
current = get_controller_temp()
socket_open("<ADD_YOUR_COMPUTERS_IP_ADDRESS>", 8080)
socket_send_string(current)
Listening on IP address10.0.0.25, port 8080
Waiting for a connection...
Accepted connection from ('10.0.0.133', 48408)
Received data: 24
- The current problem with the OpenTAP UR plugin prototype is that it cannot receive meaningful data from the cobot because the response messages from the cobot are serialized. I cannot identify any resource used for a client to deserialize the message. This is an issue because we won't be able to curate data from the cobot that might be useful test specs.
- This demonstration hints that there is an internal URScript module in between the controller and URScript sockets that is decoding the message.
- The encoding might be related to UR's real time data exchange protocol. I read on the UR forum that someone was able to decode messages using the ROS2 driver.