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.2.tar.gz (930.8 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.2-cp313-cp313-win_amd64.whl (318.3 kB view details)

Uploaded CPython 3.13Windows x86-64

florid_usb-0.1.2-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.2-cp312-cp312-win_amd64.whl (318.3 kB view details)

Uploaded CPython 3.12Windows x86-64

florid_usb-0.1.2-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.2-cp311-cp311-win_amd64.whl (316.1 kB view details)

Uploaded CPython 3.11Windows x86-64

florid_usb-0.1.2-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.2-cp310-cp310-win_amd64.whl (314.7 kB view details)

Uploaded CPython 3.10Windows x86-64

florid_usb-0.1.2-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.2.tar.gz.

File metadata

  • Download URL: florid_usb-0.1.2.tar.gz
  • Upload date:
  • Size: 930.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for florid_usb-0.1.2.tar.gz
Algorithm Hash digest
SHA256 158b28780f5116010a2326519bbe06c11c119421b90341280ec7f86d9017186e
MD5 1487dff003f467b04cf4f3cd3988d2e4
BLAKE2b-256 88ff6f3c80b0b5fb97459da484d16f360e988a122e998a78e6f848846661bf6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2.tar.gz:

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.2-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.1.2-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.13

File hashes

Hashes for florid_usb-0.1.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 78cf41a4da79f7321b3aae9e7f31fe28719b0a169645dc6827802b344262621c
MD5 2510ecde8e4f602a2e4d6247551f289e
BLAKE2b-256 9cd6454a3d5a31ac38c30b676927206185b847346986cd7983aba449646c80af

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2-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.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for florid_usb-0.1.2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c48a6bea3beb96f9bca6d6b9ae5a243256d6cf49deb77193a59ecbbc9d4e66ff
MD5 76cb3dcd13879d89c63ea08aecd2033d
BLAKE2b-256 f0ed52382176205166aaab5acec9aa7633aa478237bfa1a7d69d19b7febeca0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2-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.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.1.2-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.13

File hashes

Hashes for florid_usb-0.1.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e14ca3959bceb388f739a34d281f38096c44e1c52f21b41d40f28d411d657b0d
MD5 0191f6b9f0b026ba008f0ef0ec6a922a
BLAKE2b-256 23a6d0f9aba7ae1bef97c95a5906278a884af423e253dd539a088042d329d029

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2-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.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for florid_usb-0.1.2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8d2b11767532c6897285c3eaaaf9778ce20422666043e1005383b550867d2578
MD5 316151ad844f204d04995e1daef24f86
BLAKE2b-256 267c74d7d65ef41ad96edce5ddeab050b16d1605c8b0ad8fe818ed4f4501a174

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2-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.2-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.1.2-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.13

File hashes

Hashes for florid_usb-0.1.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 05175d9ca57af0c650e13b088949435c0c3ae1a1011d25b732bd556e330909e8
MD5 2e2e7ce4ae097d56eefeff84ee67a6f5
BLAKE2b-256 8ae472a3b6acd443c54296afe9bcc3ba9f9049c61ddd8a2dbc813db4a2c16880

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2-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.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for florid_usb-0.1.2-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ddb5f9e2164eb03a7a13149f97dd039b024a1d644eb70ace56d136fce610c419
MD5 ef0fc3bce37e199d8150dd6ed8100354
BLAKE2b-256 e630a90b11fd6f5456ea15f1699190cc0f1249d8af1fac093838d4187281eb92

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2-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.2-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.1.2-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.13

File hashes

Hashes for florid_usb-0.1.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b69afd16ddc61af34b56daa6f3003ca729dc2679f1ff231ecbbe7aefaaf6fa0d
MD5 d81ef64392bbf8eee4aa54efa1511ed3
BLAKE2b-256 4307f357cb033379c06f7f4e9ed1067f24e2e6f22a47680fd60ca25520ba195a

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2-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.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for florid_usb-0.1.2-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 198b438990608347d83f731729385b059dc083a419a0aaef4d3be5c4858c03f6
MD5 67d2843b2dafd33888b4db8d26d340e8
BLAKE2b-256 33601d8762a1260e25f0bca86c0a7834542269cb1e7ef502280dd2d13b579e4a

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.1.2-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