Communicating with the Jetson TX2 Module - Carleton-SRCL/SPOT GitHub Wiki

Basic communication with the Jetson TX2 module (located by default at the top deck of the RED platform) is included in SPOT 4.0.0-RC.2 and newer versions of the software. Communication is handled with two key pieces of code: A UDP send block in the template Simulink diagram, and a simple Python script which receives the UDP data.

Simulink

On the Simulink side, the UDP send block is located in the resources section:

Inside the subsystem labelled 'Send Data to TX2`, users will find an If-Action block that checks if a simulation is running or not. Inside the action subsystem which is only executed in experiment, users will see the following:

This simply concatenates the time value and the 19 values (1x time, then 3x positions and 3x velocities for 3x platforms), and then sends this data via UDP to:

Jetson TX2

On the TX2 board, a simple python script is used. The Python code is as follows:

import socket
import struct
import time

IP_ADDRESS = '' # Set the IP to allow connections from any address
PORT = 46875
NUM_DOUBLES = 19

# Create a UDP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
server_address = (IP_ADDRESS, PORT)
print('Starting up on {} port {}'.format(server_address[0], server_address[1]))
server_socket.bind(server_address)

# Set the socket to non-blocking
server_socket.setblocking(0)

data = b''

try:
    while True:
        try:
            # Receive the data
            more_data, client_address = server_socket.recvfrom(NUM_DOUBLES * 8 - len(data))
            if more_data:
                data += more_data
            else:
                break

            # Unpack and display the data if enough bytes have been received
            if len(data) == NUM_DOUBLES * 8:
                doubles = struct.unpack('d' * NUM_DOUBLES, data)
                print('Received doubles: {}'.format(doubles))
                data = b''  # Clear the data buffer for the next set of doubles

        except BlockingIOError:
            # No data received, continue waiting
            time.sleep(0.1)  # Optional: add a small delay to reduce CPU usage

except KeyboardInterrupt:
    print('\nExiting...')

# Close the socket
server_socket.close()

To run this code, simply SSH into the TX2 via Ubuntu on Windows:

Once you are connected to the Jetson, you can navigate to your directory of choice. If you are starting from the sample code above, then you can start a new python script by typing nano receive_data.py. This will open an instance of nano, which is the default text editor in Ubuntu. You can then directly copy the code above into the python script (this even works when you are SSH'd into the Jetson!). You should see something similar to this:

When you are ready to save and quit, press Ctrl-O then 'Enter' to save, and then Ctrl-X to quit. To run the code simply type python3 receive-data.py and the code will execute. By default this code simply print to the terminal any data it receives. Users will likely want to change this to suit their needs.