系列文章目录
- ROS2与C++入门教程-目录
- ROS2与C++入门教程-新建ros2工作空间
- ROS2与C++入门教程-新建ros2包
- ROS2与C++入门教程-编写订阅和发布
- ROS2与C++入门教程-编写服务端和客户端
- ROS2与C++入门教程-创建消息(msg)文件
- ROS2与C++入门教程-创建服务(srv)文件
- ROS2与C++入门教程-创建ros2接口
- ROS2与C++入门教程-使用参数
- ROS2与C++入门教程-创建和使用插件
- ROS2与C++入门教程-编写动作服务器
- ROS2与C++入门教程-编写动作客户端
- ROS2与C++入门教程-Topic Statistics查看话题
- ROS2与C++入门教程-增加头文件
- ROS2与C++入门教程-在C++包里增加python支持
- ROS2与C++入门教程-增加命名空间
- ROS2与C++入门教程-多线程演示
- ROS2与C++入门教程-单线程演示
- ROS2与C++入门教程-话题组件演示
- ROS2与C++入门教程-话题组件增加launch启动演示
- ROS2与C++入门教程-服务组件演示
- ROS2与C++入门教程-服务组件增加launch启动演示
- ROS2与C++入门教程-进程内(intra_process)话题发布和订阅演示
- ROS2与C++入门教程-进程内(intra_process)话题发布和订阅演示2
- ROS2与C++入门教程-进程内(intra_process)图像处理演示
- ROS2与C++入门教程-生命周期节点演示
说明:
- 介绍如何使用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'