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: u16Implementations§
Source§impl Speed
impl Speed
Sourcepub const STOP_FORWARD: Speed
pub const STOP_FORWARD: Speed
Zero speed, facing forward (+0.0, 0x0000).
Sourcepub const STOP_REVERSE: Speed
pub const STOP_REVERSE: Speed
Zero speed, facing reverse (-0.0, 0x8000).
Sourcepub fn from_mps(mps: f32, reverse: bool) -> Speed
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.
Sourcepub fn from_mph(mph: f32, reverse: bool) -> Speed
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.
Sourcepub fn is_reverse(self) -> bool
pub fn is_reverse(self) -> bool
True if the direction (sign) bit is set.
Sourcepub fn is_nan(self) -> bool
pub fn is_nan(self) -> bool
True for any half-float NaN, such as Speed::NAN.
Sourcepub fn mps(self) -> f32
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);Sourcepub fn mph(self) -> f32
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.