apply_tf_to_odom_msg.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <nav_msgs/Odometry.h>
00003 #include <tf/transform_datatypes.h>
00004 
00005 using namespace std;
00006 
00007 class ApplyTF2Odom
00008 {
00009   ros::NodeHandle nh_;
00010   ros::NodeHandle nh_private_;
00011 
00012   ros::Subscriber odom_sub_;
00013   ros::Publisher odom_pub_;
00014 
00015   tf::Transform trans_;
00016 
00017 public:
00018   ApplyTF2Odom() : nh_private_("~")
00019   {
00020     // Read parameters
00021     double x, y, z, qx, qy, qz, qw;
00022     nh_private_.param("x", x, 0.0);
00023     nh_private_.param("y", y, 0.0);
00024     nh_private_.param("z", z, 0.0);
00025     nh_private_.param("qx", qx, 0.0);
00026     nh_private_.param("qy", qy, 0.0);
00027     nh_private_.param("qz", qz, 0.0);
00028     nh_private_.param("qw", qw, 1.0);
00029 
00030     // Build tf
00031     tf::Vector3 t(x, y, z);
00032     tf::Quaternion q(qx, qy, qz, qw);
00033     tf::Transform tmp(q, t);
00034     trans_ = tmp;
00035 
00036     // Messages
00037     odom_sub_ = nh_.subscribe<nav_msgs::Odometry>("odometry_in", 1, &ApplyTF2Odom::callback, this);
00038     odom_pub_ = nh_private_.advertise<nav_msgs::Odometry>("odometry_out", 1);
00039   }
00040 
00041   void callback(const nav_msgs::Odometry::ConstPtr& odom_msg)
00042   {
00043     // Get the data
00044     double x = odom_msg->pose.pose.position.x;
00045     double y = odom_msg->pose.pose.position.y;
00046     double z = odom_msg->pose.pose.position.z;
00047 
00048     double qx = odom_msg->pose.pose.orientation.x;
00049     double qy = odom_msg->pose.pose.orientation.y;
00050     double qz = odom_msg->pose.pose.orientation.z;
00051     double qw = odom_msg->pose.pose.orientation.w;
00052 
00053     // Build the odometry pose
00054     tf::Vector3 t(x, y, z);
00055     tf::Quaternion q(qx, qy, qz, 1.0);
00056     tf::Transform pose(q, t);
00057 
00058     // Transform
00059     tf::Transform new_pose = trans_ * pose;
00060 
00061     // Publish
00062     nav_msgs::Odometry new_odom_msg = *odom_msg;
00063     tf::poseTFToMsg(new_pose, new_odom_msg.pose.pose);
00064     odom_pub_.publish(new_odom_msg);
00065   }
00066 };
00067 
00068 int main(int argc, char** argv)
00069 {
00070   ros::init(argc, argv, "apply_tf_to_odom_msg");
00071   ApplyTF2Odom node;
00072   ros::spin();
00073   return 0;
00074 }
00075 


tf_tools
Author(s): Stephan Wirth , Miquel Massot
autogenerated on Sat Jun 8 2019 20:10:24