《机器人实践开发②: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 开发环境完整搭建指南(含常见坑位)》

相关推荐
繁华似锦respect1 小时前
Linux-内核核心组成部分
linux·c++
机器觉醒时代1 小时前
星动纪元 | 清华孵化的人形机器人先锋,以「具身大脑+本体+灵巧手」定义通用智能未来
人工智能·机器人·人形机器人·灵巧手
不知所云,2 小时前
2.windows c/c++ 编译器安装, mingw和clang
c语言·c++·windows·mingw·clang·c编译器
爪哇部落算法小助手2 小时前
爪哇周赛 Round 3
数据结构·c++·算法
十五年专注C++开发2 小时前
Mimalloc:一款高性能、低开销和线程安全的C++内存分配器
c++·内存分配·mimalloc
纵有疾風起2 小时前
【C++—STL】红黑树底层封装与set/map模拟实现
开发语言·c++·经验分享·面试·开源·stl
却道天凉_好个秋2 小时前
c++ shared_ptr与unique_ptr总结
c++
冰糖小新新3 小时前
基于CanMV K230的工地巡检机器人
人工智能·信息可视化·机器人
不知所云,3 小时前
4. vscode c++ 环境及工程搭建 clangd + mingw
c++·ide·vscode·开发环境·clangd