Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
openlcb::Velocity Class Reference

This class provides a mechanism for working with velocity in different forms. More...

#include <Velocity.hxx>

Public Types

enum  { FORWARD = false , REVERSE = true }
 define an enumeration for direction More...
 

Public Member Functions

 Velocity ()
 Default constructor.
 
 Velocity (int value)
 Basic constructor.
 
 Velocity (unsigned int value)
 Basic constructor.
 
 Velocity (float value)
 Basic constructor.
 
 Velocity (double value)
 Basic constructor.
 
 Velocity (float16_t value)
 Constructor that takes the 16 bit wire format.
 
 Velocity (const Velocity &old_velocity)
 Copy constructor.
 
 ~Velocity ()
 Destructor does nothing.
 
bool isnan () const
 Checks whether the speed is unknown.
 
float speed () const
 Return the speed independent of direction.
 
bool direction () const
 Return the direction independent of speed.
 
void set_direction (bool direction)
 
void forward ()
 Set the direction to forward.
 
void reverse ()
 Set the direction to reverse.
 
float mph () const
 Convert the native meters/sec representation into mile per hour.
 
void set_mph (float mph)
 Sets the speed value from a given mph value.
 
uint8_t get_dcc_128 ()
 Get the speed in DCC 128 speed step format.
 
void set_dcc_128 (uint8_t value)
 Set the speed from DCC 128 speed step format.
 
uint8_t get_dcc_28 ()
 Get the speed in DCC 28 speed step format.
 
void set_dcc_28 (uint8_t value)
 Set the speed from DCC 28 speed step format.
 
uint8_t get_dcc_14 ()
 Get the speed in DCC 14 speed step format.
 
void set_dcc_14 (uint8_t value)
 Set the speed from DCC 14 speed step format.
 
float16_t get_wire () const
 Get a wire version of the velocity.
 
void set_wire (float16_t value)
 Set the value based on the wire version of velocity.
 
Velocity operator+ (const Velocity &v) const
 Overloaded addition operator.
 
Velocity operator+ (const float &v) const
 Overloaded addition operator.
 
Velocity operator- (const Velocity &v) const
 Overloaded subtraction operator.
 
Velocity operator- (const float &v) const
 Overloaded subtraction operator.
 
Velocity operator* (const Velocity &v) const
 Overloaded multiplication operator.
 
Velocity operator* (const float &v) const
 Overloaded multiplication operator.
 
Velocity operator/ (const Velocity &v) const
 Overloaded division operator.
 
Velocity operator/ (const float &v) const
 Overloaded division operator.
 
Velocityoperator++ ()
 Overloaded pre-increement operator.
 
Velocity operator++ (int)
 Overloaded post-increment operator.
 
Velocityoperator-- ()
 Overloaded pre-decreement operator.
 
Velocity operator-- (int)
 Overloaded post-decrement operator.
 
Velocityoperator+= (const Velocity &v)
 Overloaded addition equals operator.
 
Velocityoperator+= (const float &v)
 Overloaded addition equals operator.
 
Velocityoperator-= (const Velocity &v)
 Overloaded subtraction equals operator.
 
Velocityoperator-= (const float &v)
 Overloaded subtraction equals operator.
 
Velocityoperator*= (const Velocity &v)
 Overloaded multiplication equals operator.
 
Velocityoperator*= (const float &v)
 Overloaded multiplication equals operator.
 
Velocityoperator/= (const Velocity &v)
 Overloaded division equals operator.
 
Velocityoperator/= (const float &v)
 Overloaded division equals operator.
 
Velocityoperator= (const Velocity &v)
 Overloaded equals operator.
 
Velocityoperator= (const float &v)
 Overloaded equals operator.
 
bool operator== (const Velocity &v) const
 Overloaded equals equals operator.
 
bool operator== (const float &v) const
 Overloaded equals equals operator.
 
