0
$\begingroup$

Rosanswers logo

I'm trying to send a transformation from "world" to "base_link" based on coordinates I get from a different node. The node is actually running, and it is reaching the ROS_INFO() right before the broadcaster.sendTransform(transformStamped); (so the other node is working, and the tf2 broadcaster node gets the data and the callback is called repeatedly), but when I run rosrun tf tf_monitor to see what I get, it's empty:

RESULTS: for all Frames

Frames:

All Broadcasters:

I've been trying to find the mistake for 2 days now and I don't see what I'm doing wrong. Any advise?

#include <ros/ros.h>
#include <tf2_ros/transform_broadcaster.h>
#include <geometry_msgs/TransformStamped.h>
#include <cstdio>

#include "geometry_msgs/PoseStamped.h"

void posCallback(const geometry_msgs::PoseStamped& msg){
  tf2_ros::TransformBroadcaster broadcaster;
  geometry_msgs::TransformStamped transformStamped;

  transformStamped.header.stamp = ros::Time::now();
  transformStamped.header.frame_id = "world";
  transformStamped.child_frame_id = "base_link";
  transformStamped.transform.translation.x = msg.pose.position.x;
  transformStamped.transform.translation.y = msg.pose.position.y;
  transformStamped.transform.translation.z = msg.pose.position.z;
  transformStamped.transform.rotation.x = msg.pose.orientation.x;
  transformStamped.transform.rotation.y = msg.pose.orientation.y;
  transformStamped.transform.rotation.z = msg.pose.orientation.z;
  transformStamped.transform.rotation.w = msg.pose.orientation.w;

  ROS_INFO("broadcast");

  broadcaster.sendTransform(transformStamped);
}

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

  ros::NodeHandle node;
  ros::Subscriber sub = node.subscribe("positioning", 1000, posCallback);

  ros::spin();
  return 0;
}

Originally posted by 256shadesofgrey on ROS Answers with karma: 1 on 2019-05-07

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The first should not make a difference (TF is actually implemented on-top of TF2 for a long time now).

The second is the most likely cause here: static is not necessarily needed, but placing broadcaster in a scope that outlives that of posCallback(..) is needed, as otherwise there is no time for any subscribers to properly initialise and form a connection to your broadcaster before the message is sent. That will lead to lost messages which in turn leads to your subscribers not receiving your frames.


Originally posted by gvdhoorn with karma: 86574 on 2019-05-09

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.