Calling rospy.init_node() twice in the same python process
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:
- play the bag file as well as some external ros nodes
- call rospy.init_node, set up the subscribers
- do the processing
- shut down the rosmaster process and all child processes
- 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?
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?
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.
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.
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?
I tried that, but ran into problems. From what I can tell, rosbag play on the command line works differently from the rosbag API.