Main Content

coder.mapping.api.CodeMapping

用于 C 代码生成的模型数据和函数接口配置

自 R2020b 起

    说明

    代码映射对象和相关函数能够为 Simulink® 模型的数据和函数配置 C 代码生成。对于模型数据元素,代码映射将数据元素与由存储类和存储类属性组成的配置相关联。对于函数,代码映射将入口函数与由函数自定义模板组成的配置相关联。通过为整个模型中的数据元素和函数类别指定默认配置,减少为 C 代码生成而准备模型的工作量。通过单独配置数据元素或函数来覆盖默认配置。对于较小的模型,您可以选择单独配置每个数据元素和函数对于附带 Embedded Coder 字典的模型,您可以将模型元素映射到在该字典中定义的服务接口。

    创建对象

    当您从 Simulink 编辑器的 App 选项卡中选择一个代码生成 App 时,例如 Simulink CoderEmbedded Coder,如果代码映射尚不存在,该 App 会创建一个 coder.mapping.api.CodeMapping 对象。该 App 基于存储在模型活动配置集对象中的代码自定义设置来创建代码映射。配置集对象可以为数据和函数指定内存段。

    通过调用函数 coder.mapping.utils.create 以编程方式创建 coder.mapping.api.CodeMapping 对象。基于活动配置集对象或基于另一个配置集对象的默认内存段和共享实用工具命名规则配置创建一个映射。

    对象函数

    addSignalAdd block output signal to model code mappings
    coder.mapping.api.getGet code mappings for model
    coder.mapping.utils.createCreate code mappings object for configuring data and function interface for C and C++ code generation
    findGet model elements for the category of model code mappings
    getDataDefaultGet default storage class or storage class property setting for model data category
    getDataStoreGet code and calibration configuration from code mappings for local or shared local data store
    getDataTransferGet code configuration from code mappings for signal representing data transfer
    getDeploymentTypeGet deployment type of model
    getFunctionGet code configuration from code mappings for model function
    getFunctionDefaultGet default function customization template or memory section for model functions category
    getInportGet code and calibration configuration from code mappings for root-level inport
    getModelParameterGet code and calibration configuration from code mappings for model parameters
    getOutportGet code and calibration configuration from code mappings for root-level outport
    getSignalGet code and calibration configuration from code mappings for block output signal
    getStateGet code and calibration configuration from code mappings for block state
    removeSignalRemove block output signal from model code mappings
    setDataDefaultSet default storage class and storage class property values for model data category
    setDataStoreConfigure local or shared local data store for code and calibration file (a2l) generation
    setDataTransferSet code configuration in code mappings for signal representing data transfer
    setDeploymentTypeSet deployment type of model
    setFunctionSet coder mapping information for model function
    setFunctionDefaultSet default function customization template and memory section for model functions category
    setInportConfigure root-level inports for code and calibration file (a2l) generation
    setModelParameterConfigure model parameter for code and calibration file (a2l) generation
    setOutportConfigure root-level outport for code and calibration file (a2l) generation
    setSignalConfigure block signal data for code and calibration file (a2l) generation
    setStateConfigure block states for code and calibration file (a2l) generation

    示例

    全部折叠

    使用编程接口创建和使用 Simulink 模型的代码映射对象。

    加载模型 NoCoderMapping

    simulinkModel = "NoCoderMapping";
    load_system(simulinkModel);

    使用 try-catch 代码块确定该模型是否存在代码映射对象。在 try 模块中,尝试使用函数 coder.mapping.api.get 检索现有对象。在 catch 代码块中,使用函数 coder.mapping.utils.create 创建一个新代码映射对象。将代码映射对象存储在变量 codeMapObj 中。

    添加打印消息以显示模型是否具有现有代码映射对象。

    try
      codeMapObj = coder.mapping.api.get(simulinkModel);
              fprintf("" + ...
                " ========================================================\n" + ...
                " The model already had code mappings.\n" + ...
                " ========================================================\n");
    catch
              fprintf("" + ...
                " ==========================================\n" + ...
                " The model does not have code mappings.\n" + ...
                " Creating new code mappings for the model.\n" + ...
                " ==========================================\n");
      codeMapObj = coder.mapping.utils.create(simulinkModel);
    end
     ==========================================
     The model does not have code mappings.
     Creating new code mappings for the model.
     ==========================================
    

    检索模型的输入端口 inport_1 的存储类。

    getInport(codeMapObj,"inport_1","StorageClass")
    ans = 
    'Auto'
    

    将输入端口 inport_1 的存储类设置为 ExportedGlobal

    setInport(codeMapObj,"inport_1",StorageClass="ExportedGlobal")

    使用同一个 try-catch 代码块查看函数 coder.mapping.api.get 现在如何能够检索模型的现有代码映射。

    try
      codeMapObj = coder.mapping.api.get(simulinkModel);
              fprintf("" + ...
                " ========================================================\n" + ...
                " The model already had code mappings.\n" + ...
                " ========================================================\n");
    catch
              fprintf("" + ...
                " ==========================================\n" + ...
                " The model does not have code mappings.\n" + ... 
                " Creating new code mappings for the model.\n" + ...
                " ==========================================\n");
      codeMapObj = coder.mapping.utils.create(simulinkModel);
    end
     ========================================================
     The model already had code mappings.
     ========================================================
    

    检索模型的输入端口 inport_1 的存储类。请注意,这是您之前设置的存储类。

    getInport(codeMapObj,"inport_1","StorageClass")
    ans = 
    'ExportedGlobal'
    

    关闭模型而不保存它。

    close_system(simulinkModel,false)

    使用编程接口检索和使用 Simulink 模型的不同类型的代码映射对象。

    加载模型 CoderMapAPI

    codeMapModel = "CoderMapAPI";
    load_system(codeMapModel);

    使用函数 coder.mapping.api.get 检索和存储此模型的 Simulink® Coder™ 和 Embedded Coder® 映射对象。

    simCodeMapObj = coder.mapping.api.get(codeMapModel,"SimulinkCoderC");
    eCodeMapObj = coder.mapping.api.get(codeMapModel,"EmbeddedCoderC");

    存储所有根级输入端口名称。

    in_port_names = "in_port_"+(1:4)'
    in_port_names = 4x1 string
        "in_port_1"
        "in_port_2"
        "in_port_3"
        "in_port_4"
    
    

    对于每个代码映射对象,定义一个设置输入端口存储类的内联函数。使用 arrayfun 函数将 Simulink 代码映射对象的所有根级输入端口的存储类设置为 ImportedExtern,并将 Embedded Coder 映射对象的根级输入端口的存储类设置为 ImportedExternPointer

    configSimCoderPortStoreClassFcn = @(port_name) setInport(simCodeMapObj,port_name,StorageClass="ImportedExtern");
    configECoderPortStoreClassFcn = @(port_name) setInport(eCodeMapObj,port_name,StorageClass="ImportedExternPointer");
    arrayfun(configSimCoderPortStoreClassFcn,in_port_names);
    arrayfun(configECoderPortStoreClassFcn,in_port_names);

    对于每个代码映射对象,定义一个显示输入端口存储类的内联函数。

    dispSimCoderPortStoreClassFcn = @(port_name) disp(getInport(simCodeMapObj,port_name,"StorageClass"));
    dispECoderPortStoreClassFcn = @(port_name) disp(getInport(eCodeMapObj,port_name,"StorageClass"));

    显示每个代码映射对象的根级输入端口的存储类。

    arrayfun(dispSimCoderPortStoreClassFcn,in_port_names);
    ImportedExtern
    ImportedExtern
    ImportedExtern
    ImportedExtern
    
    arrayfun(dispECoderPortStoreClassFcn,in_port_names);
    ImportedExternPointer
    ImportedExternPointer
    ImportedExternPointer
    ImportedExternPointer
    

    具有 ImportedExternImportedExternPointer 存储类的根级输入端口在生成的模型私有头文件中的单独部分中声明。存储两个系统目标文件的头文件名,以便您可以在两个头文件中观察这些部分。

    priv_h_simcoder_file = fullfile(codeMapModel+"_grt_rtw",codeMapModel+"_private.h")
    priv_h_simcoder_file = 
    "CoderMapAPI_grt_rtw/CoderMapAPI_private.h"
    
    priv_h_ecoder_file = fullfile(codeMapModel+"_ert_rtw",codeMapModel+"_private.h")
    priv_h_ecoder_file = 
    "CoderMapAPI_ert_rtw/CoderMapAPI_private.h"
    

    将模型目标文件设置为 grt.tlc,并从模型生成代码。

    set_param(codeMapModel,SystemTargetFile="grt.tlc")
    evalc("slbuild(codeMapModel)");

    以下是 grt.tlc 头文件 priv_h_simcoder_file 中根级输入端口的声明:

    /* Imported (extern) block signals */
    extern real_T in_port_1;               /* '<Root>/in_port_1' */
    extern real_T in_port_2;               /* '<Root>/in_port_2' */
    extern real_T in_port_3;               /* '<Root>/in_port_3' */
    extern real_T in_port_4;               /* '<Root>/in_port_4' */
    

    根级输入端口在 ImportedExtern 部分中声明。

    要打开头文件,请在 MATLAB® 命令行窗口中输入以下命令:

    edit(priv_h_simcoder_file)
    

    现在将模型目标文件设置为 ert.tlc,并从模型生成代码。

    set_param(codeMapModel,SystemTargetFile="ert.tlc")
    evalc("slbuild(codeMapModel)");

    以下是 ert.tlc 头文件 priv_h_ecoder_file 中根级输入端口的声明:

    /* Imported (extern) pointer block signals */
    extern real_T *in_port_1;              /* '<Root>/in_port_1' */
    extern real_T *in_port_2;              /* '<Root>/in_port_2' */
    extern real_T *in_port_3;              /* '<Root>/in_port_3' */
    extern real_T *in_port_4;              /* '<Root>/in_port_4' */
    

    此头文件中的根级输入端口在 ImportedExternPointer 部分中声明。

    要打开头文件,请在 MATLAB 命令行窗口中输入以下命令:

    edit(priv_h_ecoder_file)
    

    版本历史记录

    在 R2020b 中推出

    全部展开