Send and Receive Data over UDP on NVIDIA Jetson Platforms
This example shows how to use MATLAB® Coder™ Support Package for NVIDIA® Jetson® and NVIDIA DRIVE® to send and receive UDP data over the network on a Jetson board.
Using this example, you can create and deploy a Simulink® model that can:
Receive data from the host computer via UDP.
Send data to the host computer via UDP.
Prerequisites
NVIDIA Jetson Nano embedded platform.
Ethernet crossover cable to connect the target board and host PC (if you cannot connect the target board to a local network).
Tools, libraries, and environment variables on the target board for the compilers and libraries. For more information, see Install and Setup Prerequisites for NVIDIA Boards.
Simulink Model for UDP Communication
This example uses a preconfigured jetsoncpu_udp_communication
model that uses the UDP Receive
and UDP Send
blocks to exchange the data. In this model, the Jetson board receives the UDP data from the host computer, manipulates the data, and sends it back to the host computer.
open_system('jetsoncpu_udp_communication')
Configure Board Parameters of the Model
To set up a live connection to the Jetson board, specify the device address, username, and password of the Jetson Nano board.
Open the Configuration Parameters dialog box and navigate to the Hardware Implementation pane.
Select your target hardware from the Hardware board drop-down list.
Set the Hardware board to NVIDIA Jetson
In the Target Hardware Resources section, open the Board Parameters pane and enter the device address, username, and password for the Jetson board.
Click Apply and then OK to close the dialog box.
Configure UDP Blocks
In the Simulink model, double-click the following blocks and verify that the specified parameter values are the same as shown below:
UDP Receive block in the Jetson UDP Communication model
On the target UDP Receive block, the
Local IP port
has to match thejetson_port
parameter in thejetsoncpu_udp.m
script.
UDP Send block in the Enabled Subsystem
On the target UDP Send block, the
Remote IP port
has to match thehost_port
parameter in thejetsoncpu_udp.m
script.In the Remote IP address parameter, specify the host computer IP address.
To find the IP address on a Windows® machine,
Open the command prompt.
Type ipconfig in the command prompt and press enter.
To find the IP address on a Linux® machine,
Open a terminal.
Type ip add in the terminal and press enter.
Configure Host Script
The example uses the jetsoncpu_udp
script to enable the host computer send data to the Jetson board,read the data coming from the board, and display the UDP data.
type jetsoncpu_udp.m
function jetsoncpu_udp() % Copyright 2021 The MathWorks, Inc. % Enter the IP address of the NVIDIA board. jetson_ipaddress = 'xxx.xxx.xxx.xxx'; % Enter the Port number on which the UDP block on the NVIDIA board is % listening jetson_port = 10000; % Enter the Port number to which the UDP block on the NVIDIA board is % sending data to. host_port = 11000; % Create a udpport object to send and receive data from the NVIDIA board. udpObj = udpport('LocalPort', host_port); % The Jetson board needs the data in Little Endian format. udpObj.ByteOrder = 'littleEndian'; % sendVal is the number to be sent to the target. sendVal = pi; disp(['Send number: ' num2str(sendVal)]); write(udpObj,sendVal,'double',jetson_ipaddress,jetson_port); % recVal is the number received from the target. recVal = read(udpObj, 1 ,'double'); disp(['Received number: ' num2str(recVal)]); end
In the script, specify the IP Address of the Jetson board in the jetson_ipaddress
parameter.
Monitor and Tune Model
When you monitor and tune the Jetson UDP Communication model, the host computer communicates with the target on which the generated executable runs.
To monitor and tune the model, open the Hardware tab and click Monitor & Tune. You can observe from the Diagnostic Viewer that the code is generated for the model and the host connects to the target after loading the generated executable.
Run the jetsoncpu_udp.m
script.
The script sends the value to the Jetson board.
The Jetson board multiplies it with the Gain block value specified in the model and sends it to the host.
The script receives this value and displays it in the Command Window.
Change the value in the Gain block and rerun the script. Observe the new values in the command window.
To stop external simulation, go to the Hardware tab and click the Stop button to terminate external mode simulation.
Other things to try
Replace the Gain block with other computation blocks.