# Does rospy.init_node need to be called everytime?

I developed two ROS packages and, until very recently, 1-2 days ago, I was calling one script from package A (that I developed) with rosrun (without ever calling roscore or whatever), which uses another package that I developed B. None of these packages, i.e. A or B, call rospy.init_node. However, today, I tried again to run package A's with rosrun A the_script (as usual), and I get the error

  line 150, in sync
filters[topics.index(topic)].signalMessage(msg)
File "/opt/ros/melodic/lib/python2.7/dist-packages/message_filters/__init__.py", line 58, in signalMessage
cb(*(msg + args))
File "/opt/ros/melodic/lib/python2.7/dist-packages/message_filters/__init__.py", line 264, in add
now = rospy.Time.now()
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/rostime.py", line 155, in now
return get_rostime()
File "/opt/ros/melodic/lib/python2.7/dist-packages/rospy/rostime.py", line 190, in get_rostime
raise rospy.exceptions.ROSInitException("time is not initialized. Have you called init_node()?")
rospy.exceptions.ROSInitException: time is not initialized. Have you called init_node()?


I am using signalMessage to synchronize a .bag file and the problem is caused by this, given that the error is raised at line filters[topics.index(topic)].signalMessage(msg).

But, as I said above, I never called init_node before and I never got this error and now I am getting it, for some reason. Why? How do I solve this problem? I didn't update ROS (as far as I know), but I only executed the latest software updates in Ubuntu. In fact, cat /var/log/dpkg.log | grep "\ install\ " does not apparently produce any ROS-related package. By the way, I already tried to do catkin build, but I still get the error.

Well, I added rospy.init_node('bag_synchronizer', anonymous=True, disable_signals=True) to the class that uses signalMessage, then I roscore and executed the script again, and now I don't have the error anymore, but I would like to know why I was able to execute that script without rospy.init_node before (I did this so many times) and now I can't do it anymore. My code does not see to publish or read topics, so I don't need to initialised the node, as far I understand.

edit retag close merge delete

Sort by » oldest newest most voted

The answer to your question depends on what else you were doing in the script before, when it did work. If you aren't using publishers, subscribers, reading from the param server, etc., then you would not need to call init_node. It seems like you added this linefilters[topics.index(topic)].signalMessage(msg) that requires rospy.get_rostime to work -- running init_node would allow that method to work.

More generally, you'd be able to use rosrun to run any executable Python script (or any other scripting language) in a ROS package, even if it never calls init_node -- rosrun doesn't even require the script to have anything ROS-related. But, if you use Python methods that require a node to be initialized, then you will need to call init_node.

more

1

@jarvisschultz: I've converted this to an answer as it is the answer.

As soon as any infrastructure from rospy is used which requires a node, init_node(..) needs to be called.

Two good examples -- which are sometimes not obvious -- are time and parameters. Both need a node to function correctly. The traceback the OP includes shows use of rospy.Time.now(), which falls in this category.

( 2020-08-18 01:08:00 -0500 )edit

I did not add anything. filters[topics.index(topic)].signalMessage(msg) was there since the beginning. @gvdhoorn How come then that I always used signalMessage since the beginning and I never got that error before?

( 2020-08-18 05:42:07 -0500 )edit

I wouldn't know. But the time infrastructure is initialised as part of init_node(..), so you cannot really use that without calling it.

( 2020-08-18 05:58:29 -0500 )edit