Home The Robot Operating System 2 (ROS2)
Post
Cancel

The Robot Operating System 2 (ROS2)

一、ROS2介绍

1
https://www.jianshu.com/p/3829624ac310 //ROS2简介 有图有介绍,很详细

image-20210628143103792

image-20210628142816662

1) 基本概念ROS1 Vs ROS2.png

1
2
3
4
5
6
Nodes-节点概念
节点即Node,是ROS里面的一个执行体,可以和其他节点直接在ROS中互相通信。Node是Package里面的可执行文件
的一个功能体现(也就是说Package的可执行文件能够实现的功能不仅仅是一个Node,还有其他的功能,但是Node
是其最基本的一个功能)。

节点通过发布messages到topic来实现互相通信。也可以调用ROS的service实现request和response的消息交换。
1
2
3
4
5
Messages-消息概念
message 是一种由类型字段(类型域)组成的简单数据结构。支持原始的标准类型(整数、浮点、布尔、数组
等),同时支持类似C语言的结构和数组嵌套。

我们使用.msg格式的的文本文件指定消息的数据结构。
1
2
3
4
5
Topic-话题概念
Topic 被称为节点信息交互的总线,话题拥有匿名发布和订阅语义,这样能够解耦信息的生产者和使用者。通常来
说,节点是不清楚他们在和谁通信,他们更关注所订阅话题的数据。

一个话题可以有多个订阅者和发布者。
1
2
3
4
5
6
7
8
9
10
11
Discovery-发现
节点之间的互相发现是ROS2底层的中间件自动运行的一个进程,通过这个进程不同的节点相互发现,建立连接。这
是区别ROS1中的master,真正实现了分布式通信。

当一个节点启动后, 它会向其他拥有相同ROS域名(ROS domain, 可以通过设置ROS_DOMAIN_ID环境变量来设置)
的节点进行广播,说明它已经上线。
其他节点在收到广播后返回自己的相关信息,这样节点间的连接就可以建立了,之后就可以通信了。

节点会定时广播它的信息,这样即使它已经错过了最初的发现过程,它也可以和新上线的节点进行连接。
节点在下线前它也会广播其他节点自己要下线了。
节点只会和具有相兼容的[服务质量]设置的节点进行通信。

2) Qos

3)Real-Time Publish/Subscribe Protocol

实时发布订阅协议(Real Time Publish Subscribe Protocol,RTPS)是一种协议,用于在单播和多播中通过 UDP等不可靠传输进行best effort和reliable的发布-订阅通信.

RTPS协议的主要特点是:

性能和服务质量(QoS)属性,为使用标准IP网络的实时应用程序提供best-effort和reliable的发布-订阅通信。
容错,可以创建没有单点故障的网络。
可扩展性,可以通过协议的扩展和新服务的增强实现向后兼容性和互操作性。
即插即用连接,新的应用程序和服务允许通过应用程序随时加入和离开网络来实现自动的、无需配置的发现。
可配置性,可以平衡每个数据交付事务的可靠性和及时性需求。
模块化,允许简单的设备实现协议的子集,并且仍然参与发布-订阅网络。
可伸缩性,使系统能够扩展到非常大的发布-订阅网络。
类型安全,防止应用程序编程错误影响发布-订阅网络中的远程节点的操作。

OMG规范: 定义了基于RTPS的DDS设计

1
2
https://www.omg.org/spec/DDSI-RTPS/2.2/PDF
https://blog.csdn.net/DDS_CSIT/article/details/104940013 spec的中文文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
https://paul.pub/dds-and-fastrtps/
DDS规范中,有两个描述标准的基本文档:
DDS Specification:描述了以数据为中心的发布-订阅模型。该规范定义了API和通信语义(行为和服务质量),
使消息从消息生产者有效地传递到匹配的消费者。DDS规范的目的可以概括为:“能够在正确的时间将正确的信息高
效,可靠地传递到正确的位置”。
DDSI-RTPS:描述了RTPSReal Time Publish Subscribe Protocol)协议。该协议通过UDP等不可靠的传输
,实现最大努力(Best-Effort)和可靠的发布-订阅通信。RTPSDDS实现的标准协议,它的目的和范围是确保基
于不同DDS供应商的应用程序可以实现互操作。
    
传输控制
Fast-RTPS实现了可插拔的传输架构,这意味着每一个参与者可以随时加入和退出。
在传输上,Fast-RTPS支持以下五种传输方式:
UDPv4 UDPv6 TCPv4 TCPv6 SHMShared Memory
默认的,当Participant创建时,会自动的配置两个传输通道:
SHM:用来与同一个机器上的参与者通信。
UDPv4:同来与跨机器的参与者通信。
当然,开发者可以改变这个默认行为,通过C++接口或者XML配置文件都可以。
SHM要求所有参与者位于同一个系统上,它是借助了操作系统提供的共享内存机制实现。共享内存的好处是:支持大
数据传输,减少了数据拷贝,并且也减少系统负载。因此通常情况下,使用SHM会获得更好的性能。使用SHM时,可
以配置共享内存的大小。

