Skip to content

Commit dc90661

Browse files
committed
odometry node cpp
1 parent 33131ac commit dc90661

1 file changed

Lines changed: 54 additions & 3 deletions

File tree

common_interfaces_cpp/src/odometry.cpp

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,65 @@
55
const double PI = M_PI;
66

77
std::string Pose3d::to_string() const {
8-
// ... tu código original ...
8+
std::ostringstream oss;
9+
oss << "Pose3D: {\n x: " << x << "\n y: " << y
10+
<< "\n z: " << z << "\n H: " << h
11+
<< "\n Yaw: " << yaw << "\n Pitch: " << pitch
12+
<< "\n Roll: " << roll
13+
<< "\n quaternion: [" << q[0] << ", " << q[1] << ", " << q[2] << ", " << q[3] << "]"
14+
<< "\n timeStamp: " << timeStamp << "\n}";
15+
return oss.str();
916
}
1017

1118
double quat2Yaw(double qw, double qx, double qy, double qz) {
12-
// ... tu código original ...
19+
double rotateZa0 = 2.0 * (qx * qy + qw * qz);
20+
double rotateZa1 = qw * qw + qx * qx - qy * qy - qz * qz;
21+
double rotateZ = 0.0;
22+
if (rotateZa0 != 0.0 && rotateZa1 != 0.0) {
23+
rotateZ = std::atan2(rotateZa0, rotateZa1);
24+
}
25+
return rotateZ;
1326
}
1427

15-
// ... resto de implementaciones exactas que ya tienes ...
28+
double quat2Pitch(double qw, double qx, double qy, double qz) {
29+
double rotateYa0 = -2.0 * (qx * qz - qw * qy);
30+
double rotateY = 0.0;
31+
if (rotateYa0 >= 1.0) {
32+
rotateY = PI / 2.0;
33+
} else if (rotateYa0 <= -1.0) {
34+
rotateY = -PI / 2.0;
35+
} else {
36+
rotateY = std::asin(rotateYa0);
37+
}
38+
return rotateY;
39+
}
40+
41+
double quat2Roll(double qw, double qx, double qy, double qz) {
42+
double rotateXa0 = 2.0 * (qy * qz + qw * qx);
43+
double rotateXa1 = qw * qw - qx * qx - qy * qy + qz * qz;
44+
double rotateX = 0.0;
45+
46+
if (rotateXa0 != 0.0 && rotateXa1 != 0.0) {
47+
rotateX = std::atan2(rotateXa0, rotateXa1);
48+
}
49+
return rotateX;
50+
}
51+
52+
Pose3d odometry2Pose3D(const nav_msgs::msg::Odometry& odom) {
53+
Pose3d pose;
54+
auto ori = odom.pose.pose.orientation;
55+
56+
pose.x = odom.pose.pose.position.x;
57+
pose.y = odom.pose.pose.position.y;
58+
pose.z = odom.pose.pose.position.z;
59+
pose.yaw = quat2Yaw(ori.w, ori.x, ori.y, ori.z);
60+
pose.pitch = quat2Pitch(ori.w, ori.x, ori.y, ori.z);
61+
pose.roll = quat2Roll(ori.w, ori.x, ori.y, ori.z);
62+
pose.q = {ori.w, ori.x, ori.y, ori.z};
63+
pose.timeStamp = odom.header.stamp.sec + (odom.header.stamp.nanosec * 1e-9);
64+
65+
return pose;
66+
}
1667

1768
OdometryNode::OdometryNode(const std::string& topic, const std::string& node_name)
1869
: Node(node_name) {

0 commit comments

Comments
 (0)