Main Content

替换和重命名数据类型以符合编码标准

默认情况下,generated code uses data type aliases such asreal_tint32_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.hin your current folder. This file represents a header file in your existing code that defines custom data type aliases by usingtypedefstatements.

#include  typedef double my_dblprecision;Typedef简短my_int16;typedef bool my_bool;

探索示例模型和默认生成的代码

  1. 打开示例模型ex_data_type_replacement

    Open_System(FullFile(Docroot,'工具箱',“ ecoder','examples',...'ex_data_type_replacement'))

  2. 造型tab, clickModel Data Editor

  3. In the Model Data Editor, inspect theSignals标签。

  4. 设置更改视图drop-down list to代码。在数据表中Storage Class列显示了命名的信号线,例如tempuse the storage class出口global。使用此设置,信号线以生成的代码作为单独的全局变量出现。

  5. 设置更改视图drop-down list toDesign

  6. 更新框图。

    Simulink assigns a specific data type to each signal that uses an inherited data type setting such asInherit: Inherit via internal rule

  7. 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 typesint16,双倍的, 和boolean。You can also see these data types in the model.

  8. 从模型生成代码。

  9. 在代码生成报告中,检查共享实用程序文件rtwtypes.h。代码使用typedef通过使用标准来重命名原始C数据类型的语句金宝app®代码r™别名。例如,代码重命名原始类型双倍的by using the aliasreal_t

    typedef double real_t;

  10. Inspect the fileex_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.hinstead of the default type names.

  1. 一个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 theDataScopeproperty to“进口”HeaderFileproperty to'my_types.h'。With these settings, the code generator does not create atypedefstatement for each object, but instead the generated code reuses the statements frommy_types.h通过包括(#include) the file.

  2. 在模型中,在“配置参数”对话框中,在代码Generation>数据类型更换窗格,选择Replace data type names in the generated code

  3. Specify the options in theReplacement Namecolumn according to the table.

    Simulink Name Replacement Name
    双倍的 my_dblPrecision
    int16 my_int16
    boolean my_bool

  4. 从模型生成代码。

  5. In the code generation report, inspect the filertwtypes.h。现在,代码使用#includedirective 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 * /

  6. Inspect the fileex_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.Airstypeobjects to represent these custom data type aliases. Use the objects to set the data types of data items in the model.

  1. In the Model Data EditorData Type列,使用金宝appsimulink.Airstype对象设置这些块输出的数据类型。您可以在模型中选择一个信号行,该信号线突出显示了模型数据编辑器中的相应行,也可以通过使用该模型数据编辑器中的信号来搜索信号过滤内容box.

    Block Output Data Type
    Flow Setpoint flow_t
    一个dd2(which feeds the相比block) difftemp_t

  2. 使用模型数据编辑器Inports/Outports选项卡以设置第三的数据类型企业块到flow_t

  3. 造型tab, underDesign, clickProperty Inspector

  4. Select the block labeled Flow Controller. In the Property Inspector, click打开至open the block dialog box.

  5. 数据类型tab, set总输出ctrl_T。Click好的

    With these data type settings, some of the named signals, such astemp流蛋白,使用唤起现实数量的数据类型,例如液体流量和温度。

  6. 一个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流蛋白flowSetPtuse the primitive data type双倍的,因此数据类型别名flow_tmaps to双倍的

  7. 更新框图。

    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

  8. 从模型生成代码。

  9. 文件ex_data_type_replacement_types.hdefines the new data typesflow_t,difftemp_t, 和ctrl_Tas aliases ofmy_dblPrecisionmy_int16

    typedef my_dblprecision flow_t;typedef my_int16 difftemp_t;typedef my_dblprecision ctrl_t;

  10. In the fileex_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' */

  11. 对于不使用新数据类型的块,相应生成的代码继续使用您之前指定的替换类型。例如,在文件中ex_data_type_replacement.h,块的输出In1In2以使用替换类型的结构字段出现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_tctrl_Tmap to the primitive data type双倍的。如果您想从双倍的单身的(float), you must remember to modify theBaseTypeproperty of both金宝appsimulink.Airstypeobjects.

To more easily make this change, you can create a金宝appsimulink.numerictypeobject and configure both金宝appsimulink.Airstype引用它的对象。然后,您只需要修改金宝appsimulink.numerictype目的。一个金宝appsimulink.numerictype对象使您可以共享数据类型而无需创建数据类型别名。

  1. 一个t the command prompt, create a金宝appsimulink.numerictype对象表示原始数据类型单身的

    sharedType = Simulink.NumericType; sharedType.DataTypeMode ='单身的';

  2. 配置金宝appsimulink.Airstypeobjectsflow_tctrl_T至acquire an underlying data type from this new object.

    flow_t。BaseType ='sharedType';ctrl_t.basetype ='sharedType';

  3. 调试tab, select信息叠加>Base Data Types信息叠加>别名数据类型和(seePort Data Types)。更新框图。

    模型中的数据类型指标表明别名flow_tctrl_Tmap to the primitive type单身的。To change this underlying primitive type, you can modify theDataTypeModeproperty of the金宝appsimulink.numerictype目的,sharedType

默认情况下,金宝appsimulink.numerictypeobject does not cause anothertypedefstatement to appear in the generated code.

If you generate code from the model while the金宝appsimulink.numerictypeobject represents the data type单身的,生成的代码图flow_tctrl_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.Airstypeobjects 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 CtypedefStatements

To create金宝appsimulink.Airstypeobjects for a large number oftypedef外部C代码中的语句,请考虑使用金宝appsimulink.importexternalctypesfunction.

相关话题