Main Content

CAN Bus Communication on NVIDIA Jetson TX2 in Simulink

This example shows how to deploy a Simulink® model that uses CAN communication for a deep learning application. The Simulink model in this example uses the CAN Transmit and CAN Receive blocks from the MATLAB® Coder™ Support Package for NVIDIA® Jetson® and NVIDIA DRIVE® Platforms to model a CAN bus system on the Jetson TX2 platform. The model uses the CAN bus to transmit the recognized traffic sign objects in a video frame from one CAN node to another CAN node.

Prerequisites

Target Board Requirements

  • NVIDIA Jetson embedded platform.

  • Ethernet crossover cable to connect the target board and host PC (if you cannot connect the target board to a local network).

  • Two WaveShare SN65HVD230 CAN transceivers.

  • A monitor connected to the display port of the target.

  • SDL (v1.2) libraries on the target.

  • GStreamer libraries on the target.

  • NVIDIA CUDA® toolkit and driver.

  • NVIDIA cuDNN library on the target.

  • Environment variables on the target for the compilers and libraries. For more information, see Install and Setup Prerequisites for NVIDIA Boards.

Connect to NVIDIA Hardware

The support package uses an SSH connection over TCP/IP to execute commands while building and running the generated CUDA code on the Jetson platforms. Connect the target platform to the same network as the host computer or use an Ethernet crossover cable to connect the board directly to the host computer. For information on how to set up and configure your board, see NVIDIA documentation.

To communicate with the NVIDIA hardware, create a live hardware connection object by using the jetson function. You must know the host name or IP address, user name, and password of the target board to create a live hardware connection object. For example, when connecting to the target board for the first time, create a live object for Jetson hardware by using the command:

hwobj = jetson('jetson-tx2-name','ubuntu','ubuntu');

During the hardware live object creation, the support package performs hardware and software checks, IO server installation, and gathers peripheral information on target. This information is displayed in the Command Window.

Verify GPU Environment on Target Board

To verify that the compilers and libraries necessary for running this example are set up correctly, use the coder.checkGpuInstall (GPU Coder) function.

envCfg = coder.gpuEnvConfig('jetson');
envCfg.DeepLibTarget = 'cudnn';
envCfg.DeepCodegen = 1;
envCfg.Quiet = 1;
envCfg.HardwareObject = hwobj;
coder.checkGpuInstall(envCfg);

Configure Jetson TX2 for CAN Communication

The Jetson TX2 platform contains two time triggered CAN controllers (TTCAN). This example uses these controllers to implement a CAN bus system containing two nodes. The transmit and receive pins of each controller is available on the 30-pin J26 header of the Jetson TX2 development board. To set up a CAN bus, the interface from the Jetson hardware requires a CAN transceiver such as the WaveShare SN65HVD230 CAN board that operates on 3.3V logic level. Make the connections between the Jetson TX2 development board and the CAN transceiver as shown in the diagram.

To update the PinMux register and IP settings of the CAN interface, launch a terminal session on the target platform.

openShell(hwobj);

Check if the CAN nodes are enabled in the kernel device tree. For more information, see CAN Setup on NVIDIA Jetson (NVIDIA). The standard output must be "okay" when executing the following command:

cat /proc/device-tree/mttcan\@c310000/status

Install the busybox tool for updating the PinMux register settings.

sudo apt-get install busybox

Run the following commands to update the PinMux register settings for all the CAN receiver and transmitter pins on the J26 header.

busybox devmem 0x0c303020 w 0x458 # For CAN Rx pin located at pin 5
busybox devmem 0x0c303018 w 0x400 # For CAN Tx pin located at pin 7
busybox devmem 0x0c303010 w 0x458 # For CAN Rx pin located at pin 15
busybox devmem 0x0c303008 w 0x400 # For CAN Tx pin located at pin 17

Load the CAN kernel drivers.

modprobe can # CAN BUS subsystem support module
modprobe can_raw # raw CAN protocol module
modprobe mttcan # real CAN interface support

