Implementing High-Accuracy Motor Positioning and Delays in S7-400 SCL: IEC Timers vs. Cyclic Ticks
In SIMATIC S7-400 and PCS 7 SCL programming, implementing motor positioning or time delays using classic S7 timers (S_ODT) introduces scan-cycle jitter, timing inaccuracies, and positioning overshoots. Classic S7 timers are evaluated asynchronously relative to the PLC scan cycle, causing timing errors of up to two scan cycles. For deterministic, high-accuracy motor positioning (e.g. moving a mechanical guillotine or linear actuator), engineers must deploy IEC Timers (SFB 4 TON) or implement deterministic time integration within a fixed cyclic interrupt OB (OB35) using parameter SAMPLE_T.
Table of Contents
- The Problem of Time Delays and Jitter in S7-400 SCL
- Classic S7 Timers (S_ODT) vs. IEC Standard Timers (SFB 4 TON)
- Deterministic Positioning via Cyclic Interrupt Accumulation (SAMPLE_T)
- Timer Implementation Mechanism Comparison Matrix
- Step-by-Step Implementation of a Deterministic SCL Positioner
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
The Problem of Time Delays and Jitter in S7-400 SCL
In process engineering and discrete packaging applications, engineers frequently develop custom algorithmic blocks in Structured Control Language (S7-SCL) to coordinate physical positioning mechanisms:
* An electric motor moves a mechanical guillotine, knife blade, or diverter flap across a linear stroke (e.g. 0.0 cm to 20.0 cm).
* In open-loop control without continuous encoder feedback, the distance traveled is calculated as a function of motor run duration:
$$\text{Distance} = \text{Velocity} \times \text{Time Delay}$$
When engineers attempt to implement this time delay in SCL using classic STEP 7 timers (such as S_ODT), they encounter severe repeatability problems:
* The guillotine stops at 19.2 cm on the first stroke, 20.8 cm on the second stroke, and drifts further over time.
* The root cause is not mechanical backlash; it is the fundamental mathematical inaccuracy of classic S7 timers in cyclic execution environments.
Classic S7 Timers (S_ODT) vs. IEC Standard Timers (SFB 4 TON)
The timing discrepancy arises from architectural differences between legacy S7 timers and standard IEC timers:
1. Classic S7 Timers (S_ODT / T1..T256):
- Built into S7-300/400 CPU firmware using an archaic BCD format (10 ms, 100 ms, 1 s time bases).
- Evaluated asynchronously: the timer updates independently of the OB execution cycle.
- If an SCL block executes within cyclic interrupt OB35 (100 ms), the classic timer may expire between scans, introducing up to $\pm 100\text{ ms}$ of jitter.
2. Standard IEC Timers (SFB 4 TON / System Function Block):
- Compliant with IEC 61131-3.
- Stored in instance data blocks with millisecond resolution (TIME data type).
- Evaluated deterministically at the exact moment the block call is executed in the program sequence.
Deterministic Positioning via Cyclic Interrupt Accumulation (SAMPLE_T)
In SIMATIC PCS 7, the professional, certified method for executing high-accuracy open-loop positioning or time integration completely bypasses timers in favor of Cyclic Task Accumulation:
* The custom SCL block is assigned to a fixed cyclic interrupt Organization Block (e.g. OB35 running at exactly $100\text{ ms}$, or OB36 at $50\text{ ms}$).
* The block receives the standard PCS 7 parameter SAMPLE_T (REAL) representing the sampling time in seconds (e.g. 0.100 s for OB35).
* While the motor runs, an internal position accumulator increments deterministically on every cyclic scan:
$$\text{CurrentPosition} = \text{CurrentPosition} + (\text{MotorVelocity} \times \text{SAMPLE_T})$$
* Because the cyclic interrupt OB is hardware-timed by the S7-400 crystal clock, scan jitter is under 100 microseconds, delivering 100% repeatable physical positioning.
Timer Implementation Mechanism Comparison Matrix
The following matrix compares methods for implementing time delays in S7-400 SCL:
S_ODT)SFB 4 TON)SAMPLE_T)S5TIME (BCD Encoded)TIME (32-bit integer milliseconds)REAL / Engineering Units (cm, mm)Step-by-Step Implementation of a Deterministic SCL Positioner
Follow this procedure to build a high-accuracy positioning block in S7-SCL:
1. Create the SCL Source Block:
- In SIMATIC Manager, create a new SCL source file named FB_GuillotinePositioner.
2. Define SCL Interface Parameters:
scl
FUNCTION_BLOCK FB_GuillotinePositioner
VAR_INPUT
TargetPos : REAL; // Target position in cm (e.g. 20.0)
Velocity : REAL; // Motor linear speed in cm/s (e.g. 5.0)
RunFbk : BOOL; // Motor running feedback
Reset : BOOL; // Reset position to zero
SAMPLE_T : REAL := 0.1;// OB35 cycle interval in seconds
END_VAR
VAR_OUTPUT
MotorCmd : BOOL; // Command to run motor
ActualPos : REAL; // Current calculated position
PosReached: BOOL; // Position achieved flag
END_VAR
VAR
PosAccum : REAL := 0.0;// Internal persistent accumulator
END_VAR
3. Implement Deterministic Calculation Logic:
scl
BEGIN
IF Reset THEN
PosAccum := 0.0;
PosReached := FALSE;
MotorCmd := FALSE;
ELSIF NOT PosReached THEN
IF ActualPos >= TargetPos THEN
MotorCmd := FALSE;
PosReached := TRUE;
ELSE
MotorCmd := TRUE;
IF RunFbk THEN
PosAccum := PosAccum + (Velocity * SAMPLE_T);
END_IF;
END_IF;
END_IF;
ActualPos := PosAccum;
END_FUNCTION_BLOCK
4. Compile SCL Source into S7 Program:
- In the SCL editor, select File -> Compile. Verify 0 Errors.
5. Place in CFC within OB35:
- Drag the newly compiled FB from the CFC catalog onto a chart.
- The CFC compiler automatically connects the sampling time input SAMPLE_T to the cyclic interrupt period of OB35 (0.1 s).
Verification
Verify deterministic positioning accuracy:
1. Open the CFC chart online in test mode.
2. Set TargetPos = 20.0 cm, Velocity = 5.0 cm/s (Expected run duration = 4.0 seconds).
3. Apply reset, then command the motor to run.
4. Measure the physical stop position across 10 consecutive stroke cycles:
- Confirm that the calculated ActualPos lands exactly at 20.0 cm every cycle.
- Confirm that the physical guillotine stops with repeatable accuracy within mechanical limits.
Limitations and Operational Precautions
- Open-loop positioning calculated via velocity $\times$ time cannot compensate for physical mechanical slip, belt stretching, or motor load variations; for safety-critical positioning, install physical end-limit switches or continuous linear encoders.
- Always wire hardware over-travel limit switches in series with the motor contactor coil to prevent mechanical jamming if software fails.
- SCL blocks utilizing
SAMPLE_Tmust be placed exclusively in cyclic interrupt OBs (OB30–OB38); never place them inOB1where scan time varies continuously.
Engineer FAQ
Why does CFC automatically populate SAMPLE_T?
In SIMATIC PCS 7, any input parameter named exactly SAMPLE_T of type REAL is automatically assigned the attribute S7_sampletime = 'true'. The CFC compiler inspects which OB the block is placed in and writes the exact cycle time in seconds.
Can I use SFB 4 TON in SCL without creating separate DBs?
Yes. By declaring SFB 4 TON as a static variable (VAR) inside your Function Block, S7-SCL creates a Multi-Instance, embedding the timer's memory directly into the parent block's instance DB.
What is the resolution of the S7-400 crystal clock?
S7-400 CPUs utilize hardware real-time crystal clocks with a resolution of 1 millisecond, ensuring cyclic interrupt tasks execute with microsecond determinism.
Technical References
- SIMATIC S7-SCL for S7-300/400 Programming Manual (A5E00706949)
- SIMATIC S7-400 System and Standard Functions Manual (A5E02789676)
- Siemens Industry Support Portal: Implementing Timers and Integrators in S7-SCL
Related Articles
- Optimizing S7-400 Execution Times and Cyclic Interrupt OBs in SIMATIC PCS 7
- Resolving Cyclic Interrupt OB Priority Conflicts During AS-Wide Interconnection Compilation
- Understanding the APL DoseL Dosing Block in SIMATIC PCS 7: Coarse, Fine, and Overrun Compensation
Related Tags
- SIMATIC PCS 7 V8.1
- S7-SCL
- IEC Timers
- OB35
- SAMPLE_T