How can I control aspects of my simulation in generated code?

Hi,
I have a simulation in which I am planning to run in generated code. Is there any way that I can access the simulation through the executable, such as through a inserting Matlab code or an external function that would change a variable and update the simulation from that point?
For example, a constant block that takes a variable from the workspace, is there any way to change the value of the constant during the simulation in the generated executable?

 Accepted Answer

By default, parameters in Simulink models are inlined so that the code is more efficient and uses less memory. However, you can certainly designate individual parameters as tunable so that you can access them from the main function driving your generated code. The most common way this is done is to place the tunable parameters in a globally visible structure.
The main link is here, which shows you lots of options to tackle this problem: http://www.mathworks.com/help/ecoder/tunable-parameters-and-expressions.html
- Sebastian

4 Comments

Hi, thanks for the help Sebastian!
Unfortunately I do not have my license info at hand so I could not view the link you posted however I did look into tunable parameters and found the option in the model optimization tab.
After building I found a file mymodel_data.c, which seems to have the values used in my simulation. Is this where I change them, or is there some other location where I can access them and (if possible) change them in real-time?
You should be able to tune these parameters in real-time while your main loop is executing. The model_data.c file contains the declaration of the parameter structure, but you can then access it during execution.
Suppose your model is named "myModel" and the parameter is called "myParam". By default, there will be a global parameter structure containing all your tunable parameters named myModel_P. So, in your main code, you can change your parameter value and then step your simulation once:
myModel_P.myParam = 5.0;
myModel_step();
- Sebastian
Thank you very much! One last question, where can I find this main file? I have found the source c file where the step function is defined, but I was under the impression that the main file is a mathworks template that just gets compiled into the executable.
It is a MathWorks template, which gets generated with Embedded Coder ( ert.tlc ) if you go to the "Code Generation > Templates" options and have "Generate an example main program" selected. This is on by default.
Once you generate code, you will see a file named ert_main.c in the generated code folder ( modelName_ert_rtw ). This is your template which you can then modify. Notice that you get a modelName.bat file in that folder too. That is what you would run to recompile the executable using the new main source code.
>> !modelName.bat
Alternatively, you could just write your own main file and compile the whole thing elsewhere. Depends on whether or not you need to integrate the generated code into a larger code base.
- Sebastian

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!