TinyRobotics
Loading...
Searching...
No Matches
Speed.h
1#pragma once
2
3namespace tinyrobotics {
4
5/**
6 * @enum SpeedUnit
7 * @ingroup units
8 * @brief Supported speed units for conversion and representation.
9 */
10enum class SpeedUnit { MPS, KPH, FPS, MPH };
11
12/**
13 * @class Speed
14 * @ingroup units
15 * @brief Represents a speed measurement with unit conversion support.
16 *
17 * The Speed class encapsulates a speed value and its unit, supporting meters
18 * per second (MPS), kilometers per hour (KPH), feet per second (FPS), and miles
19 * per hour (MPH). It provides methods to set and retrieve the speed in any
20 * supported unit, automatically handling conversions.
21 *
22 * - Internal state is always consistent with the last set value and unit.
23 * - Designed for embedded and robotics applications where unit flexibility and
24 * efficiency are required.
25 * - Use getValue() to retrieve the speed in any unit; use setValue() to update
26 * the value and unit.
27 *
28 * Example:
29 * @code
30 * Speed s(10.0, SpeedUnit::MPS);
31 * float mph = s.getValue(SpeedUnit::MPH); // Convert to miles per hour
32 * s.setValue(36.0, SpeedUnit::KPH); // Update value in km/h
33 * float mps = s.getValue(SpeedUnit::MPS); // Convert back to m/s
34 * @endcode
35 *
36 * @note Invalid conversions return -1.0f.
37 *
38 * @see SpeedUnit
39 */
40class Speed {
41 public:
42 Speed() = default;
43 Speed(float speed, SpeedUnit unit) { setValue(speed, unit); }
44
45 void setValue(float newSpeed, SpeedUnit newUnit) {
46 speed = newSpeed;
47 unit = newUnit;
48 }
49
50 float getValue(SpeedUnit desiredUnit) const {
51 if (unit == desiredUnit) return speed;
52 switch (unit) {
53 case SpeedUnit::MPS:
54 if (desiredUnit == SpeedUnit::KPH) return speed * 3.6f;
55 if (desiredUnit == SpeedUnit::FPS) return speed * 3.28084f;
56 if (desiredUnit == SpeedUnit::MPH) return speed * 2.23694f;
57 break;
58 case SpeedUnit::KPH:
59 if (desiredUnit == SpeedUnit::MPS) return speed / 3.6f;
60 if (desiredUnit == SpeedUnit::FPS) return speed * 0.911344f;
61 if (desiredUnit == SpeedUnit::MPH) return speed * 0.621371f;
62 break;
63 case SpeedUnit::FPS:
64 if (desiredUnit == SpeedUnit::MPS) return speed / 3.28084f;
65 if (desiredUnit == SpeedUnit::KPH) return speed * 1.09728f;
66 if (desiredUnit == SpeedUnit::MPH) return speed * 0.681818f;
67 break;
68 case SpeedUnit::MPH:
69 if (desiredUnit == SpeedUnit::MPS) return speed / 2.23694f;
70 if (desiredUnit == SpeedUnit::KPH) return speed * 1.60934f;
71 if (desiredUnit == SpeedUnit::FPS) return speed * 1.46667f;
72 break;
73 }
74 return -1; // Invalid conversion
75 }
76
77 Speed operator+(const Speed& other) const {
78 float otherSpeed = other.getValue(unit);
79 return Speed(speed + otherSpeed, unit);
80 }
81
82 Speed operator-(const Speed& other) const {
83 float otherSpeed = other.getValue(unit);
84 return Speed(speed - otherSpeed, unit);
85 }
86
87 Speed operator*(float scalar) const {
88 return Speed(speed * scalar, unit);
89 }
90
91 Speed operator/(float scalar) const {
92 if (scalar == 0) return Speed(0, unit); // Avoid division by zero
93 return Speed(speed / scalar, unit);
94 }
95
96 bool operator==(const Speed& other) const {
97 return speed == other.getValue(unit);
98 }
99
100 bool operator!=(const Speed& other) const { return !(*this == other); }
101
102 bool operator<(const Speed& other) const {
103 return speed < other.getValue(unit);
104 }
105
106 bool operator<=(const Speed& other) const {
107 return speed <= other.getValue(unit);
108 }
109
110 bool operator>(const Speed& other) const {
111 return speed > other.getValue(unit);
112 }
113
114 bool operator>=(const Speed& other) const {
115 return speed >= other.getValue(unit);
116 }
117
118 Speed& operator+=(const Speed& other) {
119 speed += other.getValue(unit);
120 return *this;
121 }
122
123 Speed& operator-=(const Speed& other) {
124 speed -= other.getValue(unit);
125 return *this;
126 }
127
128 Speed& operator*=(float scalar) {
129 speed *= scalar;
130 return *this;
131 }
132
133 Speed& operator/=(float scalar) {
134 if (scalar == 0) {
135 speed = 0;
136 } else {
137 speed /= scalar;
138 }
139 return *this;
140 }
141
142 protected:
143 float speed = 0.0f;
144 SpeedUnit unit = SpeedUnit::MPS; ///< Unit of the speed. @see SpeedUnit
145};
146
147using Velocity = Speed;
148
149} // namespace tinyrobotics
Represents a speed measurement with unit conversion support.
Definition: Speed.h:40
SpeedUnit unit
Unit of the speed.
Definition: Speed.h:144
SpeedUnit
Supported speed units for conversion and representation.
Definition: Speed.h:10