当前位置: 首页 > news >正文

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中

    // 创建一个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函数写入数据

    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

    // 创建一个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);

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

    // 创建条件
    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);
    });
	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
    }

相关文章:

  • JAVA-编程基础-11-04-java IO 字符流
  • 设计模式-建造者模式
  • 汇编的指令
  • Linux下的命令行参数和环境变量
  • 【Git LFS】huggingface 断点续传
  • 【机器学习】模型平移不变性/等变性归纳偏置Attention机制
  • MATLAB R2023b安装包下载链接及软件安装教程
  • Proteus仿真--一种智能频率计的设计与制作(AVR单片机+proteus仿真)
  • 经典卷积神经网络 - AlexNet
  • 轻松学习Python:基础知识汇总
  • 在 Vue react 中,哪些地方用到闭包?
  • LeetCode--快速排序
  • LVS-DR模式
  • android利用FFmpeg进行视频转换
  • Java中的常量管理:接口还是枚举,您如何选择?
  • 一、XSS加解密编码解码工具
  • c语言练习91:合并两个有序链表
  • 使用WPF模仿Windows记事本界面
  • 【单片机学习笔记】Windows+Vscode+STM32F4+freeRTOS+FatFs gcc环境搭建
  • 大学英语试卷
  • 中方拟解除对5名欧洲议会议员制裁?外交部:望中欧立法机构相向而行
  • 工业富联一季度净利增长25%,云计算业务营收增长超50%
  • 李铁案二审今日宣判
  • 中国人保不再设监事会,国寿集团未再设置监事长职务
  • 白云山一季度营收净利双降,此前称今年将挖掘盘活自身资源
  • 保利发展去年净利润约50亿元,在手现金1342亿元