image-20210628151633210

1
2
3
4
5
6
7
8
(1)The DDSI-RTPS (DDS-Interoperability Real Time Publish Subscribe) protocol 
    
(2)eProsimas FastRTPS implementation is available on GitHub and is LGPL licensed:https://github.com/eProsima/Fast-RTPS
eProsima Fast RTPS is a relatively new, lightweight, and open source implementation of RTPS.
 It allows direct access to the RTPS protocol settings and features, which is not always 
 possible with other DDS implementations. eProsimas implementation also includes a minimum 
 DDS API, IDL support, and automatic code generation and they are open to working with the 
 ROS community to meet their needs.
1
https://fast-dds.docs.eprosima.com/en/latest/ 介绍fast-dds with fast-RTPS
1
2
3
4
5
6
7
https://www.brixbot.com/ros2/fast_rtps-02-introduce_fast_buffer/ 
https://zhuanlan.zhihu.com/p/59465983
ROS 2使用Fast RTPS去实现消息的订阅和发布。而Fast RTPS要通过网络发送消息,就一定要把消息序列化成
byte array发送出去,然后消息订阅方收到消息后要把byte array还原成对应的消息对象。消息的序列化和消息对
象的还原就是Fast Buffers干的活。
按官网介绍,特点就是快。eProsima有对比Apache ThriftGoogle Protocol Buffers,其结果当然是自家的
Fast Buffers完胜。

image-20210628172218396

1
2
3
4
http://design.ros2.org/articles/ros_on_dds.html

Publish-Subscribe Transport
The DDSI-RTPS (DDS-Interoperability Real Time Publish Subscribe) protocol would replace ROSs TCPROS and UDPROS wire protocols for publish/subscribe. The DDS API provides a few more actors to the typical publish-subscribe pattern of ROS 1. In ROS the concept of a node is most clearly paralleled to a graph participant in DDS. A graph participant can have zero to many topics, which are very similar to the concept of topics in ROS, but are represented as separate code objects in DDS, and is neither a subscriber nor a publisher. Then, from a DDS topic, DDS subscribers and publishers can be created, but again these are used to represent the subscriber and publisher concepts in DDS, and not to directly read data from or write data to the topic. DDS has, in addition to the topics, subscribers, and publishers, the concept of DataReaders and DataWriters which are created with a subscriber or publisher and then specialized to a particular message type before being used to read and write data for a topic. These additional layers of abstraction allow DDS to have a high level of configuration, because you can set QoS settings at each level of the publish-subscribe stack, providing the highest granularity of configuration possible. Most of these levels of abstractions are not necessary to meet the current needs of ROS. Therefore, packaging common workflows under the simpler ROS-like interface (Node, Publisher, and Subscriber) will be one way ROS 2 can hide the complexity of DDS, while exposing some of its features.
1
2
https://docs.ros.org/en/foxy/Concepts.html#internal-ros-interfaces //ROS2 官网概念介绍
https://docs.ros.org/en/foxy/Tutorials/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html // ROS2官网简单pub-sub用例

二、DDS介绍

https://docs.ros.org/en/foxy/Tutorials/Discovery-Server/Discovery-Server.html?highlight=disc
1
http://design.ros2.org/articles/ros_on_dds.html

image-20210628143617141

1
2
3
4
5
6
数据分发服务DDS(DataDistributionService)是对象管理组织(OMG)HLACORBA等标准的基础上制定的新一代分布式实
时通信中间件技术规范,DDS采用发布/订阅体系架构,强调以数据为中心,提供丰富的QoS服务质量策略,能保障数据进行实
时、高效、灵活地分发,可满足各种分布式实时通信应用需求。DDS信息分发中间件是一种轻便的、能够提供实时信息传送的
中间件技术。
	
原文链接:https://blog.csdn.net/DDS_CSIT/article/details/104607476

三、使用

1> 编译

1、编译过程:

1
2
cd HERA
colcon build --install-base /opt/hera --merge-install

2、依赖包

综合:

1
2
3
4
yum install libacl-devel python3-importlib-resources libatomic tinyxml2 tinyxml2-devel python3-lark-parser log4cxx log4cxx-devel python3-numpy 	
yum install boost
//https://pkgs.dyn.su/el8/extras/x86_64/asio-devel-1.14.0-12.el8.x86_64.rpm
yum localinstall asio-devel-1.14.0-12.el8.x86_64.rpm

repo:

环境信息:

