How to Call a Service from a ROS2 Launch File

⬆ Back to Posts
This article was also posted at Dev

This one is pretty straight forward, but took me a non-trivial amount of searching to find for myself. To call a ros2 service from a ros2 launch file, add the following to your launch file (see the official docs for more on launch files):

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:

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