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

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

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

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
1

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

lucasw gravatar image

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

You are right about the NodeHandle in StaticTransformBroadcaster https://github.com/ros/geometry2/blob...

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 https://github.com/ros/geometry2/blob... - 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

Comments

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

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

Question Tools

2 followers

Stats

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

Seen: 182 times

Last updated: Apr 08 '19