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

Call a service from a launch.py file in ROS2

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

ffent gravatar image

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

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

2 Answers

Sort by ยป oldest newest most voted
4

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

luckierdodge gravatar image

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
1

answered 2023-01-01 04:31:46 -0500

updated 2023-01-03 14:48:14 -0500

Thanks @luckierdodge! Just add more methods. Both examples below start a sim window and add another turtle2.

Method 1

Based on @luckierdodge answer:

from launch import LaunchDescription
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
from launch_ros.actions import Node


def generate_launch_description():
    ld = LaunchDescription(
        [
            Node(
                package="turtlesim",
                executable="turtlesim_node",
                name="turtlesim",
            ),
        ]
    )
    ld.add_action(
        ExecuteProcess(
            cmd=[
                [
                    FindExecutable(name="ros2"),
                    " service call ",
                    "/spawn ",
                    "turtlesim/srv/Spawn ",
                    "\"{x: 2.0, y: 2.0, theta: 0.0, name: 'turtle2'}\"",
                ]
            ],
            shell=True,
        )
    )
    return ld

Method 2

Based on https://docs.ros.org/en/humble/Tutori...

from launch import LaunchDescription
from launch.actions import ExecuteProcess, LogInfo, RegisterEventHandler
from launch.event_handlers import OnProcessStart
from launch.substitutions import FindExecutable, LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description():
    turtlesim_node = Node(
        package="turtlesim",
        executable="turtlesim_node",
        name="sim",
    )
    spawn_turtle = ExecuteProcess(
        cmd=[
            [
                FindExecutable(name="ros2"),
                " service call ",
                "/spawn ",
                "turtlesim/srv/Spawn ",
                "\"{x: 2.0, y: 2.0, theta: 0.0, name: 'turtle2'}\"",
            ]
        ],
        shell=True,
    )
    return LaunchDescription(
        [
            turtlesim_node,
            RegisterEventHandler(
                OnProcessStart(
                    target_action=turtlesim_node,
                    on_start=[
                        LogInfo(msg="Turtlesim started, spawning turtle"),
                        spawn_turtle,
                    ],
                )
            ),
        ]
    )
edit flag offensive delete link more

Question Tools

3 followers

Stats

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

Seen: 2,056 times

Last updated: Jan 03 '23