Skip to main content

An unofficial Damiao motor control library, developed based on the official SDK. 一个非官方的达妙电机控制库, 基于官方sdk二次开发

Project description

pydamiao

一个非官方的达妙电机 Python 库。

pydamiao 基于官方 SDK 的工作流做了更 Pythonic 的封装, 使其开发体验更好。pydamiao 提供串口通信、电机管理、参数读写和常见控制模式的高层接口,适合快速集成到自己的项目中。

基本特性

  • 包含完整的类型注解, 为开发者提供舒适的开发体验。
  • 可能会失败的接口, 使用类似 Rust 的 Result 返回格式, 显式处理错误, 避免大量 None 判断
  • 提供 MotorMotorManager 等高层接口
  • 支持 MITVELPOS_VELPOS_FORCE 等常见控制模式
  • 同时支持单电机和多电机场景

开发者体验 (DX) 升级

本项目从达妙官方提供的 单文件脚本 重构为现代化的 Python 库,在易用性和可维护性上进行了全方位升级:

  • 极速环境构建:全面采用 uv 管理项目依赖与虚拟环境,提升开发与部署效率。
  • 现代类型推导:基于 Python 3.10+,全面覆盖核心 API 的原生类型注解,大幅提升 IDE 代码提示与静态检查体验。
  • 模块化代码解耦:将巨石架构拆分为 busprotocolmotor 等独立模块,并封装了底层硬编码的字节位移操作,显著降低源码阅读与二次开发门槛。

安装

PyPI 安装

uv pip install pydamiao 

pip install pydamiao 

从源码安装:

git clone https://github.com/DBinK/pydamiao.git
cd pydamiao
pip install -e .

快速开始

import math
import time

from pydamiao import Motor, MotorManager, ControlMode, MotorType, MotorReg, SerialBus

# 初始化所有对象
bus = SerialBus("COM3", baudrate=921600, timeout=0.01)
manager = MotorManager(bus)

motor1 = Motor(bus, MotorType.DM4310, 0x06, 0x12)
motor2 = Motor(bus, MotorType.DM4310, 0x05, 0x12)

# 注册电机到管理器
manager.register(motor1)
manager.register(motor2)

# 统一控制电机
manager.clean_error_all()
manager.disable_all()
manager.set_mode_all(ControlMode.MIT)

# 查看电机参数
for id, motor in manager.motors.items():
    print(f"电机 ID {id}:")
    print("CTRL_MODE:", motor.read_param(MotorReg.CTRL_MODE).value)
    print("MST_ID:", motor.read_param(MotorReg.MST_ID).value)

# 单独控制电机
if not motor1.set_mode(ControlMode.POS_VEL).is_ok:
    print("motor1 切换到 POS_VEL 失败")

if not motor2.set_mode(ControlMode.VEL).is_ok:
    print("motor2 切换到 VEL 失败")

# 运动测试
for _ in range(1000):
    q = math.sin(time.time())
    motor1.set_pos_vel(q * 8, 3)
    motor2.set_velocity(8 * q)
    time.sleep(0.001)

# 程序结束前, 记得失能所有电机 (虽然有自动失能兜底, 但是推荐养成好习惯)
manager.disable_all()
bus.close()

API 概览

  • SerialBus:共享串口通信总线
  • Motor:单个电机的高层控制对象
  • MotorManager:同一总线上的多电机管理器
  • ControlModeMotorTypeMotorReg:协议相关枚举和辅助类型

使用文档 和 示例

文档正在补充中...

可先查看示例 examples 目录, 包含了本库几乎全部用法:

项目状态

项目仍在持续迭代中,正式稳定版本发布前 API 可能会继续调整。

许可证

MIT,详见 LICENSE

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

pydamiao-0.1.3.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

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

pydamiao-0.1.3-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file pydamiao-0.1.3.tar.gz.

File metadata

  • Download URL: pydamiao-0.1.3.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pydamiao-0.1.3.tar.gz
Algorithm Hash digest
SHA256 30ea6e86f3d928af2b4f02e52018561289caf1674d5445c73f5d08d4aa026a8c
MD5 e93996136008fe06902687370c0165f5
BLAKE2b-256 7b4046707fd732109fba177e3f8fb6dad80fdb59ac77322ed55b748e8ff084c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydamiao-0.1.3.tar.gz:

Publisher: test_and_publish.yml on DBinK/pydamiao

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

File details

Details for the file pydamiao-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pydamiao-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pydamiao-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c4c721a4e0dd72b649c5dca7dd7fa350dd0342c30b8afa11b0fa3c6486dad87f
MD5 0b978a02932b6c5cbad85fe052e00b82
BLAKE2b-256 7faee73811ba324f5f07710ad13cb3f5cbd06da95e628b3766faff6a1cc11073

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydamiao-0.1.3-py3-none-any.whl:

Publisher: test_and_publish.yml on DBinK/pydamiao

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