Retentive timer (RTO) behavior in PLC Programming - Time & Space Complexity
We want to understand how the time a retentive timer takes to complete grows as we increase the input time.
Specifically, how does the timer's counting behavior affect execution steps?
Analyze the time complexity of the following retentive timer code snippet.
// Retentive Timer (RTO) example
IF Start THEN
Timer.ACC := Timer.ACC + 1; // Increment accumulated time
IF Timer.ACC >= Timer.PRE THEN
Timer.DN := TRUE; // Timer done
END_IF
ELSE
// Timer retains ACC value when not running
END_IF
This code increments the timer's accumulated value while running and sets a done flag when the preset time is reached.
Look for loops or repeated steps in the timer's operation.
- Primary operation: Incrementing the accumulated time (Timer.ACC) each cycle while running.
- How many times: This happens once per scan cycle until the preset time is reached.
The number of increments grows directly with the preset time value.
| Input Size (Timer.PRE) | Approx. Operations (increments) |
|---|---|
| 10 | 10 increments |
| 100 | 100 increments |
| 1000 | 1000 increments |
Pattern observation: The increments increase linearly as the preset time increases.
Time Complexity: O(n)
This means the timer's counting steps grow in direct proportion to the preset time value.
[X] Wrong: "The timer completes instantly regardless of preset time."
[OK] Correct: The timer must count each time unit up to the preset, so more time means more steps.
Understanding how timers scale with input helps you reason about automation timing and resource use in real systems.
"What if the timer reset its accumulated value every cycle instead of retaining it? How would the time complexity change?"
Practice
Solution
Step 1: Understand Retentive Timer behavior
Retentive timers keep track of elapsed time even when the timer is not running.Step 2: Compare options with this behavior
Only It remembers elapsed time even if the timer is paused or stopped. describes this feature correctly; others describe non-retentive timers or different timer types.Final Answer:
It remembers elapsed time even if the timer is paused or stopped. -> Option CQuick Check:
Retentive timer remembers time = A [OK]
- Thinking RTO resets automatically
- Confusing RTO with countdown timers
- Assuming RTO only works when running
Solution
Step 1: Identify how to reset RTO timers
Retentive timers are reset by setting their accumulated time (ACC) to zero manually.Step 2: Match syntax options
Only RTO.ACC = 0 correctly shows resetting ACC to zero; others are invalid or incorrect syntax.Final Answer:
RTO.ACC = 0 -> Option AQuick Check:
Reset RTO by ACC = 0 [OK]
- Trying to reset with a method call
- Setting enable (EN) to false to reset
- Using non-existent properties like TimerReset
IF Start THEN RTO.EN := TRUE; ELSE RTO.EN := FALSE; END_IF; // After 5 seconds running, Start goes FALSE for 3 seconds, then TRUE again // What will be the accumulated time (RTO.ACC) after 10 seconds total?
Solution
Step 1: Analyze timer enable periods
The timer runs for 5 seconds (Start TRUE), then pauses for 3 seconds (Start FALSE), then runs again for 2 seconds (to reach 10 seconds total).Step 2: Calculate accumulated time
Since RTO is retentive, it keeps the 5 seconds from first run, then adds 2 seconds after restart, totaling 7 seconds.Final Answer:
7 seconds -> Option BQuick Check:
Elapsed + resumed time = 7 seconds [OK]
- Assuming timer resets when stopped
- Counting total elapsed time instead of run time
- Ignoring retentive behavior
IF Start THEN RTO.EN := TRUE; ELSE RTO.EN := FALSE; RTO.ACC := 0; END_IF;
What is wrong with this code regarding RTO behavior?
Solution
Step 1: Understand retentive timer reset rules
Retentive timers keep accumulated time until manually reset; resetting ACC on every ELSE disables retention.Step 2: Analyze code effect
Resetting ACC whenever Start is FALSE clears accumulated time, so timer never retains elapsed time.Final Answer:
Resetting ACC inside ELSE resets timer every time Start is FALSE, losing retentive behavior. -> Option DQuick Check:
Reset ACC only when needed to keep retention [OK]
- Resetting ACC too often
- Confusing EN and ACC roles
- Assuming EN assignment causes errors
Solution
Step 1: Understand total run time tracking
To track total run time, timer must accumulate elapsed time across multiple runs without resetting.Step 2: Evaluate options for correct retention
Enable RTO when machine runs, disable when stopped, reset ACC only during maintenance. enables timer only when running and resets ACC only manually (e.g., maintenance), preserving total time.Step 3: Why others are incorrect
Enable RTO only once at program start, never disable or reset ACC. never disables timer, which is impractical; C resets ACC losing total time; D uses wrong timer type.Final Answer:
Enable RTO when machine runs, disable when stopped, reset ACC only during maintenance. -> Option AQuick Check:
Manual ACC reset preserves total run time [OK]
- Resetting ACC on every stop
- Leaving timer always enabled
- Using non-retentive timers for total time
Ready to prove it?
Pass the test and this pattern moves unknown
