< >
Home » ROS2与C++入门教程 » ROS2与C++入门教程-服务组件增加launch启动演示

ROS2与C++入门教程-服务组件增加launch启动演示

ROS2与C++入门教程-服务组件增加launch启动演示

说明:

  • 介绍如何使用launch来启动服务组件
  • 继续利用上一篇新建的包cpp_component_service

步骤:

  • 新建目录launch和文件composition_demo.launch.py
cd ~/dev_ws/src/cpp_component_service
mkdir launch
touch launch/composition_demo.launch.py
  • 内容如下:
"""Launch a server and a client in a component container."""

import launch
from launch_ros.actions import ComposableNodeContainer
from launch_ros.descriptions import ComposableNode


def generate_launch_description():
    """Generate launch description with multiple components."""
    container = ComposableNodeContainer(
            name='my_container',
            namespace='',
            package='rclcpp_components',
            executable='component_container',
            composable_node_descriptions=[
                ComposableNode(
                    package='cpp_component_service',
                    plugin='composition::Server',
                    name='talker'),
                ComposableNode(
                    package='cpp_component_service',
                    plugin='composition::Client',
                    name='listener')
            ],
            output='screen',
    )

    return launch.LaunchDescription([container])
  • 编辑CMakeLists.txt
  • 增加如下内容
# Install launch files.
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}
)
  • 编译
colcon build --symlink-install --packages-select cpp_component_service
  • 加载工作空间
. install/setup.bash
  • 启动launch
ros2 launch cpp_component_service composition_demo.launch.py
  • 效果如下:
$ ros2 launch cpp_component_service composition_demo.launch.py
[INFO] [launch]: All log files can be found below /home/ubuntu/.ros/log/2022-05-06-18-27-01-478678-WALKING-24872
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [component_container-1]: process started with pid [24884]
[component_container-1] [INFO] [1651832822.053383141] [my_container]: Load Library: /home/ubuntu/dev_ws/install/cpp_component_service/lib/libserver_component.so
[component_container-1] [INFO] [1651832822.055467797] [my_container]: Found class: rclcpp_components::NodeFactoryTemplate<composition::Server>
[component_container-1] [INFO] [1651832822.055552199] [my_container]: Instantiate class: rclcpp_components::NodeFactoryTemplate<composition::Server>
[INFO] [launch_ros.actions.load_composable_nodes]: Loaded node '/talker' in container '/my_container'
[component_container-1] [INFO] [1651832822.067594058] [my_container]: Load Library: /home/ubuntu/dev_ws/install/cpp_component_service/lib/libclient_component.so
[component_container-1] [INFO] [1651832822.068609301] [my_container]: Found class: rclcpp_components::NodeFactoryTemplate<composition::Client>
[component_container-1] [INFO] [1651832822.068664370] [my_container]: Instantiate class: rclcpp_components::NodeFactoryTemplate<composition::Client>
[INFO] [launch_ros.actions.load_composable_nodes]: Loaded node '/listener' in container '/my_container'
[component_container-1] [INFO] [1651832824.077420139] [talker]: Incoming request: [a: 2, b: 3]
[component_container-1] [INFO] [1651832824.078097849] [listener]: Got result: [5]
[component_container-1] [INFO] [1651832826.076300191] [talker]: Incoming request: [a: 2, b: 3]
[component_container-1] [INFO] [1651832826.076601621] [listener]: Got result: [5]
[component_container-1] [INFO] [1651832828.076329987] [talker]: Incoming request: [a: 2, b: 3]
[component_container-1] [INFO] [1651832828.076647257] [listener]: Got result: [5]

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ROS2与C++入门教程