bool operator!= (const Velocity &v) const
 Overloaded not equals operator.
 
bool operator!= (const float &v) const
 Overloaded not equals operator.
 

Static Public Member Functions

static Velocity from_mph (float mph)
 

Static Private Member Functions

static float zero_adjust (float value, float old)
 Adjust for a math result of negative 0.
 

Private Attributes

float velocity
 Floating point representation of velocity.
 

Detailed Description

This class provides a mechanism for working with velocity in different forms.

A single precision floating point value is used internally to store velocity, but this class provides the ability to easily work with different velocity formats including DCC 14/28/128 speed step formats. NMRAnet velocity is represented as a floating point meters/sec. The sign represents direction where negative is reverse and positive is forward. Sign is always preserved even when the result is 0. For example, -7.0 + 7.0 = -0.0 and 7.0 - 7.0 = +0.0 Be careful thought, per the C standards, a negative zero (-.0.0) velocity compared to constant 0.0 will compare true. Always use direction() when trying to determine the sign or direction of travel.

Definition at line 72 of file Velocity.hxx.

Member Enumeration Documentation

◆ anonymous enum

define an enumeration for direction

Enumerator
FORWARD 

forward direction

REVERSE 

reverse direction

Definition at line 77 of file Velocity.hxx.

Constructor & Destructor Documentation

◆ Velocity() [1/7]

openlcb::Velocity::Velocity ( )
inline

Default constructor.

Definition at line 85 of file Velocity.hxx.

◆ Velocity() [2/7]

openlcb::Velocity::Velocity ( int  value)
inline

Basic constructor.

Parameters
valuestarting value for Velocity.

Definition at line 93 of file Velocity.hxx.

◆ Velocity() [3/7]

openlcb::Velocity::Velocity ( unsigned int  value)
inline

Basic constructor.

Parameters
valuestarting value for Velocity.

Definition at line 101 of file Velocity.hxx.

◆ Velocity() [4/7]

openlcb::Velocity::Velocity ( float  value)
inline

Basic constructor.

Parameters
valuestarting value for Velocity.

Definition at line 109 of file Velocity.hxx.

◆ Velocity() [5/7]

openlcb::Velocity::Velocity ( double  value)
inline

Basic constructor.

Parameters
valuestarting value for Velocity.

Definition at line 117 of file Velocity.hxx.

◆ Velocity() [6/7]

openlcb::Velocity::Velocity ( float16_t  value)
inline

Constructor that takes the 16 bit wire format.

Parameters
valuestarting value for Velocity as IEEE half precision float.

Definition at line 125 of file Velocity.hxx.

◆ Velocity() [7/7]

openlcb::Velocity::Velocity ( const Velocity old_velocity)
inline

Copy constructor.

Definition at line 132 of file Velocity.hxx.

◆ ~Velocity()

openlcb::Velocity::~Velocity ( )
inline

Destructor does nothing.

Definition at line 138 of file Velocity.hxx.

Member Function Documentation

◆ direction()

bool openlcb::Velocity::direction ( ) const
inline

Return the direction independent of speed.

Returns
direction FORWARD or REVERSE

Definition at line 164 of file Velocity.hxx.

◆ forward()

void openlcb::Velocity::forward ( )
inline

Set the direction to forward.

Definition at line 186 of file Velocity.hxx.

◆ from_mph()

static Velocity openlcb::Velocity::from_mph ( float  mph)
inlinestatic

Definition at line 140 of file Velocity.hxx.

◆ get_dcc_128()

uint8_t openlcb::Velocity::get_dcc_128 ( )

Get the speed in DCC 128 speed step format.

The mapping from meters/sec is strait forward. First convert to miles/hour, then each speed step represents 1 mile/hour. Saturate at 126 miles/hour.

bit 7: direction bits 6..0: 0 = stopped, 1 = estop, 2 - 127 = speed steps 1 - 126

Returns
DCC encoded speed steps

