How to use Simulink and Arduino (Real Time Communication)

72 views (last 30 days)
Dear all,
I'm currently struggling to read sensor data from an Arduino via Simulink. While this is not a problem per se, it is complicated by the urgent need of my simulation (and whole loop) running in Real Time.
So far i tried the following things:
1.) Using the Serial Receive / Serial Send blocks provided by the "Simulink Support Package for Arduino Hardware version 16.2.5". These only work in Normal Mode, which does not run in real time. Additionally, there are periods of time during which the serial interface neither sends nor receives, sometimes over several seconds.
2.) Using TCP/IP and UDP blocks provided by the upper mentioned package. These blocks do work in External Mode and are therefore as close to Real Time as possible. However, when sending a value to the Arduino, there is a delay of almost 3s before the Arduino sends the values back.
In both cases 1.) and 2.), the Arduino Code was generated and flashed by Simulink.
3.) Using an Embedded Matlab Function. While the function worked fine in Matlab (0.09s delay between writing a value to the Arduino and receiving this value from the arduino), using the function in Simulink leads to a vast decrease in performance. Modifying the step size has an influence on the delay (smaller step size -> smaller delay), but it also affects the simulation performance.
At this point I really don't know where to go from here - therefore, I'm asking for your help concerning the following topics:
A.) Is it at all possible to use Arduinos communicating with Simulink in Real Time? From what I expected, the Hardware on both devices (Arduino Mega 2560 / i5@2,3 GHz) should be sufficient, but I don't seem to be able to use it correctly. B.) (Only relevant if you answered A.) with "yes") What is the best way to do that?
I'm grateful for anything and everything. Thanks everybody in advance! If I didn't address a key issue which is essential for either understanding or answering my question(s), please don't hesitate to tell me.
Best regards
Tim

Answers (1)

Nick Choi
Nick Choi on 3 Oct 2017
Just to clarify, the Simulink Support Package for Arduino does not generate real-time code.
Generally, external mode can be used to monitor signal data coming from the Arduino as the generated code for the model is running on the board. In external mode, you could log the signals to the base workspace with the 'To Workspace' block as described in the following documentation link: https://www.mathworks.com/help/simulink/slref/toworkspace.html
If you do not want to utilize the 'To Workspace' or 'Scope' blocks, the approaches that you mentioned above seem to cover most of the alternatives. If you are using external mode, you do not need the Serial, TCP/IP or UDP blocks because Simulink generally handles the communication protocols for you.
The following documentation link provides an example of how to read I2C sensor data using an Arduino: https://www.mathworks.com/help/supportpkg/arduino/examples/read-temperature-from-an-i2c-based-sensor-using-arduino-hardware.html
In most cases, reducing the model's step size could help reduce any delays that you encounter in external mode.
  1 Comment
AFETTOUCHE Massinissa
AFETTOUCHE Massinissa on 27 Feb 2019
Hello ,
Can I ask you a question please :
  • the code used by the arduino is the sFunction or the code we have written in arduino IDE???
Because I don't understand how arduino works in simulink with SPI or I2C communication; I havn't any idea if we have to programm the arduino in arduino IDE and receive the data in simulink or programm the arduino from sFunction in simulink directly without passing by arduino IDE????

Sign in to comment.

Categories

Find more on 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!