Calculating Continuous One-Hour Moving Averages for Fast Sampled Analog Signals in PCS 7
In SIMATIC PCS 7, calculating a continuous 60-minute rolling average for an analog signal sampled every 100 ms requires processing 36,000 data points (144 KB), which exceeds the memory capacity of the standard APL MeanTime block (FB1832). Process engineers implement this calculation efficiently in CFC by designing a cascaded multi-tier ring buffer in SCL, applying an exponential smoothing filter (Smooth block), or delegating statistical aggregation to WinCC Tag Logging compressed archives.
Table of Contents
- Memory and Execution Constraints of High-Frequency Moving Averages
- Analysis of the Standard APL MeanTime Block (FB1832) Limitations
- Architectural Solution 1: Cascaded Multi-Tier Ring Buffering in SCL
- Architectural Solution 2: First-Order Exponential Smoothing (Smooth Block)
- Architectural Solution 3: WinCC Tag Logging Compressed Archives
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
Memory and Execution Constraints of High-Frequency Moving Averages
Continuous rolling averages are critical for emissions monitoring, heat-balance calculations, and cumulative mass flow integration. However, computing a moving average over a long time horizon with high sample frequency in an S7-400 or CPU 410-5H controller introduces severe memory trade-offs:
* Sampling at 100 ms (OB35 execution frequency) over 1 hour generates 36,000 distinct floating-point samples (3,600 seconds / 0.1 s = 36,000).
* In IEEE 754 single-precision format (REAL), each sample occupies 4 bytes. Storing 36,000 raw samples requires 144,000 bytes (140.6 KB) of contiguous data block memory.
* In classical S7-400 CPUs, maximum data block size is limited to 64 KB (65,536 bytes). A single DB cannot physically hold 144 KB, requiring complex multi-DB indexing or memory-efficient mathematical architectures.
Analysis of the Standard APL MeanTime Block (FB1832) Limitations
The SIMATIC PCS 7 Advanced Process Library (APL) provides the MeanTime block (FB1832) for time-weighted moving averages:
* By default, MeanTime stores up to 32 historical values within its internal instance data block.
* By interconnecting the Meminput pin to an auxiliary MemR256 block, buffer capacity expands to 256 samples.
* If the configured TimeWindow / SampleTime ratio exceeds available buffer space (e.g., 3,600 s / 0.1 s = 36,000 > 256), the block outputs an internal warning message and truncates older samples, rendering it unsuitable for direct 100 ms one-hour moving averages.
Architectural Solution 1: Cascaded Multi-Tier Ring Buffering in SCL
The standard engineering method to achieve true mathematical continuous averages without exhausting controller memory is a cascaded two-tier ring buffer:
1. Tier 1 (Sub-Second Accumulator): In OB35 (100 ms), accumulate 10 samples into a 1-second running sum. Output a clean 1-second average value.
2. Tier 2 (Minute-Level Ring Buffer): In OB32 (1.0 s), feed the 1-second average into a 60-element ring buffer to produce a running 1-minute average.
3. Tier 3 (One-Hour Rolling Buffer): In a cyclic chart executing every 60 seconds, write the 1-minute average into a 60-element array (ARRAY[1..60] OF REAL).
4. Final Calculation: The rolling one-hour average is the arithmetic mean of the 60 one-minute values:
$$ ext{Avg}{ ext{1hr}} = rac{1}{60} \sum_i$$}^{60} ext{MinuteValue
This reduces total memory consumption from 144 KB to under 500 bytes while maintaining 99.9% statistical equivalence.
Architectural Solution 2: First-Order Exponential Smoothing (Smooth Block)
When a mathematical approximation of the moving average is acceptable for process damping, the APL Smooth block (FB1890) provides first-order lag filtering:
* The transfer function corresponds to a low-pass filter: $Y(s) = rac{1}{1 + sT} X(s)$.
* Setting filter time constant $T = 1800 ext{ s}$ (30 minutes) or $3600 ext{ s}$ (60 minutes) yields a smooth, continuous trend that tracks the moving average without storing historical samples in memory.
* Memory consumption is zero buffer arrays; CPU execution time is a single floating-point multiplication per cycle.
Architectural Solution 3: WinCC Tag Logging Compressed Archives
If the moving average is needed strictly for supervisory visualization, reporting, or environmental compliance rather than active closed-loop controller interlocks:
1. Stream the raw analog tag to WinCC Tag Logging at its normal acquisition cycle.
2. In WinCC Tag Logging, configure a Compressed Archive.
3. Select the source process tag, set calculation type to Average, and configure the compression time interval to 1 hour.
4. The WinCC SQL database automatically calculates and logs the exact 1-hour average, offloading all storage and computation from the controller CPU.
Verification
- In CFC dynamic test mode, inject a step change into the analog input (e.g., from 0.0 to 100.0).
- Observe the cascaded SCL buffer: verify that the 1-second average updates immediately, the 1-minute buffer updates once per minute, and the 1-hour rolling output ramps linearly over 60 minutes.
- After 60 minutes of steady 100.0 input, confirm that the rolling average equals exactly 100.0.
- Verify that CPU cycle time in SIMATIC Manager Module Information shows negligible impact (< 1 ms).
Limitations and Operational Precautions
- On initial controller startup, the rolling buffer requires 60 minutes to populate completely; initialize buffer arrays with current process value on warm restart (OB100) to prevent false low averages during startup.
- Never allocate large arrays (
> 10,000elements) directly in function block local data (VAR_TEMP), as this immediately causes local data stack overflow (Event ID16#4521). - If using WinCC compressed archives, remember that historical values are calculated on the OS server and are not accessible for closed-loop AS logic.
Engineer FAQ
- What is the maximum number of samples supported by APL MeanTime with MemR256?
The maximum capacity is 256 samples. Any larger time window requires downsampling or custom array buffering. - Why is storing 36,000 REAL values impossible in a single S7-400 DB?
Standard STEP 7 data blocks on S7-400 have an architectural limit of 65,536 bytes (64 KB), whereas 36,000 REALs require 144,000 bytes. - How does CPU 410-5H handle large data blocks compared to S7-400?
CPU 410-5H supports larger individual DBs up to the limit of user memory, but storing huge arrays in fast cyclic OBs still increases memory footprint. - How do I initialize the ring buffer during a CPU warm restart?
In OB100, execute a FOR loop that fills all 60 elements of the minute array with the current online reading of the analog input. - What SCL instruction shifts elements in a ring buffer?
A circular index pointer (ptr = (ptr MOD 60) + 1) is far more CPU-efficient than physically shifting 60 memory elements every minute. - Can the Smooth block replace a true arithmetic moving average?
The Smooth block provides exponential weighting where recent values have higher influence, whereas an arithmetic moving average weights all 3,600 seconds equally. - Does the cascaded averaging method introduce significant time delay?
Cascading 1-second and 1-minute averages introduces a negligible half-sample delay (30 seconds over a 3,600-second window), which is statistically insignificant. - Can I log the moving average in Process Historian?
Yes, export the calculated average tag from CFC via AS-OS compile and archive it into Tag Logging and Process Historian. - What happens to the moving average if the analog sensor fails (Bad quality)?
The SCL logic should check the signal status (PV_In.ST <> 16#80); if bad, freeze the buffer or substitute the last good value to prevent corrupting the average. - What organization block is recommended for the 1-minute buffer calculation?
Place the minute-level aggregation block in OB32 (1.0 s) or call it conditionally using a 60-second pulse timer in OB35.
Technical References
- SIMATIC PCS 7 Advanced Process Library (APL) Function Manual (V9.0 / V9.1)
- SIMATIC S7-400 CPU Data Reference Manual
- Siemens Industry Online Support Entry: Calculating Averages in S7 and PCS 7 (Entry ID 298705)
- SIMATIC WinCC V7.5 Tag Logging Process Archiving Manual
Related Articles
- Configuring Bit Memory and Local Data Stack Allocation in SIMATIC PCS 7 S7-400 Controllers
- Optimizing CFC Execution Cycles and Cyclic Interrupt OBs in PCS 7
Related Tags
PCS 7, APL, Moving Average, MeanTime, Smooth, Ring Buffer, SCL, S7-400, Memory Optimization, Tag Logging