Simulink Coder

Configuring a Model via Command Line

This example shows how to use Simulink® Coder™ and Embedded Coder® configuration options for customizing the generated code. You access code generation options through the Simulink® Configuration Parameters, also referred to as the model's configuration set.

You make configuration decisions and tradeoffs depending on how you use and interact with the generated code. You choose a configuration that best matches your needs for debugging, traceability, code efficiency, and safety precaution. Features, scripts, and documentation facilitate your creation of an ideal model configuration set for your application.

While Simulink® Coder™ provides a push-button facility to quickly configure and check a model for specified objectivesquickly configure and check a model for specified objectives, it is common to automate the model configuration process using a MATLAB® script once a desired configuration is established. The example describes:

  • Concepts of working with configuration parameters

  • Documentation to understand the code generation options

  • Tools and scripts to automate the configuration of a model

With these basic skills you are well on your way to setting up an ideal automated configuration scheme for your project.

Configuration Parameter Workflows

There are many workflows for Configuration Parameters that include persistence within a single model or persistence across multiple models. Depending on your needs, you may want to work with configuration sets as copies or references. This example shows the basics steps for working directly with the active configuration set of a model. For a comprehensive description of configuration set features and workflows, see Configuration SetsConfiguration Sets in the Simulink® documentation.

Configuration Set Basics

Load a model into memory.

model='rtwdemo_configwizard';
load_system(model)

Obtain the model's active configuration set.

cs = getActiveConfigSet(model);

The Simulink® Coder™ product exposes a subset of the code generation options. If you are using Simulink® Coder™, select the Generic Real-Time (GRT) target.

switchTarget(cs,'grt.tlc',[]);

The Embedded Coder® product exposes the complete set of code generation options. If you are using Embedded Coder®, select the Embedded Real-Time (ERT) target.

switchTarget(cs,'ert.tlc',[]);

To automate configuration of models that will be built for both GRT- and ERT-based targets, the configuration set IsERTTarget attribute is useful.

isERT = strcmp(get_param(cs,'IsERTTarget'),'on');

You can interact with code generation options via the model or the configuration set. This example gets and sets options indirectly via the model.

inlineParams = get_param(model,'InlineParams');  % Get InlineParams
set_param(model,'InlineParams',inlineParams)     % Set InlineParams

This example gets and sets options directly via the configuration set.

if isERT
    lifespan = get_param(cs,'LifeSpan');  % Get LifeSpan
    set_param(cs,'LifeSpan',lifespan)     % Set LifeSpan
end

Configuration Option Summary

The full list of code generation options are documented with tradeoffs for debugging, traceability, code efficiency, and safety precaution.

Be sure to leverage the Code Generation Advisor to obtain a model configuration optimized for your goals. In the Set Objectives dialog box, you can set and prioritize objectives for the Code Generation Advisor.

You can find documentation about the Code Generation AdvisorCode Generation Advisor in the Simulink Coder documentation and additional documentationadditional documentation specific to Embedded Coder®.

Parameter Configuration Scripts

Simulink® Coder™ provides an example configuration script that you can use as a starting point for your application. A list of the most relevant GRT and ERT code generation options are contained in rtwconfiguremodel.mrtwconfiguremodel.m.

Alternatively, you can generate a MATLAB function automatically that contains the complete list of model configuration parameters using the configuration set saveAs function.

% Go to a temporary writable directory.
currentDir = pwd;
rtwdemodir();

% Save the model's configuration parameters to file 'MyConfig.m'.
saveAs(cs,'MyConfig')

