Fast-RTPS简单测试

虽然在官方的Github中有不少的例子,但是感觉都挺复杂的,不能很好的理解,所以自己拆解成比较简单的,易于学习理解。我是刚开始学习,一定会有不扫理解有误的地方,希望大家指正

  1. 首先编写 .idl 文件,这个文件的主要作用是定义用于发布topic的数据类型

//test.idl

struct Test
{
	unsigned long index;
	string message;
};
  1. 将.idl文件制作成可以被c++认识的类型,这一步需要fastrtpsgen工具
fastrtpsgen test.idl
//如果需要生成示例
fastrtpsgen -example CMake test.idl

执行上面两条命令中的一条后,会生成至少四个文件,这四个文件将被用于定义topic的类型,和实例

生成的四个文件的分别是
test.cxx test.h testPubSubTypes.cxx testPubSubTypes.h

  1. 编写发布节点的代码

//pub.cpp

#include "test.h"
#include "testPubSubTypes.h"

#include <fastrtps/fastrtps_fwd.h>
#include <fastrtps/participant/Participant.h>
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/attributes/PublisherAttributes.h>
#include <fastrtps/publisher/PublisherListener.h>
#include <fastrtps/publisher/Publisher.h>
#include <fastrtps/Domain.h>
#include <fastrtps/utils/eClock.h>

#include <iostream>

using namespace std;

int main()
{
	//topic消息实例
    Test m_test;
    //topic类型实例
    TestPubSubType m_type;

	//下面两句为消息实例赋值
    m_test.index( 0 );
    m_test.message( "HelloTest" );
    //初始化一个参与者属性的实例,这个实例可以设置许多属性,这一点我还类有研究,使用用默认的
    eprosima::fastrtps::ParticipantAttributes parAttr;
    //创建一个参与者
    eprosima::fastrtps::Participant * par = eprosima::fastrtps::Domain::createParticipant( parAttr );
    if( par == nullptr)
        return 1;

	//注册topic的消息类型
    eprosima::fastrtps::Domain::registerType( par, &m_type);

	//创建一个发布者属性的实例,并设置消息类型和topic名字,是必须的
    eprosima::fastrtps::PublisherAttributes pubAttr;
    pubAttr.topic.topicDataType = "Test";
    pubAttr.topic.topicName = "firstTopic";
    //创建一个发布者
    eprosima::fastrtps::Publisher * pub = eprosima::fastrtps::Domain::createPublisher( par, pubAttr);
    if( pub == nullptr )
        return  2;


	//while循环发送消息,并每次发送后休眠1000毫秒
    int i = 0;
    while( i++ < 10000)
    {

        pub->write( (void *)&m_test );
        m_test.index( m_test.index() + 1);

        eprosima::fastrtps::eClock::my_sleep( 1000 );
    }

	//remove这个参与者
    eprosima::fastrtps::Domain::removeParticipant( par );

    return 0;
}

  1. 编写订阅节点
#include "test.h"
#include "testPubSubTypes.h"

#include <fastrtps/participant/Participant.h>
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/attributes/SubscriberAttributes.h>
#include <fastrtps/subscriber/SubscriberListener.h>
#include <fastrtps/subscriber/Subscriber.h>
#include <fastrtps/Domain.h>
#include <fastrtps/subscriber/SampleInfo.h>


class SubListener : public eprosima::fastrtps::SubscriberListener
{
public:
    SubListener():n_matched(0),n_samples(0){};
    ~SubListener(){};
//    void onSubscriptionMatched(eprosima::fastrtps::Subscriber* sub, eprosima::fastrtps::rtps::MatchingInfo& info);
    void onNewDataMessage(eprosima::fastrtps::Subscriber* sub)
    {
        if(sub->takeNextData((void*)&m_test, &m_info))
        {
            if(m_info.sampleKind == eprosima::fastrtps::rtps::ALIVE)
            {
                this->n_samples++;
                // Print your structure data here.
                std::cout << "Message "<< m_test.message()<< " "<< m_test.index()<< " RECEIVED"<<std::endl;
            }
        }
    }
    Test m_test;
    eprosima::fastrtps::SampleInfo_t m_info;
    int n_matched;
    uint32_t n_samples;
};


int main(int argc, char *argv[])
{
    Test m_test;
    TestPubSubType m_type;

    eprosima::fastrtps::ParticipantAttributes PParam;
    eprosima::fastrtps::Participant *participant = eprosima::fastrtps::Domain::createParticipant(PParam);
    if( participant == nullptr )
        return  false;

    eprosima::fastrtps::Domain::registerType(participant, &m_type);


    eprosima::fastrtps::SubscriberAttributes Rparam;
    Rparam.topic.topicDataType = "Test";
    Rparam.topic.topicName = "firstTopic";

	//与发布者节点相比最大的不同在于,创建订阅者的时候有一个类用于处理订阅到的消息,估计患有其他的方式处理订阅到的消息
    SubListener lis;
    eprosima::fastrtps::Subscriber * subscriber = eprosima::fastrtps::Domain::createSubscriber(participant, Rparam, &lis);


    std::cin.get();

    eprosima::fastrtps::Domain::removeParticipant( participant );

    return 0;
}

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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 库提供的各种平台和语言中使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值