Avoid starting multiple node instances with the same type

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

130s gravatar image

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

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 image ahendrix  ( 2017-10-08 02:59:08 -0500 )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 image gvdhoorn  ( 2017-10-08 03:38:43 -0500 )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 image gvdhoorn  ( 2017-10-08 03:40:23 -0500 )edit

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

gvdhoorn gravatar image gvdhoorn  ( 2017-10-08 06:51:08 -0500 )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 image Ruben Alves  ( 2017-10-13 18:13:13 -0500 )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 image gvdhoorn  ( 2017-10-14 02:43:03 -0500 )edit