无人驾驶笔记系列(四):Fast-RTPS初体验

无人驾驶笔记系列(四):Fast-RTPS初体验

背景介绍

ROS1在节点启动前,必须通过roscore启动一个master节点,通过master管理节点之间的通信。这样master节点如果出现问题,其他节点之间的通信也会出现问题。同时,通讯的实时性也不是很好。因此,ROS2和apollo框架都用上了DDS通讯。

关于什么是DDS,英文全称(Data Distribution Service for Real-Time Systems)我也不在此啰嗦了,推荐读下这篇博客。
DDS简要介绍

总结下特点:

  • DDS采用发布/订阅体系架构,强调以数据为中心
  • DDS使应用在需要进行数据访问的时候提供一个虚拟的全局数据池概念,事实上并没有一个存储所有数据的全局设备。每个应用仅在本地存储它需要的数据,当整个网络内的任何应用需要他们时,提供给对应应用。所有的数据分发工作全部都由DDS代劳了。
  • 网络中的数据对象用主题做标识,分布式节点在全局数据空间中发布或订阅感兴趣的主题信息。各个节点在逻辑上无主从关系,点与点之间都是对等关系,通信方式可以是点对点,点对多,多对多,在Qos策略的控制下建立连接,自动发现和配置网络参数

当然DDS之前一种协议,具体实现有好多版本,如:

  • RTI公司的DDS方案:美国海上战争中心(NSWC)高性能分布式计算系统(HiPer-D),美军都在用,应该很牛吧。
  • OpenDDS:OpenDDS是使用C++语言针对OMG数据分发服务(DDS)的一种开源实现。由OCI公司设计和维护,http://www.opendds.org 官网。尽管本身OpenDDS采用C++语言实现,但也提供JAVA和JMS的开发接口,这意味着JAVA程序开发也可以使用OpenDDS。
  • eProsima Fast RTPS:Fast RTPS是eProsima公司对RTPS标准的一个实现。那RTPS是什么东东?RTPS是DDS标准中的一个子集。英文名字是Real Time Publish Subscribe,是DDS标准中的通讯协议部分。

安装eProsima Fast RTPS

eProsima Fast RTPS的github主页

推荐用官网的安装包安装,源码安装比较复杂,需要你安装很多依赖。

安装包解压后,查看下文件结构:

xx:~/fastrtps$ ls
doc  install.sh  java  README.txt  src  uninstall.sh

xx:~/fastrtps$ cd src
xx:~/fastrtps/src$ ls
fastcdr  fastrtps  fastrtpsgen  foonathan_memory

可以看到,其实文件还是包含了依赖包的源码,这样保证我们安装不会出问题。

一行命令,即可完成安装

sudo bash install.sh

成功安装后,查看下安装目录

#include文件目录
/usr/local/include/fastrtps
#库文件目录
/usr/local/lib/libfastrtps.so
#cmake文件目录
/usr/local/share/fastrtps/cmake/fastrtps-targets.cmake

跑个实例试试看

你可以在github主要上把源码拉取下来。
在源码中,可以看到有个helloworld实例

cd Fast-RTPS/examples/C++/HelloWorldExample

编译

mkdir build && cd build
cmake ..
make

编译后,要在两个窗口运行。
先运行发布者

./HelloWorldExample publisher

打印结果如下:

Starting 
Publisher running 10 samples.
Publisher matched
# 在subscriber启动后打印
Message: HelloWorld with index: 1 SENT
Message: HelloWorld with index: 2 SENT
Message: HelloWorld with index: 3 SENT
Message: HelloWorld with index: 4 SENT
Message: HelloWorld with index: 5 SENT
Message: HelloWorld with index: 6 SENT
Message: HelloWorld with index: 7 SENT
Message: HelloWorld with index: 8 SENT
Message: HelloWorld with index: 9 SENT
Message: HelloWorld with index: 10 SENT

启动订阅者

./HelloWorldExample subscriber

打印结果如下:

Starting 
Subscriber running. Please press enter to stop the Subscriber
Subscriber matched
Message HelloWorld 1 RECEIVED
Message HelloWorld 2 RECEIVED
Message HelloWorld 3 RECEIVED
Message HelloWorld 4 RECEIVED
Message HelloWorld 5 RECEIVED
Message HelloWorld 6 RECEIVED
Message HelloWorld 7 RECEIVED
Message HelloWorld 8 RECEIVED
Message HelloWorld 9 RECEIVED
Message HelloWorld 10 RECEIVED
Subscriber unmatched

我们稀里糊涂的跑了官方例子,具体是怎么运行的,还是完全不明白。

原理解析

未完待续

Fast-RTPS 是一个高性能的实时通信协议,用于在分布式系统中传输实时数据。在 Fast-RTPS 中,每个消息都由头部和数据组成。在本示例中,我们将展示如何解析 Fast-RTPS 消息的头部字段以及 topic name。 首先,我们需要了解 Fast-RTPS 消息头部字段的含义。Fast-RTPS 消息头部包括以下字段: - `protocolId`:Fast-RTPS 协议的版本号。 - `vendorId`:Fast-RTPS 实现的厂商 ID。 - `guidPrefix`:全局唯一标识符的前缀,用于识别 Fast-RTPS 实例。 - `guidEntityId`:Fast-RTPS 实体的唯一标识符。 - `sequenceNumber`:消息的序列号。 - `sourceTimestamp`:消息的时间戳。 - `destinationGuidPrefix`:消息的目标实体的 GUID 前缀。 其中,`guidPrefix` 和 `guidEntityId` 组成了消息的唯一标识符 GUID,用于在 Fast-RTPS 网络中识别消息。 接下来,我们可以通过以下代码示例解析 Fast-RTPS 消息的头部字段和 topic name: ```c++ #include <fastrtps/fastrtps.h> #include <fastrtps/attributes/TopicAttributes.h> void parseFastRTPSMessageHeader(const void* buffer, size_t size) { // Deserialize the message header using Fast-RTPS library eprosima::fastcdr::FastBuffer cdrbuffer(reinterpret_cast<char*>(const_cast<void*>(buffer)), size); eprosima::fastrtps::rtps::CDRMessage_t cdrm(cdrbuffer); eprosima::fastcdr::Cdr cdr_des(cdrm, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR); // Parse the fields in the header eprosima::fastrtps::rtps::Header_t header; header.readFromCDRMessage(cdrm); std::cout << "protocolId: " << std::to_string(header.protocolId) << std::endl; std::cout << "vendorId: " << std::to_string(header.vendorId) << std::endl; std::cout << "guidPrefix: " << header.guidPrefix << std::endl; std::cout << "guidEntityId: " << header.guidEntityId << std::endl; std::cout << "sequenceNumber: " << std::to_string(header.sequenceNumber.to64long()) << std::endl; std::cout << "sourceTimestamp: " << header.sourceTimestamp.to_string() << std::endl; std::cout << "destinationGuidPrefix: " << header.destinationGuidPrefix << std::endl; // Deserialize the topic name from the message payload eprosima::fastrtps::TopicAttributes topic_attr; topic_attr.deserialize(&cdr_des); std::cout << "topic name: " << topic_attr.getTopicName() << std::endl; } ``` 在上述示例中,我们使用了 Fast-RTPS 库中的 `Header_t` 类和 `TopicAttributes` 类来解析消息头部和 topic name。首先,我们将消息头部反序列化为 `Header_t` 类型,并读取其中的各个字段。然后,我们从消息负载中反序列化出 `TopicAttributes` 类型,以获取 topic name。 该示例代码可以在 Fast-RTPS 库提供的各种平台和语言中使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值