Skip to content

Instantly share code, notes, and snippets.

@Shawn-Armstrong
Last active November 20, 2023 16:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shawn-Armstrong/001c14d8496f1362dace27bd6e1535e1 to your computer and use it in GitHub Desktop.
Save Shawn-Armstrong/001c14d8496f1362dace27bd6e1535e1 to your computer and use it in GitHub Desktop.

#URScript_socket_example.md

Overview

  • 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".

Requirements

  1. Install Python3
  2. Install / setup UR simulator; ensure the network setting in step5 are correct.

Demonstration

  1. 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()
  2. Start the simulator, turn the robot on then add the following URScript with your computer's IP address into its program using these actions:

    URScript

    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")
    

    Actions

    urscript_socket1

  3. Start the server, activate the cobot's simulation mode then execute the script.

    urscript_socket2

Additional Script Examples

Example1

URScript
current = get_actual_joint_positions()
socket_open("<ADD_YOUR_COMPUTERS_IP_ADDRESS>", 8080)
socket_send_string(current)
Output
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]

Example2

URScript
current = get_controller_temp()
socket_open("<ADD_YOUR_COMPUTERS_IP_ADDRESS>", 8080)
socket_send_string(current)
Output
Listening on IP address10.0.0.25, port 8080
Waiting for a connection...
Accepted connection from ('10.0.0.133', 48408)
Received data: 24

Final Remarks

  • 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment