MATLAB: Can I use data from the OPC toolbox to run a real time simulation with Simulink Desktop Real-Time using a model that I created with Simscape

digital twinopc toolboxplcreal time dataSimscapesimulationSimulink Desktop Real-Time

I'm creating a model using Simscape and I want to use real time data coming from a PLC to run a simulation using the Simulink Desktop Real-Time and the model created with Simscape, is it posible? Is there another way to get this real time data from the PLC?
I'm basically trying to develop a digital twin (using historical and real time data) for optimization purposes.

Best Answer

Hi Matheus
Thank you for the additional information.
1) Deployment on PLC Itself:
First of all, with most PLC you can generate C code from the model and integrate it into the PLC itself. For instance for Siemens Simatic STEP7:
Usually the code generate is from a controller/state machine, but you can as easily generate code from a physical model of your plant ("digital twin") this would be then run on the PLC itself, with no hassle regarding the communication. A disadvantage would be the reduced possibility to perform optimization of parameters, because of the few computing ressources available on the PLC, depending on your application.
2) Industrial communication - OPC DA
Your idea of using OPC Toolbox and Simulink Desktop Real-Time is a valid one. Simulink Blocks of the OPC toolbox only exist for OPC DA. So you would need to setup the OPC DA, use the block in the model and then run it with Simulink Desktop Real-Time. This is good for simulations that requires a slow time-step such as 1-100 ms.
3) Industrial Communication - MODBUS-TCP/IP
This is particularly good for Siemens PLC. If the PLC suppots MODBUS or TCP/IP, you can use the Instrument Control Toolbox to setup a connection between Simulink and the PLC, and deploy it in near-real-time using Simulink Destop Real-Time. Modbus is supported through MATLAB command, but you may use them in a MATLAB function block in Simulink.
see an example here of controlling a PLC using MODBUS Explorer:
4) If hard real-time is a requirement, you may need to go through Simulink Real-Time and a Speedgoat machine
Hope this gives you some inputs and food for thought!
Cheers
Vasco