Introduction
How do you receive and process coordinates (X, Y, Z) sent from an external vision server to the universal robot using a TCP/IP socket connection?
If that’s your question, follow this guide to solve this problem.
The robot (the client) must communicate with the vision server (the server) to retrieve the calculated X, Y, Z data. Because the data arrives as one long string (“X,Y,Z”), the robot must perform a parsing step to convert the text back into usable numbers for motion commands.
1. Defining Connection Parameters
Before the robot can communicate, it needs to know where and for how long to listen. This will be defined within the CONSTANTS section. In this example we use the custom constant Response_Timeout to control how long the robot waits for the data.
| Constant | Example | Function |
|---|---|---|
| SOCKET_HOST | “192.168.0.10” | The IP address of the external Vision Server (usually your laptop/PC). |
| SOCKET_PORT | 30020 | The port number the server is listening on for this specific data. |
| SOCKET_NAME | “vision_socket” | The local name the UR Controller assigns to this connection. |
| Response_Timeout | 0.5 | The timeout in seconds. The maximum time the robot waits for a response before continuing the program. (handling a timeout error) |
Initializing the Connection
Within the main() function, you must open the connection only once at the start of the program:
# Inside the main() function:
socket_open(SOCKET_HOST, SOCKET_PORT, SOCKET_NAME)
textmsg("Connection to Vision Server has been opened.")
What this does: This command initiates the TCP handshake, linking the robot client to the specified host and port. All following communication will use the assigned SOCKET_NAME.
Important: Place socket_open and socket_close outside the main program loop to avoid continuously resetting the connection, which can cause performance issues or a network overload.
2. Data Exchange: Request and Receive
Inside the main processing loop, the robot follows a send-receive sequence to synchronize with the Vision Server. The robot sends a request, and the server sends the data back.
Step 2.1: Requesting Data (socket_send_string)
The robot sends a signal to the server that it is ready to receive the coordinates.
We use “ready\n” as the signaling message.
# Request coordinates
socket_send_string("ready\n", SOCKET_NAME)
What this does: Sends the “ready” signal over the network. The \n (newline character) is vital; it acts as an end-of-message delimiter, signaling the server to process the request and send the coordinates.
Step 2.2: Receiving Data (socket_read_line)
The robot now pauses and waits for the server’s reply. We use socket_read_line because the vision server sends the X, Y, Z data as a single string.
data = socket_read_line(SOCKET_NAME, timeout=Response_Timeout)
What this does: Attempts to read data until a newline character (\n) is encountered, or until the Response_Timeout is reached.
Result: The data variable will contain the coordinate string
(For example, “-0.45,0.375,0.25”) if successful, or an empty string if it’s timed out.
3. Data Parsing: Converting String to Float (X, Y, Z)
The robot cannot use the text string “-0.45,0.375,0.25” for motion because for motion it requires numerical (float) values. We must split (parse) the string using commas and convert each section into a numerical floating-point value.
This process focuses on locating the two commas that separate the X, Y, and Z values and involves three steps: Find, Slice, Convert.
Step 3.1: Locating the Commas (str_find)
We will now locate the position of the two separating commas within the received string (coords).
if data != "":
coords = data
comma1 = str_find(coords, ",") # Find the first comma (separating X and Y)
comma2 = str_find(coords, ",", comma1 + 1) # Find the second comma (separating Y and Z)
if (comma1 > -1) and (comma2 > -1):
# ... proceed to slicing and conversion (see step 3.2)
What this does: This ensures that the received data is not empty and confirms the string contains the required two commas before proceeding.
Step 3.2: Slicing and Conversion (str_sub & to_num)
Using the found positions, we isolate the X, Y, and Z values and use the to_num function to convert them into usable numbers.
X-Coordinate:
X is at the front. We slice the string from 0 up to the position of the first comma (comma1).
x_vision = to_num(str_sub(coords, 0, comma1))
What this does: str_sub extracts the text. The to_num function then converts this text string into a numerical variable (float) that the robot can use for calculations.
Y-Coordinate:
Y is in the middle. We slice the string starting after the first comma (comma1 + 1) up to the second comma.
length_y = comma2 - (comma1 + 1)
y_vision = to_num(str_sub(coords, comma1 + 1, length_y))
What this does: Calculates the length of the Y segment, extracts the Y text, and converts it to a numerical float.
Z-Coordinate:
Z is at the end. We slice the string starting after the second comma (comma2 + 1) until the end of the string (length_z).
length_z = str_len(coords) - (comma2 + 1)
z_vision = to_num(str_sub(coords, comma2 + 1, length_z))
What this does: Calculates the length of the Z segment, extracts the Z text, and performs the final conversion to a numerical float.
Once the x_vision, y_vision, and z_vision variables are numerical, they become ready to use as the position coordinates for all robot motions.
URScript Functions
| Function | Type | Purpose |
|---|---|---|
| str_find | String | Returns the position of a specific character within a string. |
| str_sub | String | Extracts a portion of a string based on a starting position and length. |
| to_num | Conversion | Converts a string representation of a number into a usable floating-point variable (float). |