1
2
[root@localhost HERA]# uname -a
Linux localhost.localdomain 4.18.0-240.el8.x86_64 #1 SMP Fri Sep 25 19:48:47 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
1
2
3
4
[download-ib01.fedoraproject.org_pub_epel_8_Everything_x86_64_]
name=created by dnf config-manager from https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/
baseurl=https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/
enabled=1
1
2
3
yum install python36-colcon-core
yum install python3-colcon-common-extensions
yum install cmake

问题1:

1
HERA/src/iceoryx_v0.17.0/iceoryx_utils/platform/linux/include/iceoryx_utils/platform/acl.hpp:17:10: fatal error: sys/acl.h: No such file or directory
1
yum install libacl-devel

问题2:

1
2
3
4
Traceback (most recent call last):
  File "/opt/hera/lib/python3.6/site-packages/ament_package/templates.py", line 19, in <module>
    import importlib.resources as importlib_resources
ModuleNotFoundError: No module named 'importlib.resources'
1
yum install python3-importlib-resources

问题3:

1
2
3
4
Starting >>> test_osrf_testing_tools_cpp
--- stderr: iceoryx_posh                                                                            
/usr/bin/ld: cannot find /usr/lib64/libatomic.so.1.2.0
collect2: error: ld returned 1 exit status
1
yum install libatomic

问题4:

1
2
3
4
5
6
7
8
Starting >>> ament_cmake_pyflakes
--- stderr: tinyxml2_vendor                                                                                       
CMake Error at /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
  Could NOT find TinyXML2 (missing: TINYXML2_LIBRARY TINYXML2_INCLUDE_DIR)
Call Stack (most recent call first):
  /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:458 (_FPHSA_FAILURE_MESSAGE)
  cmake/Modules/FindTinyXML2.cmake:58 (find_package_handle_standard_args)
  CMakeLists.txt:7 (find_package)
1
yum install tinyxml2 tinyxml2-devel

问题5:

1
2
3
4
5
6
7
--- stderr: rosidl_adapter                                                           
CMake Warning at /opt/hera/share/ament_cmake_pytest/cmake/ament_add_pytest_test.cmake:77 (message):
  The Python module 'pytest' was not found, pytests cannot be run.  On Linux,
  install the 'python3-pytest' package.  On other platforms, install 'pytest'
  using pip.
Call Stack (most recent call first):
  CMakeLists.txt:14 (ament_add_pytest_test)

问题6:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
--- stderr: rosidl_generator_c                                                                              
Traceback (most recent call last):
  File "/home/luocheng/HERA/src/ros2/rosidl/rosidl_generator_c/bin/rosidl_generator_c", line 8, in <module>
    from rosidl_generator_c import generate_c
ModuleNotFoundError: No module named 'rosidl_generator_c'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/luocheng/HERA/src/ros2/rosidl/rosidl_generator_c/bin/rosidl_generator_c", line 20, in <module>
    rosidl_generator_c = loader.load_module()
  File "<frozen importlib._bootstrap_external>", line 399, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 823, in load_module
  File "<frozen importlib._bootstrap_external>", line 682, in load_module
  File "<frozen importlib._bootstrap>", line 265, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 684, in _load
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/luocheng/HERA/src/ros2/rosidl/rosidl_generator_c/rosidl_generator_c/__init__.py", line 15, in <module>
    from rosidl_cmake import convert_camel_case_to_lower_case_underscore
  File "/opt/hera/lib/python3.6/site-packages/rosidl_cmake/__init__.py", line 24, in <module>
    from rosidl_parser.parser import parse_idl_file
  File "/opt/hera/lib/python3.6/site-packages/rosidl_parser/parser.py", line 20, in <module>
    from lark import Lark
ModuleNotFoundError: No module named 'lark'
gmake[2]: *** [CMakeFiles/rosidl_generator_c_interfaces__rosidl_generator_c.dir/build.make:104: rosidl_generator_c/rosidl_generator_c/msg/arrays.h] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:298: CMakeFiles/rosidl_generator_c_interfaces__rosidl_generator_c.dir/all] Error 2
gmake[1]: *** Waiting for unfinished jobs....
gmake: *** [Makefile:160: all] Error 2
1
yum install python3-lark-parser

问题7:

1
2
3
4
5
6
7
Starting >>> test_launch_testing
--- stderr: fastrtps                                                                               
CMake Error at cmake/modules/FindAsio.cmake:22 (message):
  Not found a local version of Asio installed.
Call Stack (most recent call first):
  cmake/common/eprosima_libraries.cmake:212 (find_package)
  CMakeLists.txt:228 (eprosima_find_thirdparty)
1
2
3
yum install boost
//https://pkgs.dyn.su/el8/extras/x86_64/asio-devel-1.14.0-12.el8.x86_64.rpm
yum localinstall asio-devel-1.14.0-12.el8.x86_64.rpm

问题8:

