サブシステムに対する生成コード関数のパッケージ化の指定
この例では、コード ジェネレーターが 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 に設定されます。これらのサブシステムはパラメーター化されたマスク サブシステムです。マスク サブシステムの内容を表示するには、Subsystem ブロックを右クリックし、[マスクを編集]、[マスク内部を検索] を選択します。
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 13.164s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 14.986s
生成されたファイル GeneratedCodeFunctionReuse.c を検査します。
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) cfile=fullfile(pwd, 'GeneratedCodeFunctionReuse_grt_rtw', 'GeneratedCodeFunctionReuse.c'); coder.example.extractLines(cfile, '/* Model step', '/* Model initialize', 1, 0); % In the model step function, the subsystem code is inlined.
### 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 4.8351s
1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 5.4962s
/* 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) cfile=fullfile(pwd, 'GeneratedCodeFunctionReuse_grt_rtw', 'GeneratedCodeFunctionReuse.c'); coder.example.extractLines(cfile, '/* Model step', '/* Model initialize', 1, 0); % The model step function contains calls to the functions |myfun| and |myfun2|. % These functions have a void-void interface.
### 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 4.4021s
1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 4.911s
/* 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' */
}
[関数のパッケージ化] パラメーターを Auto に変更します。
set_param('GeneratedCodeFunctionReuse/SS1','RTWSystemCode','Auto') set_param('GeneratedCodeFunctionReuse/SS2','RTWSystemCode','Auto')
auto 設定では、Simulink Coder は最適な形式を選択します。このモデルでは、最適な形式は再利用可能な関数です。