bgg35-50
This commit is contained in:
218
EMV/EMV.py
218
EMV/EMV.py
@ -4,7 +4,9 @@ import socket
|
||||
import binascii
|
||||
import time
|
||||
import threading
|
||||
from datetime import datetime
|
||||
import logging
|
||||
from typing import Optional
|
||||
from PySide6.QtCore import Signal, QObject
|
||||
import numpy as np
|
||||
from pandas.core.arrays import boolean
|
||||
@ -133,6 +135,7 @@ class RelayController(QObject):
|
||||
|
||||
self.sensor2_ready = False #默认不打开
|
||||
self.motor_stopped_by_sensor2 = True
|
||||
self.is_drop_35=False #是否是35码
|
||||
|
||||
# ===================== 基础通信方法 =====================
|
||||
def send_command(self, command_hex, retry_count=2, source='unknown'):
|
||||
@ -142,11 +145,15 @@ class RelayController(QObject):
|
||||
byte_data = binascii.unhexlify(command_hex)
|
||||
for attempt in range(retry_count):
|
||||
try:
|
||||
# begin_time=time.time()
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
sock.settimeout(10)
|
||||
sock.settimeout(1)
|
||||
sock.connect((self.host, self.port))
|
||||
sock.send(byte_data)
|
||||
response = sock.recv(1024)
|
||||
|
||||
# end_time=time.time()
|
||||
# print(f"({source}) 耗时: {end_time-begin_time:.3f}秒")
|
||||
# hex_response = binascii.hexlify(response).decode('utf-8')
|
||||
#if source == 'sensor':
|
||||
#print(f"[传感器响应] {hex_response}")
|
||||
@ -158,7 +165,7 @@ class RelayController(QObject):
|
||||
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)
|
||||
time.sleep(1)
|
||||
self.trigger_alarm()
|
||||
return None
|
||||
|
||||
@ -221,7 +228,7 @@ class RelayController(QObject):
|
||||
print(f"[{command_type}] 无法获取响应数据")
|
||||
return responses
|
||||
|
||||
def get_emergency_is_pressed(self)->bool:
|
||||
def get_emergency_is_pressed(self):
|
||||
"获取急停信号,DI 3 位为1,正常,DI 3为0时为按下急停状态,00000000000401020107 后四位01表示一个字节,最后一位(07)二进制对应开关量"
|
||||
"按下急停为"
|
||||
command = self.read_status_command.get("sensors")
|
||||
@ -233,6 +240,7 @@ class RelayController(QObject):
|
||||
status_crc=response[8]
|
||||
loc_is_pressed =status_crc==1 and (status_byte & 0b100) == 0 # 0b100 表示第三位为1
|
||||
else:
|
||||
loc_is_pressed=None
|
||||
self.log_signal.emit(logging.ERROR,f"网络继电器[急停] 读取状态失败或响应无效")
|
||||
print(f"网络继电器[急停] 读取状态失败或响应无效")
|
||||
|
||||
@ -267,6 +275,37 @@ class RelayController(QObject):
|
||||
time.sleep(self.sensor1_loop_delay)
|
||||
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 = time.time() # 信号不稳定,重置计时
|
||||
time.sleep(check_interval)
|
||||
return False
|
||||
|
||||
def is_valid_sensor_status_1(self, sensor_name):
|
||||
stable_count = 0
|
||||
for _ in range(int(self.sensor_stable_duration / self.sensor2_loop_delay)):
|
||||
@ -337,36 +376,110 @@ class RelayController(QObject):
|
||||
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):
|
||||
def is_valid_sensor_nowait(self,sensor_name):
|
||||
"""
|
||||
检测在指定时间窗口内是否存在持续稳定的有效信号
|
||||
检查传感器状态是否有效,不等待,如果第一次没有信号不等待,有信号一起一秒钟
|
||||
参数:
|
||||
sensor_name: 传感器名称
|
||||
detection_duration: 总检测时间窗口(秒),默认为3秒
|
||||
stability_duration: 信号需要持续稳定的时间(秒),默认为2.5秒
|
||||
check_interval: 检测间隔(秒),默认为0.1秒
|
||||
|
||||
返回:
|
||||
True: 在时间窗口内检测到持续稳定的有效信号
|
||||
False: 未检测到持续稳定的有效信号
|
||||
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 # 信号持续稳定达到要求时间
|
||||
stable_count = 0
|
||||
_try_nums=5 # 尝试次数
|
||||
for _num 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:
|
||||
stable_start_time = None # 信号不稳定,重置计时
|
||||
time.sleep(check_interval)
|
||||
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:
|
||||
if _num==0:
|
||||
#首次没有信号,直接返回
|
||||
return False
|
||||
else:
|
||||
stable_count = 0
|
||||
time.sleep(self.sensor2_loop_lost)
|
||||
return False
|
||||
|
||||
|
||||
def is_valid_sensor_stable(self,sensor_name):
|
||||
"""
|
||||
检查传感器状态是否有效
|
||||
参数:
|
||||
sensor_name: 传感器名称
|
||||
返回:
|
||||
True: 传感器状态有效
|
||||
False: 传感器状态无效
|
||||
"""
|
||||
#检测一秒钟,首次没有信号直接返回
|
||||
if not self.is_valid_sensor_nowait(sensor_name):
|
||||
return False
|
||||
#需要增加超时时间,否则会一直等待
|
||||
stable_count = 0
|
||||
_try_nums=10 # 尝试次数
|
||||
for _ in range(_try_nums):
|
||||
|
||||
responses = self.get_all_sensor_responses('sensors')
|
||||
response = responses.get(sensor_name)
|
||||
if response:
|
||||
temp_status_code = self.parse_status_code(response)
|
||||
if temp_status_code in self.required_codes_1:
|
||||
stable_count += 1
|
||||
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]}:检测到信号,已检测 {stable_count} 次")
|
||||
if stable_count >= 8:
|
||||
return True
|
||||
else:
|
||||
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]}:未检测到信号,已检测 {stable_count} 次")
|
||||
else:
|
||||
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]}:[警告] 无法获取 {sensor_name} 的响应,尝试重试...")
|
||||
# else:
|
||||
# stable_count = 0
|
||||
time.sleep(self.sensor2_loop_delay)
|
||||
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 # 记录首次检测到有效信号的时间
|
||||
# stable_end_time = None # 记录最后检测到有效信号的时间
|
||||
# start_time = time.time()
|
||||
# if not self.is_valid_sensor_single(sensor_name):
|
||||
# return False # 传感器状态无效,返回
|
||||
# else:
|
||||
# stable_start_time = time.time() # 首次检测到有效信号
|
||||
# # stable_end_time = stable_start_time # 最后检测到有效信号
|
||||
# time.sleep(check_interval)
|
||||
|
||||
# while time.time() - start_time < detection_duration:
|
||||
# #1s时间
|
||||
# temp_is_valid = self.is_valid_sensor_single(sensor_name)
|
||||
# if temp_is_valid:
|
||||
# stable_start_time=time.time()
|
||||
# else:
|
||||
# stable_start_time = None # 信号不稳定,重置计时
|
||||
# # time.sleep(check_interval)
|
||||
# if stable_start_time - start_time >= stability_duration:
|
||||
# return True # 信号持续稳定达到要求时间
|
||||
# else:
|
||||
# return False
|
||||
# ===================== 动作控制方法 =====================
|
||||
def open(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False,belt=False,alarm=False,blow_sensor2=False):
|
||||
# if Constant.DebugPosition:
|
||||
@ -380,7 +493,7 @@ class RelayController(QObject):
|
||||
time.sleep(self.delay_pusher)
|
||||
if conveyor2:
|
||||
self.send_command(self.valve_commands[self.CONVEYOR2]['open'])
|
||||
time.sleep(self.delay_conveyor)
|
||||
# time.sleep(0.01)
|
||||
if clamp:
|
||||
self.send_command(self.valve_commands[self.CLAMP]['open'])
|
||||
time.sleep(self.delay_clamp)
|
||||
@ -409,10 +522,10 @@ class RelayController(QObject):
|
||||
time.sleep(self.delay_pusher)
|
||||
if conveyor2:
|
||||
self.send_command(self.valve_commands[self.CONVEYOR2]['close'])
|
||||
time.sleep(self.delay_conveyor)
|
||||
#time.sleep(self.delay_conveyor)
|
||||
if clamp:
|
||||
self.send_command(self.valve_commands[self.CLAMP]['close'])
|
||||
time.sleep(self.delay_clamp)
|
||||
time.sleep(0.05)
|
||||
if pusher1:
|
||||
self.send_command(self.valve_commands[self.PUSHER1]['close'])
|
||||
time.sleep(self.delay_pusher)
|
||||
@ -496,11 +609,18 @@ class RelayController(QObject):
|
||||
_is_pause_close=True
|
||||
|
||||
try:
|
||||
if _is_signal or self.is_valid_sensor_status_1(self.SENSOR2):
|
||||
# if _is_signal or self.is_valid_sensor_status_1(self.SENSOR2):
|
||||
#motor_stopped_by_sensor2=False(在滚动的时候)才去检测信号,否则如果后面的料子
|
||||
#有信号了,不会在FPhoto后开滚筒
|
||||
if (not self.motor_stopped_by_sensor2) and (_is_signal or self.is_valid_sensor_stable(self.SENSOR2)):
|
||||
#检测到信号,如果之前是没有信号,关闭滚筒
|
||||
print('检查到sensor2信号')
|
||||
#print('检查到sensor2信号')
|
||||
if _is_signal and self.is_valid_sensor2_status_lost(self.SENSOR2):
|
||||
print('检查到sensor2信号消失')
|
||||
self.log_signal.emit(logging.INFO,"检查到sensor2信号消失")
|
||||
if self.is_drop_35:
|
||||
time.sleep(3.5)
|
||||
self.open(conveyor2_reverse=True)
|
||||
time.sleep(2.5)
|
||||
self.close(conveyor2=True)
|
||||
#滚筒关闭标志
|
||||
self.motor_stopped_by_sensor2 = True
|
||||
@ -512,21 +632,23 @@ class RelayController(QObject):
|
||||
if self.sensor2_ready:
|
||||
#只有在FPhoto处才有效
|
||||
_is_signal=True
|
||||
if self.motor_stopped_by_sensor2:
|
||||
print('开滚筒')
|
||||
self.open(conveyor2=True)
|
||||
self.motor_stopped_by_sensor2 = False
|
||||
# if self.motor_stopped_by_sensor2:
|
||||
# self.log_signal.emit(logging.INFO,"开滚筒2")
|
||||
# print('开滚筒2开滚筒2开滚筒2开滚筒2')
|
||||
# self.open(conveyor2=True)
|
||||
# self.motor_stopped_by_sensor2 = False
|
||||
|
||||
# time.sleep(0.1)
|
||||
time.sleep(0.01)
|
||||
continue
|
||||
elif self.sensor2_ready:
|
||||
#sensor2_ready:通过Feeding:FPhoto处控制是否启动
|
||||
if self.motor_stopped_by_sensor2:
|
||||
print('开滚筒')
|
||||
self.log_signal.emit(logging.INFO,"开滚筒1")
|
||||
print('开滚筒1开滚筒1开滚筒1开滚筒1')
|
||||
self.open(conveyor2=True)
|
||||
self.motor_stopped_by_sensor2 = False
|
||||
|
||||
time.sleep(2)
|
||||
time.sleep(0.5)
|
||||
except Exception as e:
|
||||
print(f"SENSOR2 处理错误: {e}")
|
||||
self.log_signal.emit(logging.ERROR,f"SENSOR2 处理错误: {e}")
|
||||
@ -546,6 +668,13 @@ class RelayController(QObject):
|
||||
"""开启皮带"""
|
||||
self.open(belt=True)
|
||||
|
||||
def set_drop_35(self,is_drop_35):
|
||||
"""
|
||||
设置是否是35码
|
||||
is_drop_35:True是,False否
|
||||
"""
|
||||
self.is_drop_35=is_drop_35
|
||||
|
||||
|
||||
def stop_sensor(self,sensor1_thread,sensor2_thread):
|
||||
if not self._running:
|
||||
@ -561,12 +690,15 @@ class RelayController(QObject):
|
||||
|
||||
def handle_emergency_pressed(self):
|
||||
"处理急停按钮信号状态线程"
|
||||
print('检查急停按钮状态1')
|
||||
#print('检查急停按钮状态1')
|
||||
|
||||
while self._running:
|
||||
try:
|
||||
print('检查急停按钮状态')
|
||||
#print('检查急停按钮状态')
|
||||
loc_is_pressed =self.get_emergency_is_pressed()
|
||||
if loc_is_pressed is None:
|
||||
time.sleep(0.5)
|
||||
continue
|
||||
if loc_is_pressed:
|
||||
# 处理急停按钮信号状态
|
||||
if not self.emergency_is_pressed:
|
||||
@ -575,11 +707,11 @@ class RelayController(QObject):
|
||||
self.emergency_is_pressed=True
|
||||
self.emergency_signal.emit(True)
|
||||
else:
|
||||
print('急停按钮未被按下')
|
||||
#print('急停按钮未被按下')
|
||||
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)
|
||||
time.sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user