How to create a simulink block to receive/transmite a CAN message with the Arduino due (native CAN 0 and CAN1)?
3 views (last 30 days)
Show older comments
I have been trying to create a simulink block with s-function to receive/transmite a CAN message from the Arduino due with its 2 CAN, but with no sucess. I also would like to use the CAN pack/unpack block to send the message via CAN, and sometimes I have tried to create the s-function and run the code I get an error related to the data type.
1 Comment
Answers (1)
Anshuman
on 18 May 2023
Hi Alan,
Here are some general steps that you can follow to create an S-function block for sending and receiving CAN messages from an Arduino Due:
- Prepare the Arduino Due: You will need to have an Arduino Due board that is equipped with two CAN interfaces. You should also have a CAN transceiver that is compatible with the Due, and any necessary cables and connectors.
- Define the inputs and outputs: In your S-Function block, you will need to define the inputs and outputs that correspond to the messages you want to send and receive. For example, you might have an input for the message data, an input for the message ID, and an output for the received message data.
- Initialize the CAN interfaces: In your S-Function block's initialization function, you will need to initialize the two CAN interfaces on the Arduino Due. This may involve configuring the CAN bit rate, setting the message filters and masks, and enabling the interfaces.
- Implement the send function: In your S-Function block's send function, you will need to pack the message data into a CAN message and send it via the appropriate CAN interface on the Due. You can use the CAN pack block to help you with this step.
- Implement the receive function: In your S-Function block's receive function, you will need to wait for incoming messages on the appropriate CAN interface and unpack the received data into the output signals of your S-Function block. You can use the CAN unpack block to help you with this step.
- Test the S-Function block: Once you have created your S-Function block, you can test it by running a Simulink model that uses the block to send and receive CAN messages to/from the Arduino Due. You may need to adjust the bit rate and other settings to ensure that the CAN communication is reliable.
In terms of the errors you are encountering related to data types, you will need to ensure that you are using the correct data types and sizes for the inputs and outputs of your S-Function block. You can use the Data Type Assistant in Simulink to help you with this step, or manually specify the data types using the S-Function Builder tool. You should also ensure that you have the necessary include files and libraries in your S-Function block's source code to work with the CAN interfaces on the Arduino Due.
Hope it helps!
0 Comments
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!