RTI-DDS代码分析使用介绍

DDS(Data Distribution Service数据分发服务)是对象管理组织OMG的有关分布式实时系统中数据发布的规范。

DDS规范采用了发布/订阅体系结构,但对实时性要求提供更好的支持。DDS是以数据为中心的发布/订阅通信模型。

以下工程基于rti_connext_dds-7.2.0

hello_world.idl定义的HelloWorld结构体如下

使用RTI Code Generator(rtiddsgen)生成对应工程。

生成的工程目录如下

用VS2017打开工程

重点关注 hello_world_publisher.cxx和 hello_world_subscriber.cxx两个文件

Publisher

publisher实现的是发布,subscriber实现的是订阅。

在hello_world_publisher.cxx中

c 复制代码
    // 创建一个HelloWorld类型以HelloWorld Topic命名的Topic
    dds::topic::Topic<HelloWorld> topic(participant, "HelloWorld Topic");

    dds::pub::Publisher publisher(participant);

    // DataWriter将要在"HelloWorld Topic"中写入数据
    dds::pub::DataWriter<HelloWorld> writer(publisher, topic);

定义HelloWorld类型的sample,sample.msg()定义sample输出的内容

通过write函数写入数据

c 复制代码
    HelloWorld sample;
    for (unsigned int count = 0;
         !shutdown_requested && count < sample_count;
         count++) {

		sample.msg("Hello world! " + std::to_string(count));

        std::cout << "Writing HelloWorld, count " << count << std::endl;

        writer.write(sample);

        rti::util::sleep(dds::core::Duration(4));
    }

Subscriber

类似的,定义Subscriber

c 复制代码
    // 创建一个HelloWorld类型以HelloWorld Topic命名的Topic
    dds::topic::Topic<HelloWorld> topic(participant, "HelloWorld Topic");

    dds::sub::Subscriber subscriber(participant);

    // DataReader将要读取topic数据
    dds::sub::DataReader<HelloWorld> reader(subscriber, topic);

创建状态条件,满足条件才读取数据

c 复制代码
    // 创建条件
    dds::core::cond::StatusCondition status_condition(reader);

    status_condition.enabled_statuses(
            dds::core::status::StatusMask::data_available());

    // 条件触发后关联句柄
    unsigned int samples_read = 0;
    status_condition.extensions().handler([&reader, &samples_read]() {
        samples_read += process_data(reader);
    });
c 复制代码
	dds::core::cond::WaitSet waitset;
    waitset += status_condition;

    while (!shutdown_requested && samples_read < sample_count) {
        // 条件激活
        std::cout << "HelloWorld subscriber sleeping for 4 sec..."
                  << std::endl;

        waitset.dispatch(dds::core::Duration(4));  // Wait up to 4s each time
    }
相关推荐
开开心心就好1 天前
解决截图被拦截黑屏问题的免费小工具
安全·智能手机·flink·kafka·pdf·音视频·1024程序员节
开开心心就好7 天前
清理重复文件释放C盘空间的工具
安全·智能手机·pdf·gitlab·音视频·intellij idea·1024程序员节
chase20257 天前
电机发声原理和实现
经验分享·dds·电机发声·电机唱歌·低转矩振动激励
曾小蛙9 天前
【ROS2+深度相机】Orbbec相机时图像传输延迟显著 (DDS数据分发服务配置)
ros2·dds·奥比中光·orbbec
数据皮皮侠AI11 天前
中国土地利用驱动因子数据集(9种驱动因子/裁剪到省市/Tif)
大数据·人工智能·笔记·能源·1024程序员节
想成为优秀工程师的爸爸14 天前
车载以太网之要火系列 - 第58篇:郭大侠学DDS(QoS策略):服务质量花样多,刹车插队视频播
车载以太网·dds·自学笔记
数据皮皮侠AI15 天前
上市公司耐心资本数据(2010-2025)
大数据·人工智能·笔记·能源·1024程序员节
特立独行的猫a16 天前
Fast DDS Monitor Windows x64 从源码编译安装完全教程
windows·monitor·dds·fastdds·fastddsmonitor
特立独行的猫a17 天前
eProsima Fast DDS & Fast DDS Spy & Shapes Demo Windows x64 从源码编译安装完全教程
windows·dds·fastdds·fastddsspy·shapesdemo
开开心心就好17 天前
解决图片无页码添加功能的实用工具
javascript·python·安全·智能手机·pdf·音视频·1024程序员节