Skip to main content

Speed

Struct Speed 

Source
pub struct Speed(pub u16);
Expand description

A traction speed: IEEE half-float in scale m/s, sign bit = reverse.

The inner u16 is the raw half-float as sent on the wire (see crate::float16). The Traction Control Standard expresses speed in scale meters per second, with the sign bit as the direction: set means reverse. Direction is therefore kept even at zero speed, which is why there are two stop values. The magnitude accessors (Speed::mps, Speed::mph) always return a non-negative number; use Speed::is_reverse for the direction.

use rolcb::traction::Speed;

let s = Speed::from_mph(30.0, true);
assert!(s.is_reverse());
assert!((s.mph() - 30.0).abs() < 0.05);
assert!(s.mps() > 0.0);

assert_eq!(Speed::from_mps(0.0, true), Speed::STOP_REVERSE);
assert_eq!(Speed::default(), Speed::STOP_FORWARD);
assert!(Speed::NAN.is_nan());

Tuple Fields§

§0: u16

Implementations§

Source§

impl Speed

Source

pub const NAN: Speed

“Not available”, used in Query Speed replies.

Source

pub const STOP_FORWARD: Speed

Zero speed, facing forward (+0.0, 0x0000).

Source

pub const STOP_REVERSE: Speed

Zero speed, facing reverse (-0.0, 0x8000).

Source

pub fn from_mps(mps: f32, reverse: bool) -> Speed

Build from a magnitude in scale m/s and a direction. The sign of mps is ignored; only reverse sets the direction.

Source

pub fn from_mph(mph: f32, reverse: bool) -> Speed

Build from a magnitude in scale miles per hour (1 mph = 0.44704 m/s) and a direction.

Source

pub fn is_reverse(self) -> bool

True if the direction (sign) bit is set.

Source

pub fn is_nan(self) -> bool

True for any half-float NaN, such as Speed::NAN.

Source

pub fn mps(self) -> f32

Magnitude in m/s (never negative; see Speed::is_reverse).

For a NaN speed such as Speed::NAN (“not available”) this returns f32::NAN, so check Speed::is_nan before doing arithmetic or comparisons with the result.

use rolcb::traction::Speed;
assert!(Speed::NAN.mps().is_nan());
assert_eq!(Speed::from_mps(2.0, true).mps(), 2.0);
Source

pub fn mph(self) -> f32

Magnitude in mph. DCC command stations conventionally map mph 1:1 onto 128-step speed steps (126 = full). NaN for a NaN speed, like Speed::mps.

Trait Implementations§

Source§

impl Clone for Speed

Source§

fn clone(&self) -> Self

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 Speed

Source§

impl Debug for Speed

Source§

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

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

impl Default for Speed

Source§

fn default() -> Self

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

impl Eq for Speed

Source§

impl Hash for Speed

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Speed

Source§

fn eq(&self, other: &Self) -> bool

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

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Speed

Auto Trait Implementations§

§

impl Freeze for Speed

§

impl RefUnwindSafe for Speed

§

impl Send for Speed

§

impl Sync for Speed

§

impl Unpin for Speed

§

impl UnsafeUnpin for Speed

§

impl UnwindSafe for Speed

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 = !

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.