251108问题修复
This commit is contained in:
159
EMV/EMV.py
159
EMV/EMV.py
@ -14,6 +14,8 @@ import Constant
|
||||
class RelayController(QObject):
|
||||
log_signal = Signal(int, str)
|
||||
take_robot_signal = Signal()
|
||||
emergency_signal = Signal(bool)
|
||||
|
||||
def __init__(self, host='192.168.0.18', port=50000):
|
||||
super().__init__()
|
||||
# ===================== 全局线程延时参数 =====================
|
||||
@ -32,6 +34,8 @@ class RelayController(QObject):
|
||||
self.delay_clamp = 0.5 # 夹爪动作延时
|
||||
self.delay_after_pusher = 5.0 # 推板推出后到重启传动带时间
|
||||
|
||||
self.emergency_is_pressed=False
|
||||
|
||||
# ===================== 传感器稳定检测参数 =====================
|
||||
self.sensor_stable_duration = 1.0 # 传感器状态稳定检测时间(秒)
|
||||
self.sensor_max_attempts = 3 # 连续检测次数达到此值判定有效
|
||||
@ -49,8 +53,11 @@ class RelayController(QObject):
|
||||
self.PUSHER1 = 'pusher1'
|
||||
self.SENSOR1 = 'sensor1'
|
||||
self.SENSOR2 = 'sensor2'
|
||||
self.BELT = 'belt'
|
||||
self.ALARM = 'alarm'
|
||||
|
||||
self.valve_commands = {
|
||||
#包装机皮带
|
||||
self.CONVEYOR1: {'open': '000000000006010500070000', 'close': '00000000000601050007FF00'},
|
||||
# self.CONVEYOR11: {'open': '00000000000601050000FF00', 'close': '000000000006010500000000'},
|
||||
self.PUSHER: {'open': '00000000000601050001FF00', 'close': '000000000006010500010000'},
|
||||
@ -60,6 +67,10 @@ class RelayController(QObject):
|
||||
self.CLAMP: {'open': '00000000000601050003FF00', 'close': '000000000006010500030000'},
|
||||
#DO5 回 DO2推
|
||||
self.PUSHER1: {'open': '00000000000601050004FF00', 'close': '000000000006010500040000'},
|
||||
#D07 长皮带
|
||||
self.BELT: {'open': '00000000000601050006FF00', 'close': '000000000006010500060000'},
|
||||
#D01 声控报警
|
||||
self.ALARM: {'open': '00000000000601050000FF00', 'close': '000000000006010500000000'},
|
||||
#滚筒反转
|
||||
self.CONVEYOR2_REVERSE: {'open': '000100000006020620000022', 'close': '000100000006020620000001'}
|
||||
}
|
||||
@ -77,7 +88,9 @@ class RelayController(QObject):
|
||||
self.CONVEYOR2: 2,
|
||||
self.CLAMP: 3,
|
||||
self.PUSHER1: 4,
|
||||
self.CONVEYOR2_REVERSE: 5
|
||||
self.CONVEYOR2_REVERSE: 5,
|
||||
self.BELT: 6,
|
||||
self.ALARM: 7,
|
||||
}
|
||||
|
||||
self.sensor_bit_map = {
|
||||
@ -91,7 +104,9 @@ class RelayController(QObject):
|
||||
self.CONVEYOR2: "传送带2",
|
||||
self.CLAMP: "机械臂夹爪",
|
||||
self.PUSHER1: "推板关",
|
||||
self.CONVEYOR2_REVERSE: "传送带2反转"
|
||||
self.CONVEYOR2_REVERSE: "传送带2反转",
|
||||
self.BELT: "皮带",
|
||||
self.ALARM: "声控报警",
|
||||
}
|
||||
|
||||
self.sensor_name_map = {
|
||||
@ -105,8 +120,8 @@ class RelayController(QObject):
|
||||
self._sensor1_thread = None
|
||||
self._sensor2_thread = None
|
||||
|
||||
self.required_codes = {'0101', '0103'} # 有效状态码(传感器1)
|
||||
self.required_codes_1 = {'0102', '0103'} # 有效状态码(传感器2)
|
||||
self.required_codes = {'0101', '0103','0105','0107'} # 有效状态码(传感器1)
|
||||
self.required_codes_1 = {'0102', '0103','0106','0107'} # 有效状态码(传感器2)
|
||||
|
||||
self.sensor1_triggered = False
|
||||
self.sensor1_last_time = 0
|
||||
@ -117,7 +132,7 @@ class RelayController(QObject):
|
||||
# ===================== 基础通信方法 =====================
|
||||
def send_command(self, command_hex, retry_count=2, source='unknown'):
|
||||
if Constant.DebugPosition:
|
||||
# print(f"[发送命令] {command_hex} ({source})")
|
||||
print(f"[发送命令] {command_hex} ({source})")
|
||||
return None
|
||||
byte_data = binascii.unhexlify(command_hex)
|
||||
for attempt in range(retry_count):
|
||||
@ -136,12 +151,14 @@ class RelayController(QObject):
|
||||
#print(f"[通信响应] {hex_response}")
|
||||
return response
|
||||
except Exception as e:
|
||||
self.log_signal.emit(logging.INFO,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):
|
||||
self.log_signal.emit(logging.ERROR,"警告:网络继电器连续多次通信失败,请检查设备连接!")
|
||||
print("警告:网络继电器连续多次通信失败,请检查设备连接!")
|
||||
|
||||
# ===================== 状态读取方法 =====================
|
||||
@ -199,6 +216,25 @@ class RelayController(QObject):
|
||||
print(f"[{command_type}] 无法获取响应数据")
|
||||
return responses
|
||||
|
||||
def get_emergency_is_pressed(self)->bool:
|
||||
"获取急停信号,DI 3 位为1,正常,DI 3为0时为按下急停状态,00000000000401020107 后四位01表示一个字节,最后一位(07)二进制对应开关量"
|
||||
"按下急停为"
|
||||
command = self.read_status_command.get("sensors")
|
||||
response = self.send_command(command)
|
||||
loc_is_pressed=False
|
||||
if response and len(response) >= 10:
|
||||
status_byte = response[9]
|
||||
#简单验证
|
||||
status_crc=response[8]
|
||||
loc_is_pressed =status_crc==1 and (status_byte & 0b100) == 0 # 0b100 表示第三位为1
|
||||
else:
|
||||
self.log_signal.emit(logging.ERROR,f"网络继电器[急停] 读取状态失败或响应无效")
|
||||
print(f"网络继电器[急停] 读取状态失败或响应无效")
|
||||
|
||||
return loc_is_pressed
|
||||
|
||||
|
||||
|
||||
def parse_status_code(self, response):
|
||||
"""
|
||||
从 Modbus 响应字符串中提取状态码(后两位)
|
||||
@ -268,8 +304,67 @@ class RelayController(QObject):
|
||||
time.sleep(self.sensor2_loop_lost)
|
||||
return False
|
||||
|
||||
def is_valid_sensor(self,sensor_name):
|
||||
"""
|
||||
检查传感器状态是否有效
|
||||
参数:
|
||||
sensor_name: 传感器名称
|
||||
返回:
|
||||
True: 传感器状态有效
|
||||
False: 传感器状态无效
|
||||
"""
|
||||
stable_count = 0
|
||||
_try_nums=5 # 尝试次数
|
||||
for _ in range(_try_nums):
|
||||
responses = self.get_all_sensor_responses('sensors')
|
||||
response = responses.get(sensor_name)
|
||||
|
||||
if not response:
|
||||
print(f"[警告] 无法获取 {sensor_name} 的响应,尝试重试...")
|
||||
return False
|
||||
else:
|
||||
temp_status_code = self.parse_status_code(response)
|
||||
if temp_status_code in self.required_codes_1:
|
||||
stable_count += 1
|
||||
if stable_count >= 3:
|
||||
return True
|
||||
else:
|
||||
stable_count = 0
|
||||
time.sleep(self.sensor2_loop_lost)
|
||||
return False
|
||||
|
||||
def is_valid_sensor_signal_stable(self, sensor_name, detection_duration=3.0, stability_duration=2.5, check_interval=0.1):
|
||||
"""
|
||||
检测在指定时间窗口内是否存在持续稳定的有效信号
|
||||
参数:
|
||||
sensor_name: 传感器名称
|
||||
detection_duration: 总检测时间窗口(秒),默认为3秒
|
||||
stability_duration: 信号需要持续稳定的时间(秒),默认为2.5秒
|
||||
check_interval: 检测间隔(秒),默认为0.1秒
|
||||
|
||||
返回:
|
||||
True: 在时间窗口内检测到持续稳定的有效信号
|
||||
False: 未检测到持续稳定的有效信号
|
||||
"""
|
||||
stable_start_time = None # 记录首次检测到有效信号的时间
|
||||
start_time = time.time()
|
||||
if not self.is_valid_sensor(sensor_name):
|
||||
return False # 传感器状态无效,返回
|
||||
else:
|
||||
stable_start_time = time.time() # 首次检测到有效信号
|
||||
time.sleep(check_interval)
|
||||
|
||||
while time.time() - start_time < detection_duration:
|
||||
temp_is_valid = self.is_valid_sensor(sensor_name)
|
||||
if temp_is_valid:
|
||||
if time.time() - stable_start_time >= stability_duration:
|
||||
return True # 信号持续稳定达到要求时间
|
||||
else:
|
||||
stable_start_time = None # 信号不稳定,重置计时
|
||||
time.sleep(check_interval)
|
||||
return False
|
||||
# ===================== 动作控制方法 =====================
|
||||
def open(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False):
|
||||
def open(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False,belt=False,alarm=False):
|
||||
if Constant.DebugPosition:
|
||||
return
|
||||
status = self.get_all_device_status()
|
||||
@ -291,8 +386,14 @@ class RelayController(QObject):
|
||||
if conveyor2_reverse:
|
||||
self.send_command(self.valve_commands[self.CONVEYOR2_REVERSE]['open'])
|
||||
time.sleep(self.delay_conveyor)
|
||||
if belt and not status.get(self.BELT, False):
|
||||
self.send_command(self.valve_commands[self.BELT]['open'])
|
||||
# time.sleep(self.delay_belt)
|
||||
if alarm and not status.get(self.ALARM, False):
|
||||
self.send_command(self.valve_commands[self.ALARM]['open'])
|
||||
# time.sleep(self.delay_alarm)
|
||||
|
||||
def close(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False):
|
||||
def close(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False,belt=False,alarm=False):
|
||||
if conveyor1:
|
||||
self.send_command(self.valve_commands[self.CONVEYOR1]['close'])
|
||||
time.sleep(self.delay_conveyor)
|
||||
@ -311,6 +412,12 @@ class RelayController(QObject):
|
||||
if conveyor2_reverse:
|
||||
self.send_command(self.valve_commands[self.CONVEYOR2_REVERSE]['close'])
|
||||
time.sleep(self.delay_conveyor)
|
||||
if belt:
|
||||
self.send_command(self.valve_commands[self.BELT]['close'])
|
||||
# time.sleep(self.delay_belt)
|
||||
if alarm:
|
||||
self.send_command(self.valve_commands[self.ALARM]['close'])
|
||||
# time.sleep(self.delay_alarm)
|
||||
|
||||
# ===================== 传感器处理线程 =====================
|
||||
def handle_sensor1(self):
|
||||
@ -346,16 +453,17 @@ class RelayController(QObject):
|
||||
|
||||
time.sleep(1)
|
||||
# 5. 状态检查(可选)
|
||||
status = self.get_all_device_status()
|
||||
if status.get('conveyor1') and not status.get('pusher'):
|
||||
print("流程完成1:皮带运行中,推板已收回")
|
||||
else:
|
||||
print("状态异常,请检查设备")
|
||||
# status = self.get_all_device_status()
|
||||
# if status.get('conveyor1') and not status.get('pusher'):
|
||||
# print("流程完成1:皮带运行中,推板已收回")
|
||||
# else:
|
||||
# print("状态异常,请检查设备")
|
||||
# 流程结束,重置触发标志
|
||||
self.sensor1_triggered = False
|
||||
time.sleep(self.sensor1_loop_delay)
|
||||
except Exception as e:
|
||||
print(f"SENSOR1 处理错误: {e}")
|
||||
self.log_signal.emit(logging.ERROR,f"SENSOR1 处理错误: {e}")
|
||||
self.sensor1_triggered = False
|
||||
time.sleep(self.sensor1_error_delay)
|
||||
|
||||
@ -390,8 +498,10 @@ class RelayController(QObject):
|
||||
self.take_robot_signal.emit()
|
||||
_is_signal=False
|
||||
self.sensor2_ready=False #打开滚洞标识
|
||||
else:
|
||||
_is_signal=True
|
||||
else:
|
||||
if self.sensor2_ready:
|
||||
#只有在FPhoto处才有效
|
||||
_is_signal=True
|
||||
# time.sleep(0.1)
|
||||
continue
|
||||
elif self.sensor2_ready:
|
||||
@ -404,6 +514,7 @@ class RelayController(QObject):
|
||||
time.sleep(2)
|
||||
except Exception as e:
|
||||
print(f"SENSOR2 处理错误: {e}")
|
||||
self.log_signal.emit(logging.ERROR,f"SENSOR2 处理错误: {e}")
|
||||
time.sleep(self.sensor2_error_delay)
|
||||
|
||||
def pause_start_sensor(self,is_pause):
|
||||
@ -413,6 +524,7 @@ class RelayController(QObject):
|
||||
"""
|
||||
self._ispause = is_pause
|
||||
|
||||
|
||||
def stop_sensor(self,sensor1_thread,sensor2_thread):
|
||||
if not self._running:
|
||||
print("线程未在运行")
|
||||
@ -424,3 +536,22 @@ class RelayController(QObject):
|
||||
if sensor2_thread and sensor2_thread.is_alive():
|
||||
sensor2_thread.join()
|
||||
print("传感器线程已终止。")
|
||||
|
||||
def handle_emergency_pressed(self):
|
||||
"处理急停按钮信号状态线程"
|
||||
while self._running:
|
||||
try:
|
||||
loc_is_pressed =self.get_emergency_is_pressed()
|
||||
if loc_is_pressed:
|
||||
# 处理急停按钮信号状态
|
||||
self.log_signal.emit(logging.INFO,f"急停按钮被按下")
|
||||
self.emergency_is_pressed=True
|
||||
self.emergency_signal.emit(True)
|
||||
else:
|
||||
self.emergency_is_pressed=False
|
||||
self.emergency_signal.emit(False)
|
||||
time.sleep(0.5)
|
||||
except Exception as e:
|
||||
print(f"急停 处理错误: {e}")
|
||||
self.log_signal.emit(logging.ERROR,f"急停线程 处理错误: {e}")
|
||||
time.sleep(2)
|
||||
|
||||
Reference in New Issue
Block a user