MATLAB Answers

(Simulink) How can I communicate with hardware (Arduino, Raspberry Pi, I2C, etc.) in real time?

14 views (last 30 days)
Andrew Burns
Andrew Burns on 9 Sep 2018
Commented: Gao Yang on 6 Feb 2020
I am running a Hardware in the Loop test with Simulink and would like to control Arduinos, Raspberry Pis, as well as recieve information from I2C devices. I would like for my personal computer to run the simulation as it is large and computationally intensive, however I would like to communicate Simulink values to the mentioned hardware. I am aware that there are Arduino and Raspberry Pi support software, however I believe that they only allow the Hardware to run the models, and do not seem to be able to let the hardware recieve data. I am also aware that the Real Time add-on exists, but the documentation only deals with the Speedgoat software, and I am unsure of whether it would work with other hardware. There is also the Digital Output blocks and Serial Send blocks from the Data Acquisition and HDL Coder packages, however I am unsure if they will connect to the Arduino or Raspberry Pi.
I would like to know what is my best option to have my computer run the simulation and have it export the data in real time to hardware, and also for the hardware to give data in real time for the simulation to use.

Answers (2)

Madhu Govindarajan
Madhu Govindarajan on 10 Sep 2018
Edited: Madhu Govindarajan on 10 Sep 2018
Here is my attempt to answer the questions that you have -
1) MCUs such as Arduino cannot run "hard" real-time as they do not have dedicated real-time Operating system monitoring them. Simulink Real-time to my knowledge works only with devices that have real-time OS such as Speedgoat so that is probably not the way to go for Arduinos and such.
2) That being said, MCUs can still do "soft" real-time. To communicate between Simulink models and Arduinos, serial send and receive are a great way. You can have a simulink model that uses serial send to send appropriate data and on the Arduino side, deploy a model from Simulink that has serial receive and possibly whatever algorithm that you want your Arduino to respond to these requests.
HTH,
Madhu
  3 Comments
Gao Yang
Gao Yang on 6 Feb 2020
Hello,sir! I also met the simialr problem. I have used the python to code a UDP server in python to receive the real-time simulation datas. How do you parse these datas in python and send them back to simulink?

Sign in to comment.


Gao Yang
Gao Yang on 6 Feb 2020
Hello,sir! I also met the simialr problem. I have used the python to code a UDP server in python to receive the real-time simulation datas. How do you parse these datas in python and send them back to simulink?

Community Treasure Hunt

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

Start Hunting!