Skip to main content

Ton

Struct Ton 

Source
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 en becomes true, the timer starts counting from zero
  • While counting, et shows the elapsed time and q is false
  • When et reaches pt, q becomes true and et is clamped to pt
  • When en becomes false, 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: bool

Input: Enable the timer (true = counting, false = reset)

§pt: Duration

Input: Preset time (duration before output activates)

§q: bool

Output: Timer done (true when elapsed time >= preset time)

§et: Duration

Output: Elapsed time since timer was enabled

Implementations§

Source§

impl Ton

Source

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);
Source

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: true to run timer, false to reset
  • pt - 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 true
Source

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);

Trait Implementations§

Source§

impl Clone for Ton

Source§

fn clone(&self) -> Ton

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ton

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Ton

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Ton

§

impl RefUnwindSafe for Ton

§

impl Send for Ton

§

impl Sync for Ton

§

impl Unpin for Ton

§

impl UnsafeUnpin for Ton

§

impl UnwindSafe for Ton

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V