Definition at line 48 of file Velocity.cxx.

◆ get_dcc_14()

uint8_t openlcb::Velocity::get_dcc_14 ( )

Get the speed in DCC 14 speed step format.

This is a decimation of the 128 speed step mode.

bit 7..6: fixed at b'01' bit 5: direction bits 4: reserved 0 for headlight bits 3..0: 0 = stopped, 4 - 31 = speed steps 1 - 28

Returns
DCC encoded speed steps

Definition at line 171 of file Velocity.cxx.

◆ get_dcc_28()

uint8_t openlcb::Velocity::get_dcc_28 ( )

Get the speed in DCC 28 speed step format.

This is a decimation of the 128 speed step mode.

bit 7..6: fixed at b'01' bit 5: direction bits 4: speed step least significant bit bits 3..0: speed step significatn bits 4..1

Returns
DCC encoded speed steps

Definition at line 105 of file Velocity.cxx.

◆ get_wire()

float16_t openlcb::Velocity::get_wire ( ) const
inline

Get a wire version of the velocity.

Returns
IEEE half precision floating point representation of velocity

Definition at line 285 of file Velocity.hxx.

◆ isnan()

bool openlcb::Velocity::isnan ( ) const
inline

Checks whether the speed is unknown.

(NaN).

Returns
true if the speed value is NaN.

Definition at line 148 of file Velocity.hxx.

◆ mph()

float openlcb::Velocity::mph ( ) const
inline

Convert the native meters/sec representation into mile per hour.

Returns
velocity represented as miles per hour. Always non-negative.

Definition at line 206 of file Velocity.hxx.

◆ operator!=() [1/2]

bool openlcb::Velocity::operator!= ( const float v) const
inline

Overloaded not equals operator.

Definition at line 467 of file Velocity.hxx.

◆ operator!=() [2/2]

Overloaded not equals operator.

Definition at line 461 of file Velocity.hxx.

◆ operator*() [1/2]

Overloaded multiplication operator.

Definition at line 331 of file Velocity.hxx.

◆ operator*() [2/2]

Overloaded multiplication operator.

Definition at line 325 of file Velocity.hxx.

◆ operator*=() [1/2]

Overloaded multiplication equals operator.

Definition at line 414 of file Velocity.hxx.

◆ operator*=() [2/2]

Overloaded multiplication equals operator.

Definition at line 407 of file Velocity.hxx.

◆ operator+() [1/2]

Overloaded addition operator.

Definition at line 307 of file Velocity.hxx.

◆ operator+() [2/2]

Overloaded addition operator.

Definition at line 301 of file Velocity.hxx.

◆ operator++() [1/2]

Overloaded pre-increement operator.

Definition at line 349 of file Velocity.hxx.

◆ operator++() [2/2]

Overloaded post-increment operator.

Definition at line 356 of file Velocity.hxx.

◆ operator+=() [1/2]

Overloaded addition equals operator.

Definition at line 386 of file Velocity.hxx.

◆ operator+=() [2/2]

Overloaded addition equals operator.

Definition at line 379 of file Velocity.hxx.

◆ operator-() [1/2]

Overloaded subtraction operator.

Definition at line 319 of file Velocity.hxx.

◆ operator-() [2/2]

Overloaded subtraction operator.

Definition at line 313 of file Velocity.hxx.

◆ operator--() [1/2]

Overloaded pre-decreement operator.

Definition at line 364 of file Velocity.hxx.

◆ operator--() [2/2]

Overloaded post-decrement operator.

Definition at line 371 of file Velocity.hxx.

◆ operator-=() [1/2]

Overloaded subtraction equals operator.

Definition at line 400 of file Velocity.hxx.

◆ operator-=() [2/2]

Overloaded subtraction equals operator.

Definition at line 393 of file Velocity.hxx.

◆ operator/() [1/2]

Overloaded division operator.

Definition at line 343 of file Velocity.hxx.

