Skip to main content

A simple tool to convert ROS .msg files to Protobuf .proto files.

Project description

r2pb - ROS .msg to Protobuf .proto Converter

r2pb 是一个功能强大且易于使用的命令行工具和 Python 库,用于将 ROS (Robot Operating System) 的 .msg 文件转换为 Google 的 Protobuf (.proto) 文件。它支持从本地文件系统或远程 Git 仓库(如 GitHub)自动获取 ROS 包。

特性

  • CLI 和库两种使用方式: 提供 r2pb 命令行工具用于快速转换,也提供 Python API (r2pb.Converter) 用于在代码中集成。
  • 自动获取依赖: 能够自动从 GitHub 等远程 Git 仓库克隆 ROS 包及其依赖项。
  • 本地缓存: 将获取的包缓存在本地 (~/.cache/r2pb),加快后续转换速度。
  • 灵活的输出控制: 用户可以指定输出目录来存放生成的 .proto 文件。
  • 跨平台: 可在 Windows, macOS 和 Linux 上运行。

安装

通过 pip 从 PyPI 安装:

pip install r2pb

或者,如果你想从源码安装并进行开发:

git clone https://github.com/tz-bb/r2pb.git
cd r2pb
pip install -e .

使用方法

命令行接口 (CLI)

CLI 是使用 r2pb 的最简单方式。基本语法如下:

r2pb <package_name/message_name> [options]

示例:

  1. 转换单个消息

    将 std_msgs 包中的 String 消息转换为 .proto 文件,并保存到 generated_protos 目录:

    r2pb std_msgs/String -o generated_protos
    

    执行后, generated_protos/std_msgs/ 目录下会生成 String.proto 文件。

  2. [TODO] 转换整个包

未来版本将支持转换一个包中的所有消息。 选项:

  • -o, --output-dir : 指定存放生成文件的输出目录。默认为当前目录下的 generated_protos 。
  • --ros-distro : **[TODO]**指定 ROS 发行版(如 noetic , humble ),用于查找正确的包版本。默认为 noetic 。

Python API

你也可以在 Python 代码中使用 r2pb 的 Converter 类来实现更复杂的逻辑。

示例:

from r2pb import Converter

# 初始化转换器
converter = Converter()

# 定义输出目录
output_dir = "my_protos"

# 转换 std_msgs/String
# convert 方法在成功时返回 True,失败时会抛出异常
try:
    success = converter.convert(["std_msgs/String"], output_dir)
    if success:
        print(f"Successfully converted messages and saved to {output_dir}")
except Exception as e:
    print(f"An error occurred: {e}")

工作原理

  1. 解析输入 : r2pb 首先解析你提供的消息名称,如 std_msgs/String 。
  2. 查找包 : 它会在本地缓存中查找 std_msgs 包。如果找不到,它会使用 rosdistro 数据库来定位包的远程 Git 仓库。
  3. 获取包 : r2pb 会克隆仓库到本地缓存目录 ( ~/.cache/r2pb )。
  4. 解析消息文件 : 它会读取 .msg 文件的内容,分析其字段和类型。
  5. 类型映射 : r2pb 会将 ROS 的内置类型(如 string , int32 , Header )映射到对应的 Protobuf 类型(如 string , int32 , Timestamp )。
  6. 生成 .proto 文件 : 最后,它使用模板生成 .proto 文件,包含正确的 syntax , package 定义和消息结构。

贡献

欢迎任何形式的贡献!如果你发现了 bug、有功能建议或想改进代码,请随时提交 Pull Request 或创建 Issue。

许可证

本项目基于 GNU AGPLv3 发布。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

r2pb-0.1.0.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

r2pb-0.1.0-py3-none-any.whl (22.3 kB view details)

Uploaded Python 3

File details

Details for the file r2pb-0.1.0.tar.gz.

File metadata

  • Download URL: r2pb-0.1.0.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for r2pb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 33fd33433dfd83ed9672abf283ec5d30db5bcb5e1b798dc78fc2211eed5751fd
MD5 738d018603c9b3cd89be84eb46885f4e
BLAKE2b-256 91f8f22b62aa4211e4fac4f6c9921a825fc38c6720ada10ee002406b23b5307f

See more details on using hashes here.

File details

Details for the file r2pb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: r2pb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for r2pb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b959277c860b95132152b72abb5461194f1b95a998842bcf536d38d99d02655
MD5 d0748004f09bc51854b58f36a3976d29
BLAKE2b-256 60d9f7d447fbaac7b3ceee7593e8f3cf481446c06dc0920e43983292eedb489c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page