Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

条件付き入力分岐実行の使用

この例では、Switch ブロックおよび Multiport Switch ブロックを含むモデルの生成されたコードの最適化方法を示します。モデル コンフィギュレーション パラメーター [条件付き入力分岐実行] を選択すると、Simulink® は制御入力と、この制御入力によって選択されたデータ入力を計算するブロックのみを実行します。この最適化によって実行速度が向上します。

モデル例

この例では、スイッチ パスは条件付きで実行されます。Switch1 の制御入力が true である場合、Switch1Switch1:Path1 分岐にグループ化されたブロックを実行します。Switch1 の制御入力が false である場合、Switch1Switch1:Path2 分岐にグループ化されたブロックを実行します。Switch1Switch1:Path2 分岐内のブロックを実行し、Switch2 の制御入力が true である場合、Switch2Switch2:Path1 分岐内のブロックを実行します。Switch2 の制御入力が false である場合、Switch2Switch2:Path2 分岐内のブロックを実行します。次の疑似コードでこのロジックを示します。

model='ConditionalInput';
open_system(model);

コードの生成

[条件付き入力分岐実行] パラメーターは、既定の設定でオンになっています。次のコマンド ライン API を入力してこのパラメーターをオフにします。

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

モデルのビルド

slbuild(model)
### Starting build procedure for: ConditionalInput
### Successful completion of build procedure for: ConditionalInput

Build Summary

Top model targets built:

Model             Action                        Rebuild Reason                                    
==================================================================================================
ConditionalInput  Code generated and compiled.  Code generation information file does not exist.  

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

最適化を行わない生成コードを表示します。以下のコード行は 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:Path2 の生成されたコードは、Switch1:Path1if ステートメントが true であると評価されたとしても実行されます。

最適化の有効化

  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);
### Starting build procedure for: ConditionalInput
### Successful completion of build procedure for: ConditionalInput

Build Summary

Top model targets built:

Model             Action                        Rebuild Reason                   
=================================================================================
ConditionalInput  Code generated and compiled.  Generated code was out of date.  

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

/* 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 ステートメントが 1 つ含まれます。Switch1:Path2 の生成されたコードが実行されるのは、if ステートメントが false と評価される場合のみです。

モデルおよびコード生成レポートを閉じる

bdclose(model)

参考

| |

関連するトピック