◆ operator/() [2/2]

Overloaded division operator.

Definition at line 337 of file Velocity.hxx.

◆ operator/=() [1/2]

Overloaded division equals operator.

Definition at line 428 of file Velocity.hxx.

◆ operator/=() [2/2]

Overloaded division equals operator.

Definition at line 421 of file Velocity.hxx.

◆ operator=() [1/2]

Overloaded equals operator.

Definition at line 442 of file Velocity.hxx.

◆ operator=() [2/2]

Overloaded equals operator.

Definition at line 435 of file Velocity.hxx.

◆ operator==() [1/2]

bool openlcb::Velocity::operator== ( const float v) const
inline

Overloaded equals equals operator.

Definition at line 455 of file Velocity.hxx.

◆ operator==() [2/2]

Overloaded equals equals operator.

Definition at line 449 of file Velocity.hxx.

◆ reverse()

void openlcb::Velocity::reverse ( )
inline

Set the direction to reverse.

Definition at line 195 of file Velocity.hxx.

◆ set_dcc_128()

void openlcb::Velocity::set_dcc_128 ( uint8_t  value)

Set the speed from DCC 128 speed step format.

The mapping from meters/sec is strait forward. First convert to miles/hour, then each speed step represents 1 mile/hour. Saturate at 126 miles/hour.

Parameters
valuebit 7: direction bits 6..0: 0 = stopped, 1 = estop, 2 - 127 = speed steps 1 - 126

Definition at line 78 of file Velocity.cxx.

◆ set_dcc_14()

void openlcb::Velocity::set_dcc_14 ( uint8_t  value)

Set the speed from DCC 14 speed step format.

This is a decimation of the 128 speed step mode.

Parameters
valuebit 7..6: fixed at b'01' bit 5: direction bits 4: reserved 0 for headlight bits 3..0: 0 = stopped, 4 - 31 = speed steps 1 - 28

This is a decimation of the 128 speed step mode.

Parameters
valuebit 7..6: fixed at b'01' bit 5: direction bits 4: reserved 0 for headlight bits 3..0: 0 = stopped, 2 - 15 = speed steps 1 - 14

Definition at line 203 of file Velocity.cxx.

◆ set_dcc_28()

void openlcb::Velocity::set_dcc_28 ( uint8_t  value)

Set the speed from DCC 28 speed step format.

This is a decimation of the 128 speed step mode.

Parameters
valuebit 7..6: fixed at b'01' bit 5: direction bits 4: speed step least significant bit bits 3..0: speed step significatn bits 4..1

Definition at line 139 of file Velocity.cxx.

◆ set_direction()

void openlcb::Velocity::set_direction ( bool  direction)
inline

Definition at line 173 of file Velocity.hxx.

◆ set_mph()

void openlcb::Velocity::set_mph ( float  mph)
inline

Sets the speed value from a given mph value.

The sign of the mph value is ignored.

Definition at line 213 of file Velocity.hxx.

◆ set_wire()

void openlcb::Velocity::set_wire ( float16_t  value)
inline

Set the value based on the wire version of velocity.

Parameters
valueIEEE half precision floating point representation of velocity

Definition at line 295 of file Velocity.hxx.

◆ speed()

float openlcb::Velocity::speed ( ) const
inline

Return the speed independent of direction.

Returns
speed absolute value of velocity

Definition at line 156 of file Velocity.hxx.

◆ zero_adjust()

static float openlcb::Velocity::zero_adjust ( float  value,
float  old 
)
inlinestaticprivate

Adjust for a math result of negative 0.

Parameters
valuevalue of math result
oldoriginal value before expression

Definition at line 480 of file Velocity.hxx.

Member Data Documentation

◆ velocity

float openlcb::Velocity::velocity
private

Floating point representation of velocity.

Definition at line 474 of file Velocity.hxx.


The documentation for this class was generated from the following files: