How to set workspace variables before the simulation starts in the initialize function generated by the Embedded coder.

7 vues (au cours des 30 derniers jours)
Hello.
I am using the Embedded Coder to generate c-files for my simulation which I then run in Python. I am using the simulation to train a reinforcement learning agent and I would like to initialize the simulation with different start positions. These start positions are set in some Simulink blocks which has the variables as non-tunable, meaning that they are imported from the workspace and set when the simulation starts. I am unable to change the variables to tunable, since I have to disable library links for that to work. I am only interested in changing them before the simulation starts, not during the simulation.
I hope that I can change the value of these variables when terminate and reinitialize the simulation. I am able to do it in the InitFcn callback in Matlab, but have been unsuccesuful in doing it when generating code. The variable in question is called initial_conditions and in the InitFcn I am using it as such:
cyl1.init_x_m = initial_conditions.bmCyl.x_m;
cyl1.ini_Lp_bar = initial_conditions.bmCyl.l_bar;
cyl1.ini_Sp_bar = initial_conditions.bmCyl.s_bar;
cyl2.init_x_m = initial_conditions.armCyl.x_m;
cyl2.ini_Lp_bar = initial_conditions.armCyl.l_bar;
cyl2.ini_Sp_bar = initial_conditions.armCyl.s_bar;
cyl3.init_x_m = initial_conditions.bktCyl.x_m;
cyl3.ini_Lp_bar = initial_conditions.bktCyl.l_bar;
cyl3.ini_Sp_bar = initial_conditions.bktCyl.s_bar;
So, how can I set these values each time I initialize the simulation in C-code? Any help is appreciated.
I am using Matlab 2018b.

Réponses (1)

Pratyush Swain
Pratyush Swain le 18 Mar 2025
Hi Globee,
You can create tunable Simulink parameters with storage class 'ExportedGlobal' programmatically and link/assign them to your Simulink block parameters.
% Creates Tunable Simulink Parameter with value 3.2
x = Simulink.Parameter(3.2);
% Change Storage Class to Global
x.StorageClass = 'ExportedGlobal';
% Link to model/block parameter
set_param('block_path', 'ParameterName', x) % Set Value as Simulink Parameter
Now after code generation, it will generated as an extern global variable under 'model.h' file with its values defined under 'model.c' file.
You can now modify this values before running the simulation in C-code.
Hope this helps.

Catégories

En savoir plus sur Deployment, Integration, and Supported Hardware dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by