5#include "TinyRobotics/coordinates/Coordinate.h"
6#include "TinyRobotics/coordinates/GPSCoordinate.h"
7#include "TinyRobotics/utils/Common.h"
8#include "TinyRobotics/utils/LoggerClass.h"
10namespace tinyrobotics {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
45 Transform2D(
float x,
float y,
float heading_deg)
47 Transform2D(
const Coordinate<DistanceM>& coord,
float heading_deg)
59
60
61
62
64 float theta =
heading_deg *
static_cast<
float>(M_PI) / 180.0f;
65 float x = std::cos(theta) * local.x - std::sin(theta) * local.y + pos.x;
66 float y = std::sin(theta) * local.x + std::cos(theta) * local.y + pos.y;
67 return Coordinate<DistanceM>(x, y);
71
72
73
75 float theta_rad =
heading_deg *
static_cast<
float>(M_PI) / 180.0f;
76 float cos_theta = std::cos(theta_rad);
77 float sin_theta = std::sin(theta_rad);
78 float nx = pos.x + cos_theta * other.pos.x - sin_theta * other.pos.y;
79 float ny = pos.y + sin_theta * other.pos.x + cos_theta * other.pos.y;
81 nheading = normalizeAngleDeg(nheading);
86
87
89 float theta_rad =
heading_deg *
static_cast<
float>(M_PI) / 180.0f;
90 float cos_theta = std::cos(theta_rad);
91 float sin_theta = std::sin(theta_rad);
92 float ix = -pos.x * cos_theta - pos.y * sin_theta;
93 float iy = pos.x * sin_theta - pos.y * cos_theta;
95 iheading = normalizeAngleDeg(iheading);
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
131 Frame2D(
FrameType type, uint8_t idx = 0) : type(type), index(idx) {}
133 : type(type), index(idx), p_parent(&parent) {}
135 : type(type), index(idx), p_parent(&parent), tf(tf) {}
137 bool operator==(
const Frame2D& o)
const {
138 return type == o.type && index == o.index;
141 FrameType getType()
const {
return type; }
142 uint8_t getIndex()
const {
return index; }
143 const Frame2D* getParent()
const {
return p_parent; }
144 void setParent(
Frame2D& parent) { p_parent = &parent; }
145 const Transform2D& getTransform()
const {
return tf; }
147 void setTransform(
const Transform2D& transform) { tf = transform; }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
198 FrameMgr2D() =
default;
204 float rotationDeg = 90) {
205 p_gpsParent = &parent;
207 gpsRotationDeg = rotationDeg;
214 "FrameMgr2D: GPS parent frame not set, cannot convert to GPS");
220 float theta_rad = gpsRotationDeg *
static_cast<
float>(M_PI) / 180.0f;
221 float cos_theta = std::cos(theta_rad);
222 float sin_theta = std::sin(theta_rad);
223 float dx = cos_theta * tf.pos.x - sin_theta * tf.pos.y;
224 float dy = sin_theta * tf.pos.x + cos_theta * tf.pos.y;
226 float distance = std::sqrt(dx * dx + dy * dy);
227 float bearing_deg = std::atan2(dx, dy) * 180.0f /
228 static_cast<
float>(M_PI);
229 bearing_deg = normalizeAngleDeg(bearing_deg);
235
236
237
238
239
242 std::vector<
const Frame2D*> from_path = getPathToRoot(from);
243 std::vector<
const Frame2D*> to_path = getPathToRoot(to);
246 int i = from_path.size() - 1;
247 int j = to_path.size() - 1;
249 while (i >= 0 && j >= 0 && from_path[i] == to_path[j]) {
257 for (
int k = 0; k <= i; ++k) {
258 tf_from = from_path[k]->getTransform() * tf_from;
264 for (
int k = j; k >= 0; --k) {
265 tf_to = tf_to * to_path[k]->getTransform();
269 return tf_to
* tf_from;
273 Frame2D* p_gpsParent =
nullptr;
275 float gpsRotationDeg;
278
279
280
282 std::vector<
const Frame2D*> path;
283 const Frame2D* current = &frame;
285 path.push_back(current);
286 current = current->getParent();
Represents an angle with unit conversion and wrap-around support.
Definition: Angle.h:42
A generic 3D coordinate class for robotics, navigation, and spatial calculations.
Definition: Coordinate.h:57
Manages a hierarchy of 2D coordinate frames and enables SE(2) transforms and GPS conversion.
Definition: FrameMgr2D.h:196
GPSCoordinate toGPS(const Frame2D &frame) const
Converts a local frame to a GPS coordinate.
Definition: FrameMgr2D.h:211
Transform2D getTransform(const Frame2D &from, const Frame2D &to) const
Computes the transform from one frame to another using SE(2) math.
Definition: FrameMgr2D.h:240
void setGPS(Frame2D &parent, const GPSCoordinate gps, float rotationDeg=90)
Definition: FrameMgr2D.h:203
std::vector< const Frame2D * > getPathToRoot(const Frame2D &frame) const
Returns the path from the given frame up to the root (including self). The path is verly small,...
Definition: FrameMgr2D.h:281
Represents a geodetic GPS coordinate with latitude, longitude, and optional altitude.
Definition: GPSCoordinate.h:52
GPSCoordinate navigate(float distance_m, float bearing_deg, float alt_diff_m=0) const
Calculate a new GPS coordinate given a distance (in meters) and bearing.
Definition: GPSCoordinate.h:114
AngleUnit
Supported angle units for conversion and representation.
Definition: Angle.h:11
FrameType
Frame type for coordinate systems and reference frames.
Definition: Common.h:15
Represents a 2D coordinate frame in a hierarchical frame tree.
Definition: FrameMgr2D.h:130