このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
生成されたコードでの関数の再利用
この例では、Atomic サブシステムを再利用可能なコードの生成用に構成する方法を示します。サブシステム用に生成されたコードを Atomic サブシステムとして実行するように指定するには、[ブロック パラメーター] ダイアログ ボックスで [Atomic サブシステムとして扱う] パラメーターを選択します。このパラメーターにより、[コード生成] タブの [関数のパッケージ化] パラメーターが有効になります。[関数のパッケージ化] パラメーターには次の 4 つの設定があります。
Inline:サブシステム コードのインライン化Nonreusable function:I/O がグローバル データとして渡される関数Reusable function:I/O が関数の引数として渡される関数Auto:コンテキストに基づいて Simulink Coder により最適化
Reusable function および Auto に設定すると、コード ジェネレーターでサブシステム コードの再利用が許可されます。Reusable function および Nonreusable function に設定すると、[関数名オプション]、[関数名]、[ファイル名オプション] のパラメーターが有効になります。
Embedded Coder® のライセンスがある場合は、引数を受け入れるように再利用不可能なサブシステムを構成できます。
モデル例
GeneratedCodeFunctionReuse モデルには、SS1 および SS2 という 2 つの同一のサブシステムが含まれています。これらのサブシステムでは、[関数のパッケージ化] パラメーターが Reusable function に設定され、[関数名] パラメーターが myfun になっています。これらのサブシステムはパラメーター化されたマスク サブシステムです。マスク サブシステムの内容を表示するには、サブシステム ブロックを右クリックし、[マスク]、[マスク内を表示] を選択します。
model = 'GeneratedCodeFunctionReuse';
open_system(model);

