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.
Figure 1 - Mathematical model of the control system
Figure 2 - The internal structure of the pid_feedforward block
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;
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.
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.