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

Static tf broadcaster declaration seems to cause nodehandle to be created.

asked 2019-04-07 00:39:23 -0600

My code is something like this-

#include <ros/ros.h>
tf2_ros::StaticTransformBroadcaster static_broadcaster;
ros::Subscriber fiducial_transform;

int main(int argc, char **argv)
  ros::init(argc, argv, "calibrate");
  ros::NodeHandle n;
   return 0;

I am getting the following error.

 You must call ros::init() before creating the first NodeHandle

Pretty sure it is happening because of declaring the static_broadcaster. Why is this happening? How do I get around this if I have to declare the staticbroadcaster globally?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-04-08 09:13:00 -0600

lucasw gravatar image

updated 2019-04-08 09:14:04 -0600

You are right about the NodeHandle in StaticTransformBroadcaster

You should put the subscriber and the static broadcaster inside a class and construct that after ros::init(), and pass it around as needed instead of the globals if other objects need to use it.

Or if you insist then don't use StaticTransformBroadcaster, instead have a global ros publisher publish tf2_msgs::TFMessage as needed. The broadcaster isn't doing a whole lot as you can see in - it doesn't take much to duplicate the functionality.

Or make static_broadcaster a pointer and again wait until after ros::init to create it.

edit flag offensive delete link more


Or use a smart/bare ptr instead of a plain object.

gvdhoorn gravatar image gvdhoorn  ( 2019-04-08 09:16:22 -0600 )edit

Question Tools



Asked: 2019-04-07 00:39:23 -0600

Seen: 180 times

Last updated: Apr 08 '19