Skip to main content

ZeroMQ message bus: broker routing and participant SDK

Project description

Robot Bus

CI crates.io PyPI License

轻量级、免环境配置的 ROS 2 风格通信库:基于 ZeroMQ,提供 topic / service / action,以及 Executor + Node + spin 回调模型。

不依赖 ROS 发行版、不需要 source setup.bash、不搭 workspace。一个 broker 进程 + SDK(Rust / Python)即可。

设计原则:API 会尽量贴近 ROS 2 的用法与命名(如 NodeSingleThreadedExecutor / MultiThreadedExecutoradd_nodecreate_publisher / create_subscriptionspin),降低从 ROS 2 迁过来的心智负担;底层用 ZeroMQ 实现,不绑定某一 ROS 发行版。

预发布说明:当前仍处于预发布阶段。接下来 API 可能会有较多变更,运行稳定性也尚不完善,请谨慎用于生产环境。

更多 API 示例见 docs/

模块 职责
broker:: 路由进程(message / service / action)
顶层 API Publisher / Subscriber / Client / Worker
runtime::Executor 底层 poll loop(一般用下面两个包装)
runtime::SingleThreadedExecutor / MultiThreadedExecutor ROS 2 风格执行器;add_node + spin
runtime::Node / TopicPublisher / CallbackGroup 节点、publisher、callback group(互斥 / 可重入)
grpc::(默认 feature) gRPC / gRPC-Web 网关(随 broker 一起启动)
proto/ ROS 风格 Protobuf:proto/<pkg>/{msg|srv|grpc}/v1/ → Rust/Python robot_bus.<pkg>…

架构

业务代码 (Rust / Python)
  └── robot-bus SDK
              │
              │ ZMQ (tcp / ipc / inproc)
              ▼
robot_bus_broker 进程

快速开始

1. 启动 broker

Rust:

cargo run --bin robot_bus_broker

Python(pip install robot-bus 后自带可执行入口):

robot-bus-broker

或在代码里进程内启动:

import robot_bus

with robot_bus.RobotBusBroker.start() as broker:
    # ... 业务代码 ...
    pass
# 离开 with 自动 stop

# 或阻塞运行(等同于命令行,Ctrl+C 退出)
# robot_bus.run_broker()

Python

pip install robot-bus

本地开发(需 maturin):

maturin develop --features extension-module,grpc

grpc 为默认 feature;显式写出可避免 default-features = false 的构建漏掉网关。)

import robot_bus
from robot_bus.sensor_msgs.msg.v1 import Imu
from robot_bus.geometry_msgs.msg.v1 import Vector3

def on_imu(topic, payload):
    imu = Imu()
    imu.ParseFromString(payload)
    print(topic, imu.linear_acceleration)

node = robot_bus.Node("pilot")
executor = robot_bus.SingleThreadedExecutor()
executor.add_node(node)

imu_pub = node.create_publisher("/robot1/imu")
node.create_subscription("/robot1/imu", on_imu)
imu_pub.publish(
    Imu(linear_acceleration=Vector3(x=0.0, y=0.0, z=9.8)).SerializeToString(),
)
# executor.spin()  # 阻塞直到其它线程调用 executor.shutdown()

2. Rust(Executor + Node + spin)

Cargo.toml 中添加依赖:

robot-bus = { path = "../robot-bus" }
# 或 crates.io:robot-bus = "0.0.3"

语义接近 ROS 2:Node::newexecutor.add_nodecreate_publisher(topic)spin

use std::sync::Arc;
use std::time::Duration;
use prost::Message;
use robot_bus::geometry_msgs::msg::v1::Vector3;
use robot_bus::sensor_msgs::msg::v1::Imu;
use robot_bus::{Node, SingleThreadedExecutor};

let mut node = Node::new("pilot");
let executor = SingleThreadedExecutor::new();
executor.add_node(&mut node)?;

let imu_pub = node.create_publisher("/robot1/imu")?;
node.create_subscription_typed::<Imu, _>(
    "/robot1/imu",
    |topic, imu| {
        println!("{topic}: {:?}", imu.linear_acceleration);
    },
    None,
)?;

let imu = Imu {
    linear_acceleration: Some(Vector3 { x: 0.0, y: 0.0, z: 9.8 }),
    ..Default::default()
};
imu_pub.publish(&imu.encode_to_vec())?;

node.create_timer(
    Duration::from_millis(100),
    Arc::new(|| {
        // 控制周期 / 心跳
    }),
    None,
)?;

let handle = executor.shutdown_handle()?;
std::thread::spawn(move || { /* ... */ handle.shutdown(); });
executor.spin()?;
  • SingleThreadedExecutor:回调在 I/O / spin 线程串行(默认)
  • MultiThreadedExecutor::new(n):最多 n 个 worker;Reentrant group 可并行,MutuallyExclusive 组内串行
  • Callback group:MutuallyExclusive / Reentrantcreate_callback_group;默认互斥组)
  • Service / action:create_service / create_clientcreate_action / create_action_client(与 topic 一样挂在 Node)
  • Timer:create_timer(同样挂在 Node,由 spin 驱动)
  • Raw bytes:create_subscription(topic, Arc::new(|topic, payload| { ... }), None)
  • 底层 escape hatch:Executor(高级用法)

