ROS2 之 组件

9550阅读 0评论2020-06-18 iibull
分类:其他平台

ROS1 中, nodelet会被编译成 动态库, 被其他的容器进程调用. 
ROS2中, 组件 component概念类似于 nodelet, 会被变异成共享库, 没有main函数实现, 

代码:
component 作为 rclcpp::Node的子类, 因为他不是线程控制的, 所以它不应该在构造函数中执行任何长时间运行或阻塞的任务。相反,它可以使用计时器来获得定期通知。此外,它还可以创建发布者、订阅者、服务器和客户机。

编译

点击(此处)折叠或打开

  1. add_library(talker_component SHARED src/talker_component.cpp)
  2. rclcpp_components_register_nodes(talker_component "composition::Talker")
  3. # To register multiple components in the same shared library, use multiple calls
  4. # rclcpp_components_register_nodes(talker_component "composition::Talker2")

使用/调用
发现可用的组件  

点击(此处)折叠或打开

  1. $ ros2 component types
  2. composition
  3.   composition::Talker
  4.   composition::Listener
  5.   composition::Server
  6.   composition::Client
如何在运行时使用 topic 组件呢

点击(此处)折叠或打开

  1. 1. 在一个shell 中, 启用组件容器
  2.   ros2 run rclcpp_components component_container
  3. 2. 在第二个shell中, 确认容器正常.
  4.   $ ros2 component list
  5.   /ComponentManager
  6. 3. 在第二个shell中
  7.   $ ros2 component load /ComponentManager composition composition::Talker
  8.   Loaded component 1 into '/ComponentManager' container node as '/talker'## 返回调用组件的 unique ID , 此时在步骤1 的shell中会显示重复的Talker发布的消息.

  9. 4. 第二个shell中执行
  10.   $ ros2 component load /ComponentManager composition composition::Listener
  11.   Loaded component 2 into '/ComponentManager' container node as '/listener'
  12. 此时在步骤1 的shell中会显示重复的listener收到的消息.

  13. 5. 第二个shell中执行, 列出容器中被调用的组件
  14.   $ ros2 component list
  15.   /ComponentManager
  16.      1 /talker
  17.      2 /listener
如何在运行时使用 service 组件呢.

点击(此处)折叠或打开

  1. 1. shell 1 中执行
  2. ros2 run rclcpp_components component_container

  3. 2. shell 2 中执行
  4. ros2 component load /ComponentManager composition composition::Server
  5. ros2 component load /ComponentManager composition composition::Client

  6. 3. 在 shell 1 中会出现 service 的调用和反馈信息.

组件也可用通过 launch 文件来加载
ros2 launch composition composition_demo.launch.py

卸载组件

点击(此处)折叠或打开

  1. $ ros2 component unload /ComponentManager 1 2
  2. Unloaded component 1 from '/ComponentManager' container
  3. Unloaded component 2 from '/ComponentManager' container
remapping container容器 name and namespace

点击(此处)折叠或打开

  1. ros2 run rclcpp_components component_container --ros-args -r __node:=MyContainer -r __ns:=/ns
  2. ros2 component load /ns/MyContainer composition composition::Listener
  3. 容器的名称空间重新映射不会影响已加载的组件。
remapping component组件 name and namespace

点击(此处)折叠或打开

  1. ros2 run rclcpp_components component_container
  2. # Remap node name
  3. ros2 component load /ComponentManager composition composition::Talker --node-name talker2
  4. # Remap namespace
  5. ros2 component load /ComponentManager composition composition::Talker --node-namespace /ns
  6. # Remap both
  7. ros2 component load /ComponentManager composition composition::Talker --node-name talker3 --node-namespace /ns2
  8.  
  9. $ ros2 component list ## 查看结果, 容器的名称空间重新映射不会影响已加载的组件。
  10.   /ComponentManager
  11.     1 /talker2 
  12.     2 /ns/talker 
  13.     3 /ns2/talker3







上一篇:ROS2 参考例程
下一篇:ROS2 高级内容