Verify that the CAN interfaces (can0 and can1) are created by using the ifconfig utility.

ifconfig can0

ifconfig can1

Close the terminal session.

Simulink Model for Traffic Sign Detection and Recognition

Traffic sign detection and recognition is an important application for driver assistance systems, aiding and providing information to the driver about road signs. The Simulink model demonstrates how to communicate the recognized traffic sign in a video frame via the CAN bus from one CAN node to another.

  • Sub System 1: Uses the VideoRead system block to read a video file frame-by-frame. The MATLAB Function block detects and recognizes traffic signs in each frame. For more information on traffic sign and recognition, see Traffic Sign Detection and Recognition (GPU Coder) example. The recognized traffic sign class is transmitted along with the corresponding time stamp of the video frame on the CAN bus.

  • Sub System 2: Receive the traffic sign class on the CAN bus by using the CAN Receive block. The MATLAB Function block embeds the recognized traffic sign along with the corresponding time stamp on a blank image. The resultant image is displayed on the target by using the SDL Video Display block.

open_system('ex_trafficCAN');

The VideoRead block takes a video file as input and outputs each frame of the video along with the corresponding time-stamp. Double-click the block to set the File Name, Width and Height of each frame.

v = VideoReader('vipwarnsigns.avi');
Width = v.Width;
Height = v.Height;
SampleTime = v.Duration/v.NumFrames;

Open the block parameters window of the CAN Transmit block and set the name of the CAN interface as can0. Select Set up CAN interface and configure the IP link settings of the interface as shown.

Open the block parameters window of the CAN Receive block and set the name of the CAN interface as can1. Select Set up CAN interface and configure the IP link settings of the interface as shown.

Transfer the Video to the Target

The example uses the vipwarnsigns.avi sample video containing traffic signs. Transfer this video to the target before running the application.

putFile(hwobj,'vipwarnsigns.avi','~/');

Get the Pretrained Networks

This example uses the yolo_tsr and RecognitionNet MAT-files containing the pretrained networks. The files are approximately 103MB and 6MB in size, respectively. Download the files from the MathWorks website.

modelLoc = "gpucoder/cnn_models/traffic_sign_detection/v001";
detectionNetwork = matlab.internal.examples.downloadSupportFile(modelLoc,'yolo_tsr.mat');
recognitionNetwork = matlab.internal.examples.downloadSupportFile(modelLoc,'RecognitionNet.mat');

copyfile(detectionNetwork,pwd);
copyfile(recognitionNetwork,pwd);

Configure the Model for GPU Code generation

To generate CUDA code, set the following configuration parameters:

set_param('ex_trafficCAN','TargetLang','C++');
set_param('ex_trafficCAN','GenerateGPUCode','CUDA');
set_param('ex_trafficCAN','CodeInterfacePackaging','Nonreusable function');
set_param('ex_trafficCAN','DLTargetLibrary','cudnn');

Configure Hardware Settings

Configure the model to run on NVIDIA Jetson.

set_param('ex_trafficCAN','HardwareBoard','NVIDIA Jetson');

Open the model configuration parameters window. On the Hardware Implementation pane, set the Device Address, Username, and Password under Board Parameters group. On the CAN group, set the CAN Bus Speed (kBit/s) as 500. Click Apply and click OK.

Run External Mode

Open the Run on Hardware Board app and select Monitor and Tune to run the model in external mode.

View the Output

Once the appliction is launched in external mode, an SDL Display Window opens showing the recognized traffic sign and the timestamp of the video frame in seconds.

Close Model

Run close_system to close the model.

close_system('ex_trafficCAN',0);

Other things to try

  • This example uses physical CAN hardware to create a CAN bus. You can also run this example by using a virtual CAN bus.

  • This example uses NVIDIA Jetson TX2 board to create a CAN bus. You can also use other NVIDIA Jetson platforms to create a CAN bus.