Configure Model and Generate Code
About This Example
Learning Objectives
Configure a model for code generation.
Use model checking tools to discover conditions and configuration settings resulting in generation of inaccurate or inefficient code.
Generate code from a model.
Locate and identify generated code files.
Review generated code.
Prerequisites
Ability to open and modify Simulink®models and subsystems.
Ability to set model configuration parameters.
Ability to use the Code Generation Advisor.
Ability to read C code.
An installed,supported C compiler.
Required Files
rtwdemo_throttlecntrl
model file
Configure the Model for Code Generation
Model configuration parameters determine the method for generating the code and the resulting format.
Open
rtwdemo_throttlecntrl
and save a copy asthrottlecntrl
in a writable location on your MATLAB path. Then, open theSimulink Coder应用程序。Open the Configuration Parameters dialog box,Solverpane. To generate code for a model, you must configure the model to use a fixed-step solver. The following table shows the solver configuration for this example.
Parameter Setting Effect on Generated Code Type Fixed-step
Maintains a constant (fixed) step size, which is required for code generation Solver discrete (no continuous states)
Applies a fixed-step integration technique for computing the state derivative of the model Fixed-step size .001
Sets the base rate; must be the lowest common multiple of the rates in the system Set model configuration parameterSystem target fileto
grt.tlc
.Note
The GRT (Generic Real-Time Target) configuration requires a fixed-step solver. However, the
rsim.tlc
system target file supports variable step code generation.The system target file defines an environment for generating and building code for execution on a certain hardware or operating system platform. For example, one property of a system target file is the value for the
CodeFormat
TLC variable. The GRT configuration requires a fixed step solver and the rsim.tlc supports variable step code generation.Set model configuration parameterInclude directoriesas follows:
"$matlabroot$\toolbox\rtw\rtwdemos\EmbeddedCoderOverview\"
This folder includes files that are required to build an executable for the model.
Close the Model Configuration Parameters dialog box.
Save Your Model Configuration as aMATLABFunction
You can save the settings of model configuration parameters as a MATLAB®function by using thegetActiveConfigSet
function. In the MATLAB Command Window, enter:
thcntrlAcs = getActiveConfigSet('throttlecntrl'); thcntrlAcs.saveAs('throttlecntrlModelConfig');
You can then use the resulting function (for example,throttlecntrlModelConfig
) to:
Archive the model configuration.
Compare different model configurations by using differencing tools.
Set the configuration of other models.
For example, you can set the configuration of modelmyModel
to match the configuration of the throttle controller model by openingmyModel
and entering:
myModelAcs = throttlecntrlModelConfig; attachConfigSet('myModel', myModelAcs, true); setActiveConfigSet('myModel', myModelAcs.Name);
For more information, seeSave a Configuration SetandLoad a Saved Configuration Set.
Check Model Conditions and Configuration Settings
Before generating code for a model, use the Code Generation Advisor to check the model conditions and configuration settings. The check identify issues that can result in inaccurate or inefficient code.
Open model
throttlecntrl
. Then, open theSimulink Coder应用程序。In theC Codetab, clickC/C++ Code Advisor. A dialog box opens showing the model system hierarchy.
Click
rtwdemo_throttlecntrl
and then clickOK. The Code Generation Advisor window opens.Right-click on theCode Generation Advisorfolder and selectRun Selected Checks.
To review the results for checkCheck model configuration settings against code generation objectives, select the check in the left pane. The results are displayed in the right pane.
After reviewing the check results, you can choose to fix warnings and failures as described inFix a Model Advisor Check Warning or Failure.
Generate Code for the Model
Open
throttlecntrl
. Then, open theSimulink Coder应用程序。Select model configuration parameterGenerate code only.
Select model configuration parameterCreate code generation reportand clickApply.
Generate code.
消息出现在诊断查看器。鳕鱼e generator produces standard C and header files, and an HTML code generation report. The code generator places the files in abuild folder, a subfolder named
throttlecntrl_grt_rtw
under your current working folder.
Review the Generated Code
In the code generation report, click the link for the generated C model file and review the generated code. Look for these items in the report:
Identification, version, timestamp, and configuration comments.
Links to help you navigate within and between files
Data definitions
Scheduler code
Controller code
Model initialization and termination functions
Call interface for the GRT system target file — output, update, initialization, start, and terminate
Save and close model
throttlecntrl
.
Consider examining the following files. In the code generation reportContentspane, click the links.
File | Description |
---|---|
throttlecntrl.c |
C file that contains the scheduler, controller, initialization, and interface code |
throttlecntrl_data.c |
C file that assigns values to generated data structures |
throttlecntrl.h |
Header file that defines data structures |
throttlecntrl_private.h |
Header file that defines data used only by the generated code |
throttlecntrl_types.h |
Header file that defines the model data structure |
For more information, seeManage Build Process File Dependencies.
At this point, consider logging data to a MAT-file. For an example, seeLog Data for Analysis.
Generate an Executable
Open
throttlecntrl
. Then, open theSimulink Coder应用程序。Clear model configuration parameterGenerate code onlyand clickApply.
Build the model. Watch the messages in the Diagnostics Viewer. The code generator uses a template make file associated with your system target file selection to create an executable file. You can run this program on your workstation, independent of external timing and events.
Check your working folder for the file
throttlecntrl.exe
.Run the executable. In the Command Window, enter
!throttlecntrl
. The!
character passes the command that follows it to the operating system, which runs the standalone program.The program produces one line of output in the Command Window:
** starting the model **
At this point, consider logging data to a MAT-file. For an example, seeLog Data for Analysis.
Tip
For UNIX®platforms, run the executable program in the Command Window with the syntax!./
executable_name
. If preferred, run the executable program from an OS shell with the syntax./
executable_name
. For more information, seeRun External Commands, Scripts, and Programs.
Key Points
To generate code, change the model configuration to specify a fixed-step solver then select a system target file. Using the
grt.tlc
file requires a fixed-step solver. If the model contains continuous time blocks, you can use a variable-step solver with thersim.tlc
system target file.After debugging a model, consider configuring a model with parameter inlining enabled.
Use the
getActiveConfigSet
function to save a model configuration for future use or to apply it to another model.Before generating code, consider checking a model with the Code Generation Advisor.
鳕鱼e generator places generated files in a subfolder (model
_grt_rtw
) of your working folder.