Ask Your Question
6

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
2

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

#!/bin/bash
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

Comments

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 imageLorenz ( 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 imageroehling ( 2011-09-21 02:49:07 -0600 )edit
Sorry. My bad. Never mind... I didn't read your code carefully enough. :)
Lorenz gravatar imageLorenz ( 2011-09-21 02:52:02 -0600 )edit
Thanks, it worked
Javier gravatar imageJavier ( 2011-09-21 05:52:26 -0600 )edit
17

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

bhaskara gravatar image

It would be simpler just to have the call be its own node. I.e., have <node pkg="rosservice" type="rosservice" name="global_loc" args="call --wait /global_localization"/> in the launch file.

edit flag offensive delete link more

Comments

That was my first idea, too. But as Lorenz wrote above, roslaunch tends to pass additional parameters which let rosservice choke.
roehling gravatar imageroehling ( 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 imageDereck ( 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: https://github.com/ros/ros_comm/issues/180

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

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 imageOscar Lima ( 2015-05-05 07:29:54 -0600 )edit

This still works under indigo like charm!

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

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

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

still works in melodic

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

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

Seen: 7,759 times

Last updated: Sep 21 '11