【C++】开源:iceoryx通信中间件配置与使用

😏*★,°* :.☆( ̄▽ ̄)/$:.°★ 😏

这篇文章主要介绍iceoryx通信中间件配置与使用。
学其所用,用其所学。------梁启超

欢迎来到我的博客,一起学习,共同进步。

喜欢的朋友可以关注一下,下次更新不迷路🥞

文章目录

    • [:smirk:1. iceoryx介绍](#:smirk:1. iceoryx介绍)
    • [:blush:2. 环境安装与配置](#:blush:2. 环境安装与配置)
    • [:satisfied:3. 应用示例](#:satisfied:3. 应用示例)

😏1. iceoryx介绍

Iceoryx(冰羚)是一种高性能、实时通信中间件,专门设计用于处理大规模、实时数据交换的场景。Iceoryx旨在提供低延迟、高吞吐量和可靠性,适用于各种实时系统和嵌入式应用。

官网:https://iceoryx.io/v1.0.1/

Github地址:https://github.com/eclipse-iceoryx/iceoryx

以下是Iceoryx的一些关键特点和优势:

1.低延迟和高吞吐量:Iceoryx采用零拷贝和共享内存的方式进行数据传输,减少了数据复制的开销,从而实现低延迟和高吞吐量。
2.实时性能:Iceoryx专注于实时通信需求,具有可预测的性能表现,适用于需要严格实时性能的系统。
3.轻量级设计:Iceoryx采用模块化的设计,只包含必要的功能模块,使其更加轻量级并且易于集成到现有系统中。
4.支持多种通信模式:Iceoryx支持发布-订阅、请求-响应等多种通信模式,可以灵活地满足不同应用场景的通信需求。
5.跨平台兼容:Iceoryx支持多种操作系统和架构,可以在不同平台上运行,并提供一致的接口和性能。

Iceoryx广泛应用于自动驾驶、机器人技术、工业自动化等领域,为实时数据交换提供了可靠的解决方案。通过使用Iceoryx,开发人员可以更轻松地构建高性能、实时的通信系统,满足对实时性能有严格要求的应用需求。

😊2. 环境安装与配置

参考:https://iceoryx.io/v1.0.1/getting-started/installation/

Ubuntu下环境配置:

sh 复制代码
sudo apt install gcc g++ cmake libacl1-dev libncurses5-dev pkg-config
git clone https://github.com/eclipse-iceoryx/iceoryx.git
cd iceoryx
cmake -Bbuild -Hiceoryx_meta
cmake -Bbuild -Hiceoryx_meta -DCMAKE_PREFIX_PATH=$(PWD)/build/dependencies/
cmake --build build
sudo cmake --build build --target install

😆3. 应用示例

iox-roudi是Iceoryx中的一个重要组件,负责管理Iceoryx运行时的核心功能。RouDi 是 Runtime Discovery(运行时发现)和 Routing(路由)的缩写,它提供了一种轻量级的进程间通信机制,用于实现不同模块之间的通信和数据交换。

需要首先启动(类似roscore):iox-roudi

发布订阅示例:

cpp 复制代码
// pub.cpp
#include "topic_data.hpp"
#include "iox/signal_watcher.hpp"
#include "iceoryx_posh/popo/publisher.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include <iostream>

int main()
{
    // initialize runtime
    constexpr char APP_NAME[] = "iox-cpp-publisher-helloworld";
    iox::runtime::PoshRuntime::initRuntime(APP_NAME);

    // create publisher
    iox::popo::Publisher<RadarObject> publisher({"Radar", "FrontLeft", "Object"});

    double ct = 0.0;
    // wait for term
    while (!iox::hasTerminationRequested())
    {
        ++ct;

        // Retrieve a sample from shared memory
        auto loanResult = publisher.loan();
        // publish
        if (loanResult.has_value())
        {
            auto& sample = loanResult.value();
            // Sample can be held until ready to publish
            sample->x = ct;
            sample->y = ct;
            sample->z = ct;
            sample.publish();
        }
        else
        {
            auto error = loanResult.error();
            // Do something with error
            std::cerr << "Unable to loan sample, error code: " << error << std::endl;
        }

        // msg
        std::cout << APP_NAME << " sent value: " << ct << std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    return 0;
}
cpp 复制代码
// sub.cpp
#include "topic_data.hpp"
#include "iceoryx_posh/popo/subscriber.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iox/signal_watcher.hpp"
#include <iostream>

int main()
{
    // initialize runtime
    constexpr char APP_NAME[] = "iox-cpp-subscriber-helloworld";
    iox::runtime::PoshRuntime::initRuntime(APP_NAME);

    // initialize subscriber
    iox::popo::Subscriber<RadarObject> subscriber({"Radar", "FrontLeft", "Object"});

    // run until interrupted by Ctrl-C
    while (!iox::hasTerminationRequested())
    {
        // receive
        auto takeResult = subscriber.take();
        if (takeResult.has_value())
        {
            std::cout << APP_NAME << " got value: " << takeResult.value()->x << std::endl;
        }
        else
        {
            // error
            if (takeResult.error() == iox::popo::ChunkReceiveResult::NO_CHUNK_AVAILABLE)
            {
                std::cout << "No chunk available." << std::endl;
            }
            else
            {
                std::cout << "Error receiving chunk." << std::endl;
            }
        }

        // wait
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    return (EXIT_SUCCESS);
}
sh 复制代码
# CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(IceoryxExample)

set(CMAKE_CXX_STANDARD 11)

# 查找Iceoryx的包
include(GNUInstallDirs)
find_package(iceoryx_platform REQUIRED)
find_package(iceoryx_posh CONFIG REQUIRED)
find_package(iceoryx_hoofs CONFIG REQUIRED)

include(IceoryxPackageHelper)
include(IceoryxPlatform)
include(IceoryxPlatformSettings)

iox_add_executable(
    TARGET  pub_helloworld
    FILES   ./pub.cpp
    LIBS    iceoryx_posh::iceoryx_posh
)

iox_add_executable(
    TARGET  sub_helloworld
    FILES   ./sub.cpp
    LIBS    iceoryx_posh::iceoryx_posh
)

以上。

相关推荐
程序员皮皮林33 分钟前
开源PDF工具 Apache PDFBox 认识及使用(知识点+案例)
java·pdf·开源·apache
sinat_276522571 小时前
C++中move的使用
开发语言·c++
微尘81 小时前
C语言存储类型 auto,register,static,extern
服务器·c语言·开发语言·c++·后端
金博客1 小时前
Qt 模型视图(二):模型类QAbstractItemModel
c++·qt6.7.2
五味香2 小时前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
无名之逆2 小时前
计算机专业的就业方向
java·开发语言·c++·人工智能·git·考研·面试
Beauty.5682 小时前
P1328 [NOIP2014 提高组] 生活大爆炸版石头剪刀布
数据结构·c++·算法
super_journey2 小时前
RabbitMq中交换机(Exchange)、队列(Queue)和路由键(Routing Key)
分布式·中间件·rabbitmq
jimmy.hua2 小时前
C++刷怪笼(5)内存管理
开发语言·数据结构·c++
xiaobai12 32 小时前
二叉树的遍历【C++】
开发语言·c++·算法