替换和重命名数据类型以符合编码标准
默认情况下,generated code uses data type aliases such asreal_t
和int32_t
。该代码使用这些别名来定义全局和局部变量。如果您的编码标准要求您使用其他数据类型别名,包括现有代码定义的别名,则可以:
Configure data type replacement for the entire model.
Configure individual data items (such as signals, parameters, and states) to use specific data type aliases.
For information about controlling data types in a model, see控制数据类型的信号。
Inspect External C Code
Save this C code into a file namedmy_types.h
in your current folder. This file represents a header file in your existing code that defines custom data type aliases by usingtypedef
statements.
#includetypedef double my_dblprecision;Typedef简短my_int16;typedef bool my_bool;
探索示例模型和默认生成的代码
打开示例模型ex_data_type_replacement。
Open_System(FullFile(Docroot,'工具箱',“ ecoder','examples',...'ex_data_type_replacement'))
在造型tab, clickModel Data Editor。
In the Model Data Editor, inspect theSignals标签。
设置更改视图drop-down list to
代码
。在数据表中Storage Class列显示了命名的信号线,例如temp
use the storage class出口global
。使用此设置,信号线以生成的代码作为单独的全局变量出现。设置更改视图drop-down list to
Design
。更新框图。
Simulink assigns a specific data type to each signal that uses an inherited data type setting such as
Inherit: Inherit via internal rule
。In the data table, expand the width of theData Typecolumn. The column shows that the signals in the model use a mix of the data types
int16
,双倍的
, 和boolean
。You can also see these data types in the model.从模型生成代码。
在代码生成报告中,检查共享实用程序文件
rtwtypes.h
。代码使用typedef
通过使用标准来重命名原始C数据类型的语句金宝app®代码r™别名。例如,代码重命名原始类型双倍的
by using the aliasreal_t
。typedef double real_t;
Inspect the file
ex_data_type_replacement.c
。代码使用the default data type aliases to declare and define variables. For example, the code uses the data typesreal_t
,int16_t
, 和boolean_T
至define the global variables流蛋白
,temp
, 和intlk
。real_T flowIn;/ * < Root > / In3”* / int16_T临时;/ *'
/Add2' */ boolean_T intlk; /* ' /Compare' */ 该模型
步
function defines local variables by using the same data type aliases.real_trtb_Add; real_T rtb_FilterCoefficient;
重用外部数据类型定义
配置generated code to define variables by using the custom data types defined inmy_types.h
instead of the default type names.
一个t the command prompt, create a
金宝appsimulink.Airstype
外部代码定义的每个数据类型的对象别名。金宝appsimulink.importexternalctypes('my_types.h');
在基本工作区中
金宝appsimulink.importexternalctypes
函数创建对象my_dblPrecision
,my_int16
, 和my_bool
。For each object, the function sets the
DataScope
property to“进口”
和HeaderFile
property to'my_types.h'
。With these settings, the code generator does not create atypedef
statement for each object, but instead the generated code reuses the statements frommy_types.h
通过包括(#include
) the file.在模型中,在“配置参数”对话框中,在代码Generation>数据类型更换窗格,选择Replace data type names in the generated code。
Specify the options in theReplacement Namecolumn according to the table.
Simulink Name Replacement Name 双倍的
my_dblPrecision
int16
my_int16
boolean
my_bool
从模型生成代码。
In the code generation report, inspect the file
rtwtypes.h
。现在,代码使用#include
directive to import the contents of the external header filemy_types.h
, which contains the custom data type aliases.#include“ my_types.h” / *用户定义的替换数据类型int16_t real_t boolean_t * /
Inspect the file
ex_data_type_replacement.c
。代码使用the custom data type aliasesmy_dblPrecision
,my_int16
, 和my_bool
定义全球变量,例如流蛋白
,temp
, 和intlk
。my_dblPrecision flowIn; /* '
/In3' */ my_int16 temp; /* ' /Add2' */ my_bool intlk; /* ' /Compare' */ 该模型
步
函数通过使用自定义数据类型别名定义局部变量。my_dblPrecision rtb_Add; my_dblPrecision rtb_FilterCoefficient;
为单个数据项创建有意义的数据类型别名
Suppose that your coding standards require that important data items use a data type whose name indicates the real-world significance. You can create more金宝appsimulink.Airstype
objects to represent these custom data type aliases. Use the objects to set the data types of data items in the model.
In the Model Data EditorData Type列,使用
金宝appsimulink.Airstype
对象设置这些块输出的数据类型。您可以在模型中选择一个信号行,该信号线突出显示了模型数据编辑器中的相应行,也可以通过使用该模型数据编辑器中的信号来搜索信号过滤内容box.Block Output Data Type Flow Setpoint flow_t
一个dd2(which feeds the相比block) difftemp_t
使用模型数据编辑器Inports/Outports选项卡以设置第三的数据类型企业块到
flow_t
。在造型tab, underDesign, clickProperty Inspector。
Select the block labeled Flow Controller. In the Property Inspector, click打开至open the block dialog box.
在数据类型tab, set总输出至
ctrl_T
。Click好的。With these data type settings, some of the named signals, such as
temp
和流蛋白
,使用唤起现实数量的数据类型,例如液体流量和温度。一个t the command prompt, create
金宝appsimulink.Airstype
对象表示这些新的自定义数据类型别名。flow_t= Simulink.AliasType('double');difftemp_t = 金宝appsimulink.alimatype('int16');ctrl_T = Simulink.AliasType('double');
In the model, the signals
流蛋白
和flowSetPt
use the primitive data type双倍的
,因此数据类型别名flow_t
maps to双倍的
。更新框图。
Due to data type inheritance, other signals also use the custom data type aliases. For example, the output data type of the一个dd第三个喂食的街区企业块设置为
Inherit: Inherit via internal rule
。The internal rule chooses the same data type that the block inputs use,flow_t
。从模型生成代码。
文件
ex_data_type_replacement_types.h
defines the new data typesflow_t
,difftemp_t
, 和ctrl_T
as aliases ofmy_dblPrecision
和my_int16
。typedef my_dblprecision flow_t;typedef my_int16 difftemp_t;typedef my_dblprecision ctrl_t;
In the file
ex_data_type_replacement.c
, the code defines global variables by using the new type names.flow_t流蛋白;/ *'
/In3' */ flow_T flowSetPt; /* ' /Flow Setpoint' */ ctrl_T flowCtrl; /* ' /Interlock' */ diffTemp_T temp; /* ' /Add2' */ 对于不使用新数据类型的块,相应生成的代码继续使用您之前指定的替换类型。例如,在文件中
ex_data_type_replacement.h
,块的输出In1和In2以使用替换类型的结构字段出现my_int16
。/ *External inputs (root inport signals with auto storage) */ typedef struct { my_int16 In1; /* '
/In1' */ my_int16 In2; /* ' /In2' */ } ExtU_ex_data_type_replacement_T;
Create Single Point of Definition for Primitive Types
The custom data type aliasesflow_t
和ctrl_T
map to the primitive data type双倍的
。如果您想从双倍的
至单身的
(float
), you must remember to modify theBaseType
property of both金宝appsimulink.Airstype
objects.
To more easily make this change, you can create a金宝appsimulink.numerictype
object and configure both金宝appsimulink.Airstype
引用它的对象。然后,您只需要修改金宝appsimulink.numerictype
目的。一个金宝appsimulink.numerictype
对象使您可以共享数据类型而无需创建数据类型别名。
一个t the command prompt, create a
金宝appsimulink.numerictype
对象表示原始数据类型单身的
。sharedType = Simulink.NumericType; sharedType.DataTypeMode ='单身的';
配置
金宝appsimulink.Airstype
objectsflow_t
和ctrl_T
至acquire an underlying data type from this new object.flow_t。BaseType ='sharedType';ctrl_t.basetype ='sharedType';
在调试tab, select信息叠加>Base Data Types信息叠加>别名数据类型和(seePort Data Types)。更新框图。
模型中的数据类型指标表明别名
flow_t
和ctrl_T
map to the primitive type单身的
。To change this underlying primitive type, you can modify theDataTypeMode
property of the金宝appsimulink.numerictype
目的,sharedType
。
默认情况下,金宝appsimulink.numerictype
object does not cause anothertypedef
statement to appear in the generated code.
If you generate code from the model while the金宝appsimulink.numerictype
object represents the data type单身的
,生成的代码图flow_t
和ctrl_T
至the default金宝appSimulink编码器data type aliasREAL32_T
, which maps to the C data typefloat
。You can replaceREAL32_T
以您更换的方式real_t
,int16_t
, 和boolean_T
(配置参数>代码Generation>数据类型更换)。
Permanently Store Data Type Objects
The金宝appsimulink.numerictype
和金宝appsimulink.Airstype
objects in the base workspace do not persist if you end your current MATLAB®会议。要永久存储这些对象,请考虑将模型迁移到数据字典。看Migrate Models to Use Simulink Data Dictionary。
Create and Maintain Objects Corresponding to Multiple Ctypedef
Statements
To create金宝appsimulink.Airstype
objects for a large number oftypedef
外部C代码中的语句,请考虑使用金宝appsimulink.importexternalctypes
function.