134 lines
4.4 KiB
Python
134 lines
4.4 KiB
Python
#!/usr/bin/env python
|
||
# -*- coding: utf-8 -*-
|
||
'''
|
||
# @Time : 2025/7/15 17:52
|
||
# @Author : hx
|
||
# @File : motor.py
|
||
'''
|
||
import serial
|
||
import time
|
||
import math
|
||
|
||
start_speed = 8
|
||
stop_speed = 0
|
||
|
||
def send_hex_command(port, baudrate, hex_data):
|
||
"""
|
||
通过串口发送十六进制指令
|
||
:param port: 串口号 (如 'COM3' 或 '/dev/ttyUSB0')
|
||
:param baudrate: 波特率 (如 9600)
|
||
:param hex_data: 十六进制字符串 (如 "7B 01 02 01 20 0E 10 00 64 23 7D")
|
||
"""
|
||
try:
|
||
# 转换十六进制字符串为字节数据
|
||
byte_data = bytes.fromhex(hex_data.replace(" ", ""))
|
||
|
||
# 打开串口
|
||
with serial.Serial(port, baudrate, timeout=1) as ser:
|
||
print(f"已连接串口 {port}, 波特率 {baudrate}")
|
||
|
||
# 发送指令
|
||
ser.write(byte_data)
|
||
print(f"已发送指令: {hex_data}")
|
||
|
||
# 可选:等待并读取返回数据
|
||
time.sleep(0.1)
|
||
if ser.in_waiting > 0:
|
||
response = ser.read(ser.in_waiting)
|
||
print(f"收到响应: {response.hex().upper()}")
|
||
|
||
except Exception as e:
|
||
print(f"发生错误: {e}")
|
||
|
||
def generate_fixed_command(speed):
|
||
"""
|
||
生成固定的速度控制指令: 7B 01 01 00 20 00 00 00 <speed> <checksum> 7D
|
||
参数:
|
||
speed (int): 十进制速度值 (0~255)
|
||
返回:
|
||
str: 十六进制命令字符串
|
||
"""
|
||
|
||
actual_speed = int(speed * 10)
|
||
|
||
if not (0 <= actual_speed <= 255):
|
||
raise ValueError("速度必须在 0~255 范围内")
|
||
|
||
command_bytes = [
|
||
0x7B, 0x01, 0x01, # 帧头、地址、控制模式
|
||
0x00, 0x20, # 方向、细分
|
||
0x00, 0x00, # 位置高/低字节(持续旋转)
|
||
0x00, actual_speed # 保持整数速度值
|
||
]
|
||
|
||
# 计算校验和(前9个字节异或)
|
||
checksum = 0
|
||
for byte in command_bytes:
|
||
checksum ^= byte
|
||
|
||
full_command = command_bytes + [checksum, 0x7D]
|
||
return ' '.join(f'{byte:02X}' for byte in full_command)
|
||
|
||
def listen_and_respond(port, baudrate):
|
||
"""
|
||
持续监听串口,一旦有信号就执行开始和停止的指令
|
||
"""
|
||
try:
|
||
with serial.Serial(port, baudrate, timeout=1) as ser:
|
||
ser.reset_input_buffer()
|
||
print(f"[监听中] 串口 {port} 已打开,等待信号...")
|
||
|
||
while True:
|
||
if ser.in_waiting > 0:
|
||
incoming = ser.read(ser.in_waiting)
|
||
print(f"[收到信号] 内容: {incoming.hex().upper()}")
|
||
|
||
# 执行开始旋转
|
||
start_cmd = generate_fixed_command(8)
|
||
print("发送开始指令:", start_cmd)
|
||
ser.write(bytes.fromhex(start_cmd.replace(" ", "")))
|
||
|
||
# 延迟一段时间
|
||
time.sleep(4)
|
||
|
||
# 执行停止
|
||
stop_cmd = generate_fixed_command(0)
|
||
print("发送停止指令:", stop_cmd)
|
||
ser.write(bytes.fromhex(stop_cmd.replace(" ", "")))
|
||
|
||
print("[已完成一次响应]\n等待下一次信号...\n")
|
||
|
||
time.sleep(0.1) # 避免CPU占用过高
|
||
|
||
except Exception as e:
|
||
print(f"监听时发生错误: {e}")
|
||
|
||
if __name__ == "__main__":
|
||
# 配置参数
|
||
PORT = "/dev/ttyACM0" # 修改为你的串口号
|
||
BAUD_RATE = 115200 # 修改为你的设备波特率
|
||
HEX_COMMAND = {
|
||
"Clockwise_rotation":"7B 01 02 01 20 0E 10 00 64 23 7D", # 顺时针旋转360
|
||
"Counterclockwise_rotation":"7B 01 02 00 20 0E 10 00 64 22 7D",
|
||
"One_revolution_per_second":"7B 01 01 00 20 00 00 00 3F 64 7D", # 有问题
|
||
"Ten_radin_per_second":"7B 01 01 00 20 00 00 00 64 3F 7D",
|
||
"Eight_radin_per_second": "7B 01 01 00 20 00 00 00 50 0B 7D",
|
||
"stop":"7B 01 01 00 20 00 00 00 00 5B 7D"
|
||
}# 要发送的指令
|
||
|
||
try:
|
||
# 生成并发送开始旋转指令(速度 = 8)
|
||
start_cmd = generate_fixed_command(start_speed)
|
||
send_hex_command(PORT, BAUD_RATE, start_cmd)
|
||
|
||
# 延迟一段时间(比如 4 秒)
|
||
time.sleep(4)
|
||
|
||
# 生成并发送停止指令(速度 = 0)
|
||
stop_cmd = generate_fixed_command(stop_speed)
|
||
send_hex_command(PORT, BAUD_RATE, stop_cmd)
|
||
|
||
print("电机控制流程执行完成。")
|
||
|
||
except Exception as e:
|
||
print(f"运行错误: {e}") |