1
2
3
4
5
6
7
8
9
10
11
--- stderr: rcl_logging_log4cxx                                                                                
CMake Error at /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
  Log4cxx_INCLUDE_DIR (missing: Log4cxx_LIBRARY)
Call Stack (most recent call first):
  /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:458 (_FPHSA_FAILURE_MESSAGE)
  cmake/FindLog4cxx.cmake:35 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:16 (find_package)


---
Failed   <<< rcl_logging_log4cxx [1.04s, exited with code 1]
1
yum install log4c log4c-devel  log4cplus log4cplus-devel log4cxx log4cxx-devel

问题9:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Starting >>> rosidl_typesupport_cpp
--- stderr: rosidl_generator_py                                                                                
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
CMake Error at cmake/rosidl_generator_py_generate_interfaces.cmake:213 (message):
  execute_process(/usr/bin/python3 -c 'import
  numpy;print(numpy.get_include())') returned error code 1
Call Stack (most recent call first):
  /opt/hera/share/ament_cmake_core/cmake/core/ament_execute_extensions.cmake:48 (include)
  /opt/hera/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:286 (ament_execute_extensions)
  CMakeLists.txt:48 (rosidl_generate_interfaces)


---
Failed   <<< rosidl_generator_py [1.25s, exited with code 1]
Aborted  <<< rosidl_typesupport_cpp [4.56s]  
1
yum install python3-numpy

2> 用例测试

1.hera编完要这样设置下环境变量,再去编测试套。

1
source /opt/hera/setup.bash

2.clone测试套和编译

git clone ssh:/xxs/RobotSDK_Tests.git
cd RobotSDK_Tests/ROS2/dev_ws

#Step1:编译自定义的数据结构
colcon build --packages-select my_struct

#Step2:执行环境变量脚本
source install/setup.bash

#Step3:编译listen_talk包
colcon build --packages-select listen_talk

3.运行

1
2
3
4
5
6
cd build/listen_talk/
#pty1
./talker [ -z ]  ## -z zero-copy版本
    
 #pty2
 ./listener

4.result

1
2
3
4
5
#pty1
[root@localhost listen_talk]# ./talker -z
Starting to do test using zero-copy!!!!!!!!!!!!!
[INFO] [1623937655.203340827] [minimal_publisher]: Publishing: (128 64 0) 1623937655203335
[INFO] [1623937656.203293168] [minimal_publisher]: Publishing: (128 64 1) 1623937656203291
1
2
3
4
5
6
#pty2
[root@localhost listen_talk]# ./listener 
[INFO] [1623937688.203468974] [minimal_subscriber]: I heard: (128, 64, 33), size = 2097152, latency: 157 us
[INFO] [1623937689.203369801] [minimal_subscriber]: I heard: (128, 64, 34), size = 2097152, latency: 78 us
[INFO] [1623937690.203362141] [minimal_subscriber]: I heard: (128, 64, 35), size = 2097152, latency: 73 us
[INFO] [1623937691.203375374] [minimal_subscriber]: I heard: (128, 64, 36), size = 2097152, latency: 80 us

四 ROS2场景-工业机械臂

1
https://cn.element14.com/robotic-arm

​ 机械臂是工业机器人的一个关键组成部分。通过机械臂上安装的工具,可以执行各种不同的工作,如焊接、油漆和货盘装运。目前在广泛的工业应用中采用了若干类型的机械臂,即卡式机械臂、圆柱机械臂、极性机械臂和关节式机械臂。其中关节式机械臂使用最广,并且其DOF(自由度)较高,体积小,操作范围广,可以避开小范围内的障碍物。

​ 关节式机械臂通常由刚性杆和可旋转关节构成。伺服控制系统用于控制伺服电机,后者驱动关节旋转。该系统中包含三个模块:伺服电机控制模块、传感器模块和中央控制模块。伺服电机控制模块中集成了MCU、电机驱动器、编码器与电流传感器。MCU根据中央模块的指令MCU将控制信号发给电机驱动器,后者放大这些信号以驱动电机旋转。编码器和电流传感器负责监测电机,并将反馈信息发送回MCU,以确保伺服电机正常工作。传感器模块涉及多种类型的传感器,如接近传感器、图像传感器、压力传感器和LVDT传感器,这些传感器用于收集有关机械臂的运动信息,并在由信号调节电路处理之后发送给中央控制模块。中央控制模块拥有DSP和PLC模块。DSP根据传感器模块提供的数据计算关节的运动轨迹和参数。然后通过CAN总线将结果发送给伺服电机控制模块,以便机械臂实施所需操作。PLC模块提供人机界面,供用户设置程序来控制机械臂。此外,PLC模块还有用于连接紧急按钮和不同类型继电器的接口,紧急按钮和继电器用于控制机械臂上连接的工具。

1
https://wenku.baidu.com/view/9e3e54d86f1aff00bed51e2a.html
This post is licensed under CC BY 4.0 by the author.