以 Linux 为例
1 源码编译
以下编译过程为网上搜索结果,也可参考代码仓中 installation.md 文档
1.1. 安装基本依赖
sudo apt install gcc g++ cmake libacl1-dev libncurses5-dev pkg-config
1.2 下载源码
git clone https://github.com/eclipse-iceoryx/iceoryx.git
1.3 cmake版本
要求 cmake 版本不低于 3.16 ,可参照以下博客更新
https://blog.csdn.net/qq_26226375/article/details/130265617
ps:从 github 拉取 cmake 仓库可能会出现链接不到 github 的情况,如果确认 ssh 等配置OK的情况下,可能是网络问题,重复多试几次后成功(GitHub就这尿性,没有翻墙的情况下全凭运气)
1.4 利用脚本编译
#方式1:编译必要组件
cd iceoryx
cmake -Bbuild -Hiceoryx_meta
cmake --build build
sudo cmake --build build --target install
# 方式2:编译全部组件
cd iceoryx
./tools/iceoryx_build_test.sh build-all
ps1:方式2编译全部组件时若出现文件打开权限问题,请在命令前加上 sudo
ps2:在执行 “cmake -Bbuild -Hiceoryx_meta” 命令时,会自动下载 cpptoml ,可能会链接 github 不成功导致编译失败,多试几次即可
2 通信示例
冰羚代码仓中有具体 examples 可供用户使用:/iceoryx/iceoryx_examples/
2.1 创建Topic
Publisher 和 Subscriber 之间通过 Topic 进行匹配
topic_data.hpp
#ifndef IOX_EXAMPLES_ICEHELLO_TOPIC_DATA_HPP
#define IOX_EXAMPLES_ICEHELLO_TOPIC_DATA_HPP
//! [radar object]
struct RadarObject
{
double x = 0.0;
double y = 0.0;
double z = 0.0;
};
//! [radar object]
#endif // IOX_EXAMPLES_ICEHELLO_TOPIC_DATA_HPP
2.2 创建Publisher
消息发送端:
● 定义 APP_NAME,通过 APP_NAME 初始化 runtime 用于和RouDi进行通信
● 创建 publisher,并绑定 Topic
● 通过 publisher 的 loan 和 数据的 publish() 发送消息
● std::this_thread::sleep_for 设置发送间隔
Radar:服务名称
FrontLeft:代表 Radar 服务的实例的名称
Counter:定义了 FrontLeft 服务实例上的一个事件
//! [include topic]
#include "topic_data.hpp"
//! [include topic]
//! [include sig watcher]
#include "iceoryx_dust/posix_wrapper/signal_watcher.hpp"
//! [include sig watcher]
//! [include]
#include "iceoryx_posh/popo/publisher.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
//! [include]
#include <iostream>
int main()
{
//! [initialize runtime]
constexpr char APP_NAME[] = "iox-cpp-publisher-helloworld";
iox::runtime::PoshRuntime::initRuntime(APP_NAME);
//! [initialize runtime]
//! [create publisher]
iox::popo::Publisher<RadarObject> publisher({"Radar", "FrontLeft", "Object"});
//! [create publisher]
double ct = 0.0;
//! [wait for term]
while (!iox::posix::hasTerminationRequested())
//! [wait for term]
{
++ct;
// Retrieve a sample from shared memory
//! [loan]
auto loanResult = publisher.loan();
//! [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();
}
//! [publish]
//! [error]
else
{
auto error = loanResult.error();
// Do something with error
std::cerr << "Unable to loan sample, error code: " << error << std::endl;
}
//! [error]
//! [msg]
std::cout << APP_NAME << " sent value: " << ct << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
//! [msg]
}
return 0;
}
2.3 创建Subscriber
消息接收端:
● 定义 APP_NAME,通过 APP_NAME 初始化 runtime
● 创建 subscriber,并绑定 Topic (要与 publisher 的保持一致)
● 通过 subscriber 的 take 接收消息
● std::this_thread::sleep_for 设置接收间隔
//! [include]
#include "topic_data.hpp"
#include "iceoryx_dust/posix_wrapper/signal_watcher.hpp"
#include "iceoryx_posh/popo/subscriber.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
//! [include]
#include <iostream>
int main()
{
//! [initialize runtime]
constexpr char APP_NAME[] = "iox-cpp-subscriber-helloworld";
iox::runtime::PoshRuntime::initRuntime(APP_NAME);
//! [initialize runtime]
//! [initialize subscriber]
iox::popo::Subscriber<RadarObject> subscriber({"Radar", "FrontLeft", "Object"});
//! [initialize subscriber]
// run until interrupted by Ctrl-C
while (!iox::posix::hasTerminationRequested())
{
//! [receive]
auto takeResult = subscriber.take();
if (takeResult.has_value())
{
std::cout << APP_NAME << " got value: " << takeResult.value()->x << std::endl;
}
//! [receive]
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;
}
//! [error]
}
//! [wait]
std::this_thread::sleep_for(std::chrono::milliseconds(100));
//! [wait]
}
return (EXIT_SUCCESS);
}
2.4 调试运行
代码编译后在build目录下会生成对应应用的 publisher 和 subscriber 的 bin 文件
运行iceoryx程序前,一定要先启动守护进程 iox-roudi
2.4.1 运行步骤:
● 在终端运行:iox-roudi
● 另起终端运行 publisher
● 再起一个终端运行 subscriber
2.4.2 运行结果:
以代码仓中的 icehello 为例
启动 roudi:
publisher:
subscriber: