주요 콘텐츠

이 페이지는 기계 번역을 사용하여 번역되었습니다. 영어 원문을 보려면 여기를 클릭하십시오.

coder.mapping.api.CodeMapping

R2026b

C 코드 생성을 위한 모델 데이터 및 함수 인터페이스 구성

    설명

    코드 매핑 객체와 관련 함수를 사용하면 Simulink® 모델의 데이터 및 함수에 대한 C 코드 생성을 구성할 수 있습니다. 모델 데이터 요소의 경우, 코드 매핑을 통해 데이터 요소를 스토리지 클래스 및 스토리지 클래스 속성으로 구성된 구성과 연결합니다. 함수의 경우, 코드 매핑은 진입점 함수를 함수 사용자 지정 템플릿으로 구성된 구성과 연결합니다. 모델 전반에 걸쳐 데이터 요소 및 함수의 범주에 대한 기본 구성을 지정함으로써 C 코드 생성을 위한 모델 준비에 드는 수고를 줄일 수 있습니다. 데이터 요소 또는 함수를 개별적으로 구성하여 기본 구성을 재정의할 수 있습니다. 더 작은 모델의 경우, 각 데이터 요소과 함수을 개별적으로 구성할 수 있습니다. Embedded Coder 사전이 첨부된 모델의 경우, 모델 요소를 해당 사전에 정의된 서비스 인터페이스에 매핑할 수 있습니다.

    생성

    Simulink 편집기의 앱 탭에서 Simulink Coder나 Embedded Coder와 같은 코드 생성 앱을 선택하면, 코드 매핑이 아직 존재하지 않는 경우 해당 앱이 coder.mapping.api.CodeMapping 객체를 생성합니다. 이 앱은 모델의 활성 구성 세트 객체에 저장된 코드 사용자 지정 설정을 기반으로 코드 매핑을 생성합니다. 구성 세트 객체는 데이터 및 함수를 위한 메모리 섹션을 지정할 수 있습니다.

    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
    getDeploymentType모델의 배포 유형 가져오기
    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
    getModelVariantControlGet code configuration from code mappings for Simulink.VariantControl object
    getModelVariantVariableGet code configuration from code mappings for Simulink.VariantVariable object
    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
    setDeploymentType모델의 배포 유형 설정
    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
    setModelVariantControlConfigure Simulink.VariantControl object for code 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 = 4×1 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 스토리지 클래스를 갖는 루트 레벨 입력 포트는 모델의 생성된 비공개 헤더 파일 내 별도의 섹션에 선언됩니다. 두 시스템 타깃 파일의 헤더 파일 이름을 저장해 두어, 두 헤더 파일 모두에서 해당 섹션을 확인할 수 있도록 하십시오.

    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에 개발됨

    모두 확장