Avoid starting multiple node instances with the same type

asked 2017-10-07 22:56:36 -0600

130s gravatar image

updated 2017-10-07 23:01:30 -0600

It's possible by mistake to start the same launch file that's already been running so that the same nodes get started and messed up. What's a good way to avoid this situation?

We can obviously write a custom script to look for the node names and take actions we want to (I'm posting pseudo code at the bottom). But because roslaunch doesn't guarantee the order of execution this would work only unreliably.

I'm mainly (still) on ROS Indigo. I have a node that involves certain physical costly setup upon launch, so rebooting the nodes often isn't an option.

Pseudo launch:

<launch>
  <arg name="nodename_a" default="nodename_a" />
  <node type="node_detect.py" args="$(arg nodename_a)" required="true" respawn="false" />  <!-- Exits if nodename_a found, which kills the entire launch. -->
  <node type="node_a.py" name="$(arg nodename_a)" />
  :
</launch>

Pseudo script:

if nodename_a in running_nodes:
    sys.exit()
edit retag flag offensive close merge delete

Comments

1

Maybe you can write a launch-prefix that checks if the node is running, or if you're in python you may be able to query the master for the node name before you call rospy.init and avoid starting the node if there's another copy already running?

ahendrix gravatar imageahendrix ( 2017-10-08 02:59:08 -0600 )edit
1

The launch-prefix is a nice idea. It could do whatever and decide whether or not to start the actual ROS node.

gvdhoorn gravatar imagegvdhoorn ( 2017-10-08 03:38:43 -0600 )edit

@130s: are you starting things by hand in an actual (deployed) application? I know ROS1 doesn't really have any infrastructure for it, but what you describe is something that cannot happen if you have a system-level coordinator that is in charge or starting and stopping nodes.

gvdhoorn gravatar imagegvdhoorn ( 2017-10-08 03:40:23 -0600 )edit

Just noticed: is this really about type, or name?

gvdhoorn gravatar imagegvdhoorn ( 2017-10-08 06:51:08 -0600 )edit

Assuming that the process is automated and that you want to launch the rospy_tutorials talker_listener.launch, you can check if it's already launched with: ps faux | grep -v grep| grep "rospy_tutorials talker_listener.launch"

If nothing is printed, the the launch file is not running.

Ruben Alves gravatar imageRuben Alves ( 2017-10-13 18:13:13 -0600 )edit
1

I believe @130s is looking for a bit more granular approach. A launch file could include others launch files (ad infinitum) and start tenths of nodes.

gvdhoorn gravatar imagegvdhoorn ( 2017-10-14 02:43:03 -0600 )edit