更新时间:2026-08-05 GMT+08:00
分享

注册自定义适配器(作为开发者)

实现 IRobotHardwareAdapter

from dataclasses import dataclass
from typing import Any, Mapping
from r2c_sdk.core.interfaces import IRobotHardwareAdapter

@dataclass
class MyRobotHardwareAdapter(IRobotHardwareAdapter):
    config: Mapping[str, Any]

    def __post_init__(self) -> None:
        from my_robot_commands import MyRobotHomeCommand
        self.register_command_class("go_home", MyRobotHomeCommand)

    def connect(self) -> None:
        """初始化硬件连接。幂等。"""
        ...

    def disconnect(self) -> None:
        """释放硬件资源。"""
        ...

    def get_observation(self) -> Mapping[str, Any]:
        """返回最新原始观测数据。"""
        ...

    def send_action(self, command: Mapping[str, Any]) -> None:
        """下发控制指令到硬件。"""
        ...

创建工厂函数

def create_my_robot_adapter(
    config: Mapping[str, Any], **extra_kwargs: Any
) -> IRobotHardwareAdapter:
    """Entry_point 工厂。"""
    return MyRobotHardwareAdapter(config=dict(config))

声明entry_point

# pyproject.toml
[project]
name = "my-robot-r2c-adapter"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = ["hw-r2c-sdk"]

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
py-modules = ["my_robot_adapter", "my_robot_commands"]

[project.entry-points."r2c_sdk.adapters"]
my_robot = "my_robot_adapter:create_my_robot_adapter"

包结构

my-robot-r2c-adapter/
├── pyproject.toml
├── my_robot_adapter.py      # 工厂函数 + Adapter 类
└── my_robot_commands.py     # 自定义 AdapterCommand 子类

实现自定义命令

from r2c_sdk.robots.commands.base import AdapterCommand

class MyRobotHomeCommand(AdapterCommand):
    requires_pause = True   # 是否需要在暂停态执行

    def execute(self, **kwargs):
        target = self.config.get("joints")
        if target is not None:
            self.adapter.send_action({"joint_target": list(target)})

发布流程

  1. 实现适配器和命令。
  2. 编写pyproject.toml,声明entry_point。
  3. pip install build && python -m build构建。
  4. 发布到PyPI或内网pip源。
  5. 用户pip install my-robot-r2c-adapter后即可使用。

相关文档

请在本地R2C SDK的文件夹的examples/third_party_adapter/获取完整示例。

相关文档