В этом примере показано, как симулировать и сгенерировать Структурированный текст для блока MPC Controller с помощью программного обеспечения Simulink® PLC Coder™. Использование сгенерированного кода, с одинарной точностью.
Чтобы запустить этот пример, Тулбокс MPC, Simulink и Simulink PLC Coder требуются.
if ~mpcchecktoolboxinstalled('simulink') disp('Simulink is required to run this example.') return end if ~mpcchecktoolboxinstalled('plccoder') disp('Simulink PLC Coder is required to run this example.'); return end if ~mpcchecktoolboxinstalled('mpc') disp('MPC Toolbox is required to run this example.'); return end
Задайте объект SISO.
plant = ss(tf([3 1],[1 0.6 1]));
Задайте контроллер MPC для объекта.
Ts = 0.1; %Sample time p = 10; %Prediction horizon m = 2; %Control horizon Weights = struct('MV',0,'MVRate',0.01,'OV',1); % Weights MV = struct('Min',-Inf,'Max',Inf,'RateMin',-100,'RateMax',100); % Input constraints OV = struct('Min',-2,'Max',2); % Output constraints mpcobj = mpc(plant,Ts,p,m,Weights,MV,OV);
Откройте модель Simulink.
mdl = 'mpc_plcdemo';
open_system(mdl)


Чтобы сгенерировать структурированный текст для блока MPC Controller, завершите выполняющий двух шагов:
Сконфигурируйте блок MPC, чтобы использовать данные с одинарной точностью. Установите свойство типа Выходных данных блока MPC Controller к single.
open_system([mdl '/Control System/MPC Controller'])

Поместите блок MPC в подсистеме, блокируют и обрабатывают блок подсистемы как атомарный модуль. Выберите Treat как атомарное модульное свойство блока подсистемы.

Симулируйте модель в Simulink.
close_system([mdl '/Control System/MPC Controller']) open_system([mdl '/Outputs//References']) open_system([mdl '/Inputs']) sim(mdl)
-->Converting model to discrete time. -->Assuming output disturbance added to measured output channel #1 is integrated white noise. -->The "Model.Noise" property of the "mpc" object is empty. Assuming white noise on each measured output channel.


Чтобы сгенерировать код с Кодером PLC, используйте plcgeneratecode команда.
disp('Generating PLC structure text... Please wait until it finishes.') plcgeneratecode([mdl '/Control System']);
Generating PLC structure text... Please wait until it finishes.
### Generating PLC code for 'mpc_plcdemo/Control System'.
### Using <a href="matlab:configset.showParameterGroup('mpc_plcdemo', { 'PLC Code Generation' } )">model settings</a> from 'mpc_plcdemo' for PLC code generation parameters.
### Begin code generation for IDE <a href="matlab:configset.showParameterGroup('mpc_plcdemo', { 'PLC Code Generation' } )">codesys23</a>.
### Emit PLC code to file.
### Creating PLC code generation report <a href="matlab:web('/tmp/BR2021ad_1584584_202060/publish_examples0/tp4f26524f/ex47675176/plcsrc/html/mpc_plcdemo/mpc_plcdemo_codegen_rpt.html')">mpc_plcdemo_codegen_rpt.html</a>.
### PLC code generation successful for 'mpc_plcdemo/Control System'.
### Generated files:
<a href="matlab: edit('plcsrc/mpc_plcdemo.exp')">plcsrc/mpc_plcdemo.exp</a>
Диалоговое окно Viewer сообщения показывает, что генерация кода PLC была успешна.

Закройте модель Simulink и возвратитесь к исходной директории.
bdclose(mdl)