冰羚源码编译及通信示例

以 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:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值