Skip to content

Commit d39341e

Browse files
author
plnegre
committed
Add new tf tool
1 parent 0a1671f commit d39341e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

tf_tools/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ include_directories(${catkin_INCLUDE_DIRS})
1111

1212
add_executable(tf_logger src/tf_logger.cpp)
1313
add_executable(tf_filter src/tf_filter.cpp)
14+
add_executable(apply_tf_to_odom_msg src/apply_tf_to_odom_msg.cpp)
1415

1516
target_link_libraries(tf_logger ${catkin_LIBRARIES})
1617
target_link_libraries(tf_filter ${catkin_LIBRARIES})
18+
target_link_libraries(apply_tf_to_odom_msg ${catkin_LIBRARIES})
1719

tf_tools/src/apply_tf_to_odom_msg.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <ros/ros.h>
2+
#include <nav_msgs/Odometry.h>
3+
#include <tf/transform_datatypes.h>
4+
5+
using namespace std;
6+
7+
class ApplyTF2Odom
8+
{
9+
ros::NodeHandle nh_;
10+
ros::NodeHandle nh_private_;
11+
12+
ros::Subscriber odom_sub_;
13+
ros::Publisher odom_pub_;
14+
15+
tf::Transform trans_;
16+
17+
public:
18+
ApplyTF2Odom() : nh_private_("~")
19+
{
20+
// Read parameters
21+
double x, y, z, qx, qy, qz, qw;
22+
nh_private_.param("x", x, 0.0);
23+
nh_private_.param("y", y, 0.0);
24+
nh_private_.param("z", z, 0.0);
25+
nh_private_.param("qx", qx, 0.0);
26+
nh_private_.param("qy", qy, 0.0);
27+
nh_private_.param("qz", qz, 0.0);
28+
nh_private_.param("qw", qw, 0.0);
29+
30+
// Build tf
31+
tf::Vector3 t(x, y, z);
32+
tf::Quaternion q(qx, qy, qz, qw);
33+
tf::Transform tmp(q, t);
34+
trans_ = tmp;
35+
36+
// Messages
37+
odom_sub_ = nh_.subscribe<nav_msgs::Odometry>("odometry_in", 1, &ApplyTF2Odom::callback, this);
38+
odom_pub_ = nh_private_.advertise<nav_msgs::Odometry>("odometry_out", 1);
39+
}
40+
41+
void callback(const nav_msgs::Odometry::ConstPtr& odom_msg)
42+
{
43+
// Get the data
44+
double x = odom_msg->pose.pose.position.x;
45+
double y = odom_msg->pose.pose.position.y;
46+
double z = odom_msg->pose.pose.position.z;
47+
48+
double qx = odom_msg->pose.pose.orientation.x;
49+
double qy = odom_msg->pose.pose.orientation.y;
50+
double qz = odom_msg->pose.pose.orientation.z;
51+
double qw = odom_msg->pose.pose.orientation.w;
52+
53+
// Build the odometry pose
54+
tf::Vector3 t(x, y, z);
55+
tf::Quaternion q(qx, qy, qz, qw);
56+
tf::Transform pose(q, t);
57+
58+
// Transform
59+
tf::Transform new_pose = pose * trans_;
60+
61+
// Publish
62+
nav_msgs::Odometry new_odom_msg = *odom_msg;
63+
tf::poseTFToMsg(new_pose, new_odom_msg.pose.pose);
64+
odom_pub_.publish(new_odom_msg);
65+
}
66+
};
67+
68+
int main(int argc, char** argv)
69+
{
70+
ros::init(argc, argv, "apply_tf_to_odom_msg");
71+
ApplyTF2Odom node;
72+
ros::spin();
73+
return 0;
74+
}
75+

0 commit comments

Comments
 (0)