Skip to main content

SlewRateLimiter

Struct SlewRateLimiter 

Source
pub struct SlewRateLimiter<T: Numeric = f64> { /* private fields */ }
Expand description

Follows a target without ever moving faster than the rates it was given, so a step in the target comes out as a ramp.

Rising and falling have separate limits, since a machine that can speed up gently often has to slow down harder. The first call jumps straight to its target rather than ramping up from zero.

use multicalc::error::SignalError;
use multicalc::signal_processing::SlewRateLimiter;

// Climbing at one per second, falling at two, a tenth of a second at a time.
let mut limited = SlewRateLimiter::new(1.0_f64, 2.0, 0.1).unwrap();

// The first call goes straight to its target.
assert!(limited.filter(0.0).abs() < 1e-12);

// A jump to 10 comes out as a ramp: a tenth of a unit per call.
assert!((limited.filter(10.0) - 0.1).abs() < 1e-12);
for _ in 0..9 {
    let _ = limited.filter(10.0);
}
assert!((limited.value() - 1.0).abs() < 1e-12);

// Turning around, it moves twice as fast.
assert!((limited.filter(-10.0) - 0.8).abs() < 1e-12);

assert_eq!(
    SlewRateLimiter::new(0.0_f64, 1.0, 0.1),
    Err(SignalError::NonPositiveRate)
);

Implementations§

Source§

impl<T: Numeric> SlewRateLimiter<T>

Source

pub fn new( rise_per_second: T, fall_per_second: T, dt: T, ) -> Result<Self, SignalError>

Builds a limiter with separate rise and fall rates, in units per second.

Returns SignalError::NonFinite if any argument is not finite, SignalError::NonPositiveTimestep if dt is not strictly positive, or SignalError::NonPositiveRate if either rate is not strictly positive.

Source

pub fn symmetric(rate_per_second: T, dt: T) -> Result<Self, SignalError>

Builds a limiter that rises and falls at the same rate.

Returns the same errors as new.

Source

pub fn filter(&mut self, target: T) -> T

Moves one step toward the target and returns where the output now sits.

Source

pub fn reset(&mut self)

Clears the output so the next call jumps straight to its target again.

Source

pub fn value(&self) -> T

Where the output currently sits, without feeding a target.

Trait Implementations§

Source§

impl<T: Clone + Numeric> Clone for SlewRateLimiter<T>

Source§

fn clone(&self) -> SlewRateLimiter<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Copy + Numeric> Copy for SlewRateLimiter<T>

Source§

impl<T: Debug + Numeric> Debug for SlewRateLimiter<T>

Source§

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

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

impl<T: PartialEq + Numeric> PartialEq for SlewRateLimiter<T>

Source§

fn eq(&self, other: &SlewRateLimiter<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialEq + Numeric> StructuralPartialEq for SlewRateLimiter<T>

Auto Trait Implementations§

§

impl<T> Freeze for SlewRateLimiter<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for SlewRateLimiter<T>
where T: RefUnwindSafe,

§

impl<T> Send for SlewRateLimiter<T>
where T: Send,

§

impl<T> Sync for SlewRateLimiter<T>
where T: Sync,

§

impl<T> Unpin for SlewRateLimiter<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SlewRateLimiter<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for SlewRateLimiter<T>
where T: UnwindSafe,

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> 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.