ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
1

Call a service from a launch.py file in ROS2

asked 2021-08-18 02:49:44 -0600

ffent gravatar image

updated 2021-08-18 02:51:34 -0600

Question
Is there any possibility to call a service from a launch.py file in ROS2?

Details
I have written a python node with a service which is launched by a launch.py file. Within the same launch file I would like to call the service of this node multiple times (with different argument values). Is there any possibility to realize this in ROS2?

Reference
call-a-service-at-startup-with-a-launch-file

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-04 10:04:35 -0600

This one is pretty straight forward, but took me a non-trivial amount of searching to find for myself. I posted this answer somewhere else recently, but since this question is the top search result, I thought I'd copy it here as well.

from launch.substitutions import FindExecutable
from launch.actions import ExecuteProcess

...

ld.add_action(
    ExecuteProcess(
        cmd=[[
            FindExecutable(name='ros2'),
            " service call ",
            "/namespace/service_to_call ",
            "example_msgs/srv/ExampleMsg ",
            '"{param_1: True, param_2: 0.0}"',
        ]],
        shell=True
    )
)

Note the following:

  • ld here is a variable containing an instance of LaunchDescription
  • /namespace/service_to_call is replaced with the service you're looking to call (don't forget any appropriate namespaces) and can be found with ros2 service list
  • example_msgs/srv/ExampleMsg is the message type used by that service, which you can get with ros2 service info /namespace/service_to_call
  • "{param_1: True, param_2: 0.0}" is the dictionary defining the message data. To find the parameters you need to set, you may need to consult the .srv file or documentation.

Don't forget to include the shell=True argument, as the command will fail with a confusing "File not found" error without it.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

3 followers

Stats

Asked: 2021-08-18 02:49:44 -0600

Seen: 1,004 times

Last updated: Aug 18 '21