前言
阅读本章之前,确保已经了解ROS2中构件(Component)的概念,如果不了解,欢迎移步ROS2 Composition
一个小问题自测:构件与节点之间的区别?在容器进程中运行的单元是构件还是节点?
执行demo
本文通过执行几个demo,来直观展示composition的常见使用方式。
run-time 动态加载
foxy源码demos中的composition已经实现了一些构件,不妨确认一下:
$ ros2 component type
composition
composition::Talker
composition::Listener
composition::Server
composition::Client
我们将多个节点加载到单个进程的步骤是:首先创建一个容器进程,然后通过ros2 的api向这个容器进程中动态加载。
创建容器进程:ros2 run rclcpp_components component_container
确认容器进程正在运行:ros2 component list
在另一个终端中,输入命令加载构件:ros2 component load /ComponentManager composition composition::<component_name>
我们此时可以看到容器进程终端有相应的内容输出。
编译期加载
同样的shared lib也可以在代码中在编译期加载,可以看一下manual_composition.cpp的源码:
// Copyright 2016 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the L