Example #1
0
    def execute(self, context: launch.LaunchContext) -> Optional[List[Action]]:
        """
        Execute the action.

        Delegated to :meth:`launch.actions.ExecuteProcess.execute`.
        """
        # self._perform_substitutions(context)  # ensure self.node_name is expanded
        if '<node_name_unspecified>' in self.node_name:
            raise RuntimeError(
                'node_name unexpectedly incomplete for system_modes node')
        node = get_ros_node(context)

        # Create a subscription to monitor the mode changes of the subprocess.
        self.__rclpy_mode_subscription = node.create_subscription(
            system_modes_msgs.msg.ModeEvent,
            '{}/mode_event'.format(self.node_name),
            functools.partial(self._on_mode_event, context), 10)

        # Create a subscription to monitor the state changes of the subprocess.
        self.__rclpy_state_subscription = node.create_subscription(
            lifecycle_msgs.msg.TransitionEvent,
            '{}/transition_event'.format(self.node_name),
            functools.partial(self._on_state_event, context), 10)

        # Create a service client to change mode on demand.
        self.__rclpy_change_mode_client = node.create_client(
            system_modes_msgs.srv.ChangeMode,
            '{}/change_mode'.format(self.node_name))

        # Create a service client to change state on demand.
        self.__rclpy_change_state_client = node.create_client(
            lifecycle_msgs.srv.ChangeState,
            '{}/change_state'.format(self.node_name))

        # Register an event handler to change modes on a ChangeMode system_modes event.
        context.register_event_handler(
            launch.EventHandler(
                matcher=lambda event: isinstance(event, ChangeMode),
                entities=[
                    launch.actions.OpaqueFunction(
                        function=self._on_change_mode_event)
                ],
            ))

        # Register an event handler to change states on a ChangeState event.
        context.register_event_handler(
            launch.EventHandler(
                matcher=lambda event: isinstance(event, ChangeState),
                entities=[
                    launch.actions.OpaqueFunction(
                        function=self._on_change_state_event)
                ],
            ))

        # Delegate execution to ExecuteProcess.
        return super().execute(context)
Example #2
0
def get_default_launch_description(*, prefix_output_with_name=False):
    """
    Return a LaunchDescription to be included before user descriptions.

    :param: prefix_output_with_name if True, each line of output is prefixed
        with the name of the process as `[process_name] `, else it is printed
        unmodified
    """
    default_ros_launch_description = launch.LaunchDescription([
        # ROS initialization (create node and other stuff).
        ROSSpecificLaunchStartup(),
        # Handle process starts.
        launch.actions.RegisterEventHandler(
            launch.EventHandler(
                matcher=lambda event: isinstance(
                    event, launch.events.process.ProcessStarted),
                entities=[
                    launch.actions.OpaqueFunction(function=_on_process_started)
                ],
            )),
        # Handle process exit.
        launch.actions.RegisterEventHandler(
            launch.EventHandler(
                matcher=lambda event: isinstance(
                    event, launch.events.process.ProcessExited),
                entities=[
                    launch.actions.OpaqueFunction(function=_on_process_exited)
                ],
            )),
        # Add default handler for output from processes.
        launch.actions.RegisterEventHandler(
            launch.event_handlers.OnProcessIO(
                on_stdout=functools.partial(
                    _on_process_output,
                    file_name='stdout',
                    prefix_output=prefix_output_with_name),
                on_stderr=functools.partial(
                    _on_process_output,
                    file_name='stderr',
                    prefix_output=prefix_output_with_name),
            )),
    ])
    return default_ros_launch_description