pub struct Ton {
pub en: bool,
pub pt: Duration,
pub q: bool,
pub et: Duration,
/* private fields */
}Expand description
Timer On Delay (TON)
A timer that delays turning on the output. The output q becomes true
after the enable input en has been continuously true for the preset
time pt. The elapsed time is available in et.
This is equivalent to the IEC 61131-3 TON function block.
§Behavior
- When
enbecomestrue, the timer starts counting from zero - While counting,
etshows the elapsed time andqisfalse - When
etreachespt,qbecomestrueandetis clamped topt - When
enbecomesfalse, the timer resets:q=false,et= 0
§Example
use autocore_std::Ton;
use std::time::Duration;
let mut timer = Ton::new();
let delay = Duration::from_secs(5);
// Timer disabled - output is false
assert_eq!(timer.call(false, delay), false);
assert_eq!(timer.et, Duration::ZERO);
// Enable timer - starts counting
timer.call(true, delay);
assert_eq!(timer.q, false); // Not done yet
// timer.et is now counting up...
// Disable resets the timer
timer.call(false, delay);
assert_eq!(timer.et, Duration::ZERO);§Timing Diagram
en: _____|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾|_____
q: _____________|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾|_____
et: 0---|++++++++|PT----------------|0----
^ ^ ^
| | |
en rises et=pt en falls§Use Cases
- Motor start delay (allow contactors to engage)
- Debouncing switches (ignore brief transitions)
- Timeout detection (alarm if condition persists too long)
Fields§
§en: boolInput: Enable the timer (true = counting, false = reset)
pt: DurationInput: Preset time (duration before output activates)
q: boolOutput: Timer done (true when elapsed time >= preset time)
et: DurationOutput: Elapsed time since timer was enabled
Implementations§
Source§impl Ton
impl Ton
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new timer with default values.
The timer starts in the disabled state with zero elapsed time.
§Example
use autocore_std::Ton;
let timer = Ton::new();
assert_eq!(timer.q, false);
assert_eq!(timer.et, std::time::Duration::ZERO);Sourcepub fn call(&mut self, en: bool, pt: Duration) -> bool
pub fn call(&mut self, en: bool, pt: Duration) -> bool
Executes the timer logic.
Call this method once per control cycle. The timer counts real elapsed time (not cycles), so the output timing is independent of scan rate.
§Arguments
en- Enable input:trueto run timer,falseto resetpt- Preset time: duration before output activates
§Returns
The current state of the output q (true if timer has elapsed).
§Example
use autocore_std::Ton;
use std::time::Duration;
let mut timer = Ton::new();
// Use in a control loop
let motor_request = true;
let start_delay = Duration::from_millis(500);
let motor_enabled = timer.call(motor_request, start_delay);
// motor_enabled will be true after 500ms of motor_request being trueSourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the timer to its initial state.
This is equivalent to calling call(false, ...).
§Example
use autocore_std::Ton;
use std::time::Duration;
let mut timer = Ton::new();
timer.call(true, Duration::from_secs(1));
// ... timer is running ...
timer.reset();
assert_eq!(timer.q, false);
assert_eq!(timer.et, Duration::ZERO);