create code definitions to control code generation for model data and functions -凯发k8网页登录
create code definitions to control code generation for model data and functions
open the embedded coder dictionary
to open an embedded coder dictionary, in a model window, open the embedded coder app by opening the apps gallery and clicking embedded coder. on the c code tab, select code interface > embedded coder dictionary. the embedded coder dictionary window displays the code interface configuration that is stored in the dictionary.
to open the embedded coder dictionary in a simulink® data dictionary, in the model explorer model hierarchy pane:
under the dictionary node, select the embedded coder node.
if you do not see the node, right-click the dictionary node and select show empty sections.
in the dialog pane (the right pane), click open embedded coder dictionary.
examples
create and verify storage class
in a model, create a storage class that aggregates internal model data, including block states, into a structure whose characteristics you can control. then, verify the storage class by generating code from the model.
open the model
rollaxisautopilot
by typing this command:openexample('rollaxisautopilot');
if the model does not open in the embedded coder app, open the app and click the c code tab.
on the c code tab, select code interface > embedded coder dictionary. the embedded coder dictionary window displays code generation definitions that are stored in the model file.
on the left pane, click storage class. in the storage classes section, click create.
select the new storage class that appears at the bottom of the list,
storageclass1
. in the pane on the right, set the property values listed in this table.property value name internalstruct
header file internaldata_$r.h
definition file internaldata_$r.c
storage type structured
structure properties > type name internaldata_t_$m
structure properties > instance name internaldata_$m
after making your changes, in the bottom pane, verify that the pseudocode preview reflects what you expect.
return to the model editor. to open the interface editor, below the canvas, double-click code mappings. on the data defaults tab, expand the signals section. select the signals, states, and internal data row and set storage class to
internalstruct
.in the configuration parameters dialog box, on the code generation > code placement pane, set file packaging format to
modular
.generate code.
in the simulink editor code view, open and inspect the file
internaldata_rollaxisautopilot.h
. the file defines the structure typeinternaldata_t_
, whose fields represent block states in the model./* storage class 'internalstruct', for system '
' */ typedef struct { real32_t fixptunitdelay1_dstate; /* ' /fixpt unit delay1' */ real32_t integrator_dstate; /* ' /integrator' */ int8_t integrator_prevresetstate; /* ' /integrator' */ } internaldata_t_; the file also declares a global structure variable named
internaldata_
./* storage class 'internalstruct' */ extern internaldata_t_ internaldata_;
open and inspect the file
internaldata_rollaxisautopilot.c
. the file allocates memory forinternaldata_
./* storage class 'internalstruct' */ internaldata_t_ internaldata_;
create function customization template
with a function template, you can specify a rule that governs the names of generated entry-point functions. this technique helps save time and maintenance effort in a model that has many entry-point functions, such as an export-function model or a multirate, multitasking model.
this example shows how to create a function template that specifies the naming
rule func_$n_$r
. $n
is the base name of each
generated function and $r
is the name of the simulink model.
open the example model
rtwdemo_mrmtbb
.update the block diagram. this multitasking model has two execution rates, so the generated code includes two corresponding entry-point functions.
in the model, set model configuration parameter system target file to
ert.tlc
. to use a function customization template, you must use an ert-based system target file.in the simulink editor, open the embedded coder app and open the embedded coder dictionary.
in the embedded coder dictionary, on the function customization template tab, click create.
for the new function template, set these properties:
name to
myfunctions
.function name to
func_$n_$r
.
after making your changes, verify that the pseudocode preview reflects what you expect.
in the model window, open the code mappings editor. on the function defaults tab, for the initialize/terminate and execution rows, set function customization template to
myfunctions
.generate code.
in the code view, open and inspect the file
rtwdemo_mrmtbb.c
. the file defines the two execution functions,func_step0_rtwdemo_mrmtbb
andfunc_step1_rtwdemo_mrmtbb
, whose names conform to the rule that you specified in the function template.
for an example that shows how to create a memory section, see .
create storage class for use with statically and dynamically initialized data
this example shows how to create a storage class that places global variable definitions and declarations in files whose names depend on the model name. you create two copies of the storage class so that you can use one copy with parameter data (the data category model parameters) and one copy with other data.
typically, the generated code initializes parameter data statically, outside any function, and initializes other data dynamically, in the model initialization function. when you create a storage class by using the custom storage class designer or an embedded coder dictionary, you set the data initialization property to specify the initialization mechanism.
in an embedded coder dictionary, for each storage class, you must select
static
or dynamic
initialization. consider creating one copy of the storage class for parameter data
(static
) and one copy for other data
(dynamic
).
open the model
rollaxisautopilot
by typing this command:openexample('rollaxisautopilot');
if the c code tab is not open, open the embedded coder app and click the c code tab.
select code interface > embedded coder dictionary
on the left pane, click storage class. in the storage classes section, click create.
for the new storage class, set these properties:
name to
sigsstates
header file to
$r_my_data.h
definition file to
$r_my_data.c
data initialization to
dynamic
after making your changes, verify that the pseudocode preview reflects what you expect.
place your cursor over the row for the storage class sigsstates and click duplicate code definition. a new storage class,
sigsstates_copy
, appears.for the new storage class, set these properties:
name to
params
data initialization to
static
verify that the pseudocode preview reflects what you expect.
in the model, open the code mappings editor. below the model canvas, double-click code mappings - component interface.
on the data defaults tab, for the parameters > model parameters row, in the storage class column, select
params
.for the signals > signals, states, and internal data row, set storage class to
sigsstates
.configure some parameter data elements in the model so that optimizations do not eliminate those elements from the generated code. on the modeling tab, click design > model workspace.
in the model explorer, on the center pane, select the three rows that correspond to the variables
dispgain
,intgain
, andrategain
in the model workspace.right-click one of the rows and click
convert to parameter object
. the model data editor converts the workspace variables tosimulink.parameter
objects.in the row for the parameter
dispgain
, in the storage class column, click configure. the model window highlights the row for thedispgain
parameter in the code mappings editor.for each variable, in the storage class column, select
model default: params
, which means that they acquire the default storage class that you specified for model parameters.in the configuration parameters dialog box, on the code generation > code placement pane, set file packaging format to
modular
.generate code.
in the code view, open and inspect the files
rollaxisautopilot_my_data.c
androllaxisautopilot_my_data.h
. these files define and declare global variables that correspond to the parameter objects and some block states, such as the state of the integrator block in thebasicrollmode
subsystem./* storage class 'sigsstates' */ real32_t rtfixptunitdelay1_dstate; real32_t rtintegrator_dstate; int8_t rtintegrator_prevresetstate; /* storage class 'params' */ real32_t dispgain = 0.75f; real32_t intgain = 0.5f; real32_t rategain = 2.0f;
refer to code generation definitions in a package
you can configure an embedded coder dictionary to refer to code generation
definitions that you store in a package (see ). those
definitions then appear available for selection in the code mappings editor. in this
example, you configure the embedded coder dictionary in
rollaxisautopilot
to refer to definitions stored in the
built-in example package ecoderdemos
.
open the embedded coder dictionary for
rollaxisautopilot
. for instructions, see create and verify storage class.in the embedded coder dictionary window, on the memory tab, click manage packages.
in the manage packages dialog box, click refresh. wait until more options appear in the select package drop-down list.
set select package to
ecoderdemos
and click load.in the embedded coder dictionary window, on the storage classes tab, the table shows the storage classes defined in the
ecoderdemos
package. now, inrollaxisautopilot
, you can select these storage classes in the code mappings editor on the data defaults tab.to unload the package, in the manage packages dialog box, select the package in the select package drop-down list and click unload.
for an example that shows how to share code generation definitions between models by using data dictionaries, see .
for an example that shows how to configure default code mappings in a shared embedded coder dictionary, see .
related examples
parameters
limitations
a storage class or function customization template that you create in an embedded coder dictionary cannot use a memory section that you load from a package (as described in refer to code generation definitions in a package). use a memory section defined in the embedded coder dictionary.
a storage class that you create in an embedded coder dictionary that has
function
data access is not supported for the storage class of a data store, parameter argument tuning interface, or measurement interface.you cannot create code generation definitions in a
.mdl
model file.
for additional limitations for code generation definitions in the embedded coder
dictionary of a data dictionary (.sldd
file), see .