This example uses MathWorks Virtual CAN channels to set up periodic transmit and reception of CAN messages, using Simulink®. The Virtual channels are connected in a loopback configuration.
Vehicle Network Toolbox™ provides Simulink blocks for transmitting and receiving live messages via Simulink models over Controller Area Networks (CAN). This example uses the CAN Configuration, CAN Pack, CAN Transmit, CAN Receive and CAN Unpack blocks to perform data transfer over a CAN bus.
Create a model to transmit two messages at different periods and receive only specified messages and unpack the message with a specified ID.
Use a CAN Transmit block to transmit the CAN message with ID 250 to transmit messages every 1 second.
Use another CAN Transmit block to transmit the CAN message with ID 500 to transmit messages every 0.5 seconds.
Input a signal to both CAN Pack blocks to an auto-incrementing counter with a limit of 50.
Both the CAN Transmit blocks are connected to MathWorks Virtual Channel 1.
Use a CAN Receive block to receive CAN messages from MathWorks Virtual Channel 2. Set the block to:
Receive messages with ID 250 and 500 only.
The Receive block generates a function call trigger if it receives a new message at any particular timestep.
The CAN Unpack subsystem is a function-call triggered subsystem and is executed only when a new message is received by the CAN Receive block at a particular timestep.
Plot the results to see the counter value and timestamp for each unpacked message. The X-axis on the plot corresponds to the simulation timestep. The timestamp plots show that the messages are sent at the specified times. It can also be seen that the number of messages transmitted for ID 250 is half as much transmitted for ID 500 due to the different periodic rates specified for them.
MathWorks CAN Virtual channels were used for this example. You can however connect your models to other supported hardware. You can also modify the model to transmit at different rates or transmit a combination of periodic and non-periodic messages.