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.2.0.tar.gz (933.0 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.2.0-cp313-cp313-win_amd64.whl (319.0 kB view details)

Uploaded CPython 3.13Windows x86-64

florid_usb-0.2.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (360.9 kB view details)

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

florid_usb-0.2.0-cp312-cp312-win_amd64.whl (319.0 kB view details)

Uploaded CPython 3.12Windows x86-64

florid_usb-0.2.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (360.9 kB view details)

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

florid_usb-0.2.0-cp311-cp311-win_amd64.whl (316.7 kB view details)

Uploaded CPython 3.11Windows x86-64

florid_usb-0.2.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (359.1 kB view details)

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

florid_usb-0.2.0-cp310-cp310-win_amd64.whl (315.3 kB view details)

Uploaded CPython 3.10Windows x86-64

florid_usb-0.2.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (357.9 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.2.0.tar.gz.

File metadata

  • Download URL: florid_usb-0.2.0.tar.gz
  • Upload date:
  • Size: 933.0 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.2.0.tar.gz
Algorithm Hash digest
SHA256 d6059752d4755bac325ded87b39621d456f96da5935460ab0b4a3f71eaece382
MD5 b01019147659428ee289af008eac899f
BLAKE2b-256 1e700d54315bbcb51a4c57417192286a5ff2ee2455cf6d6237586d896e7128b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for florid_usb-0.2.0.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.2.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: florid_usb-0.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 319.0 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.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 8ff3146b515b9a9aafb564a2810735753e0b42d1c7e40d3008e7aedd3a8347d7
MD5 bd3452ddf78007806a0142efea0066db
BLAKE2b-256 cc5e341beb645d40cbb1859185bd667b80f21d58642dd9b0a1974b73579384e8

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for florid_usb-0.2.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0c45afe7b7b05ee4adcbbb1fc35fc806f710c806ad2b887a0d2f5553d915207c
MD5 528fa9d1b3bf125f3390c3e3ada8096f
BLAKE2b-256 c2c3d82e2df82de53cdf9c8fee96e23e70dcfc98632534caefa7270343baa724

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: florid_usb-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 319.0 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.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ee5a361603aad9b85ccc7627b28ae6b134b8bd47cdaab69d9b10f5273af9342d
MD5 2d52e6cdc8b7953bb5f6fa624b802850
BLAKE2b-256 37ee06c064312c59309032bf77ac91929b17f21c22763c110cef683668f7f2b1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for florid_usb-0.2.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e881e116e54619aa8ad7f8f030ae8d18bda4c5bec52a37b1852164ddc07fe028
MD5 c1e3a8200e372efb272e5b52d907a1f8
BLAKE2b-256 fbdd434aee244e192473c7af28cf894f25b07edc9d2ee685869c99369f4b8f6f

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: florid_usb-0.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 316.7 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.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ae307b91ba1198af9f965c83b61c24848834c9e37737de844fc6c729d6e26e28
MD5 c6b1d7b49f76c98d03b9ebe131486e13
BLAKE2b-256 67ee9d63ae4c633b67a124b1b119e482c8a741161e186038ea563d309fdbe04e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for florid_usb-0.2.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1eaefde51829e6e2443d124250eea4bd89a193fa0590405b6f8a77d9ad5652f4
MD5 ef2e7eef83ed1ab52a84f17bae942973
BLAKE2b-256 5219cdb21845dae0b2b16ffb4175307cf58d6881375fd82e0a64aa25f91d5799

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: florid_usb-0.2.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 315.3 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.2.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 4f0953bb2d5ce2297ea19df2b4efbf2bafd014a62f2452cdf0d80681e59a7805
MD5 ae218a0a26f2c86d7495baf2058b7b3c
BLAKE2b-256 32747b6674a10b9fb214bab1274863848dbde46dadcbbb6b8d3398e6ae9434ce

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for florid_usb-0.2.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e7726a07f9c74f1bb04eaa99021cbecbcccdc47dc2e5b159936f988a9ab17987
MD5 687645f918c257b36284fe909caf17f5
BLAKE2b-256 1e1e1fa60829a4909e15227996edd1e33740498af38fdcae64a398f85d8253bb

See more details on using hashes here.

Provenance

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