Calling rospy.init_node() twice in the same python process

asked 2015-11-17 08:06:04 -0600

CatherineH gravatar image

updated 2017-03-23 13:56:48 -0600

130s gravatar image

I want to use python to batch process data with ROS. This is the process that I would like to follow:

For each file/set of variables, from the same python script:

  1. play the bag file as well as some external ros nodes
  2. call rospy.init_node, set up the subscribers
  3. do the processing
  4. shut down the rosmaster process and all child processes
  5. go back to step 1 with different variables or file name

However, the following results in the error:

ROSException: rospy.init_node() has already been called with different arguments:

These are the lines in the rospy specifically that are causing the problem:

 global _init_node_args

# #972: allow duplicate init_node args if calls are identical
# NOTE: we don't bother checking for node name aliases (e.g. 'foo' == '/foo').
if _init_node_args:
    if _init_node_args != (name, argv, anonymous, log_level, disable_rostime, disable_signals):
        raise rospy.exceptions.ROSException("rospy.init_node() has already been called with different arguments: "+str(_init_node_args))
    else:
        return #already initialized

The variable _init_node_args persists even though the main rosmaster has been killed. How can I reset this variable?

edit retag flag offensive close merge delete

Comments

Why does the node need to be reinitialized? What leads you to this conclusion? You want to play multiple bag files, one at a time and each time subscribe to the bag's topics?

Mehdi. gravatar image Mehdi.  ( 2015-11-17 08:13:10 -0600 )edit

Yes, I want to play multiple bag files, one at a time, and ensure that there is no leftover information from a previous run that might taint the results.

CatherineH gravatar image CatherineH  ( 2015-11-17 08:18:52 -0600 )edit

To clarify, by "shut down everything" I mean I kill the rosmaster process and all children at step 4. Hence, I need to call init_node again when the rosmaster and all of its children again, or my subscribers will not work.

CatherineH gravatar image CatherineH  ( 2015-11-17 08:20:53 -0600 )edit

If I understood you want to execute the "rosbag play file.bag" from your python script and subscribe? Why don't you just use the rosbag API to open the rosbags in your script directly and loop over the data?

Mehdi. gravatar image Mehdi.  ( 2015-11-17 08:22:29 -0600 )edit

I tried that, but ran into problems. From what I can tell, rosbag play on the command line works differently from the rosbag API.

CatherineH gravatar image CatherineH  ( 2015-11-17 08:24:43 -0600 )edit