Configure a model for code generation.
Apply 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.
Ability to open and modify Simulink® models and subsystems.
Ability to set model configuration parameters.
Ability to use the Simulink Model Advisor.
Ability to read C code.
An installed, supported C compiler.
rtwdemo_throttlecntrl
model file
Model configuration parameters determine the method for generating the code and the resulting format.
Open rtwdemo_throttlecntrl
and save a copy as
throttlecntrl
in a writable location on your MATLAB path. Then,
open the Simulink Coder app.
Open the Configuration Parameters dialog box, Solver pane. 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 parameter System target file to
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 parameter Include directories as 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.
You can save the settings of model configuration parameters as a MATLAB® function by using the getActiveConfigSet
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 model myModel
to match
the configuration of the throttle controller model by opening myModel
and
entering:
myModelAcs = throttlecntrlModelConfig; attachConfigSet('myModel', myModelAcs, true); setActiveConfigSet('myModel', myModelAcs.Name);
For more information, see Save a Configuration Set and Load a Saved Configuration Set.
Before generating code for a model, use the Simulink Model Advisor to check the model for conditions and configuration settings. This check finds issues that can result in inaccurate or inefficient code.
Open model throttlecntrl
. Then, open the Simulink
Coder app.
Start the Model Advisor. In the C Code tab, click C/C++ Code Advisor. A dialog box opens showing the model system hierarchy.
Click rtwdemo_throttlecntrl
and then click
OK. The Model Advisor window opens.
In the right pane, select Run this check. The report shows a Run Summary that flags check warnings.
Review the report. The warnings highlight issues. At this point, you can ignore them. For more information about reports, see View Model Advisor Check Reports.
Open throttlecntrl
. Then, open the Simulink
Coder app.
Select model configuration parameter Generate code only.
Select model configuration parameter Create code generation report and click Apply.
Generate code.
Messages appear in the Diagnostics Viewer. The code generator produces standard C
and header files, and an HTML code generation report. The code generator places the
files in a build folder, a subfolder named
throttlecntrl_grt_rtw
under your current working folder.
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 report Contents pane, 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, see Manage Build Process File Dependencies.
At this point, consider logging data to a MAT-file. For an example, see Log Data for Analysis.
Open throttlecntrl
. Then, open the Simulink
Coder app.
Clear model configuration parameter Generate code only and click Apply.
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, see Log 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, see Run External Commands, Scripts, and Programs.
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 the rsim.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 Model Advisor.
The code generator places generated files in a subfolder
(model_grt_rtw
) of your working folder.