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

Set delay between starting nodes within launch file

asked 2016-05-01 07:08:15 -0500

user23fj239 gravatar image

updated 2021-11-16 11:06:38 -0500

lucasw gravatar image

I would like to put my nodes inside a launch file. The first node need to be up and running before the second node. Is it possible to set some timeout before the second starts. Like usleep(time) in roscpp

<launch>
<node name="talker" pkg="roscpp_tutorials" type="talker" />
//delay of e.g. 1second
<node name="listener" pkg="roscpp_tutorials" type="listener" />
</launch>

Is this technically possible?

edit retag flag offensive close merge delete

Comments

1

Ros has no such feature. Why do you need this function? What do you really want to achieve? Having hardcoded dependencies like this is a bad sign. What happens to your second node if the first one has to be restarted?

NEngelhard gravatar image NEngelhard  ( 2016-05-01 08:41:41 -0500 )edit

Thank you, but I do not face dependencies, this was about is it theoretically possible. E.g. holding a presentation and not wanting to go to the pc. I say some stuff about the window popping up of the first node, then after 30s I can gently move over the the second. But this is ok.

user23fj239 gravatar image user23fj239  ( 2016-05-01 09:02:46 -0500 )edit

A potential proper solution is for one node to wait until another node is ready before properly starting up, similar to how rosservice call --wait works. You can probably also use a service to indicate whether a node has started up. In fact, it'd probably be nice if nodes by default took a "wait" arg that allows setting dependencies between nodes

Rufus gravatar image Rufus  ( 2020-01-09 19:34:12 -0500 )edit

3 Answers

Sort by » oldest newest most voted
38

answered 2019-02-21 10:35:58 -0500

kushlik gravatar image

I found a one-line solution that does not require any additional packages (it was a bit tricky):

You can make use of launch-prefix feature like so:

<arg name="node_start_delay" default="1.0" />  
<node name="listener" pkg="roscpp_tutorials" type="listener" launch-prefix="bash -c 'sleep $(arg node_start_delay); $0 $@' " />

the problem with using just launch-prefix="sleep 1.0; " was that the node launching command was passed as arguments to 'sleep'

edit flag offensive delete link more

Comments

Thank you! That worked for me!

Rosbq gravatar image Rosbq  ( 2019-05-29 08:17:18 -0500 )edit

This saved my day! I was doing delays with timed_roslaunch before, but creating a launch file for every node was a bit painful. Thank you!

akim gravatar image akim  ( 2020-06-27 03:13:03 -0500 )edit

cool idea! thanks

jorge gravatar image jorge  ( 2021-01-26 19:20:29 -0500 )edit
1

Great answer! What’s the meaning for $0 $@?

sean gravatar image sean  ( 2021-09-09 05:34:18 -0500 )edit
2

$0 is the name of the node that roslaunch will call and $@ are the arguments to that node / process. The they was to separate the process executable (node) and its arguments, otherwise the sleep function would not work. $0 and $@ are both bash syntax, you can check documentation for additional info.

kushlik gravatar image kushlik  ( 2021-09-10 10:01:04 -0500 )edit

Is there a way to delay launching the part in "<include>"?

zkytony gravatar image zkytony  ( 2022-03-08 21:17:55 -0500 )edit

Hi @zkytony did you figure this out?

ThreeForElvenKings gravatar image ThreeForElvenKings  ( 2023-05-02 18:08:19 -0500 )edit
6

answered 2018-03-19 08:36:23 -0500

tik0 gravatar image

updated 2018-03-19 11:05:55 -0500

You can perform a workaround by putting the delayed node into an extra launch file and run it via timed_roslaunch. Explanation goes here. Script is available here.

In your case:

<launch>
<node name="talker" pkg="roscpp_tutorials" type="talker" />
<node pkg="timed_roslaunch" type="timed_roslaunch.sh" args="1 roscpp_tutorials my_listener.launch" name="timed_roslaunch" output="screen" />
</launch>

Content of my_listener.launch

<launch>
<node name="listener" pkg="roscpp_tutorials" type="listener" />
</launch>
edit flag offensive delete link more

Comments

just what is was looking for

jstm gravatar image jstm  ( 2018-12-11 15:04:00 -0500 )edit
4

answered 2016-05-05 22:06:57 -0500

l0g1x gravatar image

Currently there is no such implementation. However, this was a topic brought up at the last ROS Live meeting, and seems that the idea is to try and allow a user to have more 'coding' freedom from within a launch file. I have seen some workarounds that people have posted in the past, and what they did was created a regular bash script that called roslaunch files from within itself, as it was easier (or maybe currently the only way) to implement a delay between roslaunch starts.

edit flag offensive delete link more

Comments

2

To slightly clarify @l0g1x's answer, he means delaying by adding commands of the form sleep 5 (delay of 5 sec) between the desired roslaunch / rosrun commands in the bash script. (Right?)

spmaniato gravatar image spmaniato  ( 2016-05-06 07:42:09 -0500 )edit

Yes that is correct

l0g1x gravatar image l0g1x  ( 2016-05-06 13:06:07 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2016-05-01 07:08:15 -0500

Seen: 31,365 times

Last updated: Feb 21 '19