Этот пример показывает, как оптимизировать сгенерированный код для модели, которая содержит Переключатель и Многопортовые блоки switch. Когда вы выбираете образцовое выполнение ответвления входа Conditional параметра конфигурации, Simulink выполняет только блоки, которые вычисляют вход управления и ввод данных, который выбирает вход управления. Эта оптимизация улучшает скорость выполнения.
В этом примере условно выполняются пути к переключателю. Если вход управления Switch1 верен, Switch1 выполняет блоки, сгруппированные в ответвлении Switch1:Path1. Если вход управления Switch1 является ложным, Switch1 выполняет блоки, сгруппированные в ответвлении Switch1:Path2. Если Switch1 выполняет блоки в ответвлении Switch1:Path2, и вход управления Switch2 верен, Switch2 выполняет блоки в ответвлении Switch2:Path1. Если вход управления Switch2 является ложным, Switch2 выполняет блоки в ответвлении Switch2:Path2. Псевдо код показывает эту логику.
model='rtwdemo_condinput';
open_system(model);

Параметр выполнения ответвления входа Conditional включен по умолчанию. Введите следующий API командной строки, чтобы выключить параметр.
set_param(model, 'ConditionallyExecuteInputs', 'off');
Создайте временную папку для сборки и инспекционного процесса.
currentDir=pwd; [~,cgDir]=rtwdemodir();
Создайте модель.
rtwbuild(model)
### Starting build procedure for model: rtwdemo_condinput ### Successful completion of build procedure for model: rtwdemo_condinput
Просмотрите сгенерированный код без оптимизации. Эти строки кода находятся в файле 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_10'
* Constant: '<Root>/C_5'
* Gain: '<Root>/ G3'
* Inport: '<Root>/input'
* RelationalOperator: '<Root>/Relational Operator'
* Sum: '<Root>/ Sum'
*/
if (rtwdemo_condinput_U.input >= -5.0) {
rtwdemo_condinput_Y.output = 3.0 * rtwdemo_condinput_U.input;
} else {
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) {
/* Outport: '<Root>/output' incorporates:
* Constant: '<Root>/ C10'
* Sum: '<Root>/ Sum1'
*/
rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + 10.0;
}
/* End of Switch: '<Root>/Switch1' */
}
Сгенерированный код содержит оператор if-else для блока Switch1 и оператор if для блока Switch2. Поэтому сгенерированный код для Switch1:Path2 выполняется, даже если оператор if для Switch1:Path1 оценивает к истине.
Откройте диалоговое окно Configuration Parameters.
Выберите параметр выполнения ответвления входа Conditional. Также можно использовать API командной строки, чтобы включить оптимизацию.
set_param(model, 'ConditionallyExecuteInputs','on');
rtwbuild(model) cfile = fullfile(cgDir,'rtwdemo_condinput_grt_rtw','rtwdemo_condinput.c'); rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
### Starting build procedure for model: rtwdemo_condinput
### Successful completion of build procedure for model: rtwdemo_condinput
/* 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 оценивает ко лжи.
bdclose(model) rtwdemoclean; cd(currentDir)
Условное входное выполнение ответвления | Многопортовый переключатель | Переключатель