ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

StaticTransformBroadcaster does not broadcast

asked 2014-08-26 05:56:29 -0500

TheElk gravatar image

Hey, I tried to implement a simple StaticTransformBroadcaster with the following code:

#include <ros/ros.h>

#include <tf2_ros/static_transform_broadcaster.h>
#include <tf2/transform_datatypes.h>

#include <geometry_msgs/TransformStamped.h>
#include <geometry_msgs/Transform.h>

void publishStaticTransformation();
int main (int argc, char** argv)
{
    ros::init(argc, argv, "StaticBroadcaster");

    ROS_INFO("StaticBroadcaster started");
    publishStaticTransformation();
    ros::spin();
}

void publishStaticTransformation()
{
    tf2_ros::StaticTransformBroadcaster staticBroadcaster2;

    geometry_msgs::TransformStamped msg;
    msg.header.stamp = ros::Time::now();

    msg.header.frame_id = "base_link";
    msg.child_frame_id = "child_2";

    msg.transform.rotation.x = 2.0;
    msg.transform.rotation.y = 2.0;
    msg.transform.rotation.z = 0.0;
    msg.transform.rotation.w = 1.0;

    msg.transform.translation.x = 0;
    msg.transform.translation.y = 0;
    msg.transform.translation.z = 0;

    staticBroadcaster2.sendTransform(msg);
    ROS_INFO_STREAM("published");
}

This doesn't work but i don't know why. "published" is printed on the Console but rqt_graph shows no connection between my broadcaster and the tf_static topic. However, wehn I Create the StaticTransformBroadcaster first and pass it as an argument to my publishStaticTransformation-method it works.

#include <ros/ros.h>

#include <tf2_ros/static_transform_broadcaster.h>
#include <tf2/transform_datatypes.h>

#include <geometry_msgs/TransformStamped.h>
#include <geometry_msgs/Transform.h>

void publishStaticTransformation(tf2_ros::StaticTransformBroadcaster &staticBroadcaster);

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

    ROS_INFO("StaticBroadcaster started");
    tf2_ros::StaticTransformBroadcaster staticBroadcaster;
    publishStaticTransformation(staticBroadcaster);
    ros::spin();
}

void publishStaticTransformation(tf2_ros::StaticTransformBroadcaster &staticBroadcaster)
{
    geometry_msgs::TransformStamped msg;

    msg.header.stamp = ros::Time::now();

    msg.header.frame_id = "base_link";
    msg.child_frame_id = "child_2";

    msg.transform.rotation.x = 2.0;
    msg.transform.rotation.y = 2.0;
    msg.transform.rotation.z = 0.0;
    msg.transform.rotation.w = 1.0;

    msg.transform.translation.x = 0;
    msg.transform.translation.y = 0;
    msg.transform.translation.z = 0;

    staticBroadcaster.sendTransform(msg);
}

Does anyone know, what I'm doing wrong?
Any help is appreciated

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2014-08-26 08:49:31 -0500

bvbdort gravatar image

The reason was tf2_ros::StaticTransformBroadcaster has a ros::publisher to publish transforms.

In the first case tf2_ros::StaticTransformBroadcaster staticBroadcaster2 go out of scope , all the callback will be stopped being callded, which is not the case for second one.


From ros::Publisher Class Reference

Once all copies of a specific Publisher go out of scope, any subscriber status callbacks associated with that handle will stop being called. Once all Publishers for a given topic go out of scope the topic will be unadvertised.

edit flag offensive delete link more

Comments

Tank you very much for your answer !

TheElk gravatar image TheElk  ( 2014-08-26 09:43:07 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-08-26 05:56:29 -0500

Seen: 492 times

Last updated: Aug 26 '14