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

Is it possible to refer to the actions defined in a different launch?

asked 2021-04-04 12:45:55 -0600

Kenji Miyake gravatar image

Hello, I'd like to know whether it's possible to refer to the actions defined in launch A, from launch B.

For example, considering the case of: - start publisher after 5 seconds wait - launch subscriber after publisher started

pubsub.launch.py

from launch import LaunchDescription
from launch.actions import RegisterEventHandler
from launch.actions.timer_action import TimerAction
from launch.event_handlers import OnProcessStart
from launch_ros.actions import Node


def generate_launch_description():
    publisher = Node(
        package="examples_rclcpp_minimal_publisher",
        executable="publisher_lambda",
        output="screen",
    )

    launch_publisher_after_timer = TimerAction(period=5.0, actions=[publisher])

    subscriber = Node(
        package="examples_rclcpp_minimal_subscriber",
        executable="subscriber_lambda",
        output="screen",
    )

    subscriber_handler = RegisterEventHandler(
        event_handler=OnProcessStart(
            target_action=publisher,
            on_start=[
                subscriber,
            ],
        )
    )

    return LaunchDescription([launch_publisher_after_timer, subscriber_handler])

This of course works as expected. However, if I split this into two launch files, sub.launch.py doesn't work correctly. (I know my usage is wrong.)

pub.launch.py(imports are omitted)

def generate_launch_description():
    publisher = Node(
        package="examples_rclcpp_minimal_publisher",
        executable="publisher_lambda",
        output="screen",
    )

    launch_publisher_after_timer = TimerAction(period=5.0, actions=[publisher])

    return LaunchDescription([launch_publisher_after_timer])

sub.launch.py(imports are omitted)

def generate_launch_description():
    publisher = Node(
        package="examples_rclcpp_minimal_publisher",
        executable="publisher_lambda",
        output="screen",
    )

    subscriber = Node(
        package="examples_rclcpp_minimal_subscriber",
        executable="subscriber_lambda",
        output="screen",
    )

    subscriber_handler = RegisterEventHandler(
        event_handler=OnProcessStart(
            target_action=publisher,
            on_start=[
                subscriber,
            ],
        )
    )

    return LaunchDescription([subscriber_handler])

pubsub.launch.xml

<launch>
  <include file="./pub.launch.py"/>
  <include file="./sub.launch.py"/>
</launch>

So my question is, can I use publisher Node defined in pub.launch.py as target_action in sub.launch.py? I guess it's possible if there is a method to iterate all actions defined in other launch files and then filter by name.

Thank you in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-04-05 11:46:01 -0600

hidmic gravatar image

It is possible, but not with OnProcessStart.

OnProcessStart is syntactic sugar. An event handler bound to the ProcessStarted event for convenience. Generic event handlers take an arbitrary callable to match events. So you can technically filter and listen to any kind of event you may want. For process events, there's even a handful of convenient matchers you can use.

edit flag offensive delete link more

Comments

Thank you, I'll try it!

Kenji Miyake gravatar image Kenji Miyake  ( 2021-04-05 23:10:05 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2021-04-04 12:45:55 -0600

Seen: 316 times

Last updated: Apr 05 '21