Skip to main content

Ramp

Struct Ramp 

Source
pub struct Ramp { /* private fields */ }
Expand description

Moves a value toward a target by at most a fixed step each update.

Commanding an actuator straight to a new value can be harsh: a motor lurches, a valve slams, a lamp jumps. A Ramp limits how fast the commanded value may change, easing it toward the target by at most max_step per update and snapping to the target once it is within a step. It is the slew-rate limiter behind a smooth start and stop.

§Examples

use pamoja_kit::Ramp;

// Start at 0, move at most 2 per step, aim for 5.
let mut ramp = Ramp::new(0.0, 2.0);
assert_eq!(ramp.update(5.0), 2.0);
assert_eq!(ramp.update(5.0), 4.0);
assert_eq!(ramp.update(5.0), 5.0); // within a step: snaps to the target

Implementations§

Source§

impl Ramp

Source

pub fn new(start: f32, max_step: f32) -> Self

Creates a ramp starting at start that moves at most max_step per update.

§Arguments
  • start - the initial value.
  • max_step - the largest change allowed per update; its magnitude is used.
§Returns

A ramp resting at start.

Source

pub fn update(&mut self, target: f32) -> f32

Moves toward target by at most the step and returns the new value.

§Arguments
  • target - the value being approached.
§Returns

The value after one limited step, equal to target once within a step of it.

Source

pub fn update_capped(&mut self, target: f32, max_step: f32) -> f32

Moves toward target by at most max_step this update, overriding the fixed rate.

This is the variable-rate cousin of update: a bounded-acceleration limiter passes accel * dt as the step so the change allowed each update scales with the time since the last one, rather than the constant step fixed at construction.

§Arguments
  • target - the value being approached.
  • max_step - the largest change allowed this update; its magnitude is used.
§Returns

The value after one limited step, equal to target once within max_step of it.

Source

pub fn value(&self) -> f32

Returns the current value.

Source

pub fn set(&mut self, value: f32)

Jumps directly to value, bypassing the rate limit.

§Arguments
  • value - the new value.

Trait Implementations§

Source§

impl Clone for Ramp

Source§

fn clone(&self) -> Ramp

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 Copy for Ramp

Source§

impl Debug for Ramp

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Ramp

§

impl RefUnwindSafe for Ramp

§

impl Send for Ramp

§

impl Sync for Ramp

§

impl Unpin for Ramp

§

impl UnsafeUnpin for Ramp

§

impl UnwindSafe for Ramp

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.