|
| 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