new file: Legs_controller.py modified: README.md new file: __pycache__/Legs_controller.cpython-310.pyc new file: __pycache__/Legs_controller.cpython-313.pyc new file: __pycache__/balance.cpython-310.pyc new file: __pycache__/gradio.cpython-310.pyc new file: __pycache__/gradio.cpython-313.pyc new file: app_ui.py new file: balance.py new file: build/.cmake/api/v1/query/client-vscode/query.json new file: build/.cmake/api/v1/reply/cache-v2-ae4a9db768b4bbb36baa.json new file: build/.cmake/api/v1/reply/cmakeFiles-v1-389eb8769a8295e7571d.json new file: build/.cmake/api/v1/reply/codemodel-v2-5ea8cfc0b9263cbe5ae5.json new file: build/.cmake/api/v1/reply/directory-.-Debug-f5ebdc15457944623624.json new file: build/.cmake/api/v1/reply/index-2025-09-11T04-53-20-0515.json new file: build/.cmake/api/v1/reply/target-imu_py-Debug-3913d741f2156d7faae9.json new file: build/.cmake/api/v1/reply/toolchains-v1-3105704d088db7adbfb5.json new file: build/CMakeCache.txt new file: build/CMakeFiles/3.22.1/CMakeCXXCompiler.cmake new file: build/CMakeFiles/3.22.1/CMakeDetermineCompilerABI_CXX.bin new file: build/CMakeFiles/3.22.1/CMakeSystem.cmake new file: build/CMakeFiles/3.22.1/CompilerIdCXX/CMakeCXXCompilerId.cpp new file: build/CMakeFiles/3.22.1/CompilerIdCXX/a.out new file: build/CMakeFiles/CMakeDirectoryInformation.cmake new file: build/CMakeFiles/CMakeOutput.log new file: build/CMakeFiles/Makefile.cmake new file: build/CMakeFiles/Makefile2 new file: build/CMakeFiles/TargetDirectories.txt new file: build/CMakeFiles/cmake.check_cache new file: build/CMakeFiles/imu_py.dir/DependInfo.cmake new file: build/CMakeFiles/imu_py.dir/build.make new file: build/CMakeFiles/imu_py.dir/cmake_clean.cmake new file: build/CMakeFiles/imu_py.dir/compiler_depend.make new file: build/CMakeFiles/imu_py.dir/compiler_depend.ts new file: build/CMakeFiles/imu_py.dir/depend.make new file: build/CMakeFiles/imu_py.dir/flags.make new file: build/CMakeFiles/imu_py.dir/link.txt new file: build/CMakeFiles/imu_py.dir/progress.make new file: build/CMakeFiles/progress.marks new file: build/Makefile new file: build/cmake_install.cmake new file: build/compile_commands.json new file: dm_imu/bsp_crc.cpp new file: dm_imu/bsp_crc.h new file: dm_imu/imu_data.csv new file: dm_imu/imu_driver.cpp new file: dm_imu/imu_driver.h new file: dm_imu/imu_plot.png new file: dm_imu/plot_imu.py new file: dm_imu/test_imu.cpp new file: imu_data.csv new file: pybind_imu/CMakeLists.txt new file: pybind_imu/README.md new file: pybind_imu/build/CMakeCache.txt new file: pybind_imu/build/CMakeFiles/3.22.1/CMakeCXXCompiler.cmake new file: pybind_imu/build/CMakeFiles/3.22.1/CMakeDetermineCompilerABI_CXX.bin new file: pybind_imu/build/CMakeFiles/3.22.1/CMakeSystem.cmake new file: pybind_imu/build/CMakeFiles/3.22.1/CompilerIdCXX/CMakeCXXCompilerId.cpp new file: pybind_imu/build/CMakeFiles/3.22.1/CompilerIdCXX/a.out new file: pybind_imu/build/CMakeFiles/CMakeDirectoryInformation.cmake new file: pybind_imu/build/CMakeFiles/CMakeOutput.log new file: pybind_imu/build/CMakeFiles/Makefile.cmake new file: pybind_imu/build/CMakeFiles/Makefile2 new file: pybind_imu/build/CMakeFiles/TargetDirectories.txt new file: pybind_imu/build/CMakeFiles/cmake.check_cache new file: pybind_imu/build/CMakeFiles/imu_py.dir/DependInfo.cmake new file: pybind_imu/build/CMakeFiles/imu_py.dir/build.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/cmake_clean.cmake new file: pybind_imu/build/CMakeFiles/imu_py.dir/compiler_depend.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/compiler_depend.ts new file: pybind_imu/build/CMakeFiles/imu_py.dir/depend.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/flags.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/home/allenyuan/balance/dm_imu/bsp_crc.cpp.o new file: pybind_imu/build/CMakeFiles/imu_py.dir/home/allenyuan/balance/dm_imu/bsp_crc.cpp.o.d new file: pybind_imu/build/CMakeFiles/imu_py.dir/home/allenyuan/balance/dm_imu/imu_driver.cpp.o new file: pybind_imu/build/CMakeFiles/imu_py.dir/home/allenyuan/balance/dm_imu/imu_driver.cpp.o.d new file: pybind_imu/build/CMakeFiles/imu_py.dir/link.txt new file: pybind_imu/build/CMakeFiles/imu_py.dir/progress.make new file: pybind_imu/build/CMakeFiles/imu_py.dir/pybind_imu.cpp.o new file: pybind_imu/build/CMakeFiles/imu_py.dir/pybind_imu.cpp.o.d new file: pybind_imu/build/CMakeFiles/progress.marks new file: pybind_imu/build/Makefile new file: pybind_imu/build/cmake_install.cmake new file: pybind_imu/build/imu_py.cpython-310-aarch64-linux-gnu.so new file: pybind_imu/example.py new file: pybind_imu/pybind_imu.cpp new file: src/example.py new file: test_imu new file: u2can/DM_CAN.py new file: u2can/DM_Motor_Test.py new file: u2can/LICENSE new file: u2can/README.md new file: u2can/__pycache__/DM_CAN.cpython-310.pyc new file: u2can/__pycache__/DM_CAN.cpython-312.pyc new file: u2can/__pycache__/DM_CAN.cpython-313.pyc new file: u2can/__pycache__/gradio.cpython-313.pyc new file: u2can/__pycache__/motor_interface.cpython-313.pyc new file: u2can/motor_interface.py new file: u2can/requirements.txt new file: ui.log
94 lines
3.5 KiB
Python
94 lines
3.5 KiB
Python
import math
|
||
import time
|
||
import serial
|
||
from u2can.DM_CAN import (
|
||
Motor, MotorControl,
|
||
DM_Motor_Type, Control_Type, DM_variable
|
||
)
|
||
|
||
class LegsController:
|
||
"""
|
||
简单的达妙腿部/轮子控制类,只实现基本的初始化、使能、
|
||
失能、扭矩读取以及位置控制。
|
||
"""
|
||
|
||
def __init__(self, port="/dev/dm-u2can", baudrate=921600, timeout=0.5):
|
||
"""打开串口、创建 MotorControl、实例化并注册所有电机。"""
|
||
self.serial_device = serial.Serial(port, baudrate, timeout=timeout)
|
||
self.mc = MotorControl(self.serial_device)
|
||
|
||
# 四条腿电机(DM4340)
|
||
self.motor1 = Motor(DM_Motor_Type.DM4340, 0x01, 0x11)
|
||
self.motor2 = Motor(DM_Motor_Type.DM4340, 0x02, 0x12)
|
||
self.motor3 = Motor(DM_Motor_Type.DM4340, 0x03, 0x13)
|
||
self.motor4 = Motor(DM_Motor_Type.DM4340, 0x04, 0x14)
|
||
|
||
# 四个轮子电机(DMH6215)
|
||
self.wheel1 = Motor(DM_Motor_Type.DMH6215, 0x05, 0x15)
|
||
self.wheel2 = Motor(DM_Motor_Type.DMH6215, 0x06, 0x16)
|
||
self.wheel3 = Motor(DM_Motor_Type.DMH6215, 0x07, 0x17)
|
||
self.wheel4 = Motor(DM_Motor_Type.DMH6215, 0x08, 0x18)
|
||
|
||
# 注册所有电机
|
||
for m in (self.motor1, self.motor2, self.motor3, self.motor4,
|
||
self.wheel1, self.wheel2, self.wheel3, self.wheel4):
|
||
self.mc.addMotor(m)
|
||
|
||
# ---------- 串口管理 ----------
|
||
def open_serial(self):
|
||
"""重新打开已关闭的串口(如果需要)。"""
|
||
if not self.serial_device.is_open:
|
||
self.serial_device.open()
|
||
|
||
def close_serial(self):
|
||
"""关闭串口,释放资源。"""
|
||
if self.serial_device.is_open:
|
||
self.serial_device.close()
|
||
|
||
# ---------- 使能 ----------
|
||
def enable_legs(self):
|
||
"""使能四条腿电机。"""
|
||
for m in (self.motor1, self.motor2, self.motor3, self.motor4):
|
||
self.mc.enable(m)
|
||
|
||
def enable_wheels(self):
|
||
"""使能四个轮子电机。"""
|
||
for w in (self.wheel1, self.wheel2, self.wheel3, self.wheel4):
|
||
self.mc.enable(w)
|
||
|
||
def disable_all(self):
|
||
"""一次性失能所有电机(腿+轮子)。"""
|
||
for m in (self.motor1, self.motor2, self.motor3, self.motor4,
|
||
self.wheel1, self.wheel2, self.wheel3, self.wheel4):
|
||
self.mc.disable(m)
|
||
|
||
# ---------- 状态读取 ----------
|
||
def get_legs_torque(self):
|
||
"""刷新四条腿的状态并返回它们的扭矩列表。"""
|
||
for m in (self.motor1, self.motor2, self.motor3, self.motor4):
|
||
self.mc.refresh_motor_status(m)
|
||
return [
|
||
self.motor1.getTorque(),
|
||
self.motor2.getTorque(),
|
||
self.motor3.getTorque(),
|
||
self.motor4.getTorque(),
|
||
]
|
||
|
||
# ---------- 位置控制 ----------
|
||
def control_legs_pos(self, pos1, pos2, pos3, pos4, vel=0.5):
|
||
"""
|
||
使用位置‑速度模式控制四条腿。
|
||
参数:
|
||
pos1‑pos4: 目标位置(单位依据电机规格)
|
||
vel: 速度比例,默认 0.5
|
||
"""
|
||
self.mc.control_Pos_Vel(self.motor1, -pos1, vel)
|
||
self.mc.control_Pos_Vel(self.motor2, pos2, vel)
|
||
self.mc.control_Pos_Vel(self.motor3, pos3, vel)
|
||
self.mc.control_Pos_Vel(self.motor4, -pos4, vel)
|
||
|
||
def control_wheels_vel(self,vel,of_vel):
|
||
self.mc.control_Vel(self.wheel1,vel+of_vel)
|
||
self.mc.control_Vel(self.wheel2,vel-of_vel)
|
||
self.mc.control_Vel(self.wheel3,vel-of_vel)
|
||
self.mc.control_Vel(self.wheel4,vel+of_vel) |