Converting MATLAB Mathematical and State Logic into S7-SCL for PCS 7
To convert MATLAB algorithms—such as multi-variable valve and flap positioning models with persistent states and matrix calculations—into S7-SCL for SIMATIC PCS 7, structure the logic inside a Function Block (FB) rather than a Function (FC). In SCL, declare all MATLAB 'persistent' variables as static variables (VAR), ensuring state retention across cyclic CPU scans (OB35). Flatten dynamic multidimensional matrices into static arrays (ARRAY[1..N] OF REAL) and solve matrix inversions algebraically offline into scalar difference equations. Map function arguments to VAR_INPUT and VAR_OUTPUT, incorporate execution sample time (t_step), and assign system attributes ({ S7_m_c := 'true' }) for WinCC faceplate integration.
Table of Contents
- Architectural Differences Between MATLAB and S7-SCL
- Converting MATLAB Persistent Variables into SCL Static Memory
- Matrix Flattening and Real-Time Execution Constraints
- Comparison of MATLAB Language Constructs vs S7-SCL Equivalents
- Step-by-Step Procedure to Convert and Compile MATLAB Code
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
Architectural Differences Between MATLAB and S7-SCL
Control research engineers frequently prototype complex technological algorithms—such as coordinated bypass valve sequencing, decoupling matrices, or state-space predictive models—in MATLAB / Simulink. When transitioning these prototypes into industrial distributed control systems running SIMATIC PCS 7, engineers must translate the mathematical code into Structured Control Language (SCL).
MATLAB is a dynamically typed, matrix-oriented scripting environment with garbage-collected memory, executing on high-speed 64-bit PC microprocessors. S7-SCL, by contrast, compiles directly into deterministic MC7 machine code executing cyclically within the real-time kernel of an S7-400 or CPU 410-5H controller. Bridging this gap requires strict memory declaration, execution bounding, and algorithmic discretization.
Converting MATLAB Persistent Variables into SCL Static Memory
In MATLAB functions, state memory is preserved across invocations using the keyword persistent (e.g., persistent MoveToMinPos RO_Flap RC_FLAP MoveToZero FlMoveUp). Translating this into SCL requires strict adherence to memory scopes:
-
Function Blocks (FB): The SCL code must be implemented as a Function Block (FB), because FBs have an associated instance Data Block (DB) stored in retentive controller memory.
-
Declaration in
VAR(Static): Every MATLABpersistentvariable must be declared under theVAR ... END_VARsection of the FB. This guarantees that state flags, accumulator values, and previous position registers (FlPosN_1,VlvPosN_1) persist from one cyclic OB scan to the next. -
Danger of
VAR_TEMP: Never declare persistent variables inVAR_TEMP. In STEP 7, temporary local stack variables are reallocated every scan; relying onVAR_TEMPfor state retention causes catastrophic algorithmic failure.
Matrix Flattening and Real-Time Execution Constraints
MATLAB effortlessly handles matrix inversions (inv(A)), dynamic slicing, and matrix multiplications (A * B). In an S7-400 controller, however, real-time deterministic execution forbids dynamic memory allocation:
-
Static Array Sizing: All vectors and matrices must have fixed, compile-time bounds:
ARRAY[1..8, 1..8] OF REAL. -
Offline Algebraic Reduction: Rather than programming high-overhead matrix inversion algorithms (such as Gaussian elimination or LU decomposition) in SCL—which can cause CPU scan time overruns and trigger watchdog trips—solve the matrix equations symbolically in MATLAB beforehand. Reduce the matrix equations to explicit algebraic difference equations ($y_k = a_1 y_{k-1} + b_0 u_k$) that execute in microseconds.
-
Sample Time Integration (
t_step): In SCL, rate-of-change and integral calculations must evaluate the actual cyclic execution period (e.g., $T_{\text{step}} = 0.1\text{ s}$ for OB35).
Comparison of MATLAB Language Constructs vs S7-SCL Equivalents
The table below maps standard MATLAB constructs to their direct S7-SCL programming equivalents.
function [out] = fcn(in)FUNCTION_BLOCK FB_FlapValvepersistent VarNameVarName : REAL; (or BOOL)VAR ... END_VAR (Instance DB)InCtrl : REAL;VAR_INPUT ... END_VARVlvPos : REAL;VAR_OUTPUT ... END_VARfor i = 1:N ... endFOR i := 1 TO N DO ... END_FOR;if condition ... endIF condition THEN ... END_IF;A * BFOR loops or expanded scalar mathStep-by-Step Procedure to Convert and Compile MATLAB Code
Follow this procedure in SIMATIC Manager to convert MATLAB positioning logic into an industrial SCL block:
-
Create SCL Source File in SIMATIC Manager:
- In Component View, navigate to[S7 Program] -> Sources.
- Insert a new SCL source file namedFB_FlapValveControl.scl. -
Structure the Function Block Header and Declarations:
```scl
FUNCTION_BLOCK FB_FlapValveControl
TITLE = 'Coordinated Flap and Valve Positioning Algorithm'
AUTHOR : 'Eng'
FAMILY : 'Custom'
// PCS 7 System Attributes
VAR_INPUT
InCtrl : REAL; // Control demand
SPVlv : REAL; // Valve setpoint
Deadband : REAL;
Hyst : REAL;
MinPos : REAL;
t_step : REAL := 0.1; // Execution sample time in seconds
END_VAR
VAR_OUTPUT
VlvPos : REAL;
FlPos : REAL;
FlMove : BOOL;
END_VAR
VAR
// Persistent State Variables
MoveToMinPos : BOOL := FALSE;
RO_Flap : REAL := 0.0;
RC_FLAP : REAL := 0.0;
FlPosN_1 : REAL := 0.0;
VlvPosN_1 : REAL := 0.0;
END_VAR
```
-
Implement Mathematical Algorithm:
- Convert conditional logic, rate-of-change limits, and position hysteresis into deterministic SCL code.
- Update previous-scan state registers at the end of the execution block:
scl FlPosN_1 := FlPos; VlvPosN_1 := VlvPos; -
Compile and Instantiate in CFC:
- Compile the SCL source file (File -> Compile): confirm that the compiler generates the compiled FB in theBlocksfolder.
- Drag the FB onto a CFC chart in OB35: assign inputs and compile the AS.
Verification
Verify algorithmic accuracy through the following commissioning checks:
1. Switch the CFC chart to Online Test Mode: verify that with steady input demand, output positions match MATLAB simulation results.
2. Step input demand across deadband thresholds: confirm that state flags (MoveToMinPos, FlMove) transition predictably without chatter.
3. Check CPU cycle time in PLC -> Module Information: verify that block execution adds less than 1 millisecond to total OB35 execution.
4. In WinCC Runtime, verify that faceplate sliders and bar graphs track block outputs dynamically.
Limitations and Operational Precautions
Never implement unbounded while loops (WHILE condition DO) inside SCL code running in cyclic organization blocks. If an unhandled numerical condition causes an infinite loop, the controller will exceed its configured scan watchdog limit (e.g., 6000ms), triggering an immediate CPU STOP due to OB80 execution timeout. Always bound iterative loops using fixed FOR counters with strict limits.
Engineer FAQ
Can I use SIMATIC Target 1500S or S-Functions in PCS 7?
No. SIMATIC Target 1500S is designed for TIA Portal S7-1500 controllers. For S7-400 / CPU 410-5H systems in PCS 7, converting MATLAB code to native S7-SCL or using MATLAB Coder with custom C-blocks is the standard workflow.
Why must I include execution sample time (t_step) in my SCL block?
Discrete-time integration ($y = y + u \cdot \Delta t$) depends directly on the cyclic execution interval. Hardcoding time constants without scaling by the actual OB35 period will cause the algorithm to run at the wrong speed.
Can I view SCL block internal static variables online in CFC?
Yes. In CFC, any variable declared in VAR that has the system attribute { S7_m_c := 'true' } or pin visibility set to 'Visible' can be monitored dynamically in test mode.
Technical References
- SIMATIC S7-SCL for S7-300 and S7-400 Programming Manual
- SIMATIC Process Control System PCS 7 CFC for S7 Programming Manual
- Siemens Technical Documentation: Converting Mathematical Algorithms to SCL (SIOS Entry ID 109751826)
Related Articles
- Proper Initialization and Memory Rules for S7 SCL VAR_TEMP Variables in PCS 7
- Tuning PID Controllers in PCS 7 APL and CEMAT Systems
- Configuring Analog and Modulating Control Valves in PCS 7 Using APL VlvAnL
Related Tags
- S7-SCL
- MATLAB Conversion
- Function Blocks
- Static Variables
- Algorithm Engineering
- CFC