Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Velocity.hxx
Go to the documentation of this file.
1
34#ifndef _OPENLCB_VELOCITY_HXX_
35#define _OPENLCB_VELOCITY_HXX_
36
37#include <cmath>
38#include <cstdint>
39
40#include "utils/macros.h"
41
42extern "C" {
43/* These come from the ieeehalfprecision.c */
45int singles2halfp(void *target, const void *source, int numel);
47int halfp2singles(void *target, const void *source, int numel);
48}
49
51#define MPH_FACTOR 0.44704f
52
55typedef uint16_t float16_t;
56
57namespace openlcb
58{
59
73{
74public:
77 enum
78 {
79 FORWARD = false,
80 REVERSE = true,
81 };
82
86 : velocity(0)
87 {
88 }
89
93 Velocity(int value)
94 : velocity(value)
95 {
96 }
97
101 Velocity(unsigned int value)
102 : velocity(value)
103 {
104 }
105
109 Velocity(float value)
110 : velocity(value)
111 {
112 }
113
117 Velocity(double value)
118 : velocity(value)
119 {
120 }
121
126 : velocity(0)
127 {
128 halfp2singles(&velocity, &value, 1);
129 }
130
136
139
140 static Velocity from_mph(float mph) {
141 Velocity v;
142 v.set_mph(mph);
143 return v;
144 }
145
148 bool isnan() const
149 {
150 return std::isnan(velocity);
151 }
152
156 float speed() const
157 {
158 return fabsf(velocity);
159 }
160
164 bool direction() const
165 {
166 if (std::signbit(velocity))
167 {
168 return REVERSE;
169 }
170 return FORWARD;
171 }
172
173 void set_direction(bool direction)
174 {
175 if (direction == FORWARD)
176 {
177 forward();
178 }
179 else
180 {
181 reverse();
182 }
183 }
184
186 void forward()
187 {
188 if (std::signbit(velocity))
189 {
191 }
192 }
193
195 void reverse()
196 {
197 if (!std::signbit(velocity))
198 {
200 }
201 }
202
206 float mph() const
207 {
208 return speed() / MPH_FACTOR;
209 }
210
213 void set_mph(float mph)
214 {
215 // should this be std::copysign? armgcc says not.
217 }
218
229
238 void set_dcc_128(uint8_t value);
239
250
259 void set_dcc_28(uint8_t value);
260
271
280 void set_dcc_14(uint8_t value);
281
286 {
287 float16_t result;
288 singles2halfp(&result, &velocity, 1);
289 return result;
290 }
291
295 void set_wire(float16_t value)
296 {
297 halfp2singles(&velocity, &value, 1);
298 }
299
302 {
303 return Velocity(zero_adjust(velocity + v.velocity,velocity));
304 }
305
307 Velocity operator + (const float& v) const
308 {
310 }
311
314 {
315 return Velocity(zero_adjust(velocity - v.velocity,velocity));
316 }
317
319 Velocity operator - (const float& v) const
320 {
322 }
323
326 {
327 return Velocity(zero_adjust(velocity * v.velocity,velocity));
328 }
329
331 Velocity operator * (const float& v) const
332 {
334 }
335
338 {
339 return Velocity(zero_adjust(velocity / v.velocity,velocity));
340 }
341
343 Velocity operator / (const float& v) const
344 {
346 }
347
350 {
352 return *this;
353 }
354
357 {
358 Velocity result(*this);
360 return result;
361 }
362
365 {
367 return *this;
368 }
369
372 {
373 Velocity result(*this);
375 return result;
376 }
377
380 {
381 velocity = zero_adjust(velocity + v.velocity,velocity);
382 return *this;
383 }
384
386 Velocity& operator += (const float& v)
387 {
389 return *this;
390 }
391
394 {
395 velocity = zero_adjust(velocity - v.velocity,velocity);
396 return *this;
397 }
398
400 Velocity& operator -= (const float& v)
401 {
403 return *this;
404 }
405
408 {
409 velocity = zero_adjust(velocity * v.velocity,velocity);
410 return *this;
411 }
412
414 Velocity& operator *= (const float& v)
415 {
417 return *this;
418 }
419
422 {
423 velocity = zero_adjust(velocity / v.velocity,velocity);
424 return *this;
425 }
426
428 Velocity& operator /= (const float& v)
429 {
431 return *this;
432 }
433
436 {
438 return *this;
439 }
440
442 Velocity& operator = (const float& v)
443 {
444 velocity = v;
445 return *this;
446 }
447
449 bool operator == (const Velocity& v) const
450 {
451 return (v.velocity == velocity);
452 }
453
455 bool operator == (const float& v) const
456 {
457 return (v == velocity);
458 }
459
461 bool operator != (const Velocity& v) const
462 {
463 return (v.velocity != velocity);
464 }
465
467 bool operator != (const float& v) const
468 {
469 return (v != velocity);
470 }
471
472private:
474 float velocity;
475
480 static float zero_adjust(float value, float old)
481 {
482 if (value == 0)
483 {
484 return copysign(value, old);
485 }
486 return value;
487 }
488};
489
492{
493 Velocity s;
494 s.set_wire(0xFFFFU);
495 return s;
496}
497
498}; /* namespace openlcb */
499
500#endif // _OPENLCB_VELOCITY_HXX_
uint16_t float16_t
This type represents how velocity is seen on the wire (16 bit float).
Definition Velocity.hxx:55
int singles2halfp(void *target, const void *source, int numel)
Converts an IEEE single (float) to a half-precision (float16).
#define MPH_FACTOR
Conversion factor for MPH.
Definition Velocity.hxx:51
int halfp2singles(void *target, const void *source, int numel)
Converts an half-precision (float16) to an IEEE single (float).
This class provides a mechanism for working with velocity in different forms.
Definition Velocity.hxx:73
Velocity()
Default constructor.
Definition Velocity.hxx:85
float speed() const
Return the speed independent of direction.
Definition Velocity.hxx:156
void set_dcc_28(uint8_t value)
Set the speed from DCC 28 speed step format.
Definition Velocity.cxx:139
void reverse()
Set the direction to reverse.
Definition Velocity.hxx:195
~Velocity()
Destructor does nothing.
Definition Velocity.hxx:138
bool operator==(const Velocity &v) const
Overloaded equals equals operator.
Definition Velocity.hxx:449
Velocity operator/(const Velocity &v) const
Overloaded division operator.
Definition Velocity.hxx:337
Velocity & operator+=(const Velocity &v)
Overloaded addition equals operator.
Definition Velocity.hxx:379
Velocity & operator*=(const Velocity &v)
Overloaded multiplication equals operator.
Definition Velocity.hxx:407
Velocity(double value)
Basic constructor.
Definition Velocity.hxx:117
float velocity
Floating point representation of velocity.
Definition Velocity.hxx:474
float mph() const
Convert the native meters/sec representation into mile per hour.
Definition Velocity.hxx:206
Velocity(const Velocity &old_velocity)
Copy constructor.
Definition Velocity.hxx:132
bool isnan() const
Checks whether the speed is unknown.
Definition Velocity.hxx:148
@ FORWARD
forward direction
Definition Velocity.hxx:79
@ REVERSE
reverse direction
Definition Velocity.hxx:80
Velocity(float value)
Basic constructor.
Definition Velocity.hxx:109
Velocity operator+(const Velocity &v) const
Overloaded addition operator.
Definition Velocity.hxx:301
void set_wire(float16_t value)
Set the value based on the wire version of velocity.
Definition Velocity.hxx:295
Velocity & operator/=(const Velocity &v)
Overloaded division equals operator.
Definition Velocity.hxx:421
Velocity & operator++()
Overloaded pre-increement operator.
Definition Velocity.hxx:349
void forward()
Set the direction to forward.
Definition Velocity.hxx:186
static float zero_adjust(float value, float old)
Adjust for a math result of negative 0.
Definition Velocity.hxx:480
Velocity(float16_t value)
Constructor that takes the 16 bit wire format.
Definition Velocity.hxx:125
bool operator!=(const Velocity &v) const
Overloaded not equals operator.
Definition Velocity.hxx:461
void set_mph(float mph)
Sets the speed value from a given mph value.
Definition Velocity.hxx:213
uint8_t get_dcc_28()
Get the speed in DCC 28 speed step format.
Definition Velocity.cxx:105
bool direction() const
Return the direction independent of speed.
Definition Velocity.hxx:164
Velocity & operator=(const Velocity &v)
Overloaded equals operator.
Definition Velocity.hxx:435
void set_dcc_128(uint8_t value)
Set the speed from DCC 128 speed step format.
Definition Velocity.cxx:78
float16_t get_wire() const
Get a wire version of the velocity.
Definition Velocity.hxx:285
Velocity(int value)
Basic constructor.
Definition Velocity.hxx:93
Velocity & operator-=(const Velocity &v)
Overloaded subtraction equals operator.
Definition Velocity.hxx:393
Velocity & operator--()
Overloaded pre-decreement operator.
Definition Velocity.hxx:364
Velocity operator*(const Velocity &v) const
Overloaded multiplication operator.
Definition Velocity.hxx:325
uint8_t get_dcc_128()
Get the speed in DCC 128 speed step format.
Definition Velocity.cxx:48
void set_dcc_14(uint8_t value)
Set the speed from DCC 14 speed step format.
Definition Velocity.cxx:203
uint8_t get_dcc_14()
Get the speed in DCC 14 speed step format.
Definition Velocity.cxx:171
Velocity operator-(const Velocity &v) const
Overloaded subtraction operator.
Definition Velocity.hxx:313
Velocity(unsigned int value)
Basic constructor.
Definition Velocity.hxx:101
Velocity nan_to_speed()
Definition Velocity.hxx:491