Programmatic execution of Simulink (with an FMU in Simulink)

25 次查看(过去 30 天)
I am trying to co-simulate Simulink and another external code. Here is my manual process:
  1. Run external code to generate FMU to be used in a Simulink model
  2. Open the .slx file and double-click on the FMU block to the load the generated FMU from step 1
  3. Run external code again to start "listening" over TCP/IP
  4. Run Simulink to begin co-simulation
The manual process works fine on a single machine. However, I would like to do this all programmatically and I think steps 1, 3, and 4 should be doable using a script in Matlab. I also think the Simulation object should allow me to execute the first half of step 2. What I am unsure of is how to script the second part of step 2 i.e., load the externally generated FMU into the FMU block in Simulink.
Appreciate your thoughts in advance!
Thanks.

采纳的回答

Abhishek Kumar Singh
编辑:Abhishek Kumar Singh 2024-9-5
To automate the co-simulation process between Simulink and your external code, you can use MATLAB scripting to handle all of the steps. Here's how you can achieve this:Steps to Automate:
  1. Generate FMU from External Code: If your external code can be called from MATLAB, you can use the system or ! command to execute it directly from your script.
  2. Load FMU into Simulink: Use MATLAB commands like set_param to programmatically set the parameters of the FMU block in your Simulink model to point to the newly generated FMU file.
  3. Start External Code for Listening: Again, use the system or ! command to start your external code for listening over TCP/IP.
  4. Run the Simulink Model: Use the sim command to run the Simulink model programmatically.
Here's an example MATLAB script:
% Step 1: Generate FMU using external code
system('path_to_your_external_code_to_generate_fmu');
% Step 2: Load FMU into Simulink
modelName = 'your_model_name';
fmuBlockPath = [modelName '/FMUBlockName']; % Adjust this path to your FMU block
fmuFilePath = 'path_to_generated_fmu.fmu'; % Path to the generated FMU
% Load the Simulink model and set the FMU file path param
load_system(modelName);
set_param(fmuBlockPath, 'FMUName', fmuFilePath);
% Save the changes to the model
save_system(modelName);
% Step 3: Start external code for listening
system('path_to_your_external_code_to_start_listening');
% Step 4: Run the Simulink model
simOut = sim(modelName);
% Optionally, process the simulation output
% results = simOut.get('yout'); % Adjust this according to your needs
Ensure that fmuBlockPath accurately references the FMU block in your Simulink model. Also verify that the paths to your external code and FMU file are correct and accessible from MATLAB. Do consider implementing error handling for system calls and Simulink operations to address potential issues during execution. If running across multiple machines, try MATLAB's Parallel Computing Toolbox to facilitate distributed execution.
Hope this helps!
  5 个评论
Abhishek Kumar Singh
You can flawlessly run BAT scripts from Command Window as I mentioned in point 6.
You can have a more potent BAT file for robust execution of Step 3 of your process.
IceBreakerSours
IceBreakerSours 2024-9-7
编辑:IceBreakerSours 2024-9-8
Thank you @Abhishek Kumar Singh That did the trick for me.
Appreciate the generous support. I managed to execute all steps programmatically without firing up even the Matlab GUI.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by