Programmatic execution of Simulink (with an FMU in Simulink)

27 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

Abhishek Kumar Singh
Abhishek Kumar Singh el 5 de Sept. de 2024
Editada: Abhishek Kumar Singh el 5 de Sept. de 2024
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 comentarios
Abhishek Kumar Singh
Abhishek Kumar Singh el 7 de Sept. de 2024
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 el 7 de Sept. de 2024
Editada: IceBreakerSours el 8 de Sept. de 2024
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.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by