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

Call a service at startup with a launch file

asked 2011-09-21 01:02:04 -0600

Is it possible to call a service from a launch file once at startup? We want to call the service /global_localization of AMCL the first time we launch our robot.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2011-09-21 08:31:22 -0600

bhaskara gravatar image

updated 2021-09-14 11:42:33 -0600

lucasw gravatar image

Just have the call be its own node in the launch file:

<node pkg="rosservice" type="rosservice" name="global_loc" args="call --wait /global_localization" />
edit flag offensive delete link more


That was my first idea, too. But as Lorenz wrote above, roslaunch tends to pass additional parameters which let rosservice choke.
roehling gravatar image roehling  ( 2011-09-21 19:44:25 -0600 )edit

This worked for me, I just added that line verbatim directly under my AMCL launch line. I like this method better, it just feels right, light, and clean. :)

Dereck gravatar image Dereck  ( 2012-10-22 08:14:35 -0600 )edit

Apparently this no longer works in Groovy.... Comment here if you want this feature fixed I suppose:

Dereck gravatar image Dereck  ( 2013-02-28 05:47:37 -0600 )edit

This works under hydro i just tested with this line of code in launch file under ubuntu 12.04: <node pkg="rosservice" type="rosservice" name="switch_ranger" args="call --wait /switch_ranger sonar_pioneer true"/>

Oscar Lima gravatar image Oscar Lima  ( 2015-05-05 07:29:54 -0600 )edit

This still works under indigo like charm!

Karsten gravatar image Karsten  ( 2015-12-18 12:21:33 -0600 )edit

This also works under kinetic! Thank bhaskara for the answer:)

renweiros gravatar image renweiros  ( 2017-04-28 21:32:20 -0600 )edit

still works in melodic

Hakaishin gravatar image Hakaishin  ( 2019-09-10 06:07:21 -0600 )edit

Thanks works for me in melodic , for Gazebo service,

<node pkg="rosservice" type="rosservice" name="rosservice" args="call /gazebo/reset_simulation"/>
amjack gravatar image amjack  ( 2020-12-10 06:17:16 -0600 )edit

answered 2011-09-21 01:39:37 -0600

updated 2011-09-21 01:40:10 -0600

I don't know how if it is possible in a clean way. A quick and dirty hack for Linux would be the misuse of the launch-prefix attribute.

In your package foo, create a file named kickstart with the content

rosservice call --wait /global_localization &
exec "$@"

Then, make it executable using

chmod +x kickstart

Finally, add the following attribute to one of your <node> declarations in the launch file:

launch-prefix="$(find foo)/kickstart"

This will coax roslaunch into executing the kickstart script with the real node launch command line as parameters. The script performs the service call and executes the remainder of the command line, thus launching the node.

The --wait parameter ensures that the service is actually available, and the ampersand at the end of the rosservice line runs the call in the background, so the node launch will not be delayed.

edit flag offensive delete link more


I think that won't work because roslaunch passes additional parameters such as renamed topics and the node's name which causes rosservice to die with an exception. I think the only way to call the service is to not use any cmd line params in the script but put the complete command there.
Lorenz gravatar image Lorenz  ( 2011-09-21 02:11:29 -0600 )edit
That is why the kickstart script is needed. The rosservice call is completely independent of the node launch in the exec line below.
roehling gravatar image roehling  ( 2011-09-21 02:49:07 -0600 )edit
Sorry. My bad. Never mind... I didn't read your code carefully enough. :)
Lorenz gravatar image Lorenz  ( 2011-09-21 02:52:02 -0600 )edit
Thanks, it worked
Javier gravatar image Javier  ( 2011-09-21 05:52:26 -0600 )edit

Question Tools


Asked: 2011-09-21 01:02:04 -0600

Seen: 16,044 times

Last updated: Sep 14 '21