Main Content

coder.mapping.api.CodeMapping

C コード生成用のモデル データおよび関数インターフェイスの構成

R2020b 以降

    説明

    コード マッピング オブジェクトおよび関連する関数を使用すると、Simulink® モデルのデータおよび関数の C コード生成を構成できます。モデル データ要素について、コード マッピングは、データ要素をストレージ クラスとストレージ クラス プロパティから成る構成に関連付けます。関数について、コード マッピングは、エントリポイント関数を関数カスタマイズ テンプレートから成る構成に関連付けます。モデル全体のデータ要素および関数のカテゴリに対して既定の構成を指定することにより、C コード生成用にモデルを準備する作業を軽減します。データ要素または関数を個別に構成することにより、既定の構成をオーバーライドします。小規模なモデルでは、各データ要素および関数を個別に構成するよう選択できます。Embedded Coder ディクショナリと接続されているモデルでは、そのディクショナリに定義されているサービス インターフェイスにモデル要素をマッピングできます。

    作成

    Simulink エディターの [アプリ] タブから [Simulink Coder] アプリや [Embedded Coder] アプリなどのコード生成アプリを選択すると、コード マッピングがまだ存在しない場合は coder.mapping.api.CodeMapping オブジェクトが作成されます。コード マッピングは、モデルのアクティブなコンフィギュレーション セット オブジェクトに格納されているコードのカスタマイズ設定に基づいて作成されます。コンフィギュレーション セット オブジェクトでは、データと関数に対してメモリ セクションを指定できます。

    coder.mapping.api.CodeMapping オブジェクトをプログラムで作成するには、関数 coder.mapping.utils.create を呼び出します。マッピングは、アクティブなコンフィギュレーション セット オブジェクトに基づいて、または別のコンフィギュレーション セット オブジェクトのメモリ セクションと共有ユーティリティに関する既定の命名規則構成に基づいて作成します。

    オブジェクト関数

    addSignalブロックの出力信号をモデル コード マッピングに追加する
    coder.mapping.api.getモデルのコード マッピングの取得
    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
    getFunctionモデル関数に対するコード マッピングからのコード構成の取得
    getFunctionDefaultモデル関数カテゴリに対する既定の関数カスタマイズ テンプレートまたはメモリ セクションの取得
    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
    removeSignalブロックの出力信号をモデル コード マッピングから削除する
    setDataDefaultSet default storage class and storage class property values for model data category
    setDataStoreコード生成とキャリブレーション ファイル (a2l) 生成用のローカル データ ストアまたは共有ローカル データ ストアを設定する
    setDataTransferSet code configuration in code mappings for signal representing data transfer
    setDeploymentTypeSet deployment type of model
    setFunctionモデル関数のコーダー マッピング情報を設定する
    setFunctionDefaultモデル関数カテゴリの既定の関数カスタマイズ テンプレートとメモリ セクションを設定する
    setInportコードとキャリブレーション ファイル (a2l) の生成用のルートレベルの Inport の構成
    setModelParameterコード生成とキャリブレーション ファイル (a2l) 生成用のモデル パラメーターを設定する
    setOutportConfigure root-level outport for code and calibration file (a2l) generation
    setSignalコード生成とキャリブレーション ファイル (a2l) 生成用のブロック信号データを設定する
    setStateコードとキャリブレーション ファイル (a2l) の生成用のブロック状態の設定

    すべて折りたたむ

    プログラム インターフェイスを使用して、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
    

    ImportedExtern および ImportedExternPointer ストレージ クラスをもつルートレベルの入力端子は、モデルの生成されたプライベート ヘッダー ファイル内の別個のセクションで宣言されます。両方のヘッダー ファイルでこれらのセクションを確認できるように、2 つのシステム ターゲット ファイルのヘッダー ファイル名を保存します。

    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 で導入

    すべて展開する