% Display the first 50 lines of MyConfig.m.
dbtype MyConfig 1:50
1     %---------------------------------------------------------------------------
2     %  MATLAB function for configuration set generated on 04-Sep-2014 05:39:32
3     %  MATLAB version: 8.4.0.145981 (R2014b)
4     %---------------------------------------------------------------------------
5     
6     function cs=MyConfig()
7     
8     cs = Simulink.ConfigSet;
9      
10    % Original configuration set version: 1.14.3
11    if cs.versionCompare('1.14.3') < 0
12        error('Simulink:MFileVersionViolation', 'The version of the target configuration set is older than the original configuration set.');
13    end
14     
15    % Original environment character encoding: US-ASCII
16    if ~strcmpi(get_param(0, 'CharacterEncoding'), 'US-ASCII')
17        warning('Simulink:EncodingUnMatched', 'The target character encoding (%s) is different from the original (%s).',  get_param(0, 'CharacterEncoding'), 'US-ASCII');
18    end
19     
20    % Do not change the order of the following commands. There are dependencies between the parameters.
21    cs.set_param('SystemTargetFile', 'ert.tlc');   % System target file
22    cs.set_param('GenerateComments', 'on');   % Include comments 
23    cs.set_param('SimulinkBlockComments', 'on');   % Simulink block / Stateflow object comments 
24    cs.set_param('ShowEliminatedStatement', 'off');   % Show eliminated blocks 
25    cs.set_param('OperatorAnnotations', 'off');   % Operator annotations 
26    cs.set_param('ForceParamTrailComments', 'on');   % Verbose comments for SimulinkGlobal storage class 
27    cs.set_param('InsertBlockDesc', 'off');   % Simulink block descriptions 
28    cs.set_param('SimulinkDataObjDesc', 'off');   % Simulink data object descriptions 
29    cs.set_param('EnableCustomComments', 'off');   % Custom comments (MPT objects only) 
30    cs.set_param('SFDataObjDesc', 'off');   % Stateflow object descriptions 
31    cs.set_param('ReqsInCode', 'off');   % Requirements in block comments 
32    cs.set_param('IncludeHyperlinkInReport', 'on');   % Code-to-model 
33    cs.set_param('GenerateReport', 'on');   % Create code generation report 
34    cs.set_param('GenerateTraceInfo', 'off');   % Model-to-code 
35    cs.set_param('GenerateTraceReport', 'off');   % Eliminated / virtual blocks 
36    cs.set_param('GenerateTraceReportSl', 'off');   % Traceable Simulink blocks 
37    cs.set_param('GenerateTraceReportSf', 'off');   % Traceable Stateflow objects 
38    cs.set_param('GenerateTraceReportEml', 'off');   % Traceable MATLAB functions 
39    cs.set_param('LaunchReport', 'on');   % Open report automatically 
40    cs.set_param('GenerateCodeReplacementReport', 'off');   % Summarize which blocks triggered code replacements 
41    cs.set_param('GenerateCodeMetricsReport', 'off');   % Static code metrics 
42    cs.set_param('GenerateWebview', 'off');   % Generate model Web view 
43    cs.set_param('InlineParams', 'on');   % Inline parameters 
44    cs.set_param('OptimizeBlockIOStorage', 'on');   % Signal storage reuse 
45    cs.set_param('ExpressionFolding', 'on');   % Eliminate superfluous local variables (expression folding) 
46    cs.set_param('ParamNamingRule', 'None');   % Parameter naming 
47    cs.set_param('SignalNamingRule', 'None');   % Signal naming 
48    cs.set_param('UpdateModelReferenceTargets', 'IfOutOfDate');   % Rebuild 
49    cs.set_param('GenerateSampleERTMain', 'on');   % Generate an example main program 
50    cs.set_param('ProdHWDeviceType', 'Generic->32-bit Embedded Processor');   % Device type 

Each parameter setting in the generated file includes a comment for the corresponding parameter string in the Configuration Parameter dialog box. Use this link to review MyConfig.m in the MATLAB Editor: MyConfig.mMyConfig.m.

% Return to previous working directory.
cd(currentDir)

Configuration Wizard Blocks

Embedded Coder® provides a set of Configuration WizardConfiguration Wizard blocks to obtain a first cut configuration of a model for a specific goal. The predefined blocks provide configuration for:

  • ERT optimized for fixed point

  • ERT optimized for floating point

  • GRT optimized for fixed/floating point

  • GRT debug settings for fixed/floating point

  • Custom (you provide the script)

Drop the block into a model and double-click to configure the model. Open model rtwdemo_configwizardrtwdemo_configwizard and click Open Configuration Wizard Library to interact with these useful blocks.

open_system(model)

Specific instructions:

  • Open the Configuration Wizard Library by clicking the link provided in the model.

  • Open the Model's Configuration Parameters by clicking the link provided in the model.

  • Drag and drop a Configuration Wizard Block, for example ERT (optimized for fixed point), from the wizard library into the model.

  • Double-click the wizard block.

The Configuration Parameter options are modified automatically.

% cleanup
rtwdemoclean;
close_system(model,0)

Summary

Simulink provides a rich set of MATLAB functions to automate the process of configuring a model for simulation and code generation. Simulink Coder and Embedded Coder® provide additional functionality specific for code generation. The Code Generation Advisor provides an excellent means of optimizing the model's configuration based on a set of prioritized goals. The optimal configuration can be saved to a MATLAB file automatically using the configuration set saveAs function and reused across models and projects.