ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How do I compose a launch file that launches my nodes after dependency nodes?

asked 2020-12-30 14:39:50 -0500

3noch gravatar image

I want a launch file that starts my node but only after it's started several other dependency nodes. How can I achieve this?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2020-12-31 01:31:03 -0500

melwin gravatar image

This cannot be done with launchfiles (directly) because the order in which nodes are launched is not guaranteed. Also, what it means for a node to have "started" is not well defined in general: is it when the OS creates the process? it starts executing? registers with the rosmaster? After it sets up publishers/subscribers?

From http://wiki.ros.org/roslaunch/XML/node:

roslaunch does not provide any guarantees about what order nodes start in. This is intentional: there is no way to externally know when a node is fully initialized, so all code that is launched must be robust to launching in any order.

You may be able to do what you want to do with the roslaunch api http://wiki.ros.org/roslaunch/API%20U... although it is (in my opinion) usually easier and preferable to make the nodes not depend on start order.

Making the nodes not depend on startup order is really the best option and the proper ROS thing to do.

However, if you just need a quick hack to get something done (I definitely DO NOT recommend this long-term)

  1. Write a script (wait.bash) that waits for the node to be launched (for example)

    #!/usr/bin/env bash
    roslaunch package with_the_dependee_nodes_that_start_first & 
    until rosnode list | grep "node"; do
      :
    done
    echo "done"
    
  2. Write the another launchfile that launches the dependent nodes (that start last) and in that launchfile, also set a parameter using the script from part 1 as the command attribute:

    < param name="node_start" command="$(find pkg)/scripts/wait.bash">

  3. The parameters are guaranteed to be loaded prior to starting any nodes rhttp://wiki.ros.org/roslaunch/XML#Se....

Thus what happens is:

  1. The first launchfile, it tries to set the parameter node_start by running your script. It can only start the nodes after the parameter is set (and hence after the script has completed).
  2. The script then runs a launchfile to start the dependee nodes and waits until they have "started" (at least registered themselves with the rosmaster)
  3. Finally the main launchfile begins launching the other nodes
edit flag offensive delete link more

Comments

Thank you! It all makes perfect sense. Can you provide any resources on making nodes resilient to start order?

3noch gravatar image 3noch  ( 2020-12-31 12:21:41 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-12-30 14:39:50 -0500

Seen: 1,866 times

Last updated: Dec 31 '20