调试更改+挡板电机添加线程和配置文件
This commit is contained in:
@ -7,9 +7,29 @@
|
||||
# @Desc : 线条厂控制步进电机测试 应该不会丢步
|
||||
"""
|
||||
import time
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from periphery import GPIO
|
||||
|
||||
# ------------日志配置(终端+文件双输出)--------------
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s',
|
||||
# 核心新增:日志文件配置
|
||||
handlers=[
|
||||
# 1. 文件处理器:保存到.log文件
|
||||
logging.FileHandler(
|
||||
"RK1106_server.log", # Buildroot推荐路径,临时测试可改/tmp/1106_server.log
|
||||
mode='a', # 追加模式(不会覆盖历史日志)
|
||||
encoding='utf-8' # 防止中文乱码(必加)
|
||||
),
|
||||
# 2. 终端处理器:输出到控制台
|
||||
logging.StreamHandler(sys.stdout)
|
||||
]
|
||||
)
|
||||
|
||||
# ------------参数配置-------------
|
||||
# 1. 脉冲(PUL)引脚配置 → GPIO32
|
||||
PUL_Pin = 32
|
||||
@ -66,8 +86,8 @@ class StepperMotor:
|
||||
self.pul_gpio.write(False)
|
||||
self.dir_gpio.write(False)
|
||||
|
||||
print(f"✅ PUL引脚初始化完成:{self.pul_pin} 引脚")
|
||||
print(f"✅ DIR引脚初始化完成:{self.dir_pin} 引脚")
|
||||
logging.info(f"✅ PUL引脚初始化完成:{self.pul_pin} 引脚")
|
||||
logging.info(f"✅ DIR引脚初始化完成:{self.dir_pin} 引脚")
|
||||
|
||||
except PermissionError:
|
||||
raise RuntimeError("权限不足!请用sudo运行程序(sudo python xxx.py)")
|
||||
@ -76,10 +96,10 @@ class StepperMotor:
|
||||
|
||||
def _validate_params(self, rounds: float, direction: int) -> bool:
|
||||
if rounds <= 0:
|
||||
print("圈数必须为正数")
|
||||
logging.info("圈数必须为正数")
|
||||
return False
|
||||
if direction not in (0, 1):
|
||||
print("方向必须为0(逆时针)或1(顺时针)")
|
||||
logging.info("方向必须为0(逆时针)或1(顺时针)")
|
||||
return False
|
||||
return True
|
||||
|
||||
@ -97,17 +117,17 @@ class StepperMotor:
|
||||
# 设置旋转方向(DIR电平)
|
||||
if direction == 1: # 顺时针
|
||||
self.dir_gpio.write(self.clockwise_level)
|
||||
print(f"\n=== 顺时针旋转 {rounds} 圈 ===")
|
||||
logging.info(f"\n=== 顺时针旋转 {rounds} 圈 ===")
|
||||
else: # 逆时针
|
||||
self.dir_gpio.write(self.counter_clockwise_level)
|
||||
print(f"\n=== 逆时针旋转 {rounds} 圈 ===")
|
||||
logging.info(f"\n=== 逆时针旋转 {rounds} 圈 ===")
|
||||
|
||||
# 计算总脉冲数和时序(精准控制,避免丢步)
|
||||
total_pulses = int(rounds * self.pulses_per_round)
|
||||
pulse_period = 1.0 / pulse_frequency # 脉冲周期(秒)
|
||||
half_period = pulse_period / 2 # 占空比50%(MA860H最优)
|
||||
|
||||
print(f"总脉冲数:{total_pulses} | 频率:{pulse_frequency}Hz | 周期:{pulse_period * 1000:.2f}ms")
|
||||
logging.info(f"总脉冲数:{total_pulses} | 频率:{pulse_frequency}Hz | 周期:{pulse_period * 1000:.2f}ms")
|
||||
start_time = time.perf_counter() # 高精度计时(避免丢步)
|
||||
|
||||
# 发送脉冲序列(核心:占空比50%的方波)
|
||||
@ -122,13 +142,13 @@ class StepperMotor:
|
||||
# 更新下一个脉冲的起始时间
|
||||
start_time += pulse_period
|
||||
|
||||
print("✅ 旋转完成")
|
||||
logging.info("✅ 旋转完成")
|
||||
|
||||
def stop(self):
|
||||
"""紧急停止(置低脉冲引脚)"""
|
||||
if self.pul_gpio:
|
||||
self.pul_gpio.write(False)
|
||||
print("🛑 电机已停止")
|
||||
logging.info("🛑 电机已停止")
|
||||
|
||||
def close(self):
|
||||
"""释放GPIO资源"""
|
||||
@ -136,12 +156,12 @@ class StepperMotor:
|
||||
if self.pul_gpio:
|
||||
self.pul_gpio.write(False) # 脉冲引脚置低
|
||||
self.pul_gpio.close()
|
||||
print("\n✅ PUL引脚已关闭(电平置低)")
|
||||
logging.info("\n✅ PUL引脚已关闭(电平置低)")
|
||||
|
||||
if self.dir_gpio:
|
||||
self.dir_gpio.write(False) # 方向引脚置低
|
||||
self.dir_gpio.close()
|
||||
print("✅ DIR引脚已关闭(电平置低)")
|
||||
logging.info("✅ DIR引脚已关闭(电平置低)")
|
||||
|
||||
# 重置GPIO对象
|
||||
self.pul_gpio = None
|
||||
@ -163,16 +183,16 @@ def motor_start(speed: int, cycle: float, direction: int):
|
||||
:param direction: 0=负向(逆时针),1=正向(顺时针)
|
||||
"""
|
||||
try:
|
||||
print("\n=== 启动步进电机 ===")
|
||||
logging.info("\n=== 启动步进电机 ===")
|
||||
|
||||
GLOBAL_MOTOR.rotate(pulse_frequency=speed, rounds=cycle, direction=direction)
|
||||
time.sleep(5) # 暂停5秒
|
||||
|
||||
except ImportError:
|
||||
print("\n❌ 缺少依赖:请安装python-periphery")
|
||||
print("命令:pip install python-periphery")
|
||||
logging.info("\n❌ 缺少依赖:请安装python-periphery")
|
||||
logging.info("命令:pip install python-periphery")
|
||||
except Exception as e:
|
||||
print(f"\n❌ 程序异常:{str(e)}")
|
||||
logging.info(f"\n❌ 程序异常:{str(e)}")
|
||||
|
||||
def motor_stop():
|
||||
"""紧急停止(仅停止脉冲,保留实例)"""
|
||||
@ -180,7 +200,8 @@ def motor_stop():
|
||||
if GLOBAL_MOTOR:
|
||||
GLOBAL_MOTOR.stop()
|
||||
except Exception as e:
|
||||
print("停止失败:{e}")
|
||||
logging.info("停止失败:{e}")
|
||||
|
||||
|
||||
def align_wire(speed: int, cycle: float):
|
||||
"""
|
||||
@ -189,20 +210,20 @@ def align_wire(speed: int, cycle: float):
|
||||
:param cycle: 旋转圈数
|
||||
"""
|
||||
try:
|
||||
print("\n=== 启动线条对齐 ===")
|
||||
logging.info("\n=== 启动线条对齐 ===")
|
||||
|
||||
# 靠近电机方向 逆时针
|
||||
GLOBAL_MOTOR.rotate(pulse_frequency=speed, rounds=cycle, direction=0)
|
||||
time.sleep(5) # 暂停5秒
|
||||
time.sleep(2) # 暂停3秒
|
||||
# 远离电机方向 顺时针
|
||||
GLOBAL_MOTOR.rotate(pulse_frequency=speed,rounds=cycle, direction=1)
|
||||
time.sleep(5) # 暂停5秒
|
||||
time.sleep(2) # 暂停3秒
|
||||
|
||||
except ImportError:
|
||||
print("\n❌ 缺少依赖:请安装python-periphery")
|
||||
print("命令:pip install python-periphery")
|
||||
logging.info("\n❌ 缺少依赖:请安装python-periphery")
|
||||
logging.info("命令:pip install python-periphery")
|
||||
except Exception as e:
|
||||
print(f"\n❌ 程序异常:{str(e)}")
|
||||
logging.info(f"\n❌ 程序异常:{str(e)}")
|
||||
|
||||
def cleanup():
|
||||
"""程序退出时统一清理"""
|
||||
@ -212,14 +233,14 @@ def cleanup():
|
||||
|
||||
if __name__ == '__main__':
|
||||
align_wire(speed=2500, cycle=10.0)
|
||||
time.sleep(10) # 电机运动需要的时间
|
||||
time.sleep(5) # 电机运动需要的时间
|
||||
|
||||
# 测试是否电机能停止
|
||||
# # 测试是否电机能停止
|
||||
motor_start(speed=2500, cycle=5.0, direction=0)
|
||||
time.sleep(1) # 电机运动需要的时间
|
||||
motor_stop()
|
||||
motor_stop()
|
||||
time.sleep(0.2)
|
||||
|
||||
while(True): # 防止程序退出
|
||||
|
||||
while True: # 防止程序退出
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user