使用条件输入分支执行
此示例说明如何为包含 Switch 和 Multiport Switch 模块的模型优化生成代码。当您选择模型配置参数条件输入分支执行时,Simulink® 仅执行计算控制输入以及控制输入选择的数据输入的模块。此优化会提高执行速度。
示例模型
在此示例中,开关路径有条件地执行。如果 Switch1 控制输入为 true,则 Switch1 执行分组在 Switch1:Path1 分支中的模块。如果 Switch1 控制输入为 false,则 Switch1 执行分组在 Switch1:Path2 分支中的模块。如果 Switch1 执行 Switch1:Path2 分支中的模块,并且 Switch2 控制输入为 true,则 Switch2 执行 Switch2:Path1 分支中的模块。如果 Switch2 控制输入为 false,则 Switch2 执行 Switch2: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 9.1805s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.093s
查看生成的代码而不进行优化。这些代码行位于 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 语句的计算结果为 true,为 Switch1:Path2 生成的代码也会执行。
启用优化
打开“配置参数”对话框。
选择条件输入分支执行参数。您也可以使用命令行 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 9.4028s
1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 10.93s
/* 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 语句。仅在 if 语句的计算结果为 false 时,为 Switch1:Path2 生成的代码才会执行。
关闭模型和代码生成报告
bdclose(model)
另请参阅
条件输入分支执行 | Switch | Multiport Switch