📜 ⬆️ ⬇️

Code Generation for Programmable Logic Controllers in Matlab

The company MathWorks announced the release of a new product - Simulink PLC Coder. This product allows you to automatically generate, according to IEC 61131, a code for programmable logic controllers (PLCs) and programmable automation controllers. This innovation allows the use of model-oriented design for industrial and power equipment controlled by a PLC.

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

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

Create a model file in which we will create a mathematical model. For this we need the plclib library, so we run it from the Matlab command line.
')
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 - The internal structure of the pid_feedforward block

image

Figure 3 - Transient system

Select the pid_feedforward block, click the right mouse button and select Subsystem Parametrs. In this window, mark “Treat as atomic unit” and click OK.
On the function block, click the right mouse button and select PLC Coder / Options.
Select which type of PLC the code will be generated (Target IDE) 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.178109803713032;

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

(* Outport: '/ Out1' incorporates:
* Inport: '/ In1'
* 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 - Functional block FB1

With the help of the OPC server, we can exchange between the PLC and Matlab. When using real hardware, we use Simatic Net OPC Server, when using the PLC simulator (PLC-Sim), it is necessary to use the SCADA system WinCC and WinCC OPC Server.

image

Figure 5 - Data exchange between PLC and Matlab

image

Figure 6 - Comparison of PID regulators

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

Source: https://habr.com/ru/post/121818/


All Articles