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

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

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

说明:

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

步骤:

  • 新建目录launch和文件composition_demo.launch.py
cd ~/dev_ws/src/cpp_component_topic
mkdir launch
touch launch/composition_demo.launch.py
  • 内容如下:
"""Launch a talker and a listener 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_topic',
                    plugin='composition::Talker',
                    name='talker'),
                ComposableNode(
                    package='cpp_component_topic',
                    plugin='composition::Listener',
                    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_topic
  • 加载工作空间
. install/setup.bash
  • 启动launch
ros2 launch cpp_component_topic composition_demo.launch.py
  • 效果如下:
$ ros2 launch cpp_component_topic composition_demo.launch.py
[INFO] [launch]: All log files can be found below /home/ubuntu/.ros/log/2022-05-06-17-42-33-570287-WALKING-23052
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [component_container-1]: process started with pid [23084]
[component_container-1] [INFO] [1651830154.160681144] [my_container]: Load Library: /home/ubuntu/dev_ws/install/cpp_component_topic/lib/libtalker_component.so
[component_container-1] [INFO] [1651830154.161955626] [my_container]: Found class: rclcpp_components::NodeFactoryTemplate<composition::Talker>
[component_container-1] [INFO] [1651830154.162011648] [my_container]: Instantiate class: rclcpp_components::NodeFactoryTemplate<composition::Talker>
[INFO] [launch_ros.actions.load_composable_nodes]: Loaded node '/talker' in container '/my_container'
[component_container-1] [INFO] [1651830154.171378661] [my_container]: Load Library: /home/ubuntu/dev_ws/install/cpp_component_topic/lib/liblistener_component.so
[component_container-1] [INFO] [1651830154.173113934] [my_container]: Found class: rclcpp_components::NodeFactoryTemplate<composition::Listener>
[component_container-1] [INFO] [1651830154.173187814] [my_container]: Instantiate class: rclcpp_components::NodeFactoryTemplate<composition::Listener>
[INFO] [launch_ros.actions.load_composable_nodes]: Loaded node '/listener' in container '/my_container'
[component_container-1] [INFO] [1651830155.168308380] [talker]: Publishing: 'Hello World: 1'
[component_container-1] [INFO] [1651830155.169035472] [listener]: I heard: [Hello World: 1]
[component_container-1] [INFO] [1651830156.168191464] [talker]: Publishing: 'Hello World: 2'
[component_container-1] [INFO] [1651830156.168539953] [listener]: I heard: [Hello World: 2]
[component_container-1] [INFO] [1651830157.168621507] [talker]: Publishing: 'Hello World: 3'
[component_container-1] [INFO] [1651830157.168935054] [listener]: I heard: [Hello World: 3]
[component_container-1] [INFO] [1651830158.168174537] [talker]: Publishing: 'Hello World: 4'

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

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


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