Skip to main content

Ragtime Florid USB SDK - MIT direct control over USB CDC

Project description

florid-usb-sdk

Ragtime 机械臂 USB 主机 SDK — 提供基于 USB CDC 的 MIT 直接控制,支持 C++Python

架构

用户代码
  │
  ▼
florid_usb (Arm 类)
  │  ├── ProtocolStack (序列化 / 反序列化 / 可靠会话)
  │  └── Astrial (USB 串口传输,基于 Asio)
  │
  ▼
STM32H7 固件 (USB CDC)
  • 固件内置重力补偿(CasADi 生成),tau 参数是在重力补偿之上的额外前馈力矩
  • dt_usseq 由 SDK 根据两次调用之间的壁钟时间自动计算
  • 两次调用间隔 > 200ms 视为新轨迹(dt_us = 1000µs

获取源码

git clone https://github.com/Ragtime-LAB/florid-usb-sdk.git
cd florid-usb-sdk

# 初始化所有子模块
git submodule update --init --recursive

如果 clone 时忘了加 --recursive,随时补跑即可。

系统依赖(需预先安装)

C++20 编译器

项目使用 C++20 特性,必须使用支持 C++20 的编译器(GCC >= 10 / Clang >= 10 / MSVC 2022+)。

Ubuntu 24.04 及以上自带 GCC 14,可以忽略这一步; Ubuntu 22.04 默认 GCC 版本不足,建议安装 GCC 13:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-13 g++-13
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 130 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-13
# 如需切换回系统默认编译器:
sudo update-alternatives --config gcc

其他系统包

sudo apt install cmake

Python 绑定还需要:

sudo apt install python3-dev python3-numpy
用途 必需
GCC >= 10 / Clang >= 10 / MSVC 2022+(C++20) 编译
cmake(>= 3.21) 构建系统
python3-dev Python C 扩展 仅 Python 绑定
python3-numpy Python 数组接口 仅 Python 绑定

其余依赖由 CMake 自动获取(git submodule / FetchContent):

依赖 来源 用途
Astrial git submodule (3rdparty/astrial) USB 串口传输(基于 Asio,跨平台)
florid-usb-protocols git submodule (protocols/) 协议栈(ProtocolStack, ReliableSession, 包定义)
rpl 内置于 protocols/3rdparty/rpl RPL 序列化框架
unordered_dense CMake FetchContent 哈希表
pybind11 CMake FetchContent(仅 Python) C++/Python 绑定

C++ 快速开始

构建

cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DBUILD_PYTHON=ON
cmake --build build

BUILD_PYTHON=ON 表示同时构建 Python 绑定库;如果只需要 C++ 库可以去掉此选项。

使用

#include <florid/usb/Arm.hpp>

using namespace florid::usb;
using namespace std::chrono_literals;

Arm::Config cfg;
cfg.device = "/dev/ttyACM0";
cfg.baud_rate = 115200;

Arm arm(cfg);
arm.connect();

// 启动 USB 会话(阻塞,等待固件确认)
arm.startSession(1s);

// 读取当前关节位置
auto status = arm.getArmStatus();

// 发送 MIT 位置指令(fire-and-forget,非阻塞)
float q[6]  = {0, 0, 0, 0, 0, 0.5};
float dq[6] = {};
float tau[6]= {};
float kp[6] = {8, 8, 8, 8, 8, 8};
float kd[6] = {0.7, 0.7, 0.7, 0.7, 0.7, 0.7};
arm.sendMitCommand(q, dq, tau, kp, kd);

// 停止会话
arm.stopSession(1s);

CMake 集成

add_subdirectory(path/to/florid-usb-sdk)

target_link_libraries(my_app PRIVATE florid_usb)

sdk/usb 目录下 protocols/ 子目录也会被自动构建。

C++ API 参考

Arm::Config

字段 默认值 说明
device /dev/ttyACM0 串口设备路径
baud_rate 115200 波特率
session_timeout 500ms 每个可靠请求的超时
max_retries 3 最大重试次数

Arm

方法 阻塞/非阻塞 说明
connect() 非阻塞 打开串口,启动后台工作线程
disconnect() 阻塞 关闭串口,停止线程
isConnected() 非阻塞 是否已连接且持续收到遥测
startSession(timeout) 阻塞 发送 UsbSessionStart 并等待响应
stopSession(timeout) 阻塞 发送 UsbSessionStop 并等待响应
sendMitCommand(q, dq, tau, kp, kd, mode) 非阻塞 MIT 直接控制(fire-and-forget)
setMotorControlMode(joint_id, mode, timeout) 阻塞 切换电机控制模式
sendPosVelCommand(q, dq, enabled_mask) 非阻塞 POSVEL 模式控制
sendVelocityCommand(dq, enabled_mask) 非阻塞 速度模式控制
sendHybridCommand(q, dq_limit, current_limit, enabled_mask) 非阻塞 混合力位控制
sendGripperCommand(q, dq, tau, kp, kd, mode) 非阻塞 夹爪 MIT 控制
emergencyStop() 非阻塞 急停(fire-and-forget)
getArmStatus() 非阻塞 获取缓存的机械臂状态
getGripperStatus() 非阻塞 获取缓存的夹爪状态
getMotorFeedback(timeout) 阻塞 请求电机详细反馈
homeAll(timeout) 阻塞 归零所有关节
clearFaults(timeout) 阻塞 清除所有故障

Python 快速开始

安装

pip install git+https://github.com/Ragtime-LAB/florid-usb-sdk.git

或从本地构建:

pip install -e .

使用

import numpy as np
from florid_usb import Arm, Config

# 创建配置
cfg = Config()
cfg.device = "/dev/ttyACM0"
cfg.baud_rate = 115200

# 连接
arm = Arm(cfg)
arm.connect()

# 启动会话
arm.start_session(timeout=1.0)

# 发送 MIT 指令
q  = np.zeros(6, dtype=np.float32)
dq = np.zeros(6, dtype=np.float32)
tau = np.zeros(6, dtype=np.float32)
kp = np.full(6, 8.0, dtype=np.float32)
kd = np.full(6, 0.7, dtype=np.float32)
q[5] = 0.5  # J5 偏转 0.5 rad

arm.send_mit_command(q, dq, tau, kp, kd, control_mode=1)

# 读取状态
status = arm.get_arm_status()
print(status["q"])       # numpy array, 6 floats
print(status["mode"])    # int: 0=INIT, 1=IDLE, 2=RUNNING, 3=FAULT, 4=ESTOP
print(status["gripper"]) # dict: q, dq, tau, temp_c, enabled

# 切换电机模式
arm.set_motor_control_mode(joint_id=0, mode="posvel", timeout=0.5)

# 停止
arm.stop_session()
arm.disconnect()

Python API 参考

florid_usb 模块导出 ArmConfig 两个类。

Config

属性 默认值 说明
device /dev/ttyACM0 串口设备路径
baud_rate 115200 波特率
session_timeout_ms 500 每个可靠请求的超时(毫秒)
max_retries 3 最大重试次数

Arm

方法 阻塞/非阻塞 说明
connect() 打开串口并启动通信
disconnect() 关闭串口
is_connected() 连接状态
start_session(timeout=0.5) 启动 USB 会话,timeout 单位秒
stop_session(timeout=0.5) 停止 USB 会话
send_mit_command(q, dq, tau, kp, kd, control_mode=1) MIT 控制,参数为长度为 6 的 numpy 数组
set_motor_control_mode(joint_id, mode, timeout=0.5) 切换电机模式。mode 可以是字符串 `'mit'
send_posvel_command(q, dq, enabled_mask=0x3f) POSVEL 控制
send_velocity_command(dq, enabled_mask=0x3f) 速度控制
send_hybrid_command(q, dq_limit, current_limit_norm, enabled_mask=0x3f) 混合力位控制
send_gripper_command(q, dq, tau, kp, kd, control_mode=1) 夹爪控制,参数为单个 float
emergency_stop() 急停
get_arm_status() 返回 dict,包含 mode, seq, timestamp_us, q, dq, tau, gripper
get_gripper_status() 返回 dict,包含 q, dq, tau, temp_c, enabled
get_motor_feedback(timeout=0.5) 返回 dict {motors: [...]},每个电机包含 joint_id, position_rad, speed_rad_s, torque_nm, temp_c
home_all(timeout=0.5) 归零
clear_faults(timeout=0.5) 清除故障

Python 示例脚本

python/ 目录下提供了可直接运行的示例:

脚本 说明
example_simple.py MIT 控制,J5 斜坡运动 @ 500 Hz
example_gripper.py 夹爪开合控制
read_arm_status.py 持续读取并打印机械臂状态
read_dual_status.py 双机械臂状态读取
mit_pd_move_to_center.py MIT PD 控制移动到目标位置
tuning_ui.py Tkinter 调参 GUI
teleop_mit.py / teleop_posvel.py / teleop_hybrid.py 主从遥操作(MIT / POSVEL / HYBRID 模式)
gravity_compensation_control.py 重力补偿控制
computed_torque_sin_j12345.py 计算力矩 + 正弦轨迹
test_all_api.py 遍历所有 API 接口的自动化测试

CMake 构建后,通过设置 PYTHONPATH 运行:

PYTHONPATH=build/python python python/example_simple.py /dev/ttyACM0

或直接 pip install . 安装模块后运行:

pip install .
python python/example_simple.py /dev/ttyACM0

实机操作文档

详见此文档

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

florid_usb-0.1.0.tar.gz (931.7 kB view details)

Uploaded Source

Built Distributions

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

florid_usb-0.1.0-cp313-cp313-win_amd64.whl (318.3 kB view details)

Uploaded CPython 3.13Windows x86-64

florid_usb-0.1.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (360.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

florid_usb-0.1.0-cp312-cp312-win_amd64.whl (318.3 kB view details)

Uploaded CPython 3.12Windows x86-64

florid_usb-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (360.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

florid_usb-0.1.0-cp311-cp311-win_amd64.whl (316.1 kB view details)

Uploaded CPython 3.11Windows x86-64

florid_usb-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (357.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

florid_usb-0.1.0-cp310-cp310-win_amd64.whl (314.7 kB view details)

Uploaded CPython 3.10Windows x86-64

florid_usb-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (356.6 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

File details

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

File metadata

  • Download URL: florid_usb-0.1.0.tar.gz
  • Upload date:
  • Size: 931.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for florid_usb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 98d8af9200efd82b37ed9846879340e6c466f7aaaecdbe4a04d1176d4acd7ea0
MD5 2226299c97168dff4184f13f4c1d0e6f
BLAKE2b-256 4af5240a97f0c49e11a201e38fe46644e0f5d6b13f889dd83a867ea046c1f0eb

See more details on using hashes here.

File details

Details for the file florid_usb-0.1.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.1.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 318.3 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for florid_usb-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e98e74b4b9153808a43c459927b3a42034d2fb4c3c3073d59670208ceb6ea760
MD5 8b838c0be8eb3a447b2a4a95ecce2184
BLAKE2b-256 c34d611a51e04113063e449a763665d964161ae8a8e553fd9e1544e524123e7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.0-cp313-cp313-win_amd64.whl:

Publisher: publish-pypi.yml on Ragtime-LAB/florid-usb-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file florid_usb-0.1.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for florid_usb-0.1.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 691d6425cf2ad2e20964c63e0559514c84ae9d877717630c84f9a41f5b63cb8b
MD5 ec45f10e7abe7e871f42a413898f9ad2
BLAKE2b-256 65e0e740b6656679bea114e21a35bc1f5078f4125affc493ab33600f7cc175b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on Ragtime-LAB/florid-usb-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file florid_usb-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.1.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 318.3 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for florid_usb-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 42e57234d6a5d0d43798f4a60236119ab96ae3e12cb6727506586e9325967b3d
MD5 d6c73f058d53f449c58be2af9a93a445
BLAKE2b-256 baba4b6f512c7791d485a070c012aaa13b0020daf64aa93d7f6375bdc525f880

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.0-cp312-cp312-win_amd64.whl:

Publisher: publish-pypi.yml on Ragtime-LAB/florid-usb-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file florid_usb-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for florid_usb-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7cf3e4d448bee5843b246f6b5a840a33f27c4bbb06bcfc4db6e46892ec008081
MD5 d58f21ae400ebf2c1a2b6e3e56e8e6d2
BLAKE2b-256 757d21cc1f4d316dc0dc530102ada7e90e3042a44e693ad1fa5f57d34c103301

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on Ragtime-LAB/florid-usb-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file florid_usb-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.1.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 316.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for florid_usb-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7fca0976f5cc28bf103a6a8c8f26873f418fa2029683a6ca378193d3d9acfb80
MD5 209cc66ec27f3ce3b3af8af9f09432cf
BLAKE2b-256 ebe6887be57f809003f221afc084c2d4bff1cfd3ad2dae17d6c3863ecb2f0d60

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.0-cp311-cp311-win_amd64.whl:

Publisher: publish-pypi.yml on Ragtime-LAB/florid-usb-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file florid_usb-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for florid_usb-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ee33377d168d473d764d9cd93253e521b1d496109817f212465813eb8c49c1be
MD5 fa50f242e4ae646fd4eddcc7d3d01794
BLAKE2b-256 b5602218771924fa8ff9e3429e362a0db9c4c7d884714ad1e9d6d80f0208276a

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on Ragtime-LAB/florid-usb-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file florid_usb-0.1.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.1.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 314.7 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for florid_usb-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 05044655a2bed3f6e9028da39cdf8423e7e82bb0ef2e34f42d659d230402a290
MD5 c4deb048fd0b10d26d393118a665812e
BLAKE2b-256 9a885a6422eeb037844acf82f4d11423baa6c3872994f9926817a2387517dc49

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.0-cp310-cp310-win_amd64.whl:

Publisher: publish-pypi.yml on Ragtime-LAB/florid-usb-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file florid_usb-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for florid_usb-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 fc11f2b497e335eaf1e31bd6d95e1243088531f1a4f839c20a4302978c57645b
MD5 114e0cccfbd29b1127411301261b3e47
BLAKE2b-256 a19eddaec28a1179020cf624c58b22f54e292a84df95ecbead0249f70ef37dec

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish-pypi.yml on Ragtime-LAB/florid-usb-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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