Skip to main content

ZeroMQ message bus: broker routing and participant SDK

Project description

Robot Bus

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

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

模块 职责
broker:: 路由进程(message / service / action)
顶层 API Publisher / Subscriber / Client / Worker
runtime::BusRuntime 回调 executor(spin / spin_once
runtime::Node Node 门面(名字 / 命名空间 + create_*
proto/ 常用 ROS 2 消息 / Service 的 Protobuf 定义

架构

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

快速开始

1. 启动 broker

cargo run --bin robot_bus_broker

Broker 由 Rust 二进制提供;Python wheel 暂不内嵌 broker。

Python

pip install robot-bus

本地开发(需 maturin):

maturin develop --features extension-module
import robot_bus

node = robot_bus.Node("pilot", namespace="robot1")
node.create_publisher(robot_bus.message_xsub_endpoint())
node.create_subscription(
    "imu",
    lambda topic, payload: print(topic, payload),
    robot_bus.message_xpub_endpoint(),
)
node.publish("imu", b"hello")
# node.spin()  # 阻塞直到其它线程调用 node.shutdown()

拉取式:

pub = robot_bus.Publisher(robot_bus.message_xsub_endpoint())
pub.publish("wireless.imu", b"...")
sub = robot_bus.Subscriber(robot_bus.message_xpub_endpoint())
sub.subscribe("wireless.imu")
topic, payload = sub.receive(timeout=1.0)

2. Rust(拉取式)

Cargo.toml 中添加依赖:

robot-bus = { path = "../robot-bus" }
# 或 crates.io:robot-bus = "0.0.2"
use robot_bus::{Publisher, Subscriber, message_xsub_endpoint, message_xpub_endpoint};

let pub_ = Publisher::new(None)?;
pub_.publish("wireless.imu", imu_bytes)?;
let sub = Subscriber::new(Some(&message_xpub_endpoint("localhost", "tcp")?))?;
sub.subscribe("wireless.imu")?;

发送 / 接收水位(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

3. 回调式(spin)

语义接近 ROS 2 的 spin / spin_once / spin_some

use std::sync::Arc;
use robot_bus::{BusRuntime, MessageCallback, message_xpub_endpoint};

let mut rt = BusRuntime::new();
rt.connect_subscriber(Some(&message_xpub_endpoint("localhost", "tcp")?))?;
let cb: MessageCallback = Arc::new(|topic, payload| {
    println!("{topic}: {} bytes", payload.len());
});
rt.subscribe("wireless.imu", cb)?;

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

let handle = rt.shutdown_handle();
std::thread::spawn(move || { /* ... */ handle.shutdown(); });
rt.spin()?;
  • BusRuntime::new():回调在 I/O / spin 线程执行
  • BusRuntime::with_executor(n):service / action handler 最多 n 个并发线程;订阅与 timer 仍在 I/O 线程

4. Node 门面

Node 包一层 BusRuntime,API 接近 ROS 2:create_subscription / create_publisher / create_timer / create_service。相对名会加命名空间前缀;以 / 开头的绝对名不变。

use std::sync::Arc;
use robot_bus::{Node, message_xpub_endpoint, message_xsub_endpoint};

let mut node = Node::with_namespace("pilot", "robot1");
node.create_publisher(Some(&message_xsub_endpoint("localhost", "tcp")?))?;
node.create_subscription(
    "imu", // → robot1/imu
    Arc::new(|topic, payload| println!("{topic}: {} bytes", payload.len())),
    Some(&message_xpub_endpoint("localhost", "tcp")?),
)?;
node.create_timer(Duration::from_millis(100), Arc::new(|| { /* ... */ }))?;
node.publish("cmd_vel", twist_bytes)?; // → robot1/cmd_vel
node.spin()?;

二进制

二进制 说明
robot_bus_broker 一次启动三个 bus
message_bus_broker 仅 message bus
service_bus_broker 仅 service bus
action_bus_broker 仅 action bus

测试

cargo test

Protobuf 消息

proto/ 是常用 ROS 2 msg / srv 的 Protobuf 重定义,经 build.rs + prost 生成到 robot_bus::msgs

  • 传输层 body 仍是 opaque bytes;bus 不解析类型,业务侧自行 encode / decode
  • srv 是一对 *Request / *Response message,不是 gRPC

已覆盖: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.2-cp39-abi3-win_amd64.whl (434.1 kB view details)

Uploaded CPython 3.9+Windows x86-64

robot_bus-0.0.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (684.6 kB view details)

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

robot_bus-0.0.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (684.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

robot_bus-0.0.2-cp39-abi3-macosx_11_0_arm64.whl (563.4 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: robot_bus-0.0.2-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 434.1 kB
  • 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.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 4e702fb81ed40b7d1a321324dff3dfe2a26e224564f23d7a39b17dc0aa2b4e5b
MD5 d6e9450c568cdcccc69bb9fc42ef7356
BLAKE2b-256 b6a2e3fbbeebb5f220bc715db1ae4df107553331f66bff43d27514ea5e105a6a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for robot_bus-0.0.2-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98d80d95bcb5842dd0018ff12445c3cdec4816ff582cb31a547206f3e697d7c3
MD5 186f32f333899236056dd438c27cee0c
BLAKE2b-256 a4f1c865977ca90153ce8d72bce13be219cd27265a35fc346ac44c75f72e0e56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for robot_bus-0.0.2-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 0f700c56f86127c83f8f840e584b257a903cfba08c4237b193ccd0594b4ca4b4
MD5 66e70d1371a527b9ec366c9f77e7b460
BLAKE2b-256 e28eae4e10d8633ded01f19ad74c3131ac9b84cc967a0db357e38b012b830f6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for robot_bus-0.0.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dfc5e79778a30c29928539fa3127ef8c28f883c292a5ea0a285188caa72c929d
MD5 f60e249924bb24bab8634c882fd51728
BLAKE2b-256 26e3202a1efc0fff9393a222679111372c44c58350f0f9bd427f192b52c7b7cf

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