주요 콘텐츠

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

조건부 입력 분기 실행 사용

이 예제는 Switch 및 Multiport Switch 블록이 포함된 모델에 대해 생성된 코드를 최적화하는 방법을 보여줍니다. 모델 구성 파라미터 조건부 입력 분기 실행을 선택하면, Simulink®는 제어 입력이 선택한 제어 입력 및 데이터 입력을 계산하는 블록만 실행합니다. 이 최적화는 실행 속도를 향상시킵니다.

예제 모델

이 예제에서는 스위치 경로가 조건에 따라 실행됩니다. Switch1 제어 입력값이 참인 경우, Switch1Switch1:Path1 분기에 묶인 블록들을 실행합니다. Switch1 제어 입력값이 false인 경우, Switch1Switch1:Path2 분기에 묶인 블록들을 실행합니다. Switch1Switch1:Path2 분기의 블록을 실행하고 Switch2 제어 입력값이 참인 경우, Switch2Switch2:Path1 분기의 블록을 실행합니다. Switch2 제어 입력값이 false인 경우, Switch2Switch2:Path2 분기 내의 블록을 실행합니다. 이 의사 코드는 해당 논리를 보여줍니다.

model='ConditionalInput';
open_system(model);

코드 생성

조건부 입력 분기 실행 파라미터는 기본적으로 활성화되어 있습니다. 이 파라미터를 비활성화하려면 다음 명령줄 API를 입력하십시오.

set_param(model, 'ConditionallyExecuteInputs', 'off');

모델 빌드

slbuild(model)
### Searching for referenced models in model 'ConditionalInput'.
### Total of 1 models to build.
### Starting build procedure for: ConditionalInput
### Successful completion of build procedure for: ConditionalInput

Build Summary

Top model targets:

Model             Build Reason                                         Status                        Build Duration
===================================================================================================================
ConditionalInput  Information cache folder or artifacts were missing.  Code generated and compiled.  0h 0m 4.8029s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 5.2363s

최적화가 적용되지 않은 생성된 코드를 확인합니다. 이 코드 라인들은 ConditionalInput.c 파일에 있습니다.

cfile = fullfile('ConditionalInput_grt_rtw','ConditionalInput.c');
coder.example.extractLines(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void ConditionalInput_step(void)
{
  real_T rtb_Switch1;

  /* Switch: '<Root>/ Switch2' incorporates:
   *  Constant: '<Root>/C_5'
   *  Gain: '<Root>/  G3'
   *  Inport: '<Root>/input'
   *  RelationalOperator: '<Root>/Relational Operator'
   *  Sum: '<Root>/ Sum'
   */
  if (ConditionalInput_U.input >= -5.0) {
    rtb_Switch1 = 3.0 * ConditionalInput_U.input;
  } else {
    rtb_Switch1 = ConditionalInput_U.input - 10.0;
  }

  /* End of Switch: '<Root>/ Switch2' */

  /* Switch: '<Root>/Switch1' incorporates:
   *  Constant: '<Root>/C5'
   *  Inport: '<Root>/input'
   *  RelationalOperator: '<Root>/Relational Operator1'
   */
  if (ConditionalInput_U.input >= 5.0) {
    /* Outport: '<Root>/output' incorporates:
     *  Constant: '<Root>/  C10'
     *  Sum: '<Root>/ Sum1'
     */
    ConditionalInput_Y.output = ConditionalInput_U.input + 10.0;
  } else {
    /* Outport: '<Root>/output' */
    ConditionalInput_Y.output = rtb_Switch1;
  }

  /* End of Switch: '<Root>/Switch1' */
}

생성된 코드에는 Switch2 블록에 대한 if-else 문과 Switch1 블록에 대한 if 문이 포함되어 있습니다. 따라서 Switch1:Path1에 대한 if 문이 참으로 평가되더라도 Switch1:Path2에 대해 생성된 코드는 실행됩니다.

최적화 활성화

  1. 구성 파라미터 대화 상자를 엽니다.

  2. 조건부 입력 분기 실행 파라미터를 선택하십시오. 또는 명령줄 API를 사용하여 최적화를 활성화할 수도 있습니다.

set_param(model, 'ConditionallyExecuteInputs','on');

최적화를 적용하여 코드 생성

slbuild(model)
cfile = fullfile('ConditionalInput_grt_rtw','ConditionalInput.c');
coder.example.extractLines(cfile,'/* Model step', '/* Model initialize', 1, 0);
### Searching for referenced models in model 'ConditionalInput'.
### Total of 1 models to build.
### Starting build procedure for: ConditionalInput
### Successful completion of build procedure for: ConditionalInput

Build Summary

Top model targets:

Model             Build Reason                     Status                        Build Duration
===============================================================================================
ConditionalInput  Generated code was out of date.  Code generated and compiled.  0h 0m 5.0069s

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 5.7337s

/* Model step function */
void ConditionalInput_step(void)
{
  /* Switch: '<Root>/Switch1' incorporates:
   *  Constant: '<Root>/C5'
   *  Constant: '<Root>/C_5'
   *  Inport: '<Root>/input'
   *  RelationalOperator: '<Root>/Relational Operator'
   *  RelationalOperator: '<Root>/Relational Operator1'
   *  Switch: '<Root>/ Switch2'
   */
  if (ConditionalInput_U.input >= 5.0) {
    /* Outport: '<Root>/output' incorporates:
     *  Constant: '<Root>/  C10'
     *  Sum: '<Root>/ Sum1'
     */
    ConditionalInput_Y.output = ConditionalInput_U.input + 10.0;
  } else if (ConditionalInput_U.input >= -5.0) {
    /* Switch: '<Root>/ Switch2' incorporates:
     *  Gain: '<Root>/  G3'
     *  Outport: '<Root>/output'
     */
    ConditionalInput_Y.output = 3.0 * ConditionalInput_U.input;
  } else {
    /* Outport: '<Root>/output' incorporates:
     *  Sum: '<Root>/ Sum'
     *  Switch: '<Root>/ Switch2'
     */
    ConditionalInput_Y.output = ConditionalInput_U.input - 10.0;
  }

  /* End of Switch: '<Root>/Switch1' */
}

생성된 코드에는 if 문이 하나 포함되어 있습니다. Switch1:Path2에 대해 생성된 코드는 if 문이 false로 평가될 때만 실행됩니다.

모델 및 코드 생성 리포트 닫기

bdclose(model)

참고 항목

| |

도움말 항목