Ask Your Question
0

tf2 sendTransform() not actually sending anything

asked 2019-05-07 17:35:11 -0500

256shadesofgrey gravatar image

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;
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-05-09 05:20:25 -0500

gvdhoorn gravatar image

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.

edit flag offensive delete link more
0

answered 2019-05-08 16:03:34 -0500

256shadesofgrey gravatar image

updated 2019-05-08 16:04:10 -0500

Steps that resolved the problem for me:

  1. Switched to using tf instead of tf2.

  2. Made the broadcaster variable static.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-05-07 17:35:11 -0500

Seen: 464 times

Last updated: May 08 '19