Ask Your Question

Revision history [back]

I usually encapsulate things into a class to avoid using global variables. You could do something like this:

class PoseBroadcaster
{
public:
  PoseBroadcaster()
  {
    pose_sub_ = n_.subscribe("pose", 1000, &PoseBroadcaster::poseCallback, this);
  }
private:
  poseCallback(const nav_msgs::Odometry& pose)
  {
    geometry_msgs::TransformStamped pose_trans;
    pose_trans.header.stamp = pose.header.stamp;
    pose_trans.header.frame_id = "odometry_frame"; 
    pose_trans.child_frame_id = "base_link";

    pose_trans.transform.translation.x = pose.pose.pose.position.x;
    pose_trans.transform.translation.y = pose.pose.pose.position.y;
    pose_trans.transform.translation.z = pose.pose.pose.position.z;
    pose_trans.transform.rotation = pose.pose.pose.orientation;
    ROS_INFO("Sending TF broadcast");
    pose_broadcaster_.sendTransform(pose_trans);
  }

  ros::NodeHandle n_;
  tf::TransformBroadcaster pose_proadcaster_;
};

int main(int argc, char **argv)
{
  ros::init(argc, argv, "pioneer_tf");

  PoseBroadcaster pb;

  ros::spin();
}

I hope this helps