米厂码垛修改

This commit is contained in:
2025-09-10 09:16:57 +08:00
parent 2f45c4c38f
commit 65dde435c8
47 changed files with 11258 additions and 10485 deletions

View File

@ -7,6 +7,7 @@ import threading
import logging
from PySide6.QtCore import Signal, QObject
import numpy as np
import Constant
class RelayController:
@ -21,7 +22,8 @@ class RelayController:
self.sensor1_error_delay = 1.0 # SENSOR1 出错时延时(秒)
self.sensor1_post_action_delay = 0.2 # SENSOR1 每次循环后延时(秒)
self.sensor2_loop_delay = 0.5 # SENSOR2 线程轮询间隔(秒)
self.sensor2_loop_delay = 0.1 # SENSOR2 线程轮询间隔(秒)
# self.sensor2_loop_delay = 0.5 # SENSOR2 线程轮询间隔(秒)
self.sensor2_error_delay = 0.5 # SENSOR2 出错时延时(秒)
self.sensor2_post_action_delay = 0.2 # SENSOR2 每次循环后延时(秒)
# ===================== 全局动作延时参数 =====================
@ -42,6 +44,7 @@ class RelayController:
self.CONVEYOR1 = 'conveyor1'
self.PUSHER = 'pusher'
self.CONVEYOR2 = 'conveyor2'
self.CONVEYOR2_REVERSE = 'conveyor2_reverse'
self.CLAMP = 'clamp'
self.PUSHER1 = 'pusher1'
self.SENSOR1 = 'sensor1'
@ -50,11 +53,18 @@ class RelayController:
self.valve_commands = {
self.CONVEYOR1: {'open': '00000000000601050000FF00', 'close': '000000000006010500000000'},
self.PUSHER: {'open': '00000000000601050001FF00', 'close': '000000000006010500010000'},
#滚筒2000 0012正转2000 0022 2001变频器频率调整 2000正反转。
self.CONVEYOR2: {'open': '000100000006020620000012', 'close': '000100000006020620000001'},
#DO4
self.CLAMP: {'open': '00000000000601050003FF00', 'close': '000000000006010500030000'},
self.PUSHER1: {'open': '00000000000601050004FF00', 'close': '000000000006010500040000'}
#DO5
self.PUSHER1: {'open': '00000000000601050004FF00', 'close': '000000000006010500040000'},
self.CONVEYOR2_REVERSE: {'open': '000100000006020620000022', 'close': '000100000006020620000001'}
}
#devices:读取继点器的状态
#sensors 传感器的状态 D12
self.read_status_command = {
'devices': '000000000006010100000008',
'sensors': '000000000006010200000008'
@ -66,6 +76,7 @@ class RelayController:
self.CONVEYOR2: 2,
self.CLAMP: 3,
self.PUSHER1: 4,
self.CONVEYOR2_REVERSE: 5
}
self.sensor_bit_map = {
@ -79,6 +90,7 @@ class RelayController:
self.CONVEYOR2: "传送带2",
self.CLAMP: "机械臂夹爪",
self.PUSHER1: "推板关",
self.CONVEYOR2_REVERSE: "传送带2反转"
}
self.sensor_name_map = {
@ -102,6 +114,8 @@ class RelayController:
# ===================== 基础通信方法 =====================
def send_command(self, command_hex, retry_count=2, source='unknown'):
if Constant.DebugPosition:
return True
byte_data = binascii.unhexlify(command_hex)
for attempt in range(retry_count):
try:
@ -119,19 +133,21 @@ class RelayController:
#print(f"[通信响应] {hex_response}")
return response
except Exception as e:
print(f"通信错误 ({source}): {e}, 尝试重连... ({attempt + 1}/{retry_count})")
print(f"网络继电器通信错误 ({source}): {e}, 尝试重连... ({attempt + 1}/{retry_count})")
time.sleep(5)
self.trigger_alarm()
return None
def trigger_alarm(self):
print("警告:连续多次通信失败,请检查设备连接!")
print("警告:网络继电器连续多次通信失败,请检查设备连接!")
# ===================== 状态读取方法 =====================
def get_all_device_status(self, command_type='devices'):
if Constant.DebugPosition:
return {self.SENSOR2:True}
command = self.read_status_command.get(command_type)
if not command:
print(f"未知的读取类型: {command_type}")
print(f"未知的网络继电器读取类型: {command_type}")
return {}
source = 'sensor' if command_type == 'sensors' else 'device'
@ -147,7 +163,7 @@ class RelayController:
for key, bit_index in bit_map.items():
status_dict[key] = status_bin[bit_index] == '1'
else:
print(f"[{command_type}] 读取状态失败或响应无效")
print(f"网络继电器[{command_type}] 读取状态失败或响应无效")
return status_dict
@ -229,7 +245,9 @@ class RelayController:
return False
# ===================== 动作控制方法 =====================
def open(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False):
def open(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False):
if Constant.DebugPosition:
return
status = self.get_all_device_status()
if conveyor1 and not status.get(self.CONVEYOR1, False):
self.send_command(self.valve_commands[self.CONVEYOR1]['open'])
@ -246,8 +264,11 @@ class RelayController:
if pusher1 and not status.get(self.PUSHER1, False):
self.send_command(self.valve_commands[self.PUSHER1]['open'])
time.sleep(self.delay_pusher)
if conveyor2_reverse:
self.send_command(self.valve_commands[self.CONVEYOR2_REVERSE]['open'])
time.sleep(self.delay_conveyor)
def close(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False):
def close(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False):
if conveyor1:
self.send_command(self.valve_commands[self.CONVEYOR1]['close'])
time.sleep(self.delay_conveyor)
@ -263,6 +284,9 @@ class RelayController:
if pusher1:
self.send_command(self.valve_commands[self.PUSHER1]['close'])
time.sleep(self.delay_pusher)
if conveyor2_reverse:
self.send_command(self.valve_commands[self.CONVEYOR2_REVERSE]['close'])
time.sleep(self.delay_conveyor)
# ===================== 传感器处理线程 =====================
def handle_sensor1(self):
@ -314,7 +338,7 @@ class RelayController:
self.motor_stopped_by_sensor2 = True
self.sensor2_ready = True
else:
if self.sensor2_ready and self.motor_stopped_by_sensor2:
if self.sensor2_ready: #and self.motor_stopped_by_sensor2:
self.open(conveyor2=True)
self.motor_stopped_by_sensor2 = False
self.sensor2_ready = False
@ -346,6 +370,18 @@ class RelayController:
if self._sensor2_thread:
self._sensor2_thread.join()
print("传感器线程已终止。")
def stop_sensor(self,sensor1_thread,sensor2_thread):
if not self._running:
print("线程未在运行")
return
print("停止传感器线程")
self._running = False
if sensor1_thread and sensor1_thread.is_alive():
sensor1_thread.join()
if sensor2_thread and sensor2_thread.is_alive():
sensor2_thread.join()
print("传感器线程已终止。")
def start_sensor1_only(self):
if self._running: