合并35kg
This commit is contained in:
346
EMV/EMV.py
346
EMV/EMV.py
@ -36,6 +36,7 @@ class RelayController(QObject):
|
||||
self.sensor_stable_duration = 1.0 # 传感器状态稳定检测时间(秒)
|
||||
self.sensor_max_attempts = 3 # 连续检测次数达到此值判定有效
|
||||
self.sensor1_debounce_time = 1.0 # 传感器1防抖时间(秒)
|
||||
self.sensor2_debounce_time = 3.0 # 袋尾检测3秒有效信号
|
||||
|
||||
# ===================== 网络与设备映射 =====================
|
||||
self.host = host
|
||||
@ -113,6 +114,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'):
|
||||
@ -127,7 +129,7 @@ class RelayController(QObject):
|
||||
sock.connect((self.host, self.port))
|
||||
sock.send(byte_data)
|
||||
response = sock.recv(1024)
|
||||
hex_response = binascii.hexlify(response).decode('utf-8')
|
||||
# hex_response = binascii.hexlify(response).decode('utf-8')
|
||||
#if source == 'sensor':
|
||||
#print(f"[传感器响应] {hex_response}")
|
||||
#elif source == 'device':
|
||||
@ -136,13 +138,14 @@ class RelayController(QObject):
|
||||
#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})")
|
||||
self.log_signal.emit(logging.INFO,f"网络继电器通信错误 ({source}): {e}, 尝试重连... ({attempt + 1}/{retry_count})")
|
||||
time.sleep(5)
|
||||
self.trigger_alarm()
|
||||
return None
|
||||
|
||||
def trigger_alarm(self):
|
||||
print("警告:网络继电器连续多次通信失败,请检查设备连接!")
|
||||
self.log_signal.emit(logging.ERROR,"警告:网络继电器连续多次通信失败,请检查设备连接!")
|
||||
|
||||
# ===================== 状态读取方法 =====================
|
||||
def get_all_device_status(self, command_type='devices'):
|
||||
@ -186,6 +189,7 @@ class RelayController(QObject):
|
||||
|
||||
if response and len(response) >= 10:
|
||||
hex_response = binascii.hexlify(response).decode('utf-8')
|
||||
|
||||
# print(f"[原始响应][{command_type}] {hex_response}")
|
||||
|
||||
# 假设传感器数据从第 9 字节开始,长度为 2 字节
|
||||
@ -247,7 +251,63 @@ class RelayController(QObject):
|
||||
time.sleep(self.sensor2_loop_delay)
|
||||
return False
|
||||
|
||||
def is_valid_sensor(self,sensor_name):
|
||||
"""
|
||||
检查传感器状态是否有效
|
||||
参数:
|
||||
sensor_name: 传感器名称
|
||||
返回:
|
||||
True: 传感器状态有效
|
||||
False: 传感器状态无效
|
||||
"""
|
||||
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:
|
||||
return True
|
||||
else:
|
||||
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 is_valid_sensor2_status_lost(self, sensor_name):
|
||||
"""
|
||||
检查传感器2是否丢失信号
|
||||
"""
|
||||
stable_count = 0
|
||||
_try_nums=5 # 尝试次数
|
||||
for _ in range(_try_nums):
|
||||
@ -269,49 +329,104 @@ class RelayController(QObject):
|
||||
return False
|
||||
|
||||
# ===================== 动作控制方法 =====================
|
||||
def open(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False):
|
||||
if Constant.DebugPosition:
|
||||
return
|
||||
def open(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False)->bool:
|
||||
"""将if改成elif,一次只能打开一个设备,否则会造成延时sleep时间不一致问题。并返回成功核验"""
|
||||
loc_ret=False
|
||||
loc_reponse=None
|
||||
loc_send_command=None
|
||||
status = self.get_all_device_status()
|
||||
if conveyor1 and not status.get(self.CONVEYOR1, False):
|
||||
self.send_command(self.valve_commands[self.CONVEYOR1]['open'])
|
||||
time.sleep(self.delay_conveyor)
|
||||
if pusher and not status.get(self.PUSHER, False):
|
||||
self.send_command(self.valve_commands[self.PUSHER]['open'])
|
||||
time.sleep(self.delay_pusher)
|
||||
if conveyor2 and not status.get(self.CONVEYOR2, False):
|
||||
self.send_command(self.valve_commands[self.CONVEYOR2]['open'])
|
||||
time.sleep(self.delay_conveyor)
|
||||
if clamp and not status.get(self.CLAMP, False):
|
||||
self.send_command(self.valve_commands[self.CLAMP]['open'])
|
||||
time.sleep(self.delay_clamp)
|
||||
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, conveyor2_reverse=False):
|
||||
if conveyor1:
|
||||
self.send_command(self.valve_commands[self.CONVEYOR1]['close'])
|
||||
if not status.get(self.CONVEYOR1, False):
|
||||
loc_send_command=self.valve_commands[self.CONVEYOR1]['open']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_conveyor)
|
||||
else:
|
||||
loc_ret=True
|
||||
elif pusher:
|
||||
if not status.get(self.PUSHER, False):
|
||||
loc_send_command=self.valve_commands[self.PUSHER]['open']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_pusher)
|
||||
else:
|
||||
loc_ret=True
|
||||
elif conveyor2:
|
||||
if not status.get(self.CONVEYOR2, False):
|
||||
loc_send_command=self.valve_commands[self.CONVEYOR2]['open']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_conveyor)
|
||||
else:
|
||||
loc_ret=True
|
||||
elif clamp:
|
||||
if not status.get(self.CLAMP, False):
|
||||
loc_send_command=self.valve_commands[self.CLAMP]['open']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_clamp)
|
||||
else:
|
||||
loc_ret=True
|
||||
elif pusher1:
|
||||
if not status.get(self.PUSHER1, False):
|
||||
loc_send_command=self.valve_commands[self.PUSHER1]['open']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_pusher)
|
||||
else:
|
||||
loc_ret=True
|
||||
elif conveyor2_reverse:
|
||||
if not status.get(self.CONVEYOR2_REVERSE, False):
|
||||
loc_send_command=self.valve_commands[self.CONVEYOR2_REVERSE]['open']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_conveyor)
|
||||
else:
|
||||
loc_ret=True
|
||||
|
||||
|
||||
if loc_reponse and len(loc_reponse) >= 10:
|
||||
lol_hex_response = binascii.hexlify(response).decode('utf-8')
|
||||
if lol_hex_response == loc_send_command:
|
||||
loc_ret=True
|
||||
|
||||
if Constant.DebugPosition:
|
||||
loc_ret=True
|
||||
return loc_ret
|
||||
|
||||
|
||||
def close(self, conveyor1=False, pusher=False, conveyor2=False, clamp=False, pusher1=False, conveyor2_reverse=False)->bool:
|
||||
loc_ret=False
|
||||
loc_reponse=None
|
||||
loc_send_command=None
|
||||
if conveyor1:
|
||||
loc_send_command=self.valve_commands[self.CONVEYOR1]['close']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_conveyor)
|
||||
if pusher:
|
||||
self.send_command(self.valve_commands[self.PUSHER]['close'])
|
||||
loc_send_command=self.valve_commands[self.PUSHER]['close']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_pusher)
|
||||
if conveyor2:
|
||||
self.send_command(self.valve_commands[self.CONVEYOR2]['close'])
|
||||
loc_send_command=self.valve_commands[self.CONVEYOR2]['close']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_conveyor)
|
||||
if clamp:
|
||||
self.send_command(self.valve_commands[self.CLAMP]['close'])
|
||||
loc_send_command=self.valve_commands[self.CLAMP]['close']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_clamp)
|
||||
if pusher1:
|
||||
self.send_command(self.valve_commands[self.PUSHER1]['close'])
|
||||
loc_send_command=self.valve_commands[self.PUSHER1]['close']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_pusher)
|
||||
if conveyor2_reverse:
|
||||
self.send_command(self.valve_commands[self.CONVEYOR2_REVERSE]['close'])
|
||||
loc_send_command=self.valve_commands[self.CONVEYOR2_REVERSE]['close']
|
||||
loc_reponse=self.send_command(loc_send_command)
|
||||
time.sleep(self.delay_conveyor)
|
||||
|
||||
if loc_reponse and len(loc_reponse) >= 10:
|
||||
lol_hex_response = binascii.hexlify(response).decode('utf-8')
|
||||
if lol_hex_response == loc_send_command:
|
||||
loc_ret=True
|
||||
if Constant.DebugPosition:
|
||||
loc_ret=True
|
||||
return loc_ret
|
||||
|
||||
|
||||
# ===================== 传感器处理线程 =====================
|
||||
def handle_sensor1(self):
|
||||
while self._running:
|
||||
@ -356,6 +471,7 @@ class RelayController(QObject):
|
||||
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)
|
||||
|
||||
@ -363,6 +479,14 @@ class RelayController(QObject):
|
||||
_is_pause_close=True
|
||||
#是否料袋尾部(有信号--》无信号)
|
||||
_is_signal=False
|
||||
|
||||
#正发转用
|
||||
_is_reverse_2=False
|
||||
#是否反转后正转信号消息
|
||||
_is_signal_2=False
|
||||
#是否首次正转信号
|
||||
_is_first_signal_2=False
|
||||
|
||||
while self._running:
|
||||
if self._ispause:
|
||||
#暂停
|
||||
@ -376,23 +500,141 @@ class RelayController(QObject):
|
||||
continue
|
||||
#开启线程
|
||||
_is_pause_close=True
|
||||
if self.is_drop_35:
|
||||
#region 35kg 正反转打平
|
||||
try:
|
||||
if _is_signal_2 or self.is_valid_sensor_status_1(self.SENSOR2):
|
||||
#反转要加个防抖动时间
|
||||
if _is_reverse_2:
|
||||
print('回退后检查到sensor2 35KG信号,正转')
|
||||
self.open(conveyor2=True)
|
||||
_is_reverse_2=False
|
||||
_is_signal_2=True
|
||||
elif _is_signal_2 and self.is_valid_sensor2_status_lost(self.SENSOR2):
|
||||
print('检查到sensor2正转35KG信号消失')
|
||||
self.close(conveyor2=True)
|
||||
#滚筒关闭标志
|
||||
self.motor_stopped_by_sensor2 = True
|
||||
# 发送信号通知机器人取走物品
|
||||
self.take_robot_signal.emit()
|
||||
_is_signal_2=False
|
||||
#停止后即使有信号了,也不能转,直到self.sensor2_ready=True
|
||||
# _is_first_signal=False
|
||||
self.sensor2_ready=False #打开滚洞标识
|
||||
elif not _is_first_signal_2:
|
||||
print('检查到正转sensor2 35KG信号')
|
||||
#检测到信号,5秒
|
||||
time.sleep(6)
|
||||
self.open(conveyor2_reverse=True)
|
||||
_is_reverse_2=True
|
||||
_is_first_signal_2=True
|
||||
time.sleep(0.1)
|
||||
continue
|
||||
elif self.sensor2_ready:
|
||||
#sensor2_ready:通过Feeding:FPhoto处控制是否启动
|
||||
if self.motor_stopped_by_sensor2:
|
||||
print('开滚筒')
|
||||
self.open(conveyor2=True)
|
||||
self.motor_stopped_by_sensor2 = False
|
||||
_is_first_signal=False
|
||||
# _is_reverse=False
|
||||
# _is_first_signal=False
|
||||
# _is_signal=False
|
||||
|
||||
if _is_reverse_2:
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
time.sleep(2)
|
||||
except Exception as e:
|
||||
print(f"SENSOR3 处理错误: {e}")
|
||||
self.log_signal.emit(logging.ERROR,f"SENSOR3 处理错误: {e}")
|
||||
time.sleep(self.sensor2_error_delay)
|
||||
#endregion
|
||||
else:
|
||||
try:
|
||||
|
||||
if self.sensor2_ready:
|
||||
#sensor2_ready:通过Feeding:FPhoto处控制是否启动,到了,先启动
|
||||
if self.motor_stopped_by_sensor2:
|
||||
print('开滚筒')
|
||||
self.open(conveyor2=True)
|
||||
self.motor_stopped_by_sensor2 = False
|
||||
elif _is_signal or self.is_valid_sensor_signal_stable(self.SENSOR2,detection_duration=3,stability_duration=2.5,check_interval=0.5):
|
||||
#检测到信号,如果之前是没有信号,关闭滚筒
|
||||
print('检查到sensor2信号')
|
||||
if _is_signal and self.is_valid_sensor2_status_lost(self.SENSOR2):
|
||||
print('检查到sensor2信号消失')
|
||||
self.close(conveyor2=True)
|
||||
#滚筒关闭标志
|
||||
self.motor_stopped_by_sensor2 = True
|
||||
# 发送信号通知机器人取走物品
|
||||
self.take_robot_signal.emit()
|
||||
_is_signal=False
|
||||
self.sensor2_ready=False #打开滚洞标识
|
||||
else:
|
||||
_is_signal=True
|
||||
# time.sleep(0.1)
|
||||
continue
|
||||
|
||||
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 handle_sensor3(self):
|
||||
"""
|
||||
正转--》反转--》正转
|
||||
"""
|
||||
_is_pause_close=True
|
||||
#是否反转
|
||||
_is_reverse=False
|
||||
#是否反转后正转信号消息
|
||||
_is_signal=False
|
||||
#是否首次正转信号
|
||||
_is_first_signal=False
|
||||
#是否料袋尾部(有信号--》无信号)
|
||||
while self._running:
|
||||
if self._ispause:
|
||||
#暂停
|
||||
if _is_pause_close:
|
||||
self.close(conveyor2=True)
|
||||
self.motor_stopped_by_sensor2 = True
|
||||
# self.sensor2_ready = True #初始值
|
||||
_is_pause_close=False
|
||||
|
||||
time.sleep(self.sensor2_error_delay)
|
||||
continue
|
||||
#开启线程
|
||||
_is_pause_close=True
|
||||
#滚动次数
|
||||
try:
|
||||
if _is_signal or self.is_valid_sensor_status_1(self.SENSOR2):
|
||||
#检测到信号,如果之前是没有信号,关闭滚筒
|
||||
print('检查到sensor2信号')
|
||||
if _is_signal and self.is_valid_sensor2_status_lost(self.SENSOR2):
|
||||
print('检查到sensor2信号消失')
|
||||
if _is_signal or self.is_valid_sensor_status_1(self.SENSOR2):
|
||||
#反转要加个防抖动时间
|
||||
if _is_reverse:
|
||||
print('回退后检查到sensor2信号,正转')
|
||||
self.open(conveyor2=True)
|
||||
_is_reverse=False
|
||||
_is_signal=True
|
||||
elif _is_signal and self.is_valid_sensor2_status_lost(self.SENSOR2):
|
||||
print('检查到sensor2正转信号消失')
|
||||
self.close(conveyor2=True)
|
||||
#滚筒关闭标志
|
||||
self.motor_stopped_by_sensor2 = True
|
||||
# 发送信号通知机器人取走物品
|
||||
self.take_robot_signal.emit()
|
||||
_is_signal=False
|
||||
#停止后即使有信号了,也不能转,直到self.sensor2_ready=True
|
||||
# _is_first_signal=False
|
||||
self.sensor2_ready=False #打开滚洞标识
|
||||
else:
|
||||
_is_signal=True
|
||||
# time.sleep(0.1)
|
||||
elif not _is_first_signal:
|
||||
print('检查到正转sensor2信号')
|
||||
#检测到信号,5秒
|
||||
time.sleep(6)
|
||||
self.open(conveyor2_reverse=True)
|
||||
_is_reverse=True
|
||||
_is_first_signal=True
|
||||
time.sleep(0.1)
|
||||
continue
|
||||
elif self.sensor2_ready:
|
||||
#sensor2_ready:通过Feeding:FPhoto处控制是否启动
|
||||
@ -400,12 +642,20 @@ class RelayController(QObject):
|
||||
print('开滚筒')
|
||||
self.open(conveyor2=True)
|
||||
self.motor_stopped_by_sensor2 = False
|
||||
|
||||
time.sleep(2)
|
||||
_is_first_signal=False
|
||||
# _is_reverse=False
|
||||
# _is_first_signal=False
|
||||
# _is_signal=False
|
||||
|
||||
if _is_reverse:
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
time.sleep(2)
|
||||
except Exception as e:
|
||||
print(f"SENSOR2 处理错误: {e}")
|
||||
print(f"SENSOR3 处理错误: {e}")
|
||||
self.log_signal.emit(logging.ERROR,f"SENSOR3 处理错误: {e}")
|
||||
time.sleep(self.sensor2_error_delay)
|
||||
|
||||
|
||||
def pause_start_sensor(self,is_pause):
|
||||
"""
|
||||
暂停或开启传感器线程
|
||||
@ -413,6 +663,14 @@ class RelayController(QObject):
|
||||
"""
|
||||
self._ispause = is_pause
|
||||
|
||||
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:
|
||||
print("线程未在运行")
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user