TinyRobotics
Loading...
Searching...
No Matches
AngularVelocity.h
1#pragma once
2#include <cmath>
3
4namespace tinyrobotics {
5
6/**
7 * @enum AngularVelocityUnit
8 * @ingroup units
9 * @brief Supported angular velocity units for conversion and representation.
10 */
11enum class AngularVelocityUnit { RadPerSec, DegPerSec };
12
13/**
14 * @class AngularVelocity
15 * @ingroup units
16 * @brief Represents a 1D angular velocity with unit support.
17 */
18class AngularVelocity {
19 AngularVelocity operator+(const AngularVelocity& other) const {
20 float otherValue = other.getValue(unit);
21 return AngularVelocity(angularVelocity + otherValue, unit);
22 }
23
24 AngularVelocity operator-(const AngularVelocity& other) const {
25 float otherValue = other.getValue(unit);
26 return AngularVelocity(angularVelocity - otherValue, unit);
27 }
28
29 AngularVelocity operator*(float scalar) const {
30 return AngularVelocity(angularVelocity * scalar, unit);
31 }
32
33 AngularVelocity operator/(float scalar) const {
34 if (scalar == 0) return AngularVelocity(0, unit);
35 return AngularVelocity(angularVelocity / scalar, unit);
36 }
37
38 bool operator==(const AngularVelocity& other) const {
39 return angularVelocity == other.getValue(unit);
40 }
41
42 bool operator!=(const AngularVelocity& other) const { return !(*this == other); }
43
44 bool operator<(const AngularVelocity& other) const {
45 return angularVelocity < other.getValue(unit);
46 }
47
48 bool operator<=(const AngularVelocity& other) const {
49 return angularVelocity <= other.getValue(unit);
50 }
51
52 bool operator>(const AngularVelocity& other) const {
53 return angularVelocity > other.getValue(unit);
54 }
55
56 bool operator>=(const AngularVelocity& other) const {
57 return angularVelocity >= other.getValue(unit);
58 }
59
60 AngularVelocity& operator+=(const AngularVelocity& other) {
61 angularVelocity += other.getValue(unit);
62 return *this;
63 }
64
65 AngularVelocity& operator-=(const AngularVelocity& other) {
66 angularVelocity -= other.getValue(unit);
67 return *this;
68 }
69
70 AngularVelocity& operator*=(float scalar) {
71 angularVelocity *= scalar;
72 return *this;
73 }
74
75 AngularVelocity& operator/=(float scalar) {
76 if (scalar == 0) {
77 angularVelocity = 0;
78 } else {
79 angularVelocity /= scalar;
80 }
81 return *this;
82 }
83 public:
84 float angularVelocity = 0.0f;
86
87 AngularVelocity() = default;
88 AngularVelocity(float angularVelocity, AngularVelocityUnit unit)
89 : angularVelocity(angularVelocity), unit(unit) {}
90
91 void setValue(float newAngularVelocity, AngularVelocityUnit newUnit) {
92 angularVelocity = newAngularVelocity;
93 unit = newUnit;
94 }
95 float getValue(AngularVelocityUnit desiredUnit) const {
96 if (unit == desiredUnit) return angularVelocity;
97 switch (unit) {
98 case AngularVelocityUnit::RadPerSec:
99 if (desiredUnit == AngularVelocityUnit::DegPerSec)
100 return angularVelocity * 180.0f / static_cast<float>(M_PI);
101 break;
102 case AngularVelocityUnit::DegPerSec:
103 if (desiredUnit == AngularVelocityUnit::RadPerSec)
104 return angularVelocity * static_cast<float>(M_PI) / 180.0f;
105 break;
106 }
107 return 0; // Invalid conversion
108 }
109};
110
111/**
112 * @brief Represents a 3D angular velocity vector with unit support.
113 *
114 * This class encapsulates angular velocity in three dimensions (x, y, z),
115 * along with a unit (radians per second or degrees per second). It provides
116 * methods to retrieve each component in any supported unit, handling conversion
117 * as needed.
118 *
119 * AngularVelocity3D is useful for robotics, navigation, and simulation
120 * applications where 3D rotational motion must be represented and manipulated
121 * in a type-safe and unit-aware manner. It is compatible with the
122 * AngularVelocity class for 1D angular velocity.
123 *
124 * Example usage:
125 * AngularVelocity3D w(0.1, 0.0, 0.0, AngularVelocityUnit::RadPerSecond); //
126 * 0.1 rad/s about x float wx_deg = w.getX(AngularVelocityUnit::DegPerSecond);
127 * // Convert x to deg/s
128 */
129/**
130 * @class AngularVelocity3D
131 * @ingroup units
132 * @brief Represents a 3D angular velocity vector with unit support.
133 */
134class AngularVelocity3D {
135 public:
136 float x = 0.0f;
137 float y = 0.0f;
138 float z = 0.0f;
140
141 AngularVelocity3D() = default;
142 AngularVelocity3D(float x, float y, float z, AngularVelocityUnit unit)
143 : x(x), y(y), z(z), unit(unit) {}
144
145 float getX(AngularVelocityUnit desiredUnit) const {
146 if (unit == desiredUnit) return x;
147 AngularVelocity temp(x, unit);
148 return temp.getValue(desiredUnit);
149 }
150 float getY(AngularVelocityUnit desiredUnit) const {
151 if (unit == desiredUnit) return y;
152 AngularVelocity temp(y, unit);
153 return temp.getValue(desiredUnit);
154 }
155 float getZ(AngularVelocityUnit desiredUnit) const {
156 if (unit == desiredUnit) return z;
157 AngularVelocity temp(z, unit);
158 return temp.getValue(desiredUnit);
159 }
160};
161
162} // namespace tinyrobotics
Represents a 3D angular velocity vector with unit support.
Definition: AngularVelocity.h:134
Represents a 1D angular velocity with unit support.
Definition: AngularVelocity.h:18
AngularVelocityUnit
Supported angular velocity units for conversion and representation.
Definition: AngularVelocity.h:11