В этом примере показано, как оптимизировать сгенерированный код для модели, содержащей блоки коммутаторов и мультипортовых коммутаторов. При выборе параметра конфигурации модели Conditional input branch execution 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='rtwdemo_condinput';
open_system(model);

Параметр выполнения условного входного ответвления включен по умолчанию. Введите следующий API командной строки, чтобы отключить параметр.
set_param(model, 'ConditionallyExecuteInputs', 'off');
Создайте временную папку для процесса сборки и проверки.
currentDir=pwd; [~,cgDir]=rtwdemodir();
Создайте модель.
slbuild(model)
### Starting build procedure for: rtwdemo_condinput ### Successful completion of build procedure for: rtwdemo_condinput Build Summary Top model targets built: Model Action Rebuild Reason ================================================================================================== rtwdemo_condinput 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 7.064s
Просмотр созданного кода без оптимизации. Эти строки кода находятся в rtwdemo_condinput.c файл.
cfile = fullfile(cgDir,'rtwdemo_condinput_grt_rtw','rtwdemo_condinput.c'); rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void rtwdemo_condinput_step(void)
{
/* Switch: '<Root>/ Switch2' incorporates:
* Constant: '<Root>/C_5'
* Inport: '<Root>/input'
* RelationalOperator: '<Root>/Relational Operator'
*/
if (rtwdemo_condinput_U.input >= -5.0) {
/* Switch: '<Root>/Switch1' incorporates:
* Gain: '<Root>/ G3'
*/
rtwdemo_condinput_Y.output = 3.0 * rtwdemo_condinput_U.input;
} else {
/* Switch: '<Root>/Switch1' incorporates:
* Constant: '<Root>/ C_10'
* Sum: '<Root>/ Sum'
*/
rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + -10.0;
}
/* End of Switch: '<Root>/ Switch2' */
/* Switch: '<Root>/Switch1' incorporates:
* Constant: '<Root>/C5'
* Inport: '<Root>/input'
* RelationalOperator: '<Root>/Relational Operator1'
*/
if (rtwdemo_condinput_U.input >= 5.0) {
/* Switch: '<Root>/Switch1' incorporates:
* Constant: '<Root>/ C10'
* Outport: '<Root>/output'
* Sum: '<Root>/ Sum1'
*/
rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + 10.0;
}
/* End of Switch: '<Root>/Switch1' */
}
Созданный код содержит if-else заявление для Switch2 блок и if заявление для Switch1 блок. Поэтому созданный код для Switch1:Path2 выполняется, даже если if оператор для Switch1:Path1 возвращает значение true.
Откройте диалоговое окно «Параметры конфигурации».
Выберите параметр выполнения условного входного ответвления. Кроме того, для включения оптимизации можно использовать API командной строки.
set_param(model, 'ConditionallyExecuteInputs','on');
slbuild(model) cfile = fullfile(cgDir,'rtwdemo_condinput_grt_rtw','rtwdemo_condinput.c'); rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
### Starting build procedure for: rtwdemo_condinput
### Successful completion of build procedure for: rtwdemo_condinput
Build Summary
Top model targets built:
Model Action Rebuild Reason
=================================================================================
rtwdemo_condinput 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 5.986s
/* Model step function */
void rtwdemo_condinput_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 (rtwdemo_condinput_U.input >= 5.0) {
/* Outport: '<Root>/output' incorporates:
* Constant: '<Root>/ C10'
* Sum: '<Root>/ Sum1'
*/
rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + 10.0;
} else if (rtwdemo_condinput_U.input >= -5.0) {
/* Switch: '<Root>/ Switch2' incorporates:
* Gain: '<Root>/ G3'
* Outport: '<Root>/output'
*/
rtwdemo_condinput_Y.output = 3.0 * rtwdemo_condinput_U.input;
} else {
/* Outport: '<Root>/output' incorporates:
* Constant: '<Root>/ C_10'
* Sum: '<Root>/ Sum'
* Switch: '<Root>/ Switch2'
*/
rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + -10.0;
}
/* End of Switch: '<Root>/Switch1' */
}
Созданный код содержит один if заявление. Созданный код для Switch1:Path2 выполняется только в том случае, если if оператор получает значение false.
bdclose(model) rtwdemoclean; cd(currentDir)
Выполнение ветви условного ввода | Мультипортовый коммутатор | Выключатель