MATLAB: Do I lose CAN messages when I use CAN Transmit and CAN Receive blocks in Vehicle Network Toolbox

candatalossmessagesperiodicallyreceivesimulinktransmitvehicle network toolbox

Why do I lose data samples when I use CAN Transmit and CAN Receive blocks in Simulink ?
<<www-mathworks-com-matlabcentral-answers-uploaded_files-209668-f8c9af65e39e37bd6fdf9d95363d6e3a.png>>

Best Answer

1. A common reason for apparent loss of CAN messages is the “Transmit messages periodically’ option for the CAN Transmit block
For CAN Transmit block, the period in “Transmit messages periodically’ is referenced to wall-clock time and not to simulation time. Some of the messages present at the block input at each time step can end up NOT being transmitted in this mode. Only the CAN message present at the input of the "CAN Transmit" when the specified wall-clock time period occurs block is transmitted. The periodic transmit is a non-buffered operation, and CAN messages present at the input of the CAN Transmit block at simulation time steps that do not occur at the wall-clock time periodic transmission interval are not actually transmitted.
Please find the following document link, which illustrates periodic CAN communication:
If you intend to transmit a CAN message at each timestep, disable this setting to ensure not to lose any data.
2. Another common reason **for apparent loss of CAN messages is the way CAN Receive and CAN Unpack are used together
If a CAN Unpack block is directly connected to the output port of a CAN Receive block and 'Received messages at each timestep is set to "all" in CAN Receive Block the CAN Unpack block output will NOT unpack every CAN message received -- only the last CAN message of the multiple messages received at that timestep will be unpacked.
<<www-mathworks-com-matlabcentral-answers-uploaded_files-209662-d52ec2de70dd01cfa422a766629a9fab.png>>
If you need to process every single CAN message received you can use the following two approaches:
One option is to place the CAN Unpack block in a function-triggered subsystem triggered by the CAN Receive block.
In the CAN Unpack subsystem block you should have a CAN Unpack block for each message ID of interest. This approach can be seen in the following example https://www.mathworks.com/help/releases/R2018b/vnt/examples/periodic-can-communication.html (note though that this example use the transmit messages periodically option that can lead to apparent message loss, as described earlier).
*
<<www-mathworks-com-matlabcentral-answers-uploaded_files-209663-4c52b81cd8f16d09c447cc9e206e5f0d.png>>
<<www-mathworks-com-matlabcentral-answers-uploaded_files-209664-acfe2078f8fac6c52e0b93e73cbbe129.png>>
Another option is to set the "Number of received messages at each timestep" to 1.*
This will configure the CAN Receive block to read only one CAN message at each timestep from the input FIFO buffer and so the CAN Unpack block will process the one and only received message at each timestep.
With this approach you need to make sure that the simulation execution can keep up with the CAN message rate, or else the CAN message received will be output by CAN Receive with a lag with respect to wall-clock time, and this lag will only increase in time.
<<www-mathworks-com-matlabcentral-answers-uploaded_files-209665-cd8d1014ac2fc57bbcba7d1a44f9dda3.png>>
As examples of apparent CAN messages issues reported, in the following images “Transmit messages periodically" is disabled in CAN Transmit block:
1. When 'Received messages at each timestep is set to "all" in CAN Receive Block:
<<www-mathworks-com-matlabcentral-answers-uploaded_files-209666-9957ee965a65bd8c14f86ffbd2a33e39.png>>
2. When 'Received messages at each timestep is set to '1' in CAN Receive Block:
<<www-mathworks-com-matlabcentral-answers-uploaded_files-209667-5782b676c3b338222f42c25d7bac3081.png>>