发送 / 接收水位(ZMQ HWM,不是完整 QoS)可在创建时或运行中设置:

use robot_bus::{Publisher, HighWaterMark};

let pub_ = Publisher::with_hwm(None, HighWaterMark::new(10, 10))?;
pub_.set_high_water_mark(HighWaterMark { snd: 10, rcv: 10 })?;

默认:message STREAM(2/2)、service RPC(4/4)、action ACTION(8/8)。Broker 侧用 --snd-hwm / --rcv-hwm

二进制

二进制 说明
robot_bus_broker 一次启动三条总线 + gRPC / gRPC-Web 网关

gRPC / gRPC-Web 网关

robot_bus_broker / RobotBusBroker::start 一起启动;标准 gRPC 与 gRPC-Web 同端口(默认 0.0.0.0:15770)。

RPC 语义
MessageGateway.Subscribe 按 topic 前缀订阅,服务端流式返回二进制 payload
ServiceGateway.Call 一元:service_name + request bytes → response bytes
ActionGateway.Run 双向流:客户端发 GOAL / CANCEL,服务端推 ActionEventkind 区分 FEEDBACK / RESULT)
cargo run --bin robot_bus_broker
# 配置:cargo run --bin robot_bus_broker -- --help
# gRPC: http://0.0.0.0:15770

进程内:

use robot_bus::{GrpcBrokerConfig, RobotBusBroker, RobotBusConfig};

let broker = RobotBusBroker::start(RobotBusConfig {
    grpc: GrpcBrokerConfig {
        listen: "0.0.0.0:15770".parse()?,
        ..Default::default()
    },
    ..RobotBusConfig::default()
})?;
let grpc = format!("http://{}", broker.grpc_listen());

Proto(包名 robot_bus.grpc.v1,与 ROS *.msg.v1 / *.srv.v1 区分):

测试

cargo test
PYTHONPATH=python python3 tests/python/test_msgs_roundtrip.py

Protobuf 消息

proto/ 按 ROS 包布局:proto/<pkg>/{msg|srv|grpc}/v1/*.proto

语言 路径 说明
Rust robot_bus::<pkg>::{msg|srv}::v1 build.rs + prost;挂在 crate 命名空间下
Python robot_bus.<pkg>.{msg|srv}.v1 随 wheel 打包;scripts/generate_python_msgs.py 生成
  • 传输层 body 仍是 opaque bytes;bus 不解析类型,业务侧自行 encode / decode(或用 create_subscription_typed
  • srv 是一对 *Request / *Response message,不是 gRPC
  • grpcrobot_bus)是网关 RPC 契约,随 broker 启动(默认 feature grpc
  • 消息在 robot_bus 命名空间下,不占用 ROS 顶层 sensor_msgs 包名;编码是 protobuf,与 ROS CDR 不互通
  • 改 proto 后跑:python3 scripts/generate_python_msgs.py(建议 protoc 28.x,与 CI 一致)

已覆盖:builtin_interfacesstd_msgsstd_srvsgeometry_msgssensor_msgsnav_msgstf2_msgstrajectory_msgsdiagnostic_msgsunique_identifier_msgsshape_msgsvisualization_msgscontrol_msgsnav2_msgs

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

robot_bus-0.0.3-cp39-abi3-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.9+Windows x86-64

robot_bus-0.0.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

robot_bus-0.0.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.1 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

robot_bus-0.0.3-cp39-abi3-macosx_11_0_arm64.whl (1.8 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file robot_bus-0.0.3-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: robot_bus-0.0.3-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for robot_bus-0.0.3-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 da23c7608a31e6fbb0ad336eb77bb140d6cd04bfa6117f35fd6f6e3e124e7ecf
MD5 8d5e50f11c715d95a63465081a415a23
BLAKE2b-256 c420a9660b13cf3fbbba51e6653082b8c48d88d147dc82017d70d7e78ccac92f

See more details on using hashes here.

File details

Details for the file robot_bus-0.0.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for robot_bus-0.0.3-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c72ac9254608b15cf11a376954a616d64be43d35a484101b8f5de6e81fdea383
MD5 6eddea499cbab0ef863e46799a4d0ef1
BLAKE2b-256 415f4e9c0aa0802938bceda405bae096f479378749f2c14f7164d33daf8ad76b

See more details on using hashes here.

File details

Details for the file robot_bus-0.0.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for robot_bus-0.0.3-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 28b726df96b1d63d9ebdf7581558652779883b27a6d39edf1c5e4dfb38068b42
MD5 f049e721a6c435b1dbc0d4b1ad535e03
BLAKE2b-256 51fd784a82978e3068eb5049a36a2fd8bbc3763dc42eebca5d639fa9fea089ce

See more details on using hashes here.

File details

Details for the file robot_bus-0.0.3-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for robot_bus-0.0.3-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2ff06fb033c10b1ad734b800835fc4b5613765be736bb225b172271a2ed02608
MD5 d81dfb0698c51d2bf9bf1ce12bcf4ce6
BLAKE2b-256 2108ee8816b05b77bc82823087d74b7b1d89b04a1293ecd0df79671cec56817c

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