コードの生成と検査
Simulink Coder アプリまたは Embedded Coder アプリを開きます。次に、コードを生成して検査します。
slbuild(model)
### Searching for referenced models in model 'GeneratedCodeFunctionReuse'. ### Total of 1 models to build. ### Starting build procedure for: GeneratedCodeFunctionReuse ### Successful completion of build procedure for: GeneratedCodeFunctionReuse Build Summary Top model targets: Model Build Reason Status Build Duration ============================================================================================================================= GeneratedCodeFunctionReuse Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 8.6551s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 9.218s
cfile=fullfile(pwd, 'GeneratedCodeFunctionReuse_grt_rtw', 'GeneratedCodeFunctionReuse.c'); coder.example.extractLines(cfile, '/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void GeneratedCodeFunctionReuse_step(void)
{
/* Outputs for Atomic SubSystem: '<Root>/SS1' */
/* Inport: '<Root>/In1' incorporates:
* Inport: '<Root>/In2'
*/
myfun(GeneratedCodeFunctionReuse_U.In1, GeneratedCodeFunctionReuse_U.In2,
GeneratedCodeFunctionReuse_P.T1Data,
GeneratedCodeFunctionReuse_P.T1Break, &GeneratedCodeFunctionReuse_B.SS1);
/* End of Outputs for SubSystem: '<Root>/SS1' */
/* Outport: '<Root>/Out1' */
GeneratedCodeFunctionReuse_Y.Out1 =
GeneratedCodeFunctionReuse_B.SS1.LookupTable;
/* Outputs for Atomic SubSystem: '<Root>/SS2' */
/* Inport: '<Root>/In1' incorporates:
* Inport: '<Root>/In2'
*/
myfun(GeneratedCodeFunctionReuse_U.In1, GeneratedCodeFunctionReuse_U.In2,
GeneratedCodeFunctionReuse_P.T2Data,
GeneratedCodeFunctionReuse_P.T2Break, &GeneratedCodeFunctionReuse_B.SS2);
/* End of Outputs for SubSystem: '<Root>/SS2' */
/* Outport: '<Root>/Out2' */
GeneratedCodeFunctionReuse_Y.Out2 =
GeneratedCodeFunctionReuse_B.SS2.LookupTable;
}
モデルのステップ関数に、再利用可能な関数 myfun の呼び出しが 2 つあります。マスク パラメーター T1Break、T1Data、T2Break および T2Data は、関数の引数です。
[関数のパッケージ化] パラメーターを Inline に変更します。
set_param('GeneratedCodeFunctionReuse/SS1','RTWSystemCode','Inline') set_param('GeneratedCodeFunctionReuse/SS2','RTWSystemCode','Inline')
コードを生成して検査します。
slbuild(model)
### Searching for referenced models in model 'GeneratedCodeFunctionReuse'. ### Total of 1 models to build. ### Starting build procedure for: GeneratedCodeFunctionReuse ### Successful completion of build procedure for: GeneratedCodeFunctionReuse Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================================= GeneratedCodeFunctionReuse Generated code was out of date. Code generated and compiled. 0h 0m 6.0203s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 6.4086s
cfile=fullfile(pwd, 'GeneratedCodeFunctionReuse_grt_rtw', 'GeneratedCodeFunctionReuse.c'); coder.example.extractLines(cfile, '/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void GeneratedCodeFunctionReuse_step(void)
{
real_T Out1_tmp;
/* Outputs for Atomic SubSystem: '<Root>/SS2' */
/* Outputs for Atomic SubSystem: '<Root>/SS1' */
/* Sum: '<S1>/Sum' incorporates:
* Inport: '<Root>/In1'
* Inport: '<Root>/In2'
* Sum: '<S2>/Sum'
*/
Out1_tmp = GeneratedCodeFunctionReuse_U.In1 + GeneratedCodeFunctionReuse_U.In2;
/* End of Outputs for SubSystem: '<Root>/SS2' */
/* Outport: '<Root>/Out1' incorporates:
* Lookup_n-D: '<S1>/Lookup Table'
* Sum: '<S1>/Sum'
*/
GeneratedCodeFunctionReuse_Y.Out1 = look1_binlx(Out1_tmp,
GeneratedCodeFunctionReuse_P.T1Break, GeneratedCodeFunctionReuse_P.T1Data,
10U);
/* End of Outputs for SubSystem: '<Root>/SS1' */
/* Outputs for Atomic SubSystem: '<Root>/SS2' */
/* Outport: '<Root>/Out2' incorporates:
* Lookup_n-D: '<S2>/Lookup Table'
*/
GeneratedCodeFunctionReuse_Y.Out2 = look1_binlx(Out1_tmp,
GeneratedCodeFunctionReuse_P.T2Break, GeneratedCodeFunctionReuse_P.T2Data,
10U);
/* End of Outputs for SubSystem: '<Root>/SS2' */
}
モデルのステップ関数では、サブシステム コードはインライン化されます。
[関数のパッケージ化] パラメーターを Nonreusable function に変更します。SS2 の場合は、[関数名] パラメーターを myfun2 に変更します。
set_param('GeneratedCodeFunctionReuse/SS1','RTWSystemCode','Nonreusable function') set_param('GeneratedCodeFunctionReuse/SS2','RTWSystemCode','Nonreusable function') set_param('GeneratedCodeFunctionReuse/SS2','RTWFcnName','myfun2')
コードを生成して検査します。
slbuild(model)
### Searching for referenced models in model 'GeneratedCodeFunctionReuse'. ### Total of 1 models to build. ### Starting build procedure for: GeneratedCodeFunctionReuse ### Successful completion of build procedure for: GeneratedCodeFunctionReuse Build Summary Top model targets: Model Build Reason Status Build Duration ========================================================================================================= GeneratedCodeFunctionReuse Generated code was out of date. Code generated and compiled. 0h 0m 5.1267s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 5.5093s
cfile=fullfile(pwd, 'GeneratedCodeFunctionReuse_grt_rtw', 'GeneratedCodeFunctionReuse.c'); coder.example.extractLines(cfile, '/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void GeneratedCodeFunctionReuse_step(void)
{
/* Outputs for Atomic SubSystem: '<Root>/SS1' */
myfun();
/* End of Outputs for SubSystem: '<Root>/SS1' */
/* Outputs for Atomic SubSystem: '<Root>/SS2' */
myfun2();
/* End of Outputs for SubSystem: '<Root>/SS2' */
}
モデルのステップ関数に、関数 myfun と myfun2 の呼び出しが含まれています。これらの関数には void-void インターフェイスがあります。
[関数のパッケージ化] パラメーターを Auto に変更します。
set_param('GeneratedCodeFunctionReuse/SS1','RTWSystemCode','Auto') set_param('GeneratedCodeFunctionReuse/SS2','RTWSystemCode','Auto')
auto 設定では、Simulink Coder は最適な形式を選択します。このモデルでは、最適な形式は再利用可能な関数です。