mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
|
Implementation of the a general cyclic value (such as angles in radians/degrees etc). More...
#include <cyclic.h>
Public Member Functions | |
cyclic () | |
Default constructor. More... | |
cyclic (const flt val) | |
Constructor overload. More... | |
cyclic (const cyclic &other) | |
Copy constructor. More... | |
cyclic (const spec &other) | |
Copy constructor. More... | |
flt | value () const |
Getter for val . More... | |
template<class other_t > | |
other_t | convert () const |
Conversion between two different circular quantities. More... | |
cyclic & | operator= (const flt nval) |
Assignment operator. More... | |
cyclic & | operator= (const cyclic &other) |
Assignment operator. More... | |
cyclic & | operator= (cyclic &&other) |
Move operator. More... | |
cyclic & | operator+= (const cyclic &other) |
Addition compound operator. More... | |
cyclic & | operator-= (const cyclic &other) |
Subtraction compound operator. More... | |
Static Public Member Functions | |
static bool | inRange (const flt val) |
Checks if val is within the valid interval of wrapped values. More... | |
static flt | wrap (const flt val) |
Returns val , converted to the valid interval of values. More... | |
static flt | unwrap (const flt what, const flt from) |
Returns value of the parameter what modified so that there is no "jump" between from and \t what. More... | |
static flt | pdist (const flt from, const flt to) |
Returns length of the shortest walk in the positive direction from the first parameter to the second one. More... | |
static flt | pdist (const cyclic from, const cyclic to) |
static flt | diff (const flt minuend, const flt subtrahend) |
Returns the difference between the two circular values. More... | |
static flt | diff (const cyclic minuend, const cyclic subtrahend) |
static flt | dist (const flt from, const flt to) |
Returns the distance between the two circular values. More... | |
static flt | dist (const cyclic from, const cyclic to) |
static flt | interpUnwrapped (const flt from, const flt to, const flt coeff) |
Interpolation between two circular quantities without wrapping of the result. More... | |
static flt | interpUnwrapped (const cyclic from, const cyclic to, const flt coeff) |
static flt | interp (const flt from, const flt to, const flt coeff) |
Interpolation between two circular quantities. More... | |
static flt | interp (const cyclic from, const cyclic to, const flt coeff) |
static flt | pinterpUnwrapped (const flt from, const flt to, const flt coeff) |
Interpolation between two circular quantities in the positive direction without wrapping of the result. More... | |
static flt | pinterpUnwrapped (const cyclic from, const cyclic to, const flt coeff) |
static flt | pinterp (const flt from, const flt to, const flt coeff) |
Interpolation between two circular quantities in the positive direction. More... | |
static flt | pinterp (const cyclic from, const cyclic to, const flt coeff) |
template<class other_t > | |
static other_t | convert (const cyclic &what) |
Conversion between two different circular quantities. More... | |
Static Public Attributes | |
static constexpr flt | minimum = spec::minimum |
Minimum of the valid interval of wrapped values . | |
static constexpr flt | supremum = spec::supremum |
Supremum of the valid interval of wrapped values . | |
static constexpr flt | range |
Range of the valid interval of wrapped values (also the period of the cyclic quantity). More... | |
static constexpr flt | half_range |
Half of the range of the valid interval of wrapped values (used for some calculations). More... | |
Protected Attributes | |
flt | val |
Friends | |
spec | operator+ (const cyclic &lhs, const cyclic &rhs) |
Addition operator. More... | |
flt | operator- (const cyclic &lhs, const cyclic &rhs) |
Subtraction operator (uses the diff() method). More... | |
Implementation of the a general cyclic value (such as angles in radians/degrees etc).
This class implements a periodical value with a period of so that a general number represents the same value as . For the purposes of calculations in this class, is confined to a half-open interval , where is the minimum of this interval and is its supremum, and holds. This approach enables representing in different intervals on the real numbers axis; eg. angle in radians may be represented within the interval or , according to the needs of the specific application. The period is called range
for the purposes of this class, as it represents range of the interval of valid ("wrapped") values of .
This class may be used as an object or its static methods may be used on regular floating-point types, avoiding any object-related overheads (see example). Specializations for the most common cyclic values are provided and a new specialization may be easily created simply by inheriting from this class and specifying a different minimum and supremum values.
Implementation inspired by: https://www.codeproject.com/Articles/190833/Circular-Values-Math-and-Statistics-with-Cplusplus
flt | floating data type to be used by this class. |
|
inline |
Default constructor.
Sets the value to the minimum.
|
inline |
Constructor overload.
val | initialization value (will be wrapped). |
|
inline |
Copy constructor.
val | initialization value. |
|
inline |
Copy constructor.
val | initialization value. |
|
inline |
Conversion between two different circular quantities.
This method returns the circular quantity, represented by this object, converted to the other_t
type of circular quantity.
other_t
. other_t | type of the circular quantity to be converted to. |
|
inlinestatic |
Conversion between two different circular quantities.
This function converts its parameter, interpreted as the circular quantity, represented by this class, to the other_t
type of circular quantity.
what | the circular quantity to be converted. |
other_t
. other_t | type of the circular quantity to be converted to. |
|
inlinestatic |
Returns the difference between the two circular values.
The difference may also be interpreted as length of the shortest walk between the two values, with a sign according to the direction of the shortest walk.
minuend | the subtrahend will be subtracted from this value. |
subtrahend | this value will be subtracted from the minuend . |
|
inlinestatic |
Returns the distance between the two circular values.
The distance may also be interpreted as length of the shortest walk between the two values.
from | the first circular quantity. |
to | the second circular quantity. |
|
inlinestatic |
Checks if val
is within the valid interval of wrapped values.
val | the value to be checked. |
val
is within the valid interval of wrapped values.
|
inlinestatic |
Interpolation between two circular quantities.
This function wraps the returned value so that it is in the interval of valid values.
from | the first circular quantity. |
to | the second circular quantity. |
coeff | the interpolation coefficient. |
|
inlinestatic |
Interpolation between two circular quantities without wrapping of the result.
This function doesn't wrap the returned value.
from | the first circular quantity. |
to | the second circular quantity. |
coeff | the interpolation coefficient. |
minimum
and supremum
parameters of this class.
|
inline |
Addition compound operator.
other | value to be added. |
|
inline |
Subtraction compound operator.
other | value to be subtracted. |
|
inline |
Assignment operator.
other | value to be assigned. |
|
inline |
Assignment operator.
nval | value to be assigned (will be wrapped). |
|
inline |
Move operator.
other | value to be assigned. |
|
inlinestatic |
Returns length of the shortest walk in the positive direction from the first parameter to the second one.
from | the positive walk starts at this value. |
to | the positive walk ends at this value. |
|
inlinestatic |
Interpolation between two circular quantities in the positive direction.
Interpolates the two values in the positive direction from the first parameter to the second by the coefficient. This function wraps the returned value so that it is in the interval of valid values.
from | the first circular quantity. |
to | the second circular quantity. |
coeff | the interpolation coefficient. |
|
inlinestatic |
Interpolation between two circular quantities in the positive direction without wrapping of the result.
Interpolates the two values in the positive direction from the first parameter to the second by the coefficient. This function doesn't wrap the returned value.
from | the first circular quantity. |
to | the second circular quantity. |
coeff | the interpolation coefficient. |
minimum
and supremum
parameters of this class.
|
inlinestatic |
Returns value of the parameter what
modified so that there is no "jump" between from
and \t what.
The circular difference between the two input quantities is preserved, but the returned value is modified if necessary so that the linear distance between the values is the smallest. This is useful whenever you need to preserve linear continuity of consecutive values, eg. when commanding a multi-rotational servo motor or when using a simple linear Kalman filter to estimate circular quantities.
An example of inputs and outputs if :
what | from | return |
---|---|---|
20 | 10 | 20 |
20 | 350 | 380 |
0 | 350 | 360 |
200 | 10 | -160 |
what | the value to be unwrapped. |
from | the previous value from which the unwrapped value of what should have the same circular difference and minimal linear distance. |
what
.minimum
and supremum
parameters of this class.
|
inline |
Getter for val
.
|
inlinestatic |
Returns val
, converted to the valid interval of values.
The wrapped value represents the same quantity as the parameter (ie. , where is the parameter and is the returned value).
val | the value to be wrapped. |
val
wrapped to the valid interval of values.
|
friend |
Addition operator.
lhs | left-hand-side. |
rhs | right-hand-side. |
|
friend |
Subtraction operator (uses the diff() method).
lhs | left-hand-side. |
rhs | right-hand-side. |
|
staticconstexpr |
Half of the range of the valid interval of wrapped values (used for some calculations).
|
staticconstexpr |