主要内容

Simulation and Structured Text Generation Using Simulink PLC Coder

This example shows how to simulate and generate Structured Text for an MPC Controller block using Simulink® PLC Coder™ software. The generated code uses single-precision.

Required Products

To run this example, Simulink and Simulink PLC Coder are required.

if~mpcchecktoolboxinstalled('simulink') disp('Simulink is required to run this example.')返回endif~mpcchecktoolboxinstalled('plccoder') disp('Simulink PLC Coder is required to run this example.');返回end

设置环境

You must have write-permission to generate the relevant files and the executable. Therefore, before starting simulation and code generation, change the current directory to a temporary directory.

cwd = pwd; tmpdir = tempname; mkdir(tmpdir); cd(tmpdir);

Define Plant Model and MPC Controller

定义SISO植物。

plant = ss(tf([3 1],[1 0.6 1]));

Define the MPC controller for the plant.

Ts = 0.1;%采样时间p = 10;%Prediction horizonm = 2;%Control horizonWeights = struct('MV',0,'MVRate',0.01,'ov',1);重量%MV = struct('Min',-Inf,'Max',Inf,'RateMin',-100,'RateMax',100);% Input constraintsOV = struct('Min',-2,'Max',2);% Output constraintsmpcobj = mpc(plant,Ts,p,m,Weights,MV,OV);

Simulate and Generate Structured Text

打开Simulin金宝appk模型。

mdl ='mpc_plcdemo';Open_System(MDL)

To generate structured text for the MPC Controller block, complete the following two steps:

  • Configure the MPC block to use single-precision data. Set theOutput data typeproperty of the MPC Controller block tosingle.

open_system([mdl'/Control System/MPC Controller')))

  • 将MPC块放入子系统块中,并将子系统块视为原子单元。选择Treat as atomic unitproperty of the subsystem block.

在Simulink中模拟该模型。金宝app

close_system([mdl'/Control System/MPC Controller')))open_system([mdl'/outputs //参考')))open_system([mdl'/输入')))sim(mdl)
-->Converting model to discrete time. -->Assuming output disturbance added to measured output channel #1 is integrated white noise. -->The "Model.Noise" property is empty. Assuming white noise on each measured output.

To generate code with the PLC Coder, use theplcgeneratecodecommand.

disp('Generating PLC structure text... Please wait until it finishes.') plcgeneratecode([mdl'/控制系统')));
Generating PLC structure text... Please wait until it finishes. ### Generating PLC code for 'mpc_plcdemo/Control System'. ### Using model settings from 'mpc_plcdemo' for PLC code generation parameters. ### Begin code generation for IDE codesys23. ### Emit PLC code to file. ### Creating PLC code generation report mpc_plcdemo_codegen_rpt.html. ### PLC code generation successful for 'mpc_plcdemo/Control System'. ### Generated files: plcsrc/mpc_plcdemo.exp

The Message Viewer dialog box shows that PLC code generation was successful.

Close the Simulink model, and return to the original directory.

BDCLOSE(MDL)CD(CWD)

相关话题