How to do CAN Loopback test on jetson agx orin

17 views (last 30 days)
mayukh
mayukh on 9 Sep 2024
Answered: R on 10 Sep 2024
Need to send a sample packet from can0 itself as loopback test or send from CAN0 to CAN1 in NVIDIA Jetson AGX Orin.

Answers (1)

R
R on 10 Sep 2024
There's a detailed example of sending and receiving data with all the setup available in MathWorks documentation: CAN Bus Communication on NVIDIA Jetson TX2 in Simulink - MATLAB & Simulink (mathworks.com)
Some important considerations:
  1. You don't have to verify GPU environment on target board but do have to configure your Jetson AGX Orin for CAN communication. There are detailed steps given in the example for this.
  2. In Configuration Parameters->Hardware Implementation->Hardware board, select 'NVIDIA Jetson'. Also, ensure that your board is reflected correctly in Target hardware resources->Board Parameters.
  3. The example has VideoRead and some MATLAB function blocks for the DL application it is targeting, feel free to ignore and delete those from your model.
  4. You need to enter correct CAN interface under Block Parameters for CAN Transmit and CAN Receive blocks.
  5. If your sample data is not Raw data but CAN Msg, you will need CAN Pack and CAN Unpack blocks from Vehicle Network Toolbox- it complicates the design a bit but you can configure them by refering to some this example: Get Started with CAN FD Communication in Simulink - MATLAB & Simulink Example (mathworks.com)
  6. You can fetch the output from Simulation after going to the Hardware tab and selecting Build, Test and Deploy.
Otherwise, if it's just a simple testing in non-interactive mode, you can achive the same through system commands. You can use functionalities of can-gm module to create a gateway between CAN interfaces. For example you can redirect all CAN interfaces sent from can0 to can1 by executing the following command in MATLAB command prompt:
system(hwObj, 'sudo cangw -A -s can0 -d can1 -e')
This command ensures a duplex communication between can0 and can1 and ensures can1 receives every frame sent from can0.
Then, you can use cansend and candump commands to send the test message and monitor the received messages respectively. I tried it using a virtual CAN, should work for physical CAN as well. Refer to the bash script below:
#!/bin/bash
# Load necessary CAN modules
sudo modprobe vcan
sudo modprobe can-gw
# Create virtual CAN devices
sudo ip link add can0 type vcan0
sudo ip link add can1 type vcan1
# Connect virtual CAN interfaces using cangw module
sudo cangw -A -s vcan0 -d vcan1 -e
# Send test message from can0 to can1
cansend vcan0 123#DEADBEEF
# Monitor received messages on can1
received_message=$(candump vcan1)
# Compare sent and received messages
echo "Sent Message: 123#DEADBEEF"
echo "Received Message: $received_message"
Save it as can_loopback_test.sh and to execute the script from MATLAB, you can use the system command:
system(hwObj, 'bash /path/to/can_loopback_test.sh')
Addendum:
Sometimes you may face issues while using CAN gateway modules when CAN interfaces are not active. You can verify the status of a CAN interface by:
ip -details -statistics link show can1
If the interface is not up, bring it up using:
sudo ip link set up can1
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!