Skip to main content

ROS Client

一个用于连接和控制ROS(Robot Operating System)设备的Python客户端库,特别针对无人机控制场景进行了优化。

功能特性

  • WebSocket连接: 通过rosbridge连接ROS设备
  • 状态监控: 实时获取无人机状态(位置、姿态、电池等)
  • 图像处理: 接收和显示相机图像
  • 点云处理: 接收和可视化3D点云数据
  • 控制命令: 发送ROS Topic控制命令
  • 自动重连: 支持连接断开自动重连机制
  • Mock模式: 提供Mock客户端用于测试
  • GUI工具: 图形界面测试工具

安装

从 PyPI 安装(推荐)

# 基本安装(仅核心功能)
pip install rosclient

# 完整安装(包含所有可选功能)
pip install rosclient[all]

# 或分别安装可选功能
pip install rosclient[image]      # 图像处理功能
pip install rosclient[pointcloud] # 点云可视化功能
pip install rosclient[recording]  # 记录功能

从源码安装

# 克隆仓库
git clone <repository-url>
cd rosclient

# 安装依赖
pip install roslibpy numpy

# 安装包
pip install -e .

完整安装(包含图像和点云功能)

pip install roslibpy numpy opencv-python Pillow matplotlib

快速开始

基本使用

from rosclient import RosClient

# 创建客户端
client = RosClient("ws://localhost:9090")

# 异步连接
client.connect_async()

# 等待连接
import time
time.sleep(2)

# 获取状态
if client.is_connected():
    state = client.get_status()
    print(f"模式: {state.mode}, 电池: {state.battery}%")
    
    position = client.get_position()
    print(f"位置: {position}")

# 发送控制命令
client.publish("/control", "controller_msgs/cmd", {"cmd": 1})

# 断开连接
client.terminate()

使用Mock客户端(测试)

from rosclient import MockRosClient

# 创建Mock客户端(无需实际ROS连接)
client = MockRosClient("ws://localhost:9090")
client.connect_async()

# 使用方式与真实客户端相同
state = client.get_status()
print(f"状态: {state}")

项目结构

rosclient/
├── rosclient/              # 主包
│   ├── core/              # 核心功能
│   │   ├── base.py        # 基类
│   │   └── topic_service_manager.py  # Topic/Service管理器
│   ├── clients/           # 客户端实现
│   │   ├── airsim_client.py  # AirSim仿真环境客户端
│   │   ├── ros_client.py  # 生产环境客户端
│   │   ├── mock_client.py # Mock客户端
│   │   └── config.py      # 配置
│   ├── models/            # 数据模型
│   │   ├── drone.py       # 无人机状态模型
│   │   └── state.py       # 连接状态枚举
│   └── utils/             # 工具函数
│       ├── logger.py      # 日志工具
│       └── backoff.py     # 指数退避算法
├── tests/                 # 测试套件
├── rosclient_gui_test.py  # GUI测试工具
└── README.md

主要API

RosClient

# 连接管理
client.connect_async()      # 异步连接
client.terminate()          # 断开连接
client.is_connected()       # 检查连接状态

# 状态获取
client.get_status()         # 获取完整状态
client.get_position()       # 获取位置 (lat, lon, alt)
client.get_orientation()    # 获取姿态 (roll, pitch, yaw)

# 图像和点云
client.fetch_camera_image()        # 获取相机图像
client.get_latest_image()          # 获取最新图像
client.fetch_point_cloud()        # 获取点云数据
client.get_latest_point_cloud()    # 获取最新点云

# 消息发布
client.publish(topic, type, message)  # 安全发布消息

# 服务调用
client.service_call(service, type, payload)  # 安全调用服务

数据模型

from rosclient import DroneState, ConnectionState

# DroneState包含所有无人机状态信息
state = DroneState(
    connected=True,
    armed=False,
    mode="GUIDED",
    battery=85.5,
    latitude=22.5329,
    longitude=113.93029,
    altitude=100.0,
    # ... 更多字段
)

# ConnectionState枚举
state = ConnectionState.CONNECTED

GUI测试工具

项目包含一个图形界面测试工具,方便测试和调试:

python gui_tkinter.py

GUI工具提供以下功能:

  • 连接配置和测试
  • 实时状态监控
  • 图像显示
  • 点云可视化
  • 控制命令发送
  • 网络测试

Metadata

Release files for rosclient 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rosclient 1.0.0
File Size Uploaded
rosclient-1.0.0.tar.gz 69.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rosclient 1.0.0
File Interpreter ABI Platform
rosclient-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 147.1 kB

Release files / rosclient-1.0.0.tar.gz

Download URL rosclient-1.0.0.tar.gz
Size 69.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5795ba3a630526156fe2a6d6577b781c08b333c6b26bfbaa3d3beb0b77711503
BLAKE2b-256 checksum
How to use checksums
c66824a45685e08c9de0195b043a4e5f5b514c09e3abd4c01cedac72d778e6a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/41.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.2.1 tqdm/4.65.0 importlib-metadata/8.7.0 keyring/24.2.0 rfc3986/1.5.0 colorama/0.4.6 CPython/3.10.10

Release files / rosclient-1.0.0-py3-none-any.whl

Download URL rosclient-1.0.0-py3-none-any.whl
Size 77.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a526c857de2fc152b408110a3756a5f4569d518c0c4b1b51f5226f3fe8b9778a
BLAKE2b-256 checksum
How to use checksums
fb489ddd3e117ba7dddcd10dde804bff5df5f34f8e7eae7052fdbe2680e22280
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.9.6 readme-renderer/41.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.2.1 tqdm/4.65.0 importlib-metadata/8.7.0 keyring/24.2.0 rfc3986/1.5.0 colorama/0.4.6 CPython/3.10.10

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page