Code Generation for Programmable Logic Controllers in Matlab

MathWorks has announced the launch of a new product - Simulink PLC Coder. This product allows the generation of code for programmable logic controllers (PLCs) and programmable automation controllers according to IEC 61131. This innovation allows the use of model-oriented design for industrial and power equipment controlled by PLCs.

With Simulink PLC Coder, engineers can automatically generate code for industrial control systems, including closed loop and feedback control systems. Automatic code generation, an integral part of Model-oriented design, helps to eliminate the errors associated with traditional manual code writing and reduces development and validation time.

Simulink PLC Coder generates the source code in a structured text format from Simulink models, Stateflow diagrams, and Embedded MATLAB code, and then uses the integrated development environment (IDE) from the industrial automation vendor to compile the code and run it on PLC controllers.
To generate the code, Matlab is required starting with version 2010a. We will develop a program for the Siemens SIMATIC STEP 7 PLC.

Create a model file in which we will create a mathematical model. To do this, we need the plclib library, so from the Matlab command line, run it.

We create a mathematical model of a control system with a discrete PID controller (Figure 1). Figure 2 shows the internal structure of the pid_feedforward block.

image

Figure 1 - Mathematical model of the control system

image

Figure 2 - Internal structure of the pid_feedforward block

image

Figure 3 - System transient

Select the pid_feedforward block, press the right mouse button and select Subsystem Parametrs. In this window, select “Treat as atomic unit” and click OK.
On the function block, right-click and select PLC Coder / Options.
We select in which type of PLC the code (Target IDE) will be generated and press the Generate code key.
Generated code for SIMATIC STEP 7
(*
*
* File: plc.scl
*
* IEC 61131-3 Structured Text (ST) code generated for Simulink model "plc.mdl"
*
* Model version: 1.1
* Simulink PLC Coder version: 1.1 ( R2010b) 03-Aug-2010
* ST code generated on: Fri Jun 03 18:52:23 2011
*
* Target IDE selection: Siemens SIMATIC Step 7 5.4
* Test Bench included: No
*
*)
FUNCTION_BLOCK FB1
VAR_INPUT
ssMethodType: INT;
In3: REAL;
In2: REAL;
In3_c: REAL;
END_VAR
VAR_OUTPUT
Out1: REAL;
END_VAR
VAR
Integrator_DSTATE: REAL;
Filter_DSTATE: REAL;
rtb_et: REAL;
rtb_Sum: REAL;
c_rtb_FilterCoeffi: REAL;
END_VAR
CASE ssMethodType OF
2:
(* InitializeConditions for DiscreteIntegrator: '/ Integrator' *)
Integrator_DSTATE: = 0;

(* InitializeConditions for DiscreteIntegrator: '/ Filter' *)
Filter_DSTATE: = 0;

3:
(* Sum: '/ Sum' incorporates:
* Inport: '/ In1'
* Inport: '/ In2'
*)
rtb_et: = In3 - In2;

(* Gain: '/ Filter Coefficient' incorporates:
* DiscreteIntegrator: '/ Filter'
* Gain: '/ Derivative Gain'
* Sum: '/ SumD'
*)
c_rtb_FilterCoeffi: = ((-1.26102994076046 * rtb_et) - Filter_DSTATE) * 0.1781098037130 ;

(* Sum: '/ Sum' incorporates:
* DiscreteIntegrator: '/ Integrator'
* Gain: '/ Proportional Gain'
*)
rtb_Sum: = ((1.7018012505578 * rtb_et) + Integrator_DSTATE) + c_rtb_FilterCoeffi;

(* Outport: '/ Out1' incorporates:

* Sum: '/ Sum1'
*)
Out1: = In3 + rtb_Sum;

(* Update for DiscreteIntegrator: '/ Integrator' incorporates:
* Gain: '/ Integral Gain'
* Sum: '/ Sum2'
* Sum: '/ SumI1'
* Sum: '/ SumI3'
* Update for Inport: '/ In1'
* Update for Inport: '/ In3'
*)
Integrator_DSTATE: = (((In3_c - In3) - rtb_Sum) + (0.208461637073455 * rtb_et)) + Integrator_DSTATE;

(* Update for DiscreteIntegrator: '/ Filter' *)
Filter_DSTATE: = Filter_DSTATE + c_rtb_FilterCoeffi;

END_CASE
END_FUNCTION_BLOCK

The next step is to create a project in Siemens SIMATIC Step 7 and insert the generated code into the SCL editor.
The result of compiling the code is to create a function block.

image

Figure 4 - Function block FB1

Using the OPC server, we will exchange between the PLC and Matlab. When using real hardware, we use the Simatic Net OPC Server, when using the PLC simulator (PLC-Sim), you need to use the WinCC SCADA system and WinCC OPC Server.

image

Figure 5 - Data exchange between PLCs and Matlab

image

Figure 6 - Comparison of the operation of PID controllers

More information on Simulink PLC Coder can be found in the Matlab documentation, or at www.mathworks.com/products/sl-plc-coder .

Also popular now: