《机器人实践开发②:Foxglove 嵌入式移植 + CMake 集成》

导语:

将 Foxglove 集成到嵌入式平台(如 ARM Linux)是许多机器人开发者常见需求。本篇文章介绍如何将 Foxglove sdk 移植到嵌入式系统,并通过 CMake 进行工程化构建与部署,适合做自定义机器人系统的同学参考。

foxglove sdk地址 下载到本地

cpp 复制代码
foxglove-sdk# tree -L 1
.
├── CONTRIBUTING.md
├── Cargo.lock
├── Cargo.toml
├── Container.mk
├── Dockerfile
├── Dockerfile.ros
├── LICENSE
├── Makefile
├── README.md
├── c
├── cpp
├── eslint.config.mjs
├── jest.config.json
├── package.json
├── poetry.lock
├── pyproject.toml
├── python
├── ros
├── rust
├── rustfmt.toml
├── schemas
├── scripts
├── tsconfig.json
├── typescript
└── yarn.lock

配置 Cmake 及前置条件

cpp 复制代码
cd /opt/opensource/foxglove-sdk/cpp
cmake -Bbuild

进入 build 目录,生成 makefile

cpp 复制代码
cd build
cmake ../

编译,安装

cpp 复制代码
make -j10
make install

生成库

注意

生成静态库libfoxglove.a

生成动态库libfoxglove_cpp_shared.so libfoxglove.so

cpp 复制代码
tree -L 1
.
├── CMakeCache.txt
├── CMakeFiles
├── CTestTestfile.cmake
├── DartConfiguration.tcl
├── Makefile
├── Testing
├── _deps
├── cargo
├── cmake_install.cmake
├── compile_commands.json
├── corrosion
├── example_auto_serialize
├── example_connection_graph
├── example_foxglove_schemas
├── example_mcap
├── example_param_server
├── example_quickstart
├── example_server
├── example_services
├── libfoxglove.a
├── libfoxglove.so
├── libfoxglove_cpp_shared.so
├── libfoxglove_cpp_static.a
├── tests
├── tests-b12d07c_include.cmake
└── tests-b12d07c_tests.cmake

5 directories, 21 files

头文件目录

cpp 复制代码
/opt/opensource/foxglove-sdk/cpp# tree foxglove/include/foxglove/
foxglove/include/foxglove/
├── arena.hpp
├── channel.hpp
├── context.hpp
├── error.hpp
├── expected.hpp
├── foxglove.hpp
├── mcap.hpp
├── schema.hpp
├── schemas.hpp
├── server
│   ├── connection_graph.hpp
│   ├── fetch_asset.hpp
│   ├── parameter.hpp
│   └── service.hpp
└── server.hpp

配置工程的 CmakeLists.txt

将上面的头文件和库文件,放到自己的工程目录下

以下为演示目录,自己需要根据实际的情况进行修改

cpp 复制代码
#foxglove include 
include_directories(opensource/include/foxglove/include)
#foxglove lib
link_directories(opensource/lib/linux/opensource/foxglove/lib)

编写测试代码测试

cpp 复制代码
#include <foxglove/channel.hpp>
#include <foxglove/context.hpp>
#include <foxglove/error.hpp>
#include <foxglove/foxglove.hpp>
#include <foxglove/mcap.hpp>
#include <foxglove/schemas.hpp>
#include <foxglove/server.hpp>

#include <atomic>
#include <chrono>
#include <cmath>
#include <csignal>
#include <functional>
#include <iostream>
#include <thread>

//using namespace std::chrono_literals;

int main() {
    foxglove::setLogLevel(foxglove::LogLevel::Debug);
    static std::function<void()> sigint_handler;

    std::signal(SIGINT, [](int) {
        if (sigint_handler) {
            sigint_handler();
        }
    });

    foxglove::McapWriterOptions mcap_options = {};
    mcap_options.path = "quickstart-cpp-tmp5.mcap";
    auto writer_result = foxglove::McapWriter::create(mcap_options);
    if (!writer_result.has_value()) {
        std::cerr << "Failed to create writer: " << foxglove::strerror(writer_result.error()) << '\n';
        return 1;
    }
    auto writer = std::move(writer_result.value());

    // Start a server to communicate with the Foxglove app.
    foxglove::WebSocketServerOptions ws_options;
    ws_options.host = "10.211.55.5";
//    ws_options.host = "localhost";
    ws_options.port = 8791;
    auto server_result = foxglove::WebSocketServer::create(std::move(ws_options));
    if (!server_result.has_value()) {
        std::cerr << "Failed to create server: " << foxglove::strerror(server_result.error()) << '\n';
        return 1;
    }
    auto server = std::move(server_result.value());
    std::cerr << "Server listening on port " << server.port() << '\n';

#if 1
    // Create a schema for a JSON channel for logging {size: number}
    foxglove::Schema schema;
    schema.encoding = "jsonschema";
    std::string schema_data = R"({
        "type": "object",
        "properties": {
        "size": { "type": "number" }
        }
    })";
    schema.data = reinterpret_cast<const std::byte*>(schema_data.data());
    schema.data_len = schema_data.size();
    auto channel_result = foxglove::RawChannel::create("/size", "json", std::move(schema));
    if (!channel_result.has_value()) {
      std::cerr << "Failed to create channel: " << foxglove::strerror(channel_result.error()) << '\n';
      return 1;
    }
    auto size_channel = std::move(channel_result.value());

    // Create a SceneUpdateChannel for logging changes to a 3d scene
    auto scene_channel_result = foxglove::schemas::SceneUpdateChannel::create("/scene");
    if (!scene_channel_result.has_value()) {
      std::cerr << "Failed to create scene channel: "
                << foxglove::strerror(scene_channel_result.error()) << '\n';
      return 1;
    }
    auto scene_channel = std::move(scene_channel_result.value());

    std::atomic_bool done = false;
    sigint_handler = [&] {
      done = true;
    };

    while (!done) {
      auto now = std::chrono::duration_cast<std::chrono::duration<double>>(
                   std::chrono::system_clock::now().time_since_epoch()
      )
                   .count();
      double size = std::abs(std::sin(now)) + 1.0;
      std::string msg = "{\"size\": " + std::to_string(size) + "}";
      size_channel.log(reinterpret_cast<const std::byte*>(msg.data()), msg.size());

      foxglove::schemas::CubePrimitive cube;
      cube.size = foxglove::schemas::Vector3{size, size, size};
      cube.color = foxglove::schemas::Color{1, 0, 0, 1};

      foxglove::schemas::SceneEntity entity;
      entity.id = "box";
      entity.cubes.push_back(cube);

      foxglove::schemas::SceneUpdate scene_update;
      scene_update.entities.push_back(entity);

      scene_channel.log(scene_update);

        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

#else
    while (1) {
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
#endif
  return 0;
}

修改 CmakeLists.txt 添加以上的测试代码

注意:

需要使用PROPERTY CXX_STANDARD 17 否则会报错

cpp 复制代码
set(TST_FLOXGLOVE_DEMO "tst_floxglove_demo")
add_executable(${TST_FLOXGLOVE_DEMO}
       tst/foxglove/main.cpp
)
set_property(TARGET ${TST_FLOXGLOVE_DEMO} PROPERTY CXX_STANDARD 17)
set_property(TARGET ${TST_FLOXGLOVE_DEMO} PROPERTY CXX_STANDARD_REQUIRED True)
#   动态库
target_link_libraries(${TST_FLOXGLOVE_DEMO}   PUBLIC foxglove_cpp_shared foxglove  pthread m rt dl)

使用 foxglove 客户端显示,参考《机器人实践开发①:Foxglove 开发环境完整搭建指南(含常见坑位)》

相关推荐
J ..1 小时前
C++ 多线程编程基础与 std::thread 使用
c++
你的冰西瓜1 小时前
C++标准模板库(STL)全面解析
开发语言·c++·stl
闻缺陷则喜何志丹1 小时前
【计算几何】仿射变换与齐次矩阵
c++·数学·算法·矩阵·计算几何
chen_ever1 小时前
Protobuf详解(从安装到实战)
c++·rpc·信息与通信
hmbbcsm2 小时前
python做题小记(八)
开发语言·c++·算法
科士威传动2 小时前
方形滚珠导轨如何保障高速定位精度?
人工智能·科技·机器人·自动化·制造
再睡一夏就好2 小时前
深入Linux线程:从轻量级进程到双TCB架构
linux·运维·服务器·c++·学习·架构·线程
特立独行的猫a3 小时前
C++开发中的Pimpl机制与类声明机制深度解析:现代C++的编译解耦艺术
开发语言·c++·pimpl
GoWjw3 小时前
在C&C++指针的惯用方法
c语言·开发语言·c++
君义_noip3 小时前
信息学奥赛一本通 1453:移动玩具 | 洛谷 P4289 [HAOI2008] 移动玩具
c++·算法·信息学奥赛·csp-s