diff --git a/CU/Feeding.py b/CU/Feeding.py index c487bb4..781f8de 100644 --- a/CU/Feeding.py +++ b/CU/Feeding.py @@ -20,19 +20,23 @@ from enum import Enum, IntEnum from COM.COM_Robot import RobotClient, DetectType from Model.RobotModel import CMDInstructRequest, MoveType from Trace.handeye_calibration import getPosition -from Trace.handeye_calibration import getxyz,getxyz1 +from Trace.handeye_calibration import getxyz, getxyz1 from Util.util_math import get_distance from Util.util_time import CRisOrFall -#from Vision.camera_coordinate_dete import Detection + +# from Vision.camera_coordinate_dete import Detection from Util.util_log import log from Model.RobotModel import Instruction from EMV.EMV import RelayController from CU.drop import DropPositionManager + + class ResetStatus(Enum): RNone = 0 RStart = 1 RRunging = 2 - ROk =3 + ROk = 3 + class FeedStatus(IntEnum): FNone = 0 @@ -42,13 +46,14 @@ class FeedStatus(IntEnum): FPhoto = 4 FTake = 5 FBroken1 = 6 - FBroken2 =7 + FBroken2 = 7 FShake = 8 FDropBag = 9 FFinished = 10 FReverse = 11 FStartReverse = 12 + class LineType(Enum): Straight = 0 CureMid = 2 @@ -59,15 +64,17 @@ class LineType(Enum): class FeedMidStatus(Enum): FMid_Start = 1 FMid_Take = 2 - FMid_Feed= 3 + FMid_Feed = 3 + class FeedPosition: - def __init__(self,status:FeedStatus,position:Real_Position): + def __init__(self, status: FeedStatus, position: Real_Position): self.status = status self.position = position + class FeedLine: - def __init__(self, id, name, feed_positions:list): + def __init__(self, id, name, feed_positions: list): self.feed_positions = copy.deepcopy(feed_positions) self.feeding2end_pos_index = 0 self.origin2start_pos_index = 0 @@ -75,21 +82,41 @@ class FeedLine: self.name = name self.id = id - # --- 新增:用于存储从 ini 文件读取的多个投料点坐标 --- - # 这个列表将在加载 ini 时填充 [[x1,y1,z1,u1,v1,w1], [x2,y2,z2,u2,v2,w2], ...] + # --- 新增:用于存储从 ini 文件读取的多个投料点坐标 --- + # 这个列表将在加载 ini 时填充 [[x1,y1,z1,u1,v1,w1], [x2,y2,z2,u2,v2,w2], ...] self.drop_point_list = [] - def get_current_feed_position(self,is_reverse): - pos = self.feeding_to_end[ (self.feeding2end_pos_index+1)%len(self.feeding_to_end) if is_reverse else self.feeding2end_pos_index-1] - return pos - def get_current_take_position(self,is_reverse): - pos = self.start_to_take[ (self.start2take_pos_index+1)%len(self.start_to_take) if is_reverse else self.start2take_pos_index-1] - return pos - def get_current_start_position(self,is_reverse): - pos = self.origin_to_start[(self.origin2start_pos_index+1)%len(self.origin_to_start) if is_reverse else self.origin2start_pos_index-1] + def get_current_feed_position(self, is_reverse): + pos = self.feeding_to_end[ + ( + (self.feeding2end_pos_index + 1) % len(self.feeding_to_end) + if is_reverse + else self.feeding2end_pos_index - 1 + ) + ] return pos - def get_next_feed_position(self,reverse:bool=False): + def get_current_take_position(self, is_reverse): + pos = self.start_to_take[ + ( + (self.start2take_pos_index + 1) % len(self.start_to_take) + if is_reverse + else self.start2take_pos_index - 1 + ) + ] + return pos + + def get_current_start_position(self, is_reverse): + pos = self.origin_to_start[ + ( + (self.origin2start_pos_index + 1) % len(self.origin_to_start) + if is_reverse + else self.origin2start_pos_index - 1 + ) + ] + return pos + + def get_next_feed_position(self, reverse: bool = False): pos = self.feeding_to_end[self.feeding2end_pos_index] if reverse: self.feeding2end_pos_index -= 1 @@ -101,7 +128,7 @@ class FeedLine: self.feeding2end_pos_index = 0 return pos - def get_next_start_position(self,reverse:bool=False): + def get_next_start_position(self, reverse: bool = False): pos = self.origin_to_start[self.origin2start_pos_index] if reverse: self.origin2start_pos_index -= 1 @@ -114,7 +141,7 @@ class FeedLine: return pos - def get_next_take_position(self,reverse:bool=False): + def get_next_take_position(self, reverse: bool = False): pos = self.start_to_take[self.start2take_pos_index] if reverse: self.start2take_pos_index -= 1 @@ -125,6 +152,7 @@ class FeedLine: if self.start2take_pos_index >= len(self.start_to_take): self.start2take_pos_index = 0 return pos + def get_take_position(self): for i in range(len(self.feeding_to_end)): if self.feeding_to_end[i].status == FeedStatus.FTake.value: @@ -140,20 +168,21 @@ class FeedLine: if self.feeding_to_end[i].status == FeedStatus.FTake.value: # 计算 XYZ 坐标 # xyz = getxyz(position.X, position.Y, position.Z, position.a, position.b, position.c) - xyz = getxyz1(position.X, position.Y, position.Z, position.a, position.b, position.c) + xyz = getxyz1( + position.X, + position.Y, + position.Z, + position.a, + position.b, + position.c, + ) # 创建 before 和 after 位置 - befor_take_position = Real_Position().init_position(xyz[0], - xyz[1], - xyz[2], - position.U, - position.V, - position.W) - after_take_position = Real_Position().init_position(xyz[0], - xyz[1], - xyz[2], - position.U, - position.V, - position.W) + befor_take_position = Real_Position().init_position( + xyz[0], xyz[1], xyz[2], position.U, position.V, position.W + ) + after_take_position = Real_Position().init_position( + xyz[0], xyz[1], xyz[2], position.U, position.V, position.W + ) # 安全检查索引 if i > 0: self.feeding_to_end[i - 1].set_position(befor_take_position) @@ -176,7 +205,8 @@ class FeedLine: # 直接设置当前点的位置 self.feeding_to_end[i].set_position(position) print( - f"✅ FDropBag 位置已更新: ({position.X}, {position.Y}, {position.Z}, {position.U}, {position.V}, {position.W})") + f"✅ FDropBag 位置已更新: ({position.X}, {position.Y}, {position.Z}, {position.U}, {position.V}, {position.W})" + ) break # 假设只有一个丢包点 def get_position_list(self): @@ -189,11 +219,11 @@ class FeedLine: if self.feed_positions[i].status == FeedStatus.FPhoto.value: index_take = i - self.origin_to_start = self.feed_positions[: index_start+1] - self.start_to_take = self.feed_positions[index_start:index_take+1] + self.origin_to_start = self.feed_positions[: index_start + 1] + self.start_to_take = self.feed_positions[index_start : index_take + 1] self.feeding_to_end = self.feed_positions[index_take:] - for i in range(len(self.feeding_to_end)): #插入动态中间点 + for i in range(len(self.feeding_to_end)): # 插入动态中间点 if self.feeding_to_end[i].status == FeedStatus.FTake.value: befor_position_model = PositionModel() befor_position_model.init_position(None) @@ -201,19 +231,26 @@ class FeedLine: after_position_model.init_position(None) self.feeding_to_end.insert(i, befor_position_model) - self.feeding_to_end.insert(i+2, after_position_model) + self.feeding_to_end.insert(i + 2, after_position_model) break + class FeedingConfig: def __init__(self, num: int, feedLine: FeedLine, photo_locs): self.num = num self.feedLine = feedLine self.photo_locs = [self.deal_photo_locs(p) for p in photo_locs] - def deal_photo_locs(self, photo_loc): position_photo = Real_Position() - position_photo.init_position(photo_loc[0], photo_loc[1], photo_loc[2], photo_loc[3], photo_loc[4], photo_loc[5]) + position_photo.init_position( + photo_loc[0], + photo_loc[1], + photo_loc[2], + photo_loc[3], + photo_loc[4], + photo_loc[5], + ) return position_photo def get_line_info(self): @@ -224,7 +261,8 @@ class Feeding(QObject): need_origin_signal = Signal(str) take_no_photo_sigal = Signal() update_detect_image = Signal(np.ndarray) - log_signal = Signal(int,str) + log_signal = Signal(int, str) + def __init__(self, robotClient: RobotClient): super().__init__() self.feedConfig = None @@ -246,7 +284,7 @@ class Feeding(QObject): self.reset_status = ResetStatus.RNone self.reversed_positions = [] self.current_position = None - self.index=1 + self.index = 1 self.pos_index = -1 self.pos_near_index = -1 self.catch = Catch(self.robotClient) @@ -257,7 +295,7 @@ class Feeding(QObject): self.onekey = False self.debug_run_count = 0 # 初始化计数器 self.mid_take_count = 0 - #传感器判断抓包参数 + # 传感器判断抓包参数 self.sensor2_ready = False # 传感器2是否检测到料包 self.motor_stopped_by_sensor2 = False # 是否由传感器2触发停止电机 self.sensor_thread = None @@ -272,9 +310,11 @@ class Feeding(QObject): pass def close_feed(self): + self.need_origin_signal.disconnect() + self.relay_controller._running = False self.is_detected = False self.detect_thread.join() - self.detect.detection.release() + # self.detect.detection.release() def init_detection_image(self): detection_image = cv2.imread(Constant.feed_sign_path) @@ -290,12 +330,14 @@ class Feeding(QObject): # 获取事件坐标 real_position = Real_Position() self.detect.position_index = 0 - real_position.init_position(self.robotClient.status_model.world_0, - self.robotClient.status_model.world_1, - self.robotClient.status_model.world_2, - self.robotClient.status_model.world_3, - self.robotClient.status_model.world_4, - self.robotClient.status_model.world_5) + real_position.init_position( + self.robotClient.status_model.world_0, + self.robotClient.status_model.world_1, + self.robotClient.status_model.world_2, + self.robotClient.status_model.world_3, + self.robotClient.status_model.world_4, + self.robotClient.status_model.world_5, + ) # real_position.init_position(0, # 0, # 0, @@ -312,7 +354,12 @@ class Feeding(QObject): if self.feedConfig == None: self.feedStatus = FeedStatus.FNone - if self.feedConfig !=None and self.feedConfig.num == 0 and self.is_reverse and self.robotClient.origin_position.compare(real_position,is_action=True): + if ( + self.feedConfig != None + and self.feedConfig.num == 0 + and self.is_reverse + and self.robotClient.origin_position.compare(real_position, is_action=True) + ): self.feedStatus = FeedStatus.FNone self.is_reverse = False self.log_signal.emit(logging.INFO, Constant.str_feed_reverse) @@ -338,15 +385,24 @@ class Feeding(QObject): else: self.feed_Mid_Status = FeedMidStatus.FMid_Take - if self.feedConfig.feedLine.start_to_take[0].get_position().compare(real_position): + if ( + self.feedConfig.feedLine.start_to_take[0] + .get_position() + .compare(real_position) + ): self.next_position(self.is_reverse) elif self.feedStatus == FeedStatus.FStart: self.log_signal.emit(logging.INFO, Constant.str_feed_start) - self.relay_controller.open(conveyor2=True)#开电机 - #self.sensor2_thread = threading.Thread(target=self.relay_controller.handle_sensor2, daemon=True)#线程2的开始,但是在那里设置结束呢 - #self.sensor2_thread.start() - if not self.robotClient.origin_position.compare(real_position,is_action=True) and not self.is_reverse: + self.relay_controller.open(conveyor2=True) # 开电机 + # self.sensor2_thread = threading.Thread(target=self.relay_controller.handle_sensor2, daemon=True)#线程2的开始,但是在那里设置结束呢 + # self.sensor2_thread.start() + if ( + not self.robotClient.origin_position.compare( + real_position, is_action=True + ) + and not self.is_reverse + ): # QMessageBox.information(None, "提示", Constant.str_feed_start_error) # Fuck 引起异常 self.log_signal.emit(logging.ERROR, Constant.str_feed_start_error) self.need_origin_signal.emit(Constant.str_feed_start_error) @@ -373,19 +429,23 @@ class Feeding(QObject): if Constant.Debug1: self.log_signal.emit( logging.INFO, - f"[调试计数] 已进入 FMid 分支 {self.mid_take_count} 次" + f"[调试计数] 已进入 FMid 分支 {self.mid_take_count} 次", ) - #这里他写的中间点过完马上进行抓料,和整个流程不符合我就注释掉了,因为实际测试的时候还没到FTake的点他就进行了继电器关闭夹爪的抓取 - #if self.feedStatus == FeedStatus.FTake: - #self.catch.catch_status = CatchStatus.CTake + # 这里他写的中间点过完马上进行抓料,和整个流程不符合我就注释掉了,因为实际测试的时候还没到FTake的点他就进行了继电器关闭夹爪的抓取 + # if self.feedStatus == FeedStatus.FTake: + # self.catch.catch_status = CatchStatus.CTake elif self.feedStatus == FeedStatus.FPhoto: if self.feedConfig.num == 0: self.log_signal.emit(logging.INFO, Constant.str_feed_finish) self.is_reverse = True self.feed_Mid_Status = FeedMidStatus.FMid_Take - self.feedConfig.feedLine.start2take_pos_index = len(self.feedConfig.feedLine.start_to_take) - 2 - self.feedConfig.feedLine.origin2start_pos_index = len(self.feedConfig.feedLine.origin_to_start) - 2 + self.feedConfig.feedLine.start2take_pos_index = ( + len(self.feedConfig.feedLine.start_to_take) - 2 + ) + self.feedConfig.feedLine.origin2start_pos_index = ( + len(self.feedConfig.feedLine.origin_to_start) - 2 + ) self.next_position(self.is_reverse) self.log_signal.emit(logging.INFO, Constant.str_feed_photo) self.init_detection_image() @@ -395,15 +455,19 @@ class Feeding(QObject): self.log_signal.emit(logging.INFO, Constant.str_feed_takePhoto) self.feed_Mid_Status = FeedMidStatus.FMid_Feed self.log_signal.emit(logging.INFO, Constant.str_feed_takePhoto_success) - self.feedConfig.feedLine.set_take_position(self.detect.detect_position, 0) - #self.feedConfig.feedLine.set_take_position(real_position, 0)#必须设置 - '''real_position''' + self.feedConfig.feedLine.set_take_position( + self.detect.detect_position, 0 + ) + # self.feedConfig.feedLine.set_take_position(real_position, 0)#必须设置 + """real_position""" # 一直等待传感器2信号,永不超时 while True: - sensors = self.relay_controller.get_all_device_status('sensors') + sensors = self.relay_controller.get_all_device_status("sensors") sensor2_value = sensors.get(self.relay_controller.SENSOR2, False) if sensor2_value: - self.log_signal.emit(logging.INFO, "✅ 传感器2检测到料包到位,开始执行抓取") + self.log_signal.emit( + logging.INFO, "✅ 传感器2检测到料包到位,开始执行抓取" + ) break # ✅ 条件满足,跳出循环,继续执行下面的代码 else: self.log_signal.emit(logging.INFO, "⏳ 等待传感器2料包信号...") @@ -411,7 +475,7 @@ class Feeding(QObject): self.next_position() self.log_signal.emit(logging.INFO, Constant.str_sys_runing2) - #self.feedStatus = FeedStatus.FTake + # self.feedStatus = FeedStatus.FTake elif self.feedStatus == FeedStatus.FTake: self.log_signal.emit(logging.INFO, Constant.str_feed_take) @@ -424,31 +488,33 @@ class Feeding(QObject): return self.log_signal.emit(logging.INFO, "🟢 机器人已到达抓料点位") # 执行抓取动作 - #self.catch.catch_status = CatchStatus.CTake - #if self.catch.catch_status == CatchStatus.CNone: - #self.catch.catch_status = CatchStatus.CTake - #if self.catch.catch_status == CatchStatus.CTake: - #self.log_signal.emit(logging.INFO, "正在执行抓料动作...") - #self.catch.catch_status = CatchStatus.COk - #if self.catch.catch_status == CatchStatus.COk: - #self.log_signal.emit(logging.INFO, Constant.str_feed_take_success) - #self.catch.catch_status = CatchStatus.CNone - # 移动到下一个抓取点 - # 更新丢包点: 如果需要根据放置情况调整下次抓取 + # self.catch.catch_status = CatchStatus.CTake + # if self.catch.catch_status == CatchStatus.CNone: + # self.catch.catch_status = CatchStatus.CTake + # if self.catch.catch_status == CatchStatus.CTake: + # self.log_signal.emit(logging.INFO, "正在执行抓料动作...") + # self.catch.catch_status = CatchStatus.COk + # if self.catch.catch_status == CatchStatus.COk: + # self.log_signal.emit(logging.INFO, Constant.str_feed_take_success) + # self.catch.catch_status = CatchStatus.CNone + # 移动到下一个抓取点 + # 更新丢包点: 如果需要根据放置情况调整下次抓取 next_drop_pos = self.drop_manager.get_next_drop_position(lineid=1) - print("next_drop_pos--------",next_drop_pos) + print("next_drop_pos--------", next_drop_pos) if next_drop_pos: self.feedConfig.feedLine.set_drop_position(next_drop_pos) - self.log_signal.emit(logging.INFO, f"已设置放置点: X={next_drop_pos.X:.2f}") + self.log_signal.emit( + logging.INFO, f"已设置放置点: X={next_drop_pos.X:.2f}" + ) else: self.log_signal.emit(logging.ERROR, "获取放置点失败") return - #self.feedConfig.feedLine.set_drop_position(real_position)#我想在这里读取我的一个ini文件值里面有很多个drop点,每一次索引递增的点 + # self.feedConfig.feedLine.set_drop_position(real_position)#我想在这里读取我的一个ini文件值里面有很多个drop点,每一次索引递增的点 self.relay_controller.open(clamp=True) self.next_position() - #return - #else: - #self.log_signal.emit(logging.ERROR, Constant.str_feed_takePhoto_fail) + # return + # else: + # self.log_signal.emit(logging.ERROR, Constant.str_feed_takePhoto_fail) elif self.feedStatus == FeedStatus.FBroken1: @@ -456,7 +522,6 @@ class Feeding(QObject): self.log_signal.emit(logging.INFO, Constant.str_feed_broken) self.next_position() - elif self.feedStatus == FeedStatus.FBroken2: if self.get_current_position().get_position().compare(real_position): @@ -464,7 +529,11 @@ class Feeding(QObject): self.next_position() elif self.feedStatus == FeedStatus.FShake: - if self.get_current_position().get_position().compare(real_position,is_action=True): + if ( + self.get_current_position() + .get_position() + .compare(real_position, is_action=True) + ): # TODO 震动方案 self.log_signal.emit(logging.INFO, Constant.str_feed_shake) if self.catch.catch_status == CatchStatus.CNone: @@ -477,21 +546,25 @@ class Feeding(QObject): # else: self.catch.shake_continue.SetReset() self.next_position() - if self.feedStatus!=FeedStatus.FShake: + if self.feedStatus != FeedStatus.FShake: self.catch.catch_status = CatchStatus.CNone return elif self.feedStatus == FeedStatus.FDropBag: - #"""*** 处理投料点 (FDropBag) 的核心逻辑 ***""" - # 1. 确认机械臂是否已精确到达当前目标投料点 - # get_current_position() 会根据 self.feed_Mid_Status (应为 FMid_Feed) - # 调用 feedLine.get_current_feed_position(),从 feeding_to_end 列表获取 - # 由 feeding2end_pos_index 指向的点。 - if self.get_current_position().get_position().compare(real_position, is_action=True): + # """*** 处理投料点 (FDropBag) 的核心逻辑 ***""" + # 1. 确认机械臂是否已精确到达当前目标投料点 + # get_current_position() 会根据 self.feed_Mid_Status (应为 FMid_Feed) + # 调用 feedLine.get_current_feed_position(),从 feeding_to_end 列表获取 + # 由 feeding2end_pos_index 指向的点。 + if ( + self.get_current_position() + .get_position() + .compare(real_position, is_action=True) + ): # 2. 记录日志:已到达投料点 - #if not self.sensor2_ready: - #self.log_signal.emit(logging.INFO, "抓取完成,重新启动 conveyor2") - #self.relay_controller.open(conveyor2=True) + # if not self.sensor2_ready: + # self.log_signal.emit(logging.INFO, "抓取完成,重新启动 conveyor2") + # self.relay_controller.open(conveyor2=True) self.log_signal.emit(logging.INFO, Constant.str_feed_drop) # 3. 与 Catch 模块进行状态交互来驱动投料动作 @@ -519,7 +592,10 @@ class Feeding(QObject): # self.feedConfig.feedLine.set_take_position(...) # 4. 更新业务逻辑:减少剩余袋数 self.feedConfig.num = self.feedConfig.num - 1 - self.log_signal.emit(logging.INFO, f'{Constant.str_feed_feed_num}{self.feedConfig.num}') + self.log_signal.emit( + logging.INFO, + f"{Constant.str_feed_feed_num}{self.feedConfig.num}", + ) # 5. *** 关键步骤 ***: 移动到路径中的下一个点 # next_position() 会根据当前的 feed_Mid_Status (FMid_Feed) # 调用 next_Feed()。 @@ -527,17 +603,20 @@ class Feeding(QObject): def run_reset(self): real_position = Real_Position() - real_position.init_position(self.robotClient.status_model.world_0, - self.robotClient.status_model.world_1, - self.robotClient.status_model.world_2, - self.robotClient.status_model.world_3, - self.robotClient.status_model.world_4, - self.robotClient.status_model.world_5) + real_position.init_position( + self.robotClient.status_model.world_0, + self.robotClient.status_model.world_1, + self.robotClient.status_model.world_2, + self.robotClient.status_model.world_3, + self.robotClient.status_model.world_4, + self.robotClient.status_model.world_5, + ) if self.reset_status == ResetStatus.RNone: return if self.reset_status == ResetStatus.RStart: - if self.feedConfig == None: return + if self.feedConfig == None: + return # for index in range(len(self.feedConfig.feedLine.positions)): # if self.feedConfig.feedLine.positions[index].status == 2: # start_index = index @@ -545,23 +624,36 @@ class Feeding(QObject): self.pos_near_index = -1 self.reversed_positions = [] for index, pos_model in enumerate(self.feedConfig.feedLine.feed_positions): - if pos_model.get_position().compare(real_position,is_action=True): + if pos_model.get_position().compare(real_position, is_action=True): self.pos_index = index break if self.pos_index == -1: - self.log_signal.emit(logging.ERROR, Constant.str_feed_return_original_position_fail) + self.log_signal.emit( + logging.ERROR, Constant.str_feed_return_original_position_fail + ) min_distance = 99999999 - for index, pos_model in enumerate(self.feedConfig.feedLine.feed_positions): - if get_distance(pos_model.get_position(), real_position) < min_distance: - min_distance = get_distance(pos_model.get_position(), real_position) + for index, pos_model in enumerate( + self.feedConfig.feedLine.feed_positions + ): + if ( + get_distance(pos_model.get_position(), real_position) + < min_distance + ): + min_distance = get_distance( + pos_model.get_position(), real_position + ) self.pos_near_index = index if self.pos_near_index != -1: - self.reversed_positions = self.feedConfig.feedLine.feed_positions[:self.pos_near_index + 1] + self.reversed_positions = self.feedConfig.feedLine.feed_positions[ + : self.pos_near_index + 1 + ] else: return False else: - self.reversed_positions = self.feedConfig.feedLine.feed_positions[:self.pos_index+1] + self.reversed_positions = self.feedConfig.feedLine.feed_positions[ + : self.pos_index + 1 + ] self.reversed_positions = list(reversed(self.reversed_positions)) self.reverse_index = 0 self.send_emergency_sound() @@ -570,21 +662,30 @@ class Feeding(QObject): self.reset_status = ResetStatus.RRunging if self.reset_status == ResetStatus.RRunging: - if not real_position.compare(self.current_position.get_position(),is_action=True): + if not real_position.compare( + self.current_position.get_position(), is_action=True + ): return pos_model = self.reversed_positions[self.reverse_index] if pos_model.status == FeedStatus.FTake.value: # 跳过取袋节点 pos_model = self.reversed_positions[self.reverse_index + 1] - self.reverse_index = self.reverse_index+1 + self.reverse_index = self.reverse_index + 1 if pos_model.lineType == LineType.CureMid.value: pos_model1 = self.reversed_positions[self.reverse_index + 1] - self.sendTargPosition(real_position=pos_model.get_position(), move_type=MoveType.Cure, - real_position1=pos_model1.get_position(), speed=self.robotClient.reset_speed) + self.sendTargPosition( + real_position=pos_model.get_position(), + move_type=MoveType.Cure, + real_position1=pos_model1.get_position(), + speed=self.robotClient.reset_speed, + ) self.current_position = pos_model1 self.reverse_index = self.reverse_index + 2 else: - self.sendTargPosition(real_position=pos_model.get_position(), speed=self.robotClient.reset_speed) + self.sendTargPosition( + real_position=pos_model.get_position(), + speed=self.robotClient.reset_speed, + ) self.current_position = pos_model self.reverse_index = self.reverse_index + 1 @@ -598,13 +699,16 @@ class Feeding(QObject): self.run_reverse = True real_position = Real_Position() - real_position.init_position(self.robotClient.status_model.world_0, - self.robotClient.status_model.world_1, - self.robotClient.status_model.world_2, - self.robotClient.status_model.world_3, - self.robotClient.status_model.world_4, - self.robotClient.status_model.world_5) - if self.feedConfig == None: return + real_position.init_position( + self.robotClient.status_model.world_0, + self.robotClient.status_model.world_1, + self.robotClient.status_model.world_2, + self.robotClient.status_model.world_3, + self.robotClient.status_model.world_4, + self.robotClient.status_model.world_5, + ) + if self.feedConfig == None: + return start_index = -1 # for index in range(len(self.feedConfig.feedLine.positions)): # if self.feedConfig.feedLine.positions[index].status == 2: @@ -618,14 +722,18 @@ class Feeding(QObject): break if pos_index == -1: - self.log_signal.emit(logging.ERROR, Constant.str_feed_return_original_position_fail) + self.log_signal.emit( + logging.ERROR, Constant.str_feed_return_original_position_fail + ) min_distance = 99999999 for index, pos_model in enumerate(self.feedConfig.feedLine.feed_positions): - if get_distance(pos_model.get_position(), real_position)= 10: status_byte = response[9] status_bin = f"{status_byte:08b}"[::-1] - bit_map = self.device_bit_map if command_type == 'devices' else self.sensor_bit_map - name_map = self.device_name_map if command_type == 'devices' else self.sensor_name_map + bit_map = ( + self.device_bit_map + if command_type == "devices" + else self.sensor_bit_map + ) + name_map = ( + self.device_name_map + if command_type == "devices" + else self.sensor_name_map + ) for key, bit_index in bit_map.items(): - status_dict[key] = status_bin[bit_index] == '1' + status_dict[key] = status_bin[bit_index] == "1" else: print(f"[{command_type}] 读取状态失败或响应无效") return status_dict - def get_all_sensor_responses(self, command_type='sensors'): + def get_all_sensor_responses(self, command_type="sensors"): """ 获取所有传感器的原始 Modbus 响应字符串 示例:{'sensor1': '00000000000401020101', 'sensor2': '00000000000401020100'} @@ -161,12 +194,12 @@ class RelayController: print(f"未知的读取类型: {command_type}") return {} - source = 'sensor' if command_type == 'sensors' else 'device' + source = "sensor" if command_type == "sensors" else "device" response = self.send_command(command, source=source) responses = {} if response and len(response) >= 10: - hex_response = binascii.hexlify(response).decode('utf-8') + hex_response = binascii.hexlify(response).decode("utf-8") print(f"[原始响应][{command_type}] {hex_response}") # 假设传感器数据从第 9 字节开始,长度为 2 字节 @@ -192,7 +225,7 @@ class RelayController: def is_valid_sensor_status(self, sensor_name): stable_count = 0 for _ in range(int(self.sensor_stable_duration / self.sensor1_loop_delay)): - responses = self.get_all_sensor_responses('sensors') + responses = self.get_all_sensor_responses("sensors") response = responses.get(sensor_name) if not response: @@ -211,7 +244,7 @@ class RelayController: def is_valid_sensor_status_1(self, sensor_name): stable_count = 0 for _ in range(int(self.sensor_stable_duration / self.sensor2_loop_delay)): - responses = self.get_all_sensor_responses('sensors') + responses = self.get_all_sensor_responses("sensors") response = responses.get(sensor_name) if not response: @@ -229,49 +262,57 @@ 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 + ): status = self.get_all_device_status() if conveyor1 and not status.get(self.CONVEYOR1, False): - self.send_command(self.valve_commands[self.CONVEYOR1]['open']) + 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']) + 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']) + 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']) + 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']) + self.send_command(self.valve_commands[self.PUSHER1]["open"]) time.sleep(self.delay_pusher) - 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 + ): if conveyor1: - self.send_command(self.valve_commands[self.CONVEYOR1]['close']) + self.send_command(self.valve_commands[self.CONVEYOR1]["close"]) time.sleep(self.delay_conveyor) if pusher: - self.send_command(self.valve_commands[self.PUSHER]['close']) + self.send_command(self.valve_commands[self.PUSHER]["close"]) time.sleep(self.delay_pusher) if conveyor2: - self.send_command(self.valve_commands[self.CONVEYOR2]['close']) + self.send_command(self.valve_commands[self.CONVEYOR2]["close"]) time.sleep(self.delay_conveyor) if clamp: - self.send_command(self.valve_commands[self.CLAMP]['close']) + self.send_command(self.valve_commands[self.CLAMP]["close"]) time.sleep(self.delay_clamp) if pusher1: - self.send_command(self.valve_commands[self.PUSHER1]['close']) + self.send_command(self.valve_commands[self.PUSHER1]["close"]) time.sleep(self.delay_pusher) # ===================== 传感器处理线程 ===================== def handle_sensor1(self): + print("传感器线程1启动!!!") while self._running: try: if self.is_valid_sensor_status(self.SENSOR1): current_time = time.time() - if not self.sensor1_triggered and ( - current_time - self.sensor1_last_time) > self.sensor1_debounce_time: + if ( + not self.sensor1_triggered + and (current_time - self.sensor1_last_time) + > self.sensor1_debounce_time + ): self.sensor1_triggered = True self.sensor1_last_time = current_time # 1.停止包装机皮带电机:关闭 conveyor1 @@ -293,7 +334,7 @@ class RelayController: time.sleep(1) # 5. 状态检查(可选) status = self.get_all_device_status() - if status.get('conveyor1') and not status.get('pusher'): + if status.get("conveyor1") and not status.get("pusher"): print("流程完成1:皮带运行中,推板已收回") else: print("状态异常,请检查设备") @@ -306,6 +347,7 @@ class RelayController: time.sleep(self.sensor1_error_delay) def handle_sensor2(self): + print("传感器线程12启动!!!") while self._running: try: if self.is_valid_sensor_status_1(self.SENSOR2): @@ -357,7 +399,7 @@ class RelayController: self._sensor1_thread.start() -if __name__ == '__main__': +if __name__ == "__main__": controller = RelayController() controller.start() try: diff --git a/main.py b/main.py index bdd9d3c..e7c7181 100644 --- a/main.py +++ b/main.py @@ -16,10 +16,33 @@ import numpy as np from PyQt5.uic.properties import QtWidgets from PySide6 import QtCore from PySide6.QtCore import QThread, Signal, Slot, QObject, QEvent, QTimer -from PySide6.QtGui import QIntValidator, QStandardItemModel, QStandardItem, Qt, QMovie, QIcon, QCursor, QColor, \ - QTextCursor -from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QHeaderView, QTableWidget, \ - QTableWidgetItem, QWidget, QHBoxLayout, QAbstractItemView, QMessageBox, QSizePolicy, QComboBox, QMenu +from PySide6.QtGui import ( + QIntValidator, + QStandardItemModel, + QStandardItem, + Qt, + QMovie, + QIcon, + QCursor, + QColor, + QTextCursor, +) +from PySide6.QtWidgets import ( + QApplication, + QMainWindow, + QPushButton, + QLabel, + QHeaderView, + QTableWidget, + QTableWidgetItem, + QWidget, + QHBoxLayout, + QAbstractItemView, + QMessageBox, + QSizePolicy, + QComboBox, + QMenu, +) from datetime import datetime from click import clear @@ -29,10 +52,18 @@ from Model.FeedModel import LineModel, PositionModel from Util.util_ini import writeFeedLine_to_ini import Constant from CU.Command import FeedCommand -from CU.Feeding import FeedLine, FeedingConfig, Feeding, FeedStatus, FeedMidStatus, ResetStatus +from CU.Feeding import ( + FeedLine, + FeedingConfig, + Feeding, + FeedStatus, + FeedMidStatus, + ResetStatus, +) from Util.util_log import QTextEditLogger from Util.util_time import CRisOrFall -#from Vision.camera_coordinate_dete import Detection + +# from Vision.camera_coordinate_dete import Detection from COM.COM_Robot import RobotClient from Expection import Error_Code from queue import Queue @@ -43,29 +74,31 @@ from Model.Position import Real_Position, Detection_Position from threading import Thread from CU.Command import Status from Util.util_log import log -#from Vision.detect_person import DetectionPerson + +# from Vision.detect_person import DetectionPerson from ui_MainWin import Ui_MainWindow from view.ResetView import StopDialog from EMV.EMV import RelayController -#from view.ResetView import StopDialog +# from view.ResetView import StopDialog class MainWindow(QMainWindow, Ui_MainWindow): updateUI_seting = Signal() + def __init__(self): super(MainWindow, self).__init__() self.setupUi(self) - #传感器继电器加入变量 + # 传感器继电器加入变量 self.relay_controller = RelayController() self.sensor_thread = None - self.sensor2_thread = None + self.sensor2_thread = None self.last_status_printed = None self.last_pause_printed = None - #传感器加入变量结束 - #self.setWindowFlags(Qt.WindowType.FramelessWindowHint) + # 传感器加入变量结束 + # self.setWindowFlags(Qt.WindowType.FramelessWindowHint) self.thread_signal = True self.init_qss() self.init_UI() @@ -79,128 +112,136 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.init_table_position() self.table_line_changed = False self.table_position_changed = False - self.selected_line_section = '' + self.selected_line_section = "" def init_IOPanel(self): self.dirt_io_points = {} - self.dirt_io_points.setdefault('y1', self.widget_y1) - self.dirt_io_points.setdefault('y2', self.widget_y2) - self.dirt_io_points.setdefault('y3', self.widget_y3) - self.dirt_io_points.setdefault('y4', self.widget_y4) - self.dirt_io_points.setdefault('y5', self.widget_y5) - self.dirt_io_points.setdefault('y6', self.widget_y6) - self.dirt_io_points.setdefault('y7', self.widget_y7) - self.dirt_io_points.setdefault('y8', self.widget_y8) - self.dirt_io_points.setdefault('y9', self.widget_y9) - self.dirt_io_points.setdefault('y10', self.widget_y10) - self.dirt_io_points.setdefault('y11', self.widget_y11) - self.dirt_io_points.setdefault('y12', self.widget_y12) - self.dirt_io_points.setdefault('y13', self.widget_y13) - self.dirt_io_points.setdefault('y14', self.widget_y14) - self.dirt_io_points.setdefault('y15', self.widget_y15) - self.dirt_io_points.setdefault('y16', self.widget_y16) - self.dirt_io_points.setdefault('y17', self.widget_y17) - self.dirt_io_points.setdefault('y18', self.widget_y18) - self.dirt_io_points.setdefault('y19', self.widget_y19) - self.dirt_io_points.setdefault('y20', self.widget_y20) - self.dirt_io_points.setdefault('y21', self.widget_y21) - self.dirt_io_points.setdefault('y22', self.widget_y22) - self.dirt_io_points.setdefault('y23', self.widget_y23) - self.dirt_io_points.setdefault('y24', self.widget_y24) - self.dirt_io_points.setdefault('y25', self.widget_y25) - self.dirt_io_points.setdefault('y26', self.widget_y26) - self.dirt_io_points.setdefault('y27', self.widget_y27) - self.dirt_io_points.setdefault('y28', self.widget_y28) - self.dirt_io_points.setdefault('y29', self.widget_y29) - self.dirt_io_points.setdefault('y30', self.widget_y30) - self.dirt_io_points.setdefault('y31', self.widget_y31) - self.dirt_io_points.setdefault('y32', self.widget_y32) + self.dirt_io_points.setdefault("y1", self.widget_y1) + self.dirt_io_points.setdefault("y2", self.widget_y2) + self.dirt_io_points.setdefault("y3", self.widget_y3) + self.dirt_io_points.setdefault("y4", self.widget_y4) + self.dirt_io_points.setdefault("y5", self.widget_y5) + self.dirt_io_points.setdefault("y6", self.widget_y6) + self.dirt_io_points.setdefault("y7", self.widget_y7) + self.dirt_io_points.setdefault("y8", self.widget_y8) + self.dirt_io_points.setdefault("y9", self.widget_y9) + self.dirt_io_points.setdefault("y10", self.widget_y10) + self.dirt_io_points.setdefault("y11", self.widget_y11) + self.dirt_io_points.setdefault("y12", self.widget_y12) + self.dirt_io_points.setdefault("y13", self.widget_y13) + self.dirt_io_points.setdefault("y14", self.widget_y14) + self.dirt_io_points.setdefault("y15", self.widget_y15) + self.dirt_io_points.setdefault("y16", self.widget_y16) + self.dirt_io_points.setdefault("y17", self.widget_y17) + self.dirt_io_points.setdefault("y18", self.widget_y18) + self.dirt_io_points.setdefault("y19", self.widget_y19) + self.dirt_io_points.setdefault("y20", self.widget_y20) + self.dirt_io_points.setdefault("y21", self.widget_y21) + self.dirt_io_points.setdefault("y22", self.widget_y22) + self.dirt_io_points.setdefault("y23", self.widget_y23) + self.dirt_io_points.setdefault("y24", self.widget_y24) + self.dirt_io_points.setdefault("y25", self.widget_y25) + self.dirt_io_points.setdefault("y26", self.widget_y26) + self.dirt_io_points.setdefault("y27", self.widget_y27) + self.dirt_io_points.setdefault("y28", self.widget_y28) + self.dirt_io_points.setdefault("y29", self.widget_y29) + self.dirt_io_points.setdefault("y30", self.widget_y30) + self.dirt_io_points.setdefault("y31", self.widget_y31) + self.dirt_io_points.setdefault("y32", self.widget_y32) self.widget_y1.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y1.index=1 + self.widget_y1.index = 1 self.widget_y2.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y2.index=2 + self.widget_y2.index = 2 self.widget_y3.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y3.index=3 + self.widget_y3.index = 3 self.widget_y4.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y4.index=4 + self.widget_y4.index = 4 self.widget_y5.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y5.index=5 + self.widget_y5.index = 5 self.widget_y6.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y6.index=6 + self.widget_y6.index = 6 self.widget_y7.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y7.index=7 + self.widget_y7.index = 7 self.widget_y8.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y8.index=8 + self.widget_y8.index = 8 self.widget_y9.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y9.index=9 + self.widget_y9.index = 9 self.widget_y10.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y10.index=10 + self.widget_y10.index = 10 self.widget_y11.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y11.index=11 + self.widget_y11.index = 11 self.widget_y12.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y12.index=12 + self.widget_y12.index = 12 self.widget_y13.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y13.index=13 + self.widget_y13.index = 13 self.widget_y14.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y14.index=14 + self.widget_y14.index = 14 self.widget_y15.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y15.index=15 + self.widget_y15.index = 15 self.widget_y16.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y16.index=16 + self.widget_y16.index = 16 self.widget_y17.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y17.index=17 + self.widget_y17.index = 17 self.widget_y18.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y18.index=18 + self.widget_y18.index = 18 self.widget_y19.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y19.index=19 + self.widget_y19.index = 19 self.widget_y20.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y20.index=20 + self.widget_y20.index = 20 self.widget_y21.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y21.index=21 + self.widget_y21.index = 21 self.widget_y22.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y22.index=22 + self.widget_y22.index = 22 self.widget_y23.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y23.index=23 + self.widget_y23.index = 23 self.widget_y24.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y24.index=24 + self.widget_y24.index = 24 self.widget_y25.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y25.index=25 + self.widget_y25.index = 25 self.widget_y26.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y26.index=26 + self.widget_y26.index = 26 self.widget_y27.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y27.index=27 + self.widget_y27.index = 27 self.widget_y28.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y28.index=28 + self.widget_y28.index = 28 self.widget_y29.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y29.index=29 + self.widget_y29.index = 29 self.widget_y30.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y30.index=30 + self.widget_y30.index = 30 self.widget_y31.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y31.index=31 + self.widget_y31.index = 31 self.widget_y32.clicked.connect(self.send_IO_Point_OpenOrClose) - self.widget_y32.index=32 + self.widget_y32.index = 32 # region 战线,点位表格初始化 def init_table_lines(self): self.tableWidget_lines.setColumnCount(4) - self.tableWidget_lines.horizontalHeader().setStyleSheet(f"background-color: {QColor(44,44,44).name()};") - self.tableWidget_lines.verticalHeader().setStyleSheet(f"background-color: {QColor(44,44,44).name()};") + self.tableWidget_lines.horizontalHeader().setStyleSheet( + f"background-color: {QColor(44,44,44).name()};" + ) + self.tableWidget_lines.verticalHeader().setStyleSheet( + f"background-color: {QColor(44,44,44).name()};" + ) self.tableWidget_lines.verticalHeader().hide() self.tableWidget_lines.setHorizontalHeaderLabels(["节名", "线名", "id", "操作"]) self.tableWidget_lines.setSelectionBehavior(QTableWidget.SelectRows) self.tableWidget_lines.setAutoScroll(True) - self.tableWidget_lines. setColumnHidden(0,True) - self.tableWidget_lines. setColumnHidden(2,True) + self.tableWidget_lines.setColumnHidden(0, True) + self.tableWidget_lines.setColumnHidden(2, True) # 自动调整列宽 header = self.tableWidget_lines.horizontalHeader() header.setSectionResizeMode(0, PySide6.QtWidgets.QHeaderView.ResizeMode.Stretch) header.setSectionResizeMode(1, PySide6.QtWidgets.QHeaderView.ResizeMode.Stretch) header.setSectionResizeMode(2, PySide6.QtWidgets.QHeaderView.ResizeMode.Stretch) - self.tableWidget_lines.setContextMenuPolicy(PySide6.QtCore.Qt.ContextMenuPolicy.CustomContextMenu) - self.tableWidget_lines.customContextMenuRequested.connect(self.show_lines_context_menu) + self.tableWidget_lines.setContextMenuPolicy( + PySide6.QtCore.Qt.ContextMenuPolicy.CustomContextMenu + ) + self.tableWidget_lines.customContextMenuRequested.connect( + self.show_lines_context_menu + ) self.tableWidget_lines.cellChanged.connect(self.send_table_lines_cell_changed) self.init_table_lines_data() @@ -209,77 +250,120 @@ class MainWindow(QMainWindow, Ui_MainWindow): def send_table_line_position_cell_changed(self): self.table_position_changed = True + def send_table_lines_cell_changed(self): self.table_line_changed = True + def send_save_lines_data(self): if self.table_position_changed or self.table_line_changed: section = self.selected_line_section for row_i in range(self.tableWidget_lines.rowCount()): if self.tableWidget_lines.item(row_i, 0).text() == section: line_model = LineModel() - if len(self.get_table_positions()) == 0 and self.feedLine_dict.keys().__contains__(section): + if len( + self.get_table_positions() + ) == 0 and self.feedLine_dict.keys().__contains__(section): line_model = self.feedLine_dict[section] line_model.name = self.tableWidget_lines.item(row_i, 1).text() else: line_model.section = section line_model.name = self.tableWidget_lines.item(row_i, 1).text() - line_model.id = int(self.tableWidget_lines.item(row_i, 2).text()) + line_model.id = int( + self.tableWidget_lines.item(row_i, 2).text() + ) line_model.positions = self.get_table_positions() config_reader = configparser.ConfigParser() - config_reader.read(Constant.feedLine_set_file, encoding='utf-8') - if len(self.get_table_positions()) != 0 and line_model.id != self.get_table_positions()[0].lineId: + config_reader.read(Constant.feedLine_set_file, encoding="utf-8") + if ( + len(self.get_table_positions()) != 0 + and line_model.id != self.get_table_positions()[0].lineId + ): return line_model.save_line_model(config_reader) - config_reader.write(open(Constant.feedLine_set_file, 'w', encoding='utf-8')) + config_reader.write( + open(Constant.feedLine_set_file, "w", encoding="utf-8") + ) self.init_FeedLine() break self.init_table_lines_data() self.table_line_changed = False self.table_position_changed = False + def get_table_positions(self): position_models = [] for row_i in range(self.tableWidget_line_positions.rowCount()): section = self.tableWidget_line_positions.item(row_i, 0).text() position_model = PositionModel(index=row_i) position_model.section = section - position_model.X = float(self.tableWidget_line_positions.item(row_i, 1).text()) - position_model.Y = float(self.tableWidget_line_positions.item(row_i, 2).text()) - position_model.Z = float(self.tableWidget_line_positions.item(row_i, 3).text()) - position_model.U = float(self.tableWidget_line_positions.item(row_i, 4).text()) - position_model.V = float(self.tableWidget_line_positions.item(row_i, 5).text()) - position_model.W = float(self.tableWidget_line_positions.item(row_i, 6).text()) + position_model.X = float( + self.tableWidget_line_positions.item(row_i, 1).text() + ) + position_model.Y = float( + self.tableWidget_line_positions.item(row_i, 2).text() + ) + position_model.Z = float( + self.tableWidget_line_positions.item(row_i, 3).text() + ) + position_model.U = float( + self.tableWidget_line_positions.item(row_i, 4).text() + ) + position_model.V = float( + self.tableWidget_line_positions.item(row_i, 5).text() + ) + position_model.W = float( + self.tableWidget_line_positions.item(row_i, 6).text() + ) combox_line_type = self.tableWidget_line_positions.cellWidget(row_i, 7) position_model.lineType = combox_line_type.currentData() combox_status = self.tableWidget_line_positions.cellWidget(row_i, 8) position_model.status = combox_status.currentData() - position_model.id = int(self.tableWidget_line_positions.item(row_i, 9).text()) + position_model.id = int( + self.tableWidget_line_positions.item(row_i, 9).text() + ) position_model.order = row_i - position_model.lineId = int(self.tableWidget_line_positions.item(row_i, 11).text()) + position_model.lineId = int( + self.tableWidget_line_positions.item(row_i, 11).text() + ) position_models.append(position_model) return position_models + def init_table_lines_data(self): # self.tableWidget_lines.setItem(0, 0, QTableWidgetItem("线1")) # self.tableWidget_lines.setItem(1, 0, QTableWidgetItem("线2")) # self.tableWidget_lines.setItem() self.tableWidget_lines.blockSignals(True) - self.selected_line_section = '' + self.selected_line_section = "" self.table_line_changed = False self.table_position_changed = False self.tableWidget_lines.setRowCount(0) for key, value in self.feedLine_dict.items(): self.tableWidget_lines.insertRow(self.tableWidget_lines.rowCount()) - self.tableWidget_lines.setItem(self.tableWidget_lines.rowCount()-1, 0, QTableWidgetItem(key)) - self.tableWidget_lines.setItem(self.tableWidget_lines.rowCount()-1, 1, QTableWidgetItem(value.name)) - self.tableWidget_lines.setItem(self.tableWidget_lines.rowCount()-1, 2, QTableWidgetItem(str(value.id))) + self.tableWidget_lines.setItem( + self.tableWidget_lines.rowCount() - 1, 0, QTableWidgetItem(key) + ) + self.tableWidget_lines.setItem( + self.tableWidget_lines.rowCount() - 1, 1, QTableWidgetItem(value.name) + ) + self.tableWidget_lines.setItem( + self.tableWidget_lines.rowCount() - 1, + 2, + QTableWidgetItem(str(value.id)), + ) show_pos_btn = QPushButton("显示路径") show_pos_btn.clicked.connect( - lambda _, index=self.tableWidget_lines.rowCount()-1: self.show_lines_pos_data(index)) - self.tableWidget_lines.setCellWidget(self.tableWidget_lines.rowCount() - 1, 3, show_pos_btn) + lambda _, index=self.tableWidget_lines.rowCount() - 1: self.show_lines_pos_data( + index + ) + ) + self.tableWidget_lines.setCellWidget( + self.tableWidget_lines.rowCount() - 1, 3, show_pos_btn + ) - self.selected_line_section = '' + self.selected_line_section = "" self.tableWidget_line_positions.setRowCount(0) self.tableWidget_lines.blockSignals(False) + def show_lines_context_menu(self): menu = QMenu(self) action_add = menu.addAction("添加投料线") @@ -289,33 +373,53 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.add_new_line() elif action == action_del: self.del_line() + def del_line(self): - if self.tableWidget_lines.rowCount()==0: + if self.tableWidget_lines.rowCount() == 0: return - if self.tableWidget_lines.currentRow()==-1: + if self.tableWidget_lines.currentRow() == -1: return self.tableWidget_lines.removeRow(self.tableWidget_lines.currentRow()) self.table_line_changed = True def add_new_line(self): - add_section = f'{Constant.feedLine_set_section}{1}' - for i in range(1,Constant.MAX_Line_num): + add_section = f"{Constant.feedLine_set_section}{1}" + for i in range(1, Constant.MAX_Line_num): if not self.feedLine_dict.get(f"{Constant.feedLine_set_section}{i}"): add_section = f"{Constant.feedLine_set_section}{i}" break self.tableWidget_lines.insertRow(self.tableWidget_lines.rowCount()) - self.tableWidget_lines.setItem(self.tableWidget_lines.rowCount()-1, 0, QTableWidgetItem(add_section)) - self.tableWidget_lines.setItem(self.tableWidget_lines.rowCount()-1, 1, QTableWidgetItem("线" + str(self.tableWidget_lines.rowCount()))) - self.tableWidget_lines.setItem(self.tableWidget_lines.rowCount()-1, 2, QTableWidgetItem(str(i))) + self.tableWidget_lines.setItem( + self.tableWidget_lines.rowCount() - 1, 0, QTableWidgetItem(add_section) + ) + self.tableWidget_lines.setItem( + self.tableWidget_lines.rowCount() - 1, + 1, + QTableWidgetItem("线" + str(self.tableWidget_lines.rowCount())), + ) + self.tableWidget_lines.setItem( + self.tableWidget_lines.rowCount() - 1, 2, QTableWidgetItem(str(i)) + ) show_pos_btn = QPushButton("显示路径") - show_pos_btn.clicked.connect(lambda _, index=self.tableWidget_lines.rowCount()-1: self.show_lines_pos_data(index)) - self.tableWidget_lines.setCellWidget(self.tableWidget_lines.rowCount()-1, 3,show_pos_btn) + show_pos_btn.clicked.connect( + lambda _, index=self.tableWidget_lines.rowCount() - 1: self.show_lines_pos_data( + index + ) + ) + self.tableWidget_lines.setCellWidget( + self.tableWidget_lines.rowCount() - 1, 3, show_pos_btn + ) self.table_position_changed = True self.feedLine_dict[add_section] = LineModel(i) - def show_lines_pos_data(self,line_index): - if self.selected_line_section!='' and self.selected_line_section != self.tableWidget_lines.item(line_index,0).text(): + + def show_lines_pos_data(self, line_index): + if ( + self.selected_line_section != "" + and self.selected_line_section + != self.tableWidget_lines.item(line_index, 0).text() + ): if self.table_position_changed or self.table_line_changed: msgBox = QMessageBox() msgBox.setText("已修改的数据,是否保存?") @@ -329,39 +433,71 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.init_table_lines_data() self.table_line_changed = False self.table_position_changed = False - line_name = self.tableWidget_lines.item(line_index,0).text() + line_name = self.tableWidget_lines.item(line_index, 0).text() line_model = self.feedLine_dict.get(line_name) self.init_table_positions_data(line_model.positions) self.selected_line_section = line_name self.label_table_line_position_title.setText(line_model.name) def init_table_position(self): - #self.tableWidget_line_positions.setRowCount(5) + # self.tableWidget_line_positions.setRowCount(5) self.tableWidget_line_positions.setColumnCount(14) - self.tableWidget_line_positions.setHorizontalHeaderLabels(["节段","X","Y","Z","U","V","W","运动类型","点位类型","序号","排序","线号","操作1","操作2"]) + self.tableWidget_line_positions.setHorizontalHeaderLabels( + [ + "节段", + "X", + "Y", + "Z", + "U", + "V", + "W", + "运动类型", + "点位类型", + "序号", + "排序", + "线号", + "操作1", + "操作2", + ] + ) self.tableWidget_line_positions.setSelectionBehavior(QTableWidget.SelectRows) self.tableWidget_line_positions.setColumnHidden(0, True) - self.tableWidget_line_positions.setColumnHidden(9,True) + self.tableWidget_line_positions.setColumnHidden(9, True) self.tableWidget_line_positions.setColumnHidden(10, True) self.tableWidget_line_positions.setColumnHidden(11, True) self.tableWidget_line_positions.setAutoScroll(True) self.tableWidget_line_positions.verticalHeader().hide() # 自动调整列宽 header = self.tableWidget_line_positions.horizontalHeader() - header.setSectionResizeMode(0, PySide6.QtWidgets.QHeaderView.ResizeMode.Interactive) - self.tableWidget_line_positions.setHorizontalScrollBarPolicy(PySide6.QtCore.Qt.ScrollBarPolicy.ScrollBarAsNeeded) + header.setSectionResizeMode( + 0, PySide6.QtWidgets.QHeaderView.ResizeMode.Interactive + ) + self.tableWidget_line_positions.setHorizontalScrollBarPolicy( + PySide6.QtCore.Qt.ScrollBarPolicy.ScrollBarAsNeeded + ) for i in range(14): - header.setSectionResizeMode(i, PySide6.QtWidgets.QHeaderView.ResizeMode.Interactive) - self.tableWidget_line_positions.setContextMenuPolicy(PySide6.QtCore.Qt.ContextMenuPolicy.CustomContextMenu) - self.tableWidget_line_positions.customContextMenuRequested.connect(self.show_positions_context_menu) - self.tableWidget_line_positions.cellChanged.connect(self.send_table_line_position_cell_changed) - def init_table_positions_data(self,position_list): + header.setSectionResizeMode( + i, PySide6.QtWidgets.QHeaderView.ResizeMode.Interactive + ) + self.tableWidget_line_positions.setContextMenuPolicy( + PySide6.QtCore.Qt.ContextMenuPolicy.CustomContextMenu + ) + self.tableWidget_line_positions.customContextMenuRequested.connect( + self.show_positions_context_menu + ) + self.tableWidget_line_positions.cellChanged.connect( + self.send_table_line_position_cell_changed + ) + + def init_table_positions_data(self, position_list): self.tableWidget_line_positions.blockSignals(True) self.tableWidget_line_positions.setRowCount(0) - for index,position_model in enumerate(position_list): - self.tableWidget_line_positions.insertRow(self.tableWidget_line_positions.rowCount()) - self.set_positionModel_to_tabel(index,position_model) + for index, position_model in enumerate(position_list): + self.tableWidget_line_positions.insertRow( + self.tableWidget_line_positions.rowCount() + ) + self.set_positionModel_to_tabel(index, position_model) self.tableWidget_line_positions.blockSignals(False) def show_positions_context_menu(self): @@ -375,61 +511,68 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.del_position() def del_position(self): - if self.tableWidget_line_positions.rowCount()==0: + if self.tableWidget_line_positions.rowCount() == 0: return - if self.tableWidget_line_positions.currentRow()==-1: + if self.tableWidget_line_positions.currentRow() == -1: return - section = self.tableWidget_line_positions.item(self.tableWidget_line_positions.currentRow(),0).text() + section = self.tableWidget_line_positions.item( + self.tableWidget_line_positions.currentRow(), 0 + ).text() - id = int(self.tableWidget_line_positions.item(self.tableWidget_line_positions.currentRow(), 9).text()) + id = int( + self.tableWidget_line_positions.item( + self.tableWidget_line_positions.currentRow(), 9 + ).text() + ) line_model = self.feedLine_dict.get(self.selected_line_section) for pos_model in line_model.positions: if pos_model.id == id: line_model.positions.remove(pos_model) break - self.tableWidget_line_positions.removeRow(self.tableWidget_line_positions.currentRow()) + self.tableWidget_line_positions.removeRow( + self.tableWidget_line_positions.currentRow() + ) config_writer = configparser.ConfigParser() - config_writer.read(Constant.feedLine_set_file, encoding = 'utf-8') + config_writer.read(Constant.feedLine_set_file, encoding="utf-8") config_writer.remove_section(section) - config_writer.write(open(Constant.feedLine_set_file,'w',encoding='utf-8')) - + config_writer.write(open(Constant.feedLine_set_file, "w", encoding="utf-8")) self.table_position_changed = True - def add_new_position(self):# 选中添加 - if self.selected_line_section == '': return + def add_new_position(self): # 选中添加 + if self.selected_line_section == "": + return config_reader = configparser.ConfigParser() - config_reader.read(Constant.feedLine_set_file, encoding = 'utf-8') + config_reader.read(Constant.feedLine_set_file, encoding="utf-8") position_model = PositionModel(999) ids = [] for j in range(self.tableWidget_line_positions.rowCount()): ids.append(int(self.tableWidget_line_positions.item(j, 9).text())) - for i in range(1,Constant.MAX_Position_num): + for i in range(1, Constant.MAX_Position_num): if not config_reader.has_section(f"{Constant.position_set_section}{i}"): if ids.__contains__(i): continue position_model = PositionModel(i) break - if self.tableWidget_line_positions.currentRow()==-1: + if self.tableWidget_line_positions.currentRow() == -1: row_i = self.tableWidget_line_positions.rowCount() else: row_i = self.tableWidget_line_positions.currentRow() - # self.tableWidget_line_positions.setRowCount(self.tableWidget_line_positions.rowCount()+1) - self.tableWidget_line_positions.insertRow(row_i+1) + # self.tableWidget_line_positions.setRowCount(self.tableWidget_line_positions.rowCount()+1) + self.tableWidget_line_positions.insertRow(row_i + 1) position_model.order = 0 position_model.status = 1 line_model = self.feedLine_dict.get(self.selected_line_section) line_id = line_model.id position_model.lineId = line_id - self.set_positionModel_to_tabel(row_i+1,position_model) + self.set_positionModel_to_tabel(row_i + 1, position_model) self.table_line_changed = True - - def tabel_move_position(self,row_i): - if self.tableWidget_line_positions.currentRow()==-1: + def tabel_move_position(self, row_i): + if self.tableWidget_line_positions.currentRow() == -1: QMessageBox.information(self, "提示", Constant.str_sys_set_position_error) return @@ -444,29 +587,71 @@ class MainWindow(QMainWindow, Ui_MainWindow): float(self.tableWidget_line_positions.item(row_i, 5).text()), float(self.tableWidget_line_positions.item(row_i, 6).text()), ) - if lineType == 0: #直线 + if lineType == 0: # 直线 if row_i != 0: - combox_line_type_inner = self.tableWidget_line_positions.cellWidget(row_i-1, 7) + combox_line_type_inner = self.tableWidget_line_positions.cellWidget( + row_i - 1, 7 + ) lineType_inner = combox_line_type_inner.currentData() if lineType_inner == 2: p2 = Real_Position().init_position( - float(self.tableWidget_line_positions.item(row_i-1, 1).text()), - float(self.tableWidget_line_positions.item(row_i-1, 2).text()), - float(self.tableWidget_line_positions.item(row_i-1, 3).text()), - float(self.tableWidget_line_positions.item(row_i-1, 4).text()), - float(self.tableWidget_line_positions.item(row_i-1, 5).text()), - float(self.tableWidget_line_positions.item(row_i-1, 6).text()), + float( + self.tableWidget_line_positions.item(row_i - 1, 1).text() + ), + float( + self.tableWidget_line_positions.item(row_i - 1, 2).text() + ), + float( + self.tableWidget_line_positions.item(row_i - 1, 3).text() + ), + float( + self.tableWidget_line_positions.item(row_i - 1, 4).text() + ), + float( + self.tableWidget_line_positions.item(row_i - 1, 5).text() + ), + float( + self.tableWidget_line_positions.item(row_i - 1, 6).text() + ), + ) + self.send_position_command( + x1=p1.X, + x2=p1.Y, + x3=p1.Z, + x4=p1.U, + x5=p1.V, + x6=p1.W, + move_type=MoveType.Cure, + speed=self.robotClient.debug_speed, + p2=p2, ) - self.send_position_command(x1=p1.X, x2=p1.Y, x3=p1.Z, x4=p1.U, x5=p1.V, x6=p1.W, move_type=MoveType.Cure, speed=self.robotClient.debug_speed, p2=p2) return - self.send_position_command(x1=p1.X, x2=p1.Y, x3=p1.Z, x4=p1.U, x5=p1.V, x6=p1.W, move_type=MoveType.WORLD,speed=self.robotClient.debug_speed) + self.send_position_command( + x1=p1.X, + x2=p1.Y, + x3=p1.Z, + x4=p1.U, + x5=p1.V, + x6=p1.W, + move_type=MoveType.WORLD, + speed=self.robotClient.debug_speed, + ) return - if lineType == 4: # 自由路径 - self.send_position_command(x1=p1.X, x2=p1.Y, x3=p1.Z, x4=p1.U, x5=p1.V, x6=p1.W, move_type=MoveType.AXIS,speed=self.robotClient.debug_speed) + if lineType == 4: # 自由路径 + self.send_position_command( + x1=p1.X, + x2=p1.Y, + x3=p1.Z, + x4=p1.U, + x5=p1.V, + x6=p1.W, + move_type=MoveType.AXIS, + speed=self.robotClient.debug_speed, + ) return - def tabel_get_position(self,row_i): - if self.tableWidget_line_positions.currentRow()==-1: + def tabel_get_position(self, row_i): + if self.tableWidget_line_positions.currentRow() == -1: QMessageBox.information(self, "提示", Constant.str_sys_set_position_error) return @@ -479,64 +664,90 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.set_position_tabel(row_i, self.status_address.getRealPosition()) self.table_line_changed = True - def tabel_delete_position(self,row_i): + def tabel_delete_position(self, row_i): self.tableWidget_line_positions.removeRow(row_i) self.table_line_changed = True - def set_positionModel_to_tabel(self,row_i,position_model:PositionModel): - self.tableWidget_line_positions.setItem(row_i,0,QTableWidgetItem(str(position_model.section))) - self.set_position_tabel(row_i,position_model.get_position()) + def set_positionModel_to_tabel(self, row_i, position_model: PositionModel): + self.tableWidget_line_positions.setItem( + row_i, 0, QTableWidgetItem(str(position_model.section)) + ) + self.set_position_tabel(row_i, position_model.get_position()) combox_lineType = QComboBox() - combox_lineType.addItem("直线",0) - combox_lineType.addItem("曲线中间点",2) - combox_lineType.addItem("曲线终点",3) - combox_lineType.addItem("自由路径",4) - combox_lineType.setCurrentIndex(combox_lineType.findData(position_model.lineType)) - combox_lineType.currentIndexChanged.connect(self.send_table_position_status_changed) + combox_lineType.addItem("直线", 0) + combox_lineType.addItem("曲线中间点", 2) + combox_lineType.addItem("曲线终点", 3) + combox_lineType.addItem("自由路径", 4) + combox_lineType.setCurrentIndex( + combox_lineType.findData(position_model.lineType) + ) + combox_lineType.currentIndexChanged.connect( + self.send_table_position_status_changed + ) self.tableWidget_line_positions.setCellWidget(row_i, 7, combox_lineType) combox = QComboBox() combox.addItem("初始化点", 2) combox.addItem("中间点", 3) - combox.addItem("相机/待抓点",4) + combox.addItem("相机/待抓点", 4) combox.addItem("抓取前点", 3) - combox.addItem("抓取",5) + combox.addItem("抓取", 5) combox.addItem("抓取后点", 3) - combox.addItem("破带点1",6) - combox.addItem("破带点2",7) - combox.addItem("震动点",8) - combox.addItem("扔带点",9) + combox.addItem("破带点1", 6) + combox.addItem("破带点2", 7) + combox.addItem("震动点", 8) + combox.addItem("扔带点", 9) combox.setCurrentIndex(combox.findData(position_model.status)) combox.currentIndexChanged.connect(self.send_table_position_status_changed) self.tableWidget_line_positions.setCellWidget(row_i, 8, combox) - #self.tableWidget_line_positions.setItem(row_i, 7, QTableWidgetItem(str(position_model.status))) - self.tableWidget_line_positions.setItem(row_i, 9, QTableWidgetItem(str(position_model.id))) - self.tableWidget_line_positions.setItem(row_i, 10, QTableWidgetItem(str(position_model.order))) - self.tableWidget_line_positions.setItem(row_i, 11, QTableWidgetItem(str(position_model.lineId))) + # self.tableWidget_line_positions.setItem(row_i, 7, QTableWidgetItem(str(position_model.status))) + self.tableWidget_line_positions.setItem( + row_i, 9, QTableWidgetItem(str(position_model.id)) + ) + self.tableWidget_line_positions.setItem( + row_i, 10, QTableWidgetItem(str(position_model.order)) + ) + self.tableWidget_line_positions.setItem( + row_i, 11, QTableWidgetItem(str(position_model.lineId)) + ) get_pos_btn = QPushButton("获取点位") get_pos_btn.clicked.connect( - lambda _, index=row_i: self.tabel_get_position(index)) + lambda _, index=row_i: self.tabel_get_position(index) + ) self.tableWidget_line_positions.setCellWidget(row_i, 12, get_pos_btn) - move_pos_btn = QPushButton("移至点位") - move_pos_btn.clicked.connect(lambda _, index=row_i: self.tabel_move_position(index)) + move_pos_btn.clicked.connect( + lambda _, index=row_i: self.tabel_move_position(index) + ) self.tableWidget_line_positions.setCellWidget(row_i, 13, move_pos_btn) + def send_table_position_status_changed(self): self.table_position_changed = True def set_position_tabel(self, row_i, position): + self.tableWidget_line_positions.setItem( + row_i, 1, QTableWidgetItem(str(position.X)) + ) + self.tableWidget_line_positions.setItem( + row_i, 2, QTableWidgetItem(str(position.Y)) + ) + self.tableWidget_line_positions.setItem( + row_i, 3, QTableWidgetItem(str(position.Z)) + ) + self.tableWidget_line_positions.setItem( + row_i, 4, QTableWidgetItem(str(position.U)) + ) + self.tableWidget_line_positions.setItem( + row_i, 5, QTableWidgetItem(str(position.V)) + ) + self.tableWidget_line_positions.setItem( + row_i, 6, QTableWidgetItem(str(position.W)) + ) - self.tableWidget_line_positions.setItem(row_i, 1, QTableWidgetItem(str(position.X))) - self.tableWidget_line_positions.setItem(row_i, 2, QTableWidgetItem(str(position.Y))) - self.tableWidget_line_positions.setItem(row_i, 3, QTableWidgetItem(str(position.Z))) - self.tableWidget_line_positions.setItem(row_i, 4, QTableWidgetItem(str(position.U))) - self.tableWidget_line_positions.setItem(row_i, 5, QTableWidgetItem(str(position.V))) - self.tableWidget_line_positions.setItem(row_i, 6, QTableWidgetItem(str(position.W))) - - #endregion + # endregion def init_qss(self): pass @@ -544,23 +755,27 @@ class MainWindow(QMainWindow, Ui_MainWindow): log.log_info_signal.connect(self.log_info_message) log.log_error_signal.connect(self.log_error_message) log.log_warning_signal.connect(self.log_warning_message) - log.init_log(self.textEdit_log_info, self.textEdit_log_error, Constant.log_file_path) + log.init_log( + self.textEdit_log_info, self.textEdit_log_error, Constant.log_file_path + ) log.log_message(logging.INFO, Constant.str_sys_start) - def log_info_message(self,message): - now = datetime.now() - formatted_time = now.strftime("%Y-%m-%d %H:%M:%S") - self.textEdit_log_info.append(f'{formatted_time}-提示: {message}') - def log_warning_message(self,message): + def log_info_message(self, message): now = datetime.now() formatted_time = now.strftime("%Y-%m-%d %H:%M:%S") - self.textEdit_log_info.append(f'{formatted_time}-注意: {message}') - self.textEdit_log_error.append(f'{formatted_time}-注意: {message}') - def log_error_message(self,message): + self.textEdit_log_info.append(f"{formatted_time}-提示: {message}") + + def log_warning_message(self, message): now = datetime.now() formatted_time = now.strftime("%Y-%m-%d %H:%M:%S") - self.textEdit_log_info.append(f'{formatted_time}-报警: {message}') - self.textEdit_log_error.append(f'{formatted_time}-报警: {message}') + self.textEdit_log_info.append(f"{formatted_time}-注意: {message}") + self.textEdit_log_error.append(f"{formatted_time}-注意: {message}") + + def log_error_message(self, message): + now = datetime.now() + formatted_time = now.strftime("%Y-%m-%d %H:%M:%S") + self.textEdit_log_info.append(f"{formatted_time}-报警: {message}") + self.textEdit_log_error.append(f"{formatted_time}-报警: {message}") if self.tabWidget.currentIndex() != 1: self.tabWidget.setCurrentIndex(1) @@ -574,7 +789,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.pushButton_AddNum.clicked.connect(self.send_addNum_button_click) self.pushButton_SubNum.clicked.connect(self.send_subNum_button_click) - self.lineEdit_j1.returnPressed.connect(self.send_position_returnPressed) self.lineEdit_j2.returnPressed.connect(self.send_position_returnPressed) self.lineEdit_j3.returnPressed.connect(self.send_position_returnPressed) @@ -618,23 +832,33 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.pushButton_emergency.clicked.connect(self.send_emergency_alarm_command) self.pushButton_reset.clicked.connect(self.send_reset_button_click) - self.pushButton_saveSeting.clicked.connect(self.send_setSpeed_clicked) self.pushButton_num_confirm.clicked.connect(self.send_custom_num_returnPressed) - #self.tabWidget_control.currentChanged.connect(self.send_tabWidget_control_change) + # self.tabWidget_control.currentChanged.connect(self.send_tabWidget_control_change) self.frame_sys_seting.setVisible(False) self.pushButton_sysseting.clicked.connect(self.send_click_sysmeuExpand) - self.pushButton_leftmenu_produce.clicked.connect(lambda _, index=0: self.send_click_change_stackView(index)) + self.pushButton_leftmenu_produce.clicked.connect( + lambda _, index=0: self.send_click_change_stackView(index) + ) self.pushButton_leftmenu_robot_seting.clicked.connect( - lambda _, index=2: self.send_click_change_stackView(index)) - self.pushButton_leftmenu_IO.clicked.connect(lambda _, index=1: self.send_click_change_stackView(index)) - self.pushButton_leftmenu_position.clicked.connect(lambda _, index=5: self.send_click_change_stackView(index)) - self.pushButton_leftmenu_baseSeting.clicked.connect(lambda _, index=3: self.send_click_change_stackView(index)) - self.pushButton_leftmenu_posDebug.clicked.connect(lambda _, index=4: self.send_click_change_stackView(index)) + lambda _, index=2: self.send_click_change_stackView(index) + ) + self.pushButton_leftmenu_IO.clicked.connect( + lambda _, index=1: self.send_click_change_stackView(index) + ) + self.pushButton_leftmenu_position.clicked.connect( + lambda _, index=5: self.send_click_change_stackView(index) + ) + self.pushButton_leftmenu_baseSeting.clicked.connect( + lambda _, index=3: self.send_click_change_stackView(index) + ) + self.pushButton_leftmenu_posDebug.clicked.connect( + lambda _, index=4: self.send_click_change_stackView(index) + ) self.pushButton_exit.clicked.connect(self.send_exit_button_click) self.pushButton_onekeyfeed.clicked.connect(self.send_onekeyfeed_button_click) @@ -645,69 +869,102 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.pushButton_startFeed.clicked.connect(self.send_startFeed_button_click) - self.horizontalSlider_feedingNum.blockSignals(True) self.horizontalSlider_feedingNum.setMinimum(0) - - - def init_Run(self): self.robotClient = None self.configReader = configparser.ConfigParser() - #TODO 关闭图像 + # TODO 关闭图像 self.command_position_quene = Queue() self.status_address = DataAddress() self.feedLine_dict = {} self.command_quene = Queue() self.main_threading = None - self.detection_person = None # DetectionPerson() + self.detection_person = None # DetectionPerson() self.index = 1 self.configReader.read(Constant.set_ini) - ip = self.configReader.get('Robot_Feed', 'IPAddress') - port = int(self.configReader.get('Robot_Feed', 'Port')) - photo_locs = [(float(self.configReader.get('Robot_Feed', 'photo_x1')), - float(self.configReader.get('Robot_Feed', 'photo_y1')), float(self.configReader.get('Robot_Feed', 'photo_z1')), - float(self.configReader.get('Robot_Feed', 'photo_u1')),float(self.configReader.get('Robot_Feed', 'photo_v1')),float(self.configReader.get('Robot_Feed', 'photo_w1'))), - (float(self.configReader.get('Robot_Feed', 'photo_x2')), - float(self.configReader.get('Robot_Feed', 'photo_y2')), float(self.configReader.get('Robot_Feed', 'photo_z2')), - float(self.configReader.get('Robot_Feed', 'photo_u2')),float(self.configReader.get('Robot_Feed', 'photo_v2')),float(self.configReader.get('Robot_Feed', 'photo_w2')) - ), - (float(self.configReader.get('Robot_Feed', 'photo_x3')), - float(self.configReader.get('Robot_Feed', 'photo_y3')),float(self.configReader.get('Robot_Feed', 'photo_z3')), - float(self.configReader.get('Robot_Feed', 'photo_u3')),float(self.configReader.get('Robot_Feed', 'photo_v3')),float(self.configReader.get('Robot_Feed', 'photo_w3')) - ), - (float(self.configReader.get('Robot_Feed', 'photo_x4')), - float(self.configReader.get('Robot_Feed', 'photo_y4')),float(self.configReader.get('Robot_Feed', 'photo_z4')), - float(self.configReader.get('Robot_Feed', 'photo_u4')),float(self.configReader.get('Robot_Feed', 'photo_v4')),float(self.configReader.get('Robot_Feed', 'photo_w4')) - ), - (float(self.configReader.get('Robot_Feed', 'photo_x5')), - float(self.configReader.get('Robot_Feed', 'photo_y5')),float(self.configReader.get('Robot_Feed', 'photo_z5')), - float(self.configReader.get('Robot_Feed', 'photo_u5')),float(self.configReader.get('Robot_Feed', 'photo_v5')),float(self.configReader.get('Robot_Feed', 'photo_w5')) - ) - ] - origin_position = Real_Position().init_position(float(self.configReader.get('Origin', 'X')), - float(self.configReader.get('Origin', 'Y')), - float(self.configReader.get('Origin', 'Z')), - float(self.configReader.get('Origin', 'U')), - float(self.configReader.get('Origin', 'V')), - float(self.configReader.get('Origin', 'W'))) - debug_speed = int(self.configReader.get('Speed', 'debug_speed')) - feed_speed = int(self.configReader.get('Speed', 'feed_speed')) - reset_speed = int(self.configReader.get('Speed', 'reset_speed')) - io_take_addr = int(self.configReader.get('Robot_Feed', 'io_take_addr')) - io_zip_addr = int(self.configReader.get('Robot_Feed', 'io_zip_addr')) - io_shake_addr = int(self.configReader.get('Robot_Feed', 'io_shake_addr')) - time_delay_take = float(self.configReader.get('Robot_Feed', 'takeDelay')) - time_delay_put = float(self.configReader.get('Robot_Feed', 'putDelay')) - time_delay_shake = float(self.configReader.get('Robot_Feed', 'shakeDelay')) - max_angle_interval = float(self.configReader.get('Robot_Feed', 'max_angle_interval')) - smooth = int(self.configReader.get('Robot_Feed', 'smooth')) - dynamic_height = float(self.configReader.get('Robot_Feed', 'dynamic_height')) - #TODO - #dropDelay_time = int(self.configReader.get('Robot_Feed', 'dropDelay_time')) - self.robotClient = RobotClient(ip, port, photo_locs, self.command_position_quene, self.status_address,[io_take_addr, io_zip_addr, io_shake_addr],time_delay_take,time_delay_put,time_delay_shake,origin_position) + ip = self.configReader.get("Robot_Feed", "IPAddress") + port = int(self.configReader.get("Robot_Feed", "Port")) + photo_locs = [ + ( + float(self.configReader.get("Robot_Feed", "photo_x1")), + float(self.configReader.get("Robot_Feed", "photo_y1")), + float(self.configReader.get("Robot_Feed", "photo_z1")), + float(self.configReader.get("Robot_Feed", "photo_u1")), + float(self.configReader.get("Robot_Feed", "photo_v1")), + float(self.configReader.get("Robot_Feed", "photo_w1")), + ), + ( + float(self.configReader.get("Robot_Feed", "photo_x2")), + float(self.configReader.get("Robot_Feed", "photo_y2")), + float(self.configReader.get("Robot_Feed", "photo_z2")), + float(self.configReader.get("Robot_Feed", "photo_u2")), + float(self.configReader.get("Robot_Feed", "photo_v2")), + float(self.configReader.get("Robot_Feed", "photo_w2")), + ), + ( + float(self.configReader.get("Robot_Feed", "photo_x3")), + float(self.configReader.get("Robot_Feed", "photo_y3")), + float(self.configReader.get("Robot_Feed", "photo_z3")), + float(self.configReader.get("Robot_Feed", "photo_u3")), + float(self.configReader.get("Robot_Feed", "photo_v3")), + float(self.configReader.get("Robot_Feed", "photo_w3")), + ), + ( + float(self.configReader.get("Robot_Feed", "photo_x4")), + float(self.configReader.get("Robot_Feed", "photo_y4")), + float(self.configReader.get("Robot_Feed", "photo_z4")), + float(self.configReader.get("Robot_Feed", "photo_u4")), + float(self.configReader.get("Robot_Feed", "photo_v4")), + float(self.configReader.get("Robot_Feed", "photo_w4")), + ), + ( + float(self.configReader.get("Robot_Feed", "photo_x5")), + float(self.configReader.get("Robot_Feed", "photo_y5")), + float(self.configReader.get("Robot_Feed", "photo_z5")), + float(self.configReader.get("Robot_Feed", "photo_u5")), + float(self.configReader.get("Robot_Feed", "photo_v5")), + float(self.configReader.get("Robot_Feed", "photo_w5")), + ), + ] + origin_position = Real_Position().init_position( + float(self.configReader.get("Origin", "X")), + float(self.configReader.get("Origin", "Y")), + float(self.configReader.get("Origin", "Z")), + float(self.configReader.get("Origin", "U")), + float(self.configReader.get("Origin", "V")), + float(self.configReader.get("Origin", "W")), + ) + debug_speed = int(self.configReader.get("Speed", "debug_speed")) + feed_speed = int(self.configReader.get("Speed", "feed_speed")) + reset_speed = int(self.configReader.get("Speed", "reset_speed")) + io_take_addr = int(self.configReader.get("Robot_Feed", "io_take_addr")) + io_zip_addr = int(self.configReader.get("Robot_Feed", "io_zip_addr")) + io_shake_addr = int(self.configReader.get("Robot_Feed", "io_shake_addr")) + time_delay_take = float(self.configReader.get("Robot_Feed", "takeDelay")) + time_delay_put = float(self.configReader.get("Robot_Feed", "putDelay")) + time_delay_shake = float(self.configReader.get("Robot_Feed", "shakeDelay")) + max_angle_interval = float( + self.configReader.get("Robot_Feed", "max_angle_interval") + ) + smooth = int(self.configReader.get("Robot_Feed", "smooth")) + dynamic_height = float(self.configReader.get("Robot_Feed", "dynamic_height")) + # TODO + # dropDelay_time = int(self.configReader.get('Robot_Feed', 'dropDelay_time')) + self.robotClient = RobotClient( + ip, + port, + photo_locs, + self.command_position_quene, + self.status_address, + [io_take_addr, io_zip_addr, io_shake_addr], + time_delay_take, + time_delay_put, + time_delay_shake, + origin_position, + ) self.robotClient.debug_speed = debug_speed self.robotClient.feed_speed = feed_speed self.robotClient.reset_speed = reset_speed @@ -720,8 +977,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.feeding.update_detect_image.connect(self.updateUI_label_detection) self.feeding.log_signal.connect(self.log_message) self.last_time = time.time() - self.remain_lineName = self.configReader.get('Robot_Feed', 'remain_lineName') - self.remain_Count = int(self.configReader.get('Robot_Feed', 'remain_Count')) + self.remain_lineName = self.configReader.get("Robot_Feed", "remain_lineName") + self.remain_Count = int(self.configReader.get("Robot_Feed", "remain_Count")) self.updateUI_seting.connect(self.update_seting_frame) try: self.robotClient.CreatConnect() @@ -737,18 +994,29 @@ class MainWindow(QMainWindow, Ui_MainWindow): rows = len(self.feedLine_dict.keys()) + 1 self.tableWidget_feedSeting.setRowCount(rows) - self.tableWidget_feedSeting.setColumnCount(20+2*6) + self.tableWidget_feedSeting.setColumnCount(20 + 2 * 6) # 设置第一重表头的合并 (三列一组) self.tableWidget_feedSeting_addtional_col_num = 2 - self.tableWidget_feedSeting.setSpan(0, 0 + self.tableWidget_feedSeting_addtional_col_num, 1, 6) # 合并前3列 - self.tableWidget_feedSeting.setSpan(0, 6 + self.tableWidget_feedSeting_addtional_col_num, 1, 6) # 合并后3列 - self.tableWidget_feedSeting.setSpan(0, 12 + self.tableWidget_feedSeting_addtional_col_num, 1, 6) # 合并后3列 - self.tableWidget_feedSeting.setSpan(0, 18 + self.tableWidget_feedSeting_addtional_col_num, 1, 6) # 合并后3列 - self.tableWidget_feedSeting.setSpan(0, 24 + self.tableWidget_feedSeting_addtional_col_num, 1, 6) # 合并后3列 + self.tableWidget_feedSeting.setSpan( + 0, 0 + self.tableWidget_feedSeting_addtional_col_num, 1, 6 + ) # 合并前3列 + self.tableWidget_feedSeting.setSpan( + 0, 6 + self.tableWidget_feedSeting_addtional_col_num, 1, 6 + ) # 合并后3列 + self.tableWidget_feedSeting.setSpan( + 0, 12 + self.tableWidget_feedSeting_addtional_col_num, 1, 6 + ) # 合并后3列 + self.tableWidget_feedSeting.setSpan( + 0, 18 + self.tableWidget_feedSeting_addtional_col_num, 1, 6 + ) # 合并后3列 + self.tableWidget_feedSeting.setSpan( + 0, 24 + self.tableWidget_feedSeting_addtional_col_num, 1, 6 + ) # 合并后3列 - - self.tableWidget_feedSeting.itemChanged.connect(self.send_tabelFeedSet_itemChanged) + self.tableWidget_feedSeting.itemChanged.connect( + self.send_tabelFeedSet_itemChanged + ) btn_safe = QPushButton("获取安全位置") widget_safe = QWidget() @@ -759,7 +1027,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): # 将布局设置到 QWidget 容器中 widget_safe.setLayout(layout_safe) - btn_bk1 = QPushButton("获取破袋位置1") widget_bk1 = QWidget() layout_bk1 = QHBoxLayout() @@ -785,7 +1052,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): layout_shake.setContentsMargins(0, 0, 0, 0) widget_shake.setLayout(layout_shake) - btn_dropbag = QPushButton("获取丢袋位置") widget_dropbag = QWidget() layout_dropbag = QHBoxLayout() @@ -795,10 +1061,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): # 将布局设置到 QWidget 容器中 widget_dropbag.setLayout(layout_dropbag) - - - - btn_safe.clicked.connect(self.send_get_safe_position_button_click) btn_bk1.clicked.connect(self.send_get_broken1_position_button_click) btn_bk2.clicked.connect(self.send_get_broken2_position_button_click) @@ -807,64 +1069,113 @@ class MainWindow(QMainWindow, Ui_MainWindow): # 添加第一重表头项 - self.tableWidget_feedSeting.setCellWidget(0, 0 + self.tableWidget_feedSeting_addtional_col_num, widget_safe) - self.tableWidget_feedSeting.setCellWidget(0, 6 + self.tableWidget_feedSeting_addtional_col_num, widget_bk1) - self.tableWidget_feedSeting.setCellWidget(0, 12 + self.tableWidget_feedSeting_addtional_col_num, widget_bk2) - self.tableWidget_feedSeting.setCellWidget(0, 18 + self.tableWidget_feedSeting_addtional_col_num, widget_shake) - self.tableWidget_feedSeting.setCellWidget(0, 24 + self.tableWidget_feedSeting_addtional_col_num, widget_dropbag) - + self.tableWidget_feedSeting.setCellWidget( + 0, 0 + self.tableWidget_feedSeting_addtional_col_num, widget_safe + ) + self.tableWidget_feedSeting.setCellWidget( + 0, 6 + self.tableWidget_feedSeting_addtional_col_num, widget_bk1 + ) + self.tableWidget_feedSeting.setCellWidget( + 0, 12 + self.tableWidget_feedSeting_addtional_col_num, widget_bk2 + ) + self.tableWidget_feedSeting.setCellWidget( + 0, 18 + self.tableWidget_feedSeting_addtional_col_num, widget_shake + ) + self.tableWidget_feedSeting.setCellWidget( + 0, 24 + self.tableWidget_feedSeting_addtional_col_num, widget_dropbag + ) self.tableWidget_feedSeting.setSelectionBehavior(QTableWidget.SelectRows) self.tableWidget_feedSeting.setAutoScroll(True) # 添加第二重表头 self.tableWidget_feedSeting.setHorizontalHeaderLabels( - ['header', '线名', 'X1', 'Y1', 'Z1', 'U1', 'V1', 'W1', 'X2', 'Y2', 'Z2', 'U2', 'V2', 'W2', 'X3', 'Y3', 'Z3', - 'U3', 'V3', 'W3', 'X4', 'Y4', 'Z4', 'U4', 'V4', 'W4', 'X5', 'Y5', 'Z5', 'U5', 'V5', 'W5']) + [ + "header", + "线名", + "X1", + "Y1", + "Z1", + "U1", + "V1", + "W1", + "X2", + "Y2", + "Z2", + "U2", + "V2", + "W2", + "X3", + "Y3", + "Z3", + "U3", + "V3", + "W3", + "X4", + "Y4", + "Z4", + "U4", + "V4", + "W4", + "X5", + "Y5", + "Z5", + "U5", + "V5", + "W5", + ] + ) self.tableWidget_feedSeting.hideColumn(0) # 填充数据行 for row, (feed_line_key, feed_line) in enumerate(self.feedLine_dict.items()): - self.tableWidget_feedSeting.setItem(row + 1, 0, QTableWidgetItem(feed_line_key)) - self.tableWidget_feedSeting.setItem(row + 1, 1, QTableWidgetItem(feed_line.name)) + self.tableWidget_feedSeting.setItem( + row + 1, 0, QTableWidgetItem(feed_line_key) + ) + self.tableWidget_feedSeting.setItem( + row + 1, 1, QTableWidgetItem(feed_line.name) + ) self.set_position_to_tabel(row + 1, 0, feed_line.safe_position) self.set_position_to_tabel(row + 1, 1, feed_line.broken1_position) self.set_position_to_tabel(row + 1, 2, feed_line.broken2_position) self.set_position_to_tabel(row + 1, 3, feed_line.broken2_position) self.set_position_to_tabel(row + 1, 4, feed_line.drop_bag_position) - # 禁用自动表头 self.tableWidget_feedSeting.verticalHeader().setVisible(True) - self.pushButton_tableFeedSet_addRow.clicked.connect(self.send_tabelFeedSet_addRow) - self.pushButton_tableFeedSet_deleRow.clicked.connect(self.send_tabelFeedSet_delRow) + self.pushButton_tableFeedSet_addRow.clicked.connect( + self.send_tabelFeedSet_addRow + ) + self.pushButton_tableFeedSet_deleRow.clicked.connect( + self.send_tabelFeedSet_delRow + ) self.pushButton_tableFeedSet_save.clicked.connect(self.send_tabelFeedSet_save) def init_FeedLine(self): # line_count = self.configReader.get('Robot_Feed', 'LineCount', fallback=0) configReader = configparser.ConfigParser() - configReader.read(Constant.feedLine_set_file, encoding='utf-8') - for i in range(1,Constant.MAX_Line_num): - line_str = f'{Constant.feedLine_set_section}{i}' + configReader.read(Constant.feedLine_set_file, encoding="utf-8") + for i in range(1, Constant.MAX_Line_num): + line_str = f"{Constant.feedLine_set_section}{i}" if configReader.has_section(line_str): feed_line = LineModel() - feed_line.read_line_model(config_reader=configReader,index=i) - self.feedLine_dict[f'{Constant.feedLine_set_section}{i}'] = feed_line + feed_line.read_line_model(config_reader=configReader, index=i) + self.feedLine_dict[f"{Constant.feedLine_set_section}{i}"] = feed_line self.updateUI_Select_Line() pass def init_robot_info(self): - j1_min = int(self.configReader.get('Robot_Feed', 'j1_min')) - j1_max = int(self.configReader.get('Robot_Feed', 'j1_max')) - j2_min = int(self.configReader.get('Robot_Feed', 'j2_min')) - j2_max = int(self.configReader.get('Robot_Feed', 'j2_max')) - j3_min = int(self.configReader.get('Robot_Feed', 'j3_min')) - j3_max = int(self.configReader.get('Robot_Feed', 'j3_max')) - j4_min = int(self.configReader.get('Robot_Feed', 'j4_min')) - j4_max = int(self.configReader.get('Robot_Feed', 'j4_max')) - j5_min = int(self.configReader.get('Robot_Feed', 'j5_min')) - j5_max = int(self.configReader.get('Robot_Feed', 'j5_max')) - j6_min = int(self.configReader.get('Robot_Feed', 'j6_min')) - j6_max = int(self.configReader.get('Robot_Feed', 'j6_max')) + j1_min = int(self.configReader.get("Robot_Feed", "j1_min")) + j1_max = int(self.configReader.get("Robot_Feed", "j1_max")) + j2_min = int(self.configReader.get("Robot_Feed", "j2_min")) + j2_max = int(self.configReader.get("Robot_Feed", "j2_max")) + j3_min = int(self.configReader.get("Robot_Feed", "j3_min")) + j3_max = int(self.configReader.get("Robot_Feed", "j3_max")) + j4_min = int(self.configReader.get("Robot_Feed", "j4_min")) + j4_max = int(self.configReader.get("Robot_Feed", "j4_max")) + j5_min = int(self.configReader.get("Robot_Feed", "j5_min")) + j5_max = int(self.configReader.get("Robot_Feed", "j5_max")) + j6_min = int(self.configReader.get("Robot_Feed", "j6_min")) + j6_max = int(self.configReader.get("Robot_Feed", "j6_max")) self.horizontalSlider_J1.setMinimum(j1_min) self.horizontalSlider_J1.setMaximum(j1_max) self.horizontalSlider_J2.setMinimum(j2_min) @@ -891,8 +1202,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.label_j6_max.setText(str(j6_max)) def start_Runing(self): - #多线程启动 - self.main_threading = Thread(target=self.run)#主循环 + # 多线程启动 + self.main_threading = Thread(target=self.run) # 主循环 self.robot_connect_threading = Thread(target=self.robotClient.run) self.main_UI_threading = Thread(target=self.updateUI) self.detect_person_thread = Thread(target=self.run_detect_persion) @@ -901,29 +1212,38 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.main_UI_threading.start() self.detect_person_thread.start() pass + def check_continue(self): - if self.remain_Count!=0: + if self.remain_Count != 0: for key in self.feedLine_dict.keys(): feed_line = self.feedLine_dict[key] - if f'FeedLine{self.remain_lineName}' == key: - #提示 + if f"FeedLine{self.remain_lineName}" == key: + # 提示 remain_messageBox = QMessageBox() remain_messageBox.setIcon(QMessageBox.Icon.Warning) - remain_messageBox.setText(f'还有{self.remain_Count}袋未投料,是否继续?') # 这里返回多少袋没有投料 + remain_messageBox.setText( + f"还有{self.remain_Count}袋未投料,是否继续?" + ) # 这里返回多少袋没有投料 # remain_messageBox.setStandardButtons(QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel) - ok_button = remain_messageBox.addButton("确定", PySide6.QtWidgets.QMessageBox.ButtonRole.AcceptRole) - cancel_button = remain_messageBox.addButton("取消", PySide6.QtWidgets.QMessageBox.ButtonRole.RejectRole) + ok_button = remain_messageBox.addButton( + "确定", PySide6.QtWidgets.QMessageBox.ButtonRole.AcceptRole + ) + cancel_button = remain_messageBox.addButton( + "取消", PySide6.QtWidgets.QMessageBox.ButtonRole.RejectRole + ) result = remain_messageBox.exec() if remain_messageBox.clickedButton() == cancel_button: self.remain_Count = 0 return - self.configReader.read(Constant.feedLine_set_file, encoding='utf-8') - line_name = self.configReader.get(key, 'name') + self.configReader.read(Constant.feedLine_set_file, encoding="utf-8") + line_name = self.configReader.get(key, "name") current_index = self.comboBox_lineIndex.findText(line_name) self.comboBox_lineIndex.setCurrentIndex(current_index) self.horizontalSlider_feedingNum.setMaximum(self.remain_Count) - self.label_maxNum.setText(str(self.remain_Count)) #目标投料袋数设置为setini文件中保存的remain_count + self.label_maxNum.setText( + str(self.remain_Count) + ) # 目标投料袋数设置为setini文件中保存的remain_count self.horizontalSlider_feedingNum.setValue(0) self.send_startFeed_button_click() @@ -946,14 +1266,18 @@ class MainWindow(QMainWindow, Ui_MainWindow): msg_box_person.setText("是否确认生产区域安全继续生产?") msg_box_person.setWindowTitle("提示") - ok_button = msg_box_person.addButton("确定", PySide6.QtWidgets.QMessageBox.ButtonRole.AcceptRole) - cancel_button = msg_box_person.addButton("取消", PySide6.QtWidgets.QMessageBox.ButtonRole.RejectRole) + ok_button = msg_box_person.addButton( + "确定", PySide6.QtWidgets.QMessageBox.ButtonRole.AcceptRole + ) + cancel_button = msg_box_person.addButton( + "取消", PySide6.QtWidgets.QMessageBox.ButtonRole.RejectRole + ) result = msg_box_person.exec() if msg_box_person.clickedButton() == cancel_button: return - log.log_message(logging.INFO, '第一层确认生产') + log.log_message(logging.INFO, "第一层确认生产") has_person = False # has_person, _ = self.detection_person.get_person() # TODO @@ -966,16 +1290,31 @@ class MainWindow(QMainWindow, Ui_MainWindow): result = msg_box_person.exec() if result == QMessageBox.StandardButton.Cancel: return - log.log_message(logging.ERROR, '人员进入安全区') + log.log_message(logging.ERROR, "人员进入安全区") num = self.horizontalSlider_feedingNum.maximum() line_head = self.comboBox_lineIndex.currentData() - self.command_quene.put(FeedCommand(FeedingConfig(num, FeedLine(self.feedLine_dict[line_head].id,self.feedLine_dict[line_head].name,self.feedLine_dict[line_head].positions), self.feeding.robotClient.photo_locs[:]))) + self.command_quene.put( + FeedCommand( + FeedingConfig( + num, + FeedLine( + self.feedLine_dict[line_head].id, + self.feedLine_dict[line_head].name, + self.feedLine_dict[line_head].positions, + ), + self.feeding.robotClient.photo_locs[:], + ) + ) + ) # self.stackedWidget_num.setCurrentIndex(1) self.set_run_status_button(True) self.feeding.pause = False self.feeding.onekey = False - log.log_message(logging.INFO, f'{self.feedLine_dict[line_head].name}:{Constant.str_feed_start}') + log.log_message( + logging.INFO, + f"{self.feedLine_dict[line_head].name}:{Constant.str_feed_start}", + ) def send_num_button_click(self): button = self.sender() @@ -989,55 +1328,59 @@ class MainWindow(QMainWindow, Ui_MainWindow): def send_subOneAsix_button_click(self): btn_str = self.sender().objectName() - Constant.manual_adjust_accuracy = float(self.lineEdit_manual_adjust_accuracy.text()) + Constant.manual_adjust_accuracy = float( + self.lineEdit_manual_adjust_accuracy.text() + ) x1 = float(self.label_j1.text()) x2 = float(self.label_j2.text()) x3 = float(self.label_j3.text()) x4 = float(self.label_j4.text()) x5 = float(self.label_j5.text()) x6 = float(self.label_j6.text()) - if 'j1' in btn_str: + if "j1" in btn_str: x1 = x1 - Constant.manual_adjust_accuracy - elif 'j2' in btn_str: + elif "j2" in btn_str: x2 = x2 - Constant.manual_adjust_accuracy pass - elif 'j3' in btn_str: + elif "j3" in btn_str: x3 = x3 - Constant.manual_adjust_accuracy pass - elif 'j4' in btn_str: + elif "j4" in btn_str: x4 = x4 - Constant.manual_adjust_accuracy pass - elif 'j5' in btn_str: + elif "j5" in btn_str: x5 = x5 - Constant.manual_adjust_accuracy pass - elif 'j6' in btn_str: + elif "j6" in btn_str: x6 = x6 - Constant.manual_adjust_accuracy self.send_position_command(x1, x2, x3, x4, x5, x6, MoveType.AXIS) def send_addOneAsix_button_click(self, btn): btn_str = self.sender().objectName() - Constant.manual_adjust_accuracy = float(self.lineEdit_manual_adjust_accuracy.text()) + Constant.manual_adjust_accuracy = float( + self.lineEdit_manual_adjust_accuracy.text() + ) x1 = float(self.label_j1.text()) x2 = float(self.label_j2.text()) x3 = float(self.label_j3.text()) x4 = float(self.label_j4.text()) x5 = float(self.label_j5.text()) x6 = float(self.label_j6.text()) - if 'j1' in btn_str: + if "j1" in btn_str: x1 = x1 + Constant.manual_adjust_accuracy - elif 'j2' in btn_str: + elif "j2" in btn_str: x2 = x2 + Constant.manual_adjust_accuracy pass - elif 'j3' in btn_str: + elif "j3" in btn_str: x3 = x3 + Constant.manual_adjust_accuracy pass - elif 'j4' in btn_str: + elif "j4" in btn_str: x4 = x4 + Constant.manual_adjust_accuracy pass - elif 'j5' in btn_str: + elif "j5" in btn_str: x5 = x5 + Constant.manual_adjust_accuracy pass - elif 'j6' in btn_str: + elif "j6" in btn_str: x6 = x6 + Constant.manual_adjust_accuracy self.send_position_command(x1, x2, x3, x4, x5, x6, move_type=MoveType.AXIS) @@ -1050,7 +1393,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): def send_subNum_button_click(self): if self.feeding.feedConfig.num <= 1: - #self.send_stopFeed_button_click() + # self.send_stopFeed_button_click() self.feeding.feedStatus = FeedStatus.FNone # 清空运行命令 self.send_clear_auto_command() @@ -1058,17 +1401,19 @@ class MainWindow(QMainWindow, Ui_MainWindow): return self.feeding.feedConfig.num -= 1 log.log_message(logging.INFO, Constant.str_sys_feedNum_sub) - max_num = int(self.label_maxNum.text())-1 + max_num = int(self.label_maxNum.text()) - 1 self.horizontalSlider_feedingNum.setMaximum(max_num) self.label_maxNum.setText(str(max_num)) def send_custom_num_returnPressed(self): - if self.lineEdit_num.text()== '' or not self.lineEdit_num.text().isdigit(): + if self.lineEdit_num.text() == "" or not self.lineEdit_num.text().isdigit(): return self.horizontalSlider_feedingNum.setMaximum(int(self.lineEdit_num.text())) self.horizontalSlider_feedingNum.setValue(0) self.label_maxNum.setText(self.lineEdit_num.text()) - log.log_message(logging.INFO, f'{Constant.str_sys_setFeedNum}:{self.label_maxNum.text()} ') + log.log_message( + logging.INFO, f"{Constant.str_sys_setFeedNum}:{self.label_maxNum.text()} " + ) def send_position_button_click(self): # if True: @@ -1081,14 +1426,16 @@ class MainWindow(QMainWindow, Ui_MainWindow): def send_position_returnPressed(self): log.log_message(logging.INFO, Constant.str_sys_manualPosition) try: - self.send_position_command(float(self.lineEdit_j1.text()), - float(self.lineEdit_j2.text()), - float(self.lineEdit_j3.text()), - float(self.lineEdit_j4.text()), - float(self.lineEdit_j5.text()), - float(self.lineEdit_j6.text())) + self.send_position_command( + float(self.lineEdit_j1.text()), + float(self.lineEdit_j2.text()), + float(self.lineEdit_j3.text()), + float(self.lineEdit_j4.text()), + float(self.lineEdit_j5.text()), + float(self.lineEdit_j6.text()), + ) except Exception as e: - self.show_infomessage_box(Constant.str_sys_log_move_error) + self.show_infomessage_box(Constant.str_sys_log_move_error) def send_setSpeed_label_doubelClick(self): pass @@ -1114,76 +1461,126 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.robotClient.time_delay_shake = time_delay_shake self.configReader = configparser.ConfigParser() self.configReader.read(Constant.set_ini) - self.configReader.set('Robot_Feed', 'io_take_addr', str(take_addr)) - self.configReader.set('Robot_Feed', 'io_zip_addr', str(press_addr)) - self.configReader.set('Robot_Feed', 'io_shake_addr', str(shake_addr)) - self.configReader.set('Robot_Feed', 'takeDelay', str(time_delay_take)) - self.configReader.set('Robot_Feed', 'putDelay', str(time_delay_put)) - self.configReader.set('Robot_Feed', 'shakeDelay', str(time_delay_shake)) - self.configReader.set('Speed', 'feed_speed', str(self.robotClient.feed_speed)) - self.configReader.set('Speed', 'debug_speed', str(self.robotClient.debug_speed)) - self.configReader.set('Speed', 'reset_speed', str(self.robotClient.reset_speed)) - self.configReader.set('Robot_Feed', 'smooth', str(self.robotClient.smooth)) - self.configReader.set('Robot_Feed', 'dynamic_height', str(self.robotClient.dynamic_height)) - self.configReader.write(open(Constant.set_ini, 'w', encoding='utf-8')) + self.configReader.set("Robot_Feed", "io_take_addr", str(take_addr)) + self.configReader.set("Robot_Feed", "io_zip_addr", str(press_addr)) + self.configReader.set("Robot_Feed", "io_shake_addr", str(shake_addr)) + self.configReader.set("Robot_Feed", "takeDelay", str(time_delay_take)) + self.configReader.set("Robot_Feed", "putDelay", str(time_delay_put)) + self.configReader.set("Robot_Feed", "shakeDelay", str(time_delay_shake)) + self.configReader.set( + "Speed", "feed_speed", str(self.robotClient.feed_speed) + ) + self.configReader.set( + "Speed", "debug_speed", str(self.robotClient.debug_speed) + ) + self.configReader.set( + "Speed", "reset_speed", str(self.robotClient.reset_speed) + ) + self.configReader.set("Robot_Feed", "smooth", str(self.robotClient.smooth)) + self.configReader.set( + "Robot_Feed", "dynamic_height", str(self.robotClient.dynamic_height) + ) + self.configReader.write(open(Constant.set_ini, "w", encoding="utf-8")) except Exception as e: - log.log_message(logging.ERROR, Constant.str_sys_set_error+e) + log.log_message(logging.ERROR, Constant.str_sys_set_error + e) pass - #self.pushButton_speed.setText(str(Constant.speed)) - log.log_message(logging.INFO, Constant.str_sys_setSpeed + str(self.robotClient.debug_speed)+'|'+str(self.robotClient.feed_speed)) + # self.pushButton_speed.setText(str(Constant.speed)) + log.log_message( + logging.INFO, + Constant.str_sys_setSpeed + + str(self.robotClient.debug_speed) + + "|" + + str(self.robotClient.feed_speed), + ) pass def send_get_safe_position_button_click(self): real_position = self.robotClient.status_model.getRealPosition() row_i = self.tableWidget_feedSeting.currentRow() head = self.tableWidget_feedSeting.item(row_i, 0).text() - self.feedLine_dict[head].safe_position.init_position(real_position.X, real_position.Y, real_position.Z, - real_position.U, real_position.V, real_position.W) + self.feedLine_dict[head].safe_position.init_position( + real_position.X, + real_position.Y, + real_position.Z, + real_position.U, + real_position.V, + real_position.W, + ) self.set_position_to_tabel(row_i, 0, real_position) - def send_get_broken1_position_button_click(self): real_position = self.robotClient.status_model.getRealPosition() row_i = self.tableWidget_feedSeting.currentRow() head = self.tableWidget_feedSeting.item(row_i, 0).text() - self.feedLine_dict[head].broken1_position.init_position(real_position.X, real_position.Y, real_position.Z, - real_position.U, real_position.V, real_position.W) + self.feedLine_dict[head].broken1_position.init_position( + real_position.X, + real_position.Y, + real_position.Z, + real_position.U, + real_position.V, + real_position.W, + ) self.set_position_to_tabel(row_i, 1, real_position) def send_get_broken2_position_button_click(self): real_position = self.robotClient.status_model.getRealPosition() row_i = self.tableWidget_feedSeting.currentRow() head = self.tableWidget_feedSeting.item(row_i, 0).text() - self.feedLine_dict[head].broken2_position.init_position(real_position.X, real_position.Y, real_position.Z, - real_position.U, real_position.V, real_position.W) + self.feedLine_dict[head].broken2_position.init_position( + real_position.X, + real_position.Y, + real_position.Z, + real_position.U, + real_position.V, + real_position.W, + ) self.set_position_to_tabel(row_i, 2, real_position) def send_get_shake_position_button_click(self): real_position = self.robotClient.status_model.getRealPosition() row_i = self.tableWidget_feedSeting.currentRow() head = self.tableWidget_feedSeting.item(row_i, 0).text() - self.feedLine_dict[head].shake_position.init_position(real_position.X, real_position.Y, real_position.Z, - real_position.U, real_position.V, real_position.W) + self.feedLine_dict[head].shake_position.init_position( + real_position.X, + real_position.Y, + real_position.Z, + real_position.U, + real_position.V, + real_position.W, + ) self.set_position_to_tabel(row_i, 3, real_position) def send_get_dropbag_position_button_click(self): real_position = self.robotClient.status_model.getRealPosition() row_i = self.tableWidget_feedSeting.currentRow() head = self.tableWidget_feedSeting.item(row_i, 0).text() - self.feedLine_dict[head].drop_bag_position.init_position(real_position.X, real_position.Y, real_position.Z, - real_position.U, real_position.V, real_position.W) + self.feedLine_dict[head].drop_bag_position.init_position( + real_position.X, + real_position.Y, + real_position.Z, + real_position.U, + real_position.V, + real_position.W, + ) self.set_position_to_tabel(row_i, 4, real_position) def send_get_zipbag_position_button_click(self): real_position = self.robotClient.status_model.getRealPosition() row_i = self.tableWidget_feedSeting.currentRow() head = self.tableWidget_feedSeting.item(row_i, 0).text() - self.feedLine_dict[head].zip_bag_position.init_position(real_position.X, real_position.Y, real_position.Z, - real_position.U, real_position.V, real_position.W) + self.feedLine_dict[head].zip_bag_position.init_position( + real_position.X, + real_position.Y, + real_position.Z, + real_position.U, + real_position.V, + real_position.W, + ) self.set_position_to_tabel(row_i, 6, real_position) + def send_tabelFeedSet_addRow(self): for i in range(1, 20): - head = f'{Constant.feedLine_set_section}{i}' + head = f"{Constant.feedLine_set_section}{i}" if head not in self.feedLine_dict: row_position = self.tableWidget_feedSeting.rowCount() # 当前行数 self.tableWidget_feedSeting.insertRow(row_position) @@ -1193,23 +1590,49 @@ class MainWindow(QMainWindow, Ui_MainWindow): shake_position = Real_Position() drop_bag_position = Real_Position() - self.feedLine_dict[head] = FeedLine('新建', safe_position, break_1_position, break_2_position,shake_position,drop_bag_position) - self.tableWidget_feedSeting.setItem(self.tableWidget_feedSeting.rowCount() - 1, 0, - QTableWidgetItem(head)) - self.tableWidget_feedSeting.setItem(self.tableWidget_feedSeting.rowCount() - 1, 1, - QTableWidgetItem('新建')) - self.set_position_to_tabel(self.tableWidget_feedSeting.rowCount() - 1, 0, safe_position) - self.set_position_to_tabel(self.tableWidget_feedSeting.rowCount() - 1, 1, break_1_position) - self.set_position_to_tabel(self.tableWidget_feedSeting.rowCount() - 1, 2, break_2_position) - self.set_position_to_tabel(self.tableWidget_feedSeting.rowCount() - 1, 3, shake_position) - self.set_position_to_tabel(self.tableWidget_feedSeting.rowCount() - 1, 4, drop_bag_position) + self.feedLine_dict[head] = FeedLine( + "新建", + safe_position, + break_1_position, + break_2_position, + shake_position, + drop_bag_position, + ) + self.tableWidget_feedSeting.setItem( + self.tableWidget_feedSeting.rowCount() - 1, + 0, + QTableWidgetItem(head), + ) + self.tableWidget_feedSeting.setItem( + self.tableWidget_feedSeting.rowCount() - 1, + 1, + QTableWidgetItem("新建"), + ) + self.set_position_to_tabel( + self.tableWidget_feedSeting.rowCount() - 1, 0, safe_position + ) + self.set_position_to_tabel( + self.tableWidget_feedSeting.rowCount() - 1, 1, break_1_position + ) + self.set_position_to_tabel( + self.tableWidget_feedSeting.rowCount() - 1, 2, break_2_position + ) + self.set_position_to_tabel( + self.tableWidget_feedSeting.rowCount() - 1, 3, shake_position + ) + self.set_position_to_tabel( + self.tableWidget_feedSeting.rowCount() - 1, 4, drop_bag_position + ) break def send_tabelFeedSet_delRow(self): selectRow = self.tableWidget_feedSeting.currentRow() if selectRow >= 0: for feed_line_key, feed_line in self.feedLine_dict.items(): - if feed_line_key == self.tableWidget_feedSeting.item(selectRow, 0).text(): + if ( + feed_line_key + == self.tableWidget_feedSeting.item(selectRow, 0).text() + ): self.feedLine_dict.pop(feed_line_key) self.tableWidget_feedSeting.removeRow(selectRow) break @@ -1219,32 +1642,32 @@ class MainWindow(QMainWindow, Ui_MainWindow): def send_tabelFeedSet_save(self): count = len(self.feedLine_dict.keys()) self.configReader.read(Constant.set_ini) - self.configReader.set('Robot_Feed', 'LineCount', str(count)) + self.configReader.set("Robot_Feed", "LineCount", str(count)) writeFeedLine_to_ini(self.feedLine_dict, Constant.feedLine_set_file) pass def send_stopFeed_button_click(self): # 清空状态 - #self.feeding.feedStatus = FeedStatus.FNone - #self.feeding.reset_status = ResetStatus.RNone + self.feeding.feedStatus = FeedStatus.FNone + self.feeding.reset_status = ResetStatus.RNone # 清空运行命令 self.send_clear_auto_command() - #self.feeding.feedConfig.num = 0 + # self.feeding.feedConfig.num = 0 log.log_message(logging.INFO, Constant.str_feed_stop) def send_pauseFeed_button_click(self): - if self.pushButton_pauseFeed.text() == '暂停': + if self.pushButton_pauseFeed.text() == "暂停": icon9 = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.SystemReboot)) self.pushButton_pauseFeed.setIcon(icon9) - self.pushButton_pauseFeed.setText('继续') + self.pushButton_pauseFeed.setText("继续") log.log_message(logging.INFO, Constant.str_feed_pause) self.send_pause_command(True) self.feeding.pause = True else: icon9 = QIcon(QIcon.fromTheme(QIcon.ThemeIcon.MediaPlaybackPause)) self.pushButton_pauseFeed.setIcon(icon9) - self.pushButton_pauseFeed.setText('暂停') + self.pushButton_pauseFeed.setText("暂停") self.send_pause_command(False) log.log_message(logging.INFO, Constant.str_feed_continue) self.send_start_tool_command() @@ -1252,6 +1675,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.feeding.pause = False pass + def send_tabWidget_control_change(self): if self.robotClient.status_model.curMode != 7: self.send_switch_tool_command() @@ -1266,23 +1690,28 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.send_start_tool_command() log.log_message(logging.INFO, Constant.str_sys_start_tool) - line_head = self.comboBox_lineIndex.currentData() self.send_clear_auto_command() log.log_message(logging.INFO, Constant.str_feed_reset_start) - #safe_position = self.feedLine_dict[line_head].safe_position + # safe_position = self.feedLine_dict[line_head].safe_position # self.send_position_command(safe_position.X, safe_position.Y, safe_position.Z, safe_position.U, safe_position.V, safe_position.W,move_type=MoveType.WORLD) - if self.remain_lineName != '': - line_head = f'{Constant.feedLine_set_section}{self.remain_lineName}' + if self.remain_lineName != "": + line_head = f"{Constant.feedLine_set_section}{self.remain_lineName}" return_positions = copy.deepcopy(self.feedLine_dict[line_head].positions) position_origin = PositionModel() position_origin.init_position(self.robotClient.origin_position) position_origin.status = 1 - return_positions.insert(0,position_origin) + return_positions.insert(0, position_origin) if self.feedLine_dict.keys().__contains__(line_head): - self.feeding.feedConfig= FeedingConfig(0, FeedLine(self.feedLine_dict[line_head].id, self.feedLine_dict[line_head].name, - return_positions), - self.feeding.robotClient.photo_locs[:]) + self.feeding.feedConfig = FeedingConfig( + 0, + FeedLine( + self.feedLine_dict[line_head].id, + self.feedLine_dict[line_head].name, + return_positions, + ), + self.feeding.robotClient.photo_locs[:], + ) else: log.log_message(logging.ERROR, Constant.str_feed_reset_no_line_error) return @@ -1295,7 +1724,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): # thread = threading.Thread(target=self.feeding.return_original_position()) # thread.start() - def stop_reset_thread(self): self.feeding.reset_status = ResetStatus.ROk self.send_clear_auto_command() @@ -1377,38 +1805,52 @@ class MainWindow(QMainWindow, Ui_MainWindow): # 主线程的逻辑就是 feeding.run() 接口和错误状态处理 while self.thread_signal: time.sleep(0.1) - if Constant.feedStatus: #feedStatus的状态打印 + if Constant.feedStatus: # feedStatus的状态打印 current_status = self.feeding.feedStatus is_paused = self.feeding.pause # 只有当状态或暂停标志发生变化时才打印 - if (current_status != self.last_status_printed or - is_paused != self.last_pause_printed): - print(f"[调试] 当前 feedStatus: {current_status}, 是否暂停: {is_paused}") + if ( + current_status != self.last_status_printed + or is_paused != self.last_pause_printed + ): + print( + f"[调试] 当前 feedStatus: {current_status}, 是否暂停: {is_paused}" + ) self.last_status_printed = current_status self.last_pause_printed = is_paused + # print("进入了主线程的 run") + # 获取状态和控制标志 current_feed_status = self.feeding.feedStatus - is_running = (current_feed_status == FeedStatus.FStart) and (not self.feeding.pause) #加上未暂停 + is_running = (current_feed_status == FeedStatus.FStart) and ( + not self.feeding.pause + ) # 加上未暂停 is_stopped = ( # 以下情况应关闭传感器 - current_feed_status in [FeedStatus.FFinished, FeedStatus.FNone, FeedStatus.FReverse, - FeedStatus.FStartReverse] - or self.feeding.pause # 暂停时也视为“停止” + current_feed_status + in [ + FeedStatus.FFinished, + FeedStatus.FNone, + FeedStatus.FReverse, + FeedStatus.FStartReverse, + ] + or self.feeding.pause # 暂停时也视为“停止” ) # 启动条件:处于 FStart 且未暂停,且线程未运行 if is_running and self.sensor_thread is None: + print("启动传感器线程..........!!!!!!!!!!!!!!!!!!!!!!") try: self.relay_controller._running = True self.sensor_thread = threading.Thread( target=self.relay_controller.handle_sensor1, name="Sensor1MonitorThread", - daemon=True + daemon=True, ) self.sensor_thread.start() self.sensor2_thread = threading.Thread( - target=self.relay_controller.handle_sensor2, - daemon=True) + target=self.relay_controller.handle_sensor2, daemon=True + ) self.sensor2_thread.start() print("🟢 传感器1监控线程已启动(开始投料)") print("🟢 传感器2监控线程已启动(关闭滚筒)") @@ -1417,6 +1859,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): # 关闭条件:暂停、完成、回退等状态 if is_stopped and self.sensor_thread is not None: + print("进入了传感器线程关闭流程") try: self.relay_controller._running = False if self.sensor_thread.is_alive(): @@ -1424,6 +1867,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): print("🛑 传感器监控线程已关闭(暂停/完成/回退)") self.relay_controller.close(conveyor2=True) except Exception as e: + print("传感器线程关闭流程发生了异常") log.log_message(logging.ERROR, f"关闭传感器线程异常: {e}") finally: self.sensor_thread = None @@ -1433,18 +1877,25 @@ class MainWindow(QMainWindow, Ui_MainWindow): # 处理命令队列 if not self.command_quene.empty(): command = self.command_quene.get() - if isinstance(command, FeedCommand) and command.status == Status.Prepareing: + if ( + isinstance(command, FeedCommand) + and command.status == Status.Prepareing + ): if self.feeding.feedStatus == FeedStatus.FNone: position_origin = PositionModel() position_origin.init_position(self.robotClient.origin_position) position_origin.status = 1 - command.feed_config.feedLine.feed_positions.insert(0, position_origin) + command.feed_config.feedLine.feed_positions.insert( + 0, position_origin + ) self.robotClient.send_emergency_sound() time.sleep(5) self.robotClient.send_emergency_stop() self.feeding.feedConfig = command.feed_config - self.feeding.feedStatus = FeedStatus.FStart # ✅ 投料开始,会触发传感器线程启动 + self.feeding.feedStatus = ( + FeedStatus.FStart + ) # ✅ 投料开始,会触发传感器线程启动 command.status = Status.Runing self.record_remain_num() @@ -1459,11 +1910,17 @@ class MainWindow(QMainWindow, Ui_MainWindow): try: if self.sensor_thread is not None: self.relay_controller._running = False + print( + "self.relay_controller._running: ", self.relay_controller._running + ) if self.sensor_thread.is_alive(): + print("进入 传感器线程等待") self.sensor_thread.join(timeout=1) print("⏹ 主程序退出,传感器线程已关闭") self.sensor_thread = None + print("self.sensor_thread: ", self.sensor_thread) except Exception as e: + print(f"主循环退出时关闭线程异常: {e}") log.log_message(logging.ERROR, f"主循环退出时关闭线程异常: {e}") def run_detect_persion(self): @@ -1479,7 +1936,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): # result = msg_box_person.exec() # if result == QMessageBox.StandardButton.Cancel: # return - log.log_message(logging.ERROR, '人员进入安全区') + log.log_message(logging.ERROR, "人员进入安全区") time.sleep(1) pass @@ -1493,21 +1950,27 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.set_label_status_style(False) if self.feeding.feedStatus != FeedStatus.FNone: self.horizontalSlider_feedingNum.setValue( - self.horizontalSlider_feedingNum.maximum() - self.feeding.feedConfig.num) - # self.label_remain_num.setText(str(self.feeding.feedConfig.num)) + self.horizontalSlider_feedingNum.maximum() + - self.feeding.feedConfig.num + ) + # self.label_remain_num.setText(str(self.feeding.feedConfig.num)) else: self.set_run_status_button(False) - if self.feeding.feedConfig!=None: - #修改点1:这里修改显示剩余袋数的逻辑 - #原来的逻辑是显示剩余袋数:self.label_remain_num.setText(self.feeding.feedConfig.num)) - #我改成了目标袋数减剩余袋数就是已经投料的袋数self.label_remain_num.setText(str(self.horizontalSlider_feedingNum.maximum()-self.feeding.feedConfig.num)) - self.label_remain_num.setText(str(self.horizontalSlider_feedingNum.maximum()-self.feeding.feedConfig.num)) + if self.feeding.feedConfig != None: + # 修改点1:这里修改显示剩余袋数的逻辑 + # 原来的逻辑是显示剩余袋数:self.label_remain_num.setText(self.feeding.feedConfig.num)) + # 我改成了目标袋数减剩余袋数就是已经投料的袋数self.label_remain_num.setText(str(self.horizontalSlider_feedingNum.maximum()-self.feeding.feedConfig.num)) + self.label_remain_num.setText( + str( + self.horizontalSlider_feedingNum.maximum() + - self.feeding.feedConfig.num + ) + ) if self.feeding.feedStatus == FeedStatus.FNone: self.stackedWidget_num.setCurrentIndex(0) else: self.stackedWidget_num.setCurrentIndex(1) - self.label_date.setText(datetime.now().strftime("%Y-%m-%d")) self.label_time.setText(datetime.now().strftime("%H:%M:%S")) @@ -1547,7 +2010,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): try: io_bits = self.robotClient.status_model.get_IO_bits() for index, bit in enumerate(io_bits): - toggle = self.dirt_io_points[f'y{index+1}'] + toggle = self.dirt_io_points[f"y{index+1}"] if toggle.is_on == bit: continue else: @@ -1555,48 +2018,62 @@ class MainWindow(QMainWindow, Ui_MainWindow): except Exception as e: log.log_message(logging.ERROR, Constant.str_sys_log_IO_error) pass + @Slot() - def updateUI_label_detection(self,img:np.ndarray): + def updateUI_label_detection(self, img: np.ndarray): img_copy = np.copy(img) backgroud_img = Util.util_pic.cv2_to_qpixmap(img_copy) if backgroud_img == None: return - backgroud_img = backgroud_img.scaled(self.label_showDetection.size().width(),self.label_showDetection.size().height(), Qt.AspectRatioMode.IgnoreAspectRatio,Qt.TransformationMode.SmoothTransformation) + backgroud_img = backgroud_img.scaled( + self.label_showDetection.size().width(), + self.label_showDetection.size().height(), + Qt.AspectRatioMode.IgnoreAspectRatio, + Qt.TransformationMode.SmoothTransformation, + ) self.label_showDetection.setPixmap(backgroud_img) - @Slot() - def log_message(self,level,message): - log.log_message(level,message) + def log_message(self, level, message): + log.log_message(level, message) def updateUI_Select_Line(self): self.comboBox_lineIndex.clear() for key, value in self.feedLine_dict.items(): self.comboBox_lineIndex.addItem(value.name, key) + def updateUI_label_status(self): - if self.robotClient.status_model.isMoving==1: + if self.robotClient.status_model.isMoving == 1: self.label_move_sign.show() - if self.label_move_sign.text() == '正在移动.': - self.label_move_sign.setText('正在移动..') + if self.label_move_sign.text() == "正在移动.": + self.label_move_sign.setText("正在移动..") else: - self.label_move_sign.setText('正在移动.') + self.label_move_sign.setText("正在移动.") else: self.label_move_sign.hide() if self.robotClient.status_model.curMode >= 0: try: - self.label_status_model.setText(Constant.mode_array[self.robotClient.status_model.curMode]) + self.label_status_model.setText( + Constant.mode_array[self.robotClient.status_model.curMode] + ) except: - self.label_status_model.setText('未知模式') - self.label_status_remoteCmdLen.setText(str(self.robotClient.status_model.RemoteCmdLen)) + self.label_status_model.setText("未知模式") + self.label_status_remoteCmdLen.setText( + str(self.robotClient.status_model.RemoteCmdLen) + ) if self.robotClient.status_model.curAlarm != 0: - log.log_message(logging.ERROR, Constant.str_sys_log_alarm_error+str(self.robotClient.status_model.curAlarm)) + log.log_message( + logging.ERROR, + Constant.str_sys_log_alarm_error + + str(self.robotClient.status_model.curAlarm), + ) self.feeding.pause = True self.label_status_alarmcode.setText(str(self.robotClient.status_model.curAlarm)) - self.label_status_toolIndex.setText(str(self.robotClient.status_model.toolCoord)) + self.label_status_toolIndex.setText( + str(self.robotClient.status_model.toolCoord) + ) self.label_status_speed.setText(str(self.robotClient.status_model.curSpeed_n)) - - def updateUI_Position(self): self.horizontalSlider_J1.setValue(self.status_address.axis_0) self.horizontalSlider_J2.setValue(self.status_address.axis_1) @@ -1619,62 +2096,91 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.horizontalSlider_J6.setValue(self.status_address.axis_5) def set_position_to_tabel(self, row_i, position_j, real_Position: Real_Position): - self.tableWidget_feedSeting.setItem(row_i, position_j * 6 + self.tableWidget_feedSeting_addtional_col_num, - QTableWidgetItem(str(real_Position.X))) - self.tableWidget_feedSeting.setItem(row_i, position_j * 6 + 1 + self.tableWidget_feedSeting_addtional_col_num, - QTableWidgetItem(str(real_Position.Y))) - self.tableWidget_feedSeting.setItem(row_i, position_j * 6 + 2 + self.tableWidget_feedSeting_addtional_col_num, - QTableWidgetItem(str(real_Position.Z))) - self.tableWidget_feedSeting.setItem(row_i, position_j * 6 + 3 + self.tableWidget_feedSeting_addtional_col_num, - QTableWidgetItem(str(real_Position.U))) - self.tableWidget_feedSeting.setItem(row_i, position_j * 6 + 4 + self.tableWidget_feedSeting_addtional_col_num, - QTableWidgetItem(str(real_Position.V))) - self.tableWidget_feedSeting.setItem(row_i, position_j * 6 + 5 + self.tableWidget_feedSeting_addtional_col_num, - QTableWidgetItem(str(real_Position.W))) - def updateUI_frame_sign(self,sign:FeedStatus): + self.tableWidget_feedSeting.setItem( + row_i, + position_j * 6 + self.tableWidget_feedSeting_addtional_col_num, + QTableWidgetItem(str(real_Position.X)), + ) + self.tableWidget_feedSeting.setItem( + row_i, + position_j * 6 + 1 + self.tableWidget_feedSeting_addtional_col_num, + QTableWidgetItem(str(real_Position.Y)), + ) + self.tableWidget_feedSeting.setItem( + row_i, + position_j * 6 + 2 + self.tableWidget_feedSeting_addtional_col_num, + QTableWidgetItem(str(real_Position.Z)), + ) + self.tableWidget_feedSeting.setItem( + row_i, + position_j * 6 + 3 + self.tableWidget_feedSeting_addtional_col_num, + QTableWidgetItem(str(real_Position.U)), + ) + self.tableWidget_feedSeting.setItem( + row_i, + position_j * 6 + 4 + self.tableWidget_feedSeting_addtional_col_num, + QTableWidgetItem(str(real_Position.V)), + ) + self.tableWidget_feedSeting.setItem( + row_i, + position_j * 6 + 5 + self.tableWidget_feedSeting_addtional_col_num, + QTableWidgetItem(str(real_Position.W)), + ) + + def updateUI_frame_sign(self, sign: FeedStatus): # print("start") if sign == FeedStatus.FNone: self.set_sign_status(self.label_sign_go, self.pushButton_sign_go, False) - self.set_sign_status(self.label_sign_photo, self.pushButton_sign_photo, False) + self.set_sign_status( + self.label_sign_photo, self.pushButton_sign_photo, False + ) self.set_sign_status(self.label_sign_take, self.pushButton_sign_take, False) self.set_sign_status(self.label_sign_feed, self.pushButton_sign_feed, False) return if sign < FeedStatus.FPhoto: - self.set_sign_status(self.label_sign_go,self.pushButton_sign_go,True) - self.set_sign_status(self.label_sign_photo, self.pushButton_sign_photo, False) + self.set_sign_status(self.label_sign_go, self.pushButton_sign_go, True) + self.set_sign_status( + self.label_sign_photo, self.pushButton_sign_photo, False + ) self.set_sign_status(self.label_sign_take, self.pushButton_sign_take, False) self.set_sign_status(self.label_sign_feed, self.pushButton_sign_feed, False) return if sign <= FeedStatus.FPhoto: self.set_sign_status(self.label_sign_go, self.pushButton_sign_go, False) - self.set_sign_status(self.label_sign_photo,self.pushButton_sign_photo,True) + self.set_sign_status( + self.label_sign_photo, self.pushButton_sign_photo, True + ) self.set_sign_status(self.label_sign_take, self.pushButton_sign_take, False) self.set_sign_status(self.label_sign_feed, self.pushButton_sign_feed, False) return if sign <= FeedStatus.FTake: self.set_sign_status(self.label_sign_go, self.pushButton_sign_go, False) - self.set_sign_status(self.label_sign_photo, self.pushButton_sign_photo, False) + self.set_sign_status( + self.label_sign_photo, self.pushButton_sign_photo, False + ) self.set_sign_status(self.label_sign_take, self.pushButton_sign_take, True) self.set_sign_status(self.label_sign_feed, self.pushButton_sign_feed, False) return if sign <= FeedStatus.FBroken1: self.set_sign_status(self.label_sign_go, self.pushButton_sign_go, False) - self.set_sign_status(self.label_sign_photo, self.pushButton_sign_photo, False) + self.set_sign_status( + self.label_sign_photo, self.pushButton_sign_photo, False + ) self.set_sign_status(self.label_sign_take, self.pushButton_sign_take, False) self.set_sign_status(self.label_sign_feed, self.pushButton_sign_feed, True) return pass - def set_label_status_style(self, connected: bool): - #return + # return if connected and not self.label_connect_status.isEnabled(): self.label_connect_status.setEnabled(True) if not connected and self.label_connect_status.isEnabled(): self.label_connect_status.setDisabled(True) - def set_sign_status(self,label,btn,signed:bool): - #return - try : + + def set_sign_status(self, label, btn, signed: bool): + # return + try: if signed and label.isEnabled(): label.setDisabled(True) btn.setDisabled(True) @@ -1697,8 +2203,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): except Exception as e: print(e) - - def set_run_status_button(self, isRuning: bool): self.pushButton_pauseFeed.setText("暂停") ##self.pushButton_startFeed.setEnabled(False) @@ -1714,14 +2218,25 @@ class MainWindow(QMainWindow, Ui_MainWindow): def send_clear_auto_command(self): clear_command = CMDInstructRequest() - clear_command.emptyList = '1' + clear_command.emptyList = "1" request_command = clear_command.toString() log.log_message(logging.INFO, Constant.str_sys_clearAlarm) self.robotClient.add_sendQuene(request_command) # self.command_quene.put(request_command) - def send_position_command(self, x1, x2, x3, x4, x5, x6, move_type: MoveType = MoveType.WORLD,speed=5,p2=None): - #ailaimi + def send_position_command( + self, + x1, + x2, + x3, + x4, + x5, + x6, + move_type: MoveType = MoveType.WORLD, + speed=5, + p2=None, + ): + # ailaimi position_instruction = Instruction() position_instruction.m0 = float(x1) position_instruction.m1 = float(x2) @@ -1735,19 +2250,21 @@ class MainWindow(QMainWindow, Ui_MainWindow): position_instruction.m1_p = p2.Y position_instruction.m2_p = p2.Z position_instruction.m3_p = p2.U - #position_instruction.m4_p = p2.V - #position_instruction.m5_p = p2.W + # position_instruction.m4_p = p2.V + # position_instruction.m5_p = p2.W instruction_command = CMDInstructRequest() instruction_command.instructions.append(position_instruction) request_command = instruction_command.toString() - log_str = f'移动到位置:{"自由路径" if move_type == MoveType.AXIS else "姿势直线"}:' \ - f'm0:{position_instruction.m0}-' \ - f'm2:{position_instruction.m1}-' \ - f'm3:{position_instruction.m2}-' \ - f'm4:{position_instruction.m3}-' - log.log_message(logging.INFO,log_str) - print("request_command",request_command) + log_str = ( + f'移动到位置:{"自由路径" if move_type == MoveType.AXIS else "姿势直线"}:' + f"m0:{position_instruction.m0}-" + f"m2:{position_instruction.m1}-" + f"m3:{position_instruction.m2}-" + f"m4:{position_instruction.m3}-" + ) + log.log_message(logging.INFO, log_str) + print("request_command", request_command) self.robotClient.add_sendQuene(request_command) def send_pause_command(self, pause: bool): @@ -1772,8 +2289,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.feeding.send_emergency_sound() self.feeding.feedConfig.num = 0 log.log_message(logging.INFO, Constant.str_sys_emergencyStop) - #TODO - + # TODO def send_clear_alarm_command(self, pause: bool): self.send_clear_auto_command() @@ -1806,11 +2322,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): msg_box.setStandardButtons(QMessageBox.Yes) msg_box.setDefaultButton(QMessageBox.Yes) - result = msg_box.exec() - - if result == QMessageBox.Yes: self.feeding.pause = False self.send_pause_command(False) @@ -1825,7 +2338,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.lineEdit_v1.setText(str(realPosition.V)) self.lineEdit_w1.setText(str(realPosition.W)) - def get_p2_button_click(self): realPosition = self.robotClient.status_model.getRealPosition() self.lineEdit_x2.setText(str(realPosition.X)) @@ -1835,7 +2347,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.lineEdit_v2.setText(str(realPosition.V)) self.lineEdit_w2.setText(str(realPosition.W)) - def get_p3_button_click(self): realPosition = self.robotClient.status_model.getRealPosition() self.lineEdit_x3.setText(str(realPosition.X)) @@ -1881,16 +2392,16 @@ class MainWindow(QMainWindow, Ui_MainWindow): v1 = float(self.lineEdit_v1.text()) w1 = float(self.lineEdit_w1.text()) - self.robotClient.photo_locs[0] = (x1, y1, z1,u1,v1,w1) + self.robotClient.photo_locs[0] = (x1, y1, z1, u1, v1, w1) self.configReader.read(Constant.set_ini) - self.configReader.set('Robot_Feed', 'photo_x1', str(x1)) - self.configReader.set('Robot_Feed', 'photo_y1', str(y1)) - self.configReader.set('Robot_Feed', 'photo_z1', str(z1)) - self.configReader.set('Robot_Feed', 'photo_u1', str(u1)) - self.configReader.set('Robot_Feed', 'photo_v1', str(v1)) - self.configReader.set('Robot_Feed', 'photo_w1', str(w1)) - self.configReader.write(open(Constant.set_ini,'w',encoding='utf-8')) - log.log_message(logging.INFO, f'设置拍照点1:{x1},{y1},{z1}') + self.configReader.set("Robot_Feed", "photo_x1", str(x1)) + self.configReader.set("Robot_Feed", "photo_y1", str(y1)) + self.configReader.set("Robot_Feed", "photo_z1", str(z1)) + self.configReader.set("Robot_Feed", "photo_u1", str(u1)) + self.configReader.set("Robot_Feed", "photo_v1", str(v1)) + self.configReader.set("Robot_Feed", "photo_w1", str(w1)) + self.configReader.write(open(Constant.set_ini, "w", encoding="utf-8")) + log.log_message(logging.INFO, f"设置拍照点1:{x1},{y1},{z1}") except: self.show_infomessage_box("设置拍照点1失败") @@ -1902,20 +2413,19 @@ class MainWindow(QMainWindow, Ui_MainWindow): u2 = float(self.lineEdit_u2.text()) v2 = float(self.lineEdit_v2.text()) w2 = float(self.lineEdit_w2.text()) - self.robotClient.photo_locs[1] = (x2, y2, z2,u2,v2,w2) + self.robotClient.photo_locs[1] = (x2, y2, z2, u2, v2, w2) self.configReader.read(Constant.set_ini) - self.configReader.set('Robot_Feed', 'photo_x2', str(x2)) - self.configReader.set('Robot_Feed', 'photo_y2', str(y2)) - self.configReader.set('Robot_Feed', 'photo_z2', str(z2)) - self.configReader.set('Robot_Feed', 'photo_u2', str(u2)) - self.configReader.set('Robot_Feed', 'photo_v2', str(v2)) - self.configReader.set('Robot_Feed', 'photo_w2', str(w2)) - self.configReader.write(open(Constant.set_ini, 'w', encoding='utf-8')) - log.log_message(logging.INFO, f'设置拍照点2:{x2},{y2},{z2}') + self.configReader.set("Robot_Feed", "photo_x2", str(x2)) + self.configReader.set("Robot_Feed", "photo_y2", str(y2)) + self.configReader.set("Robot_Feed", "photo_z2", str(z2)) + self.configReader.set("Robot_Feed", "photo_u2", str(u2)) + self.configReader.set("Robot_Feed", "photo_v2", str(v2)) + self.configReader.set("Robot_Feed", "photo_w2", str(w2)) + self.configReader.write(open(Constant.set_ini, "w", encoding="utf-8")) + log.log_message(logging.INFO, f"设置拍照点2:{x2},{y2},{z2}") except: self.show_infomessage_box("设置拍照点2失败") - def set_p3_button_click(self): try: x3 = float(self.lineEdit_x3.text()) @@ -1924,16 +2434,16 @@ class MainWindow(QMainWindow, Ui_MainWindow): u3 = float(self.lineEdit_u3.text()) v3 = float(self.lineEdit_v3.text()) w3 = float(self.lineEdit_w3.text()) - self.robotClient.photo_locs[2] = (x3, y3, z3,u3,v3,w3) + self.robotClient.photo_locs[2] = (x3, y3, z3, u3, v3, w3) self.configReader.read(Constant.set_ini) - self.configReader.set('Robot_Feed', 'photo_x3', str(x3)) - self.configReader.set('Robot_Feed', 'photo_y3', str(y3)) - self.configReader.set('Robot_Feed', 'photo_z3', str(z3)) - self.configReader.set('Robot_Feed', 'photo_u3', str(u3)) - self.configReader.set('Robot_Feed', 'photo_v3', str(v3)) - self.configReader.set('Robot_Feed', 'photo_w3', str(w3)) - self.configReader.write(open(Constant.set_ini, 'w', encoding='utf-8')) - log.log_message(logging.INFO, f'设置拍照点3:{x3},{y3},{z3}') + self.configReader.set("Robot_Feed", "photo_x3", str(x3)) + self.configReader.set("Robot_Feed", "photo_y3", str(y3)) + self.configReader.set("Robot_Feed", "photo_z3", str(z3)) + self.configReader.set("Robot_Feed", "photo_u3", str(u3)) + self.configReader.set("Robot_Feed", "photo_v3", str(v3)) + self.configReader.set("Robot_Feed", "photo_w3", str(w3)) + self.configReader.write(open(Constant.set_ini, "w", encoding="utf-8")) + log.log_message(logging.INFO, f"设置拍照点3:{x3},{y3},{z3}") except: self.show_infomessage_box("设置拍照点3失败") @@ -1945,16 +2455,16 @@ class MainWindow(QMainWindow, Ui_MainWindow): u4 = float(self.lineEdit_u4.text()) v4 = float(self.lineEdit_v4.text()) w4 = float(self.lineEdit_w4.text()) - self.robotClient.photo_locs[3] = (x4, y4, z4,u4,v4,w4) + self.robotClient.photo_locs[3] = (x4, y4, z4, u4, v4, w4) self.configReader.read(Constant.set_ini) - self.configReader.set('Robot_Feed', 'photo_x4', str(x4)) - self.configReader.set('Robot_Feed', 'photo_y4', str(y4)) - self.configReader.set('Robot_Feed', 'photo_z4', str(z4)) - self.configReader.set('Robot_Feed', 'photo_u4', str(u4)) - self.configReader.set('Robot_Feed', 'photo_v4', str(v4)) - self.configReader.set('Robot_Feed', 'photo_w4', str(w4)) - self.configReader.write(open(Constant.set_ini, 'w', encoding='utf-8')) - log.log_message(logging.INFO, f'设置拍照点4:{x4},{y4},{z4}') + self.configReader.set("Robot_Feed", "photo_x4", str(x4)) + self.configReader.set("Robot_Feed", "photo_y4", str(y4)) + self.configReader.set("Robot_Feed", "photo_z4", str(z4)) + self.configReader.set("Robot_Feed", "photo_u4", str(u4)) + self.configReader.set("Robot_Feed", "photo_v4", str(v4)) + self.configReader.set("Robot_Feed", "photo_w4", str(w4)) + self.configReader.write(open(Constant.set_ini, "w", encoding="utf-8")) + log.log_message(logging.INFO, f"设置拍照点4:{x4},{y4},{z4}") except: self.show_infomessage_box("设置拍照点4失败") @@ -1966,18 +2476,19 @@ class MainWindow(QMainWindow, Ui_MainWindow): u5 = float(self.lineEdit_u5.text()) v5 = float(self.lineEdit_v5.text()) w5 = float(self.lineEdit_w5.text()) - self.robotClient.photo_locs[4] = (x5, y5, z5,u5,v5,w5) + self.robotClient.photo_locs[4] = (x5, y5, z5, u5, v5, w5) self.configReader.read(Constant.set_ini) - self.configReader.set('Robot_Feed', 'photo_x5', str(x5)) - self.configReader.set('Robot_Feed', 'photo_y5', str(y5)) - self.configReader.set('Robot_Feed', 'photo_z5', str(z5)) - self.configReader.set('Robot_Feed', 'photo_u5', str(u5)) - self.configReader.set('Robot_Feed', 'photo_v5', str(v5)) - self.configReader.set('Robot_Feed', 'photo_w5', str(w5)) - self.configReader.write(open(Constant.set_ini, 'w', encoding='utf-8')) - log.log_message(logging.INFO, f'设置拍照点5:{x5},{y5},{z5}') + self.configReader.set("Robot_Feed", "photo_x5", str(x5)) + self.configReader.set("Robot_Feed", "photo_y5", str(y5)) + self.configReader.set("Robot_Feed", "photo_z5", str(z5)) + self.configReader.set("Robot_Feed", "photo_u5", str(u5)) + self.configReader.set("Robot_Feed", "photo_v5", str(v5)) + self.configReader.set("Robot_Feed", "photo_w5", str(w5)) + self.configReader.write(open(Constant.set_ini, "w", encoding="utf-8")) + log.log_message(logging.INFO, f"设置拍照点5:{x5},{y5},{z5}") except: self.show_infomessage_box("设置拍照点5失败") + def set_origin_button_click(self): try: x = float(self.lineEdit_origin_x.text()) @@ -1987,23 +2498,21 @@ class MainWindow(QMainWindow, Ui_MainWindow): v = float(self.lineEdit_origin_v.text()) w = float(self.lineEdit_origin_w.text()) origin_position = Real_Position() - origin_position.init_position(x,y,z,u,v,w) + origin_position.init_position(x, y, z, u, v, w) self.robotClient.origin_position = origin_position self.configReader.read(Constant.set_ini) - self.configReader.set('Origin', 'X', str(x)) - self.configReader.set('Origin', 'Y', str(y)) - self.configReader.set('Origin', 'Z', str(z)) - self.configReader.set('Origin', 'U', str(u)) - self.configReader.set('Origin', 'V', str(v)) - self.configReader.set('Origin', 'W', str(w)) - self.configReader.write(open(Constant.set_ini, 'w', encoding='utf-8')) - log.log_message(logging.INFO, f'设置原点:{x},{y},{z}') + self.configReader.set("Origin", "X", str(x)) + self.configReader.set("Origin", "Y", str(y)) + self.configReader.set("Origin", "Z", str(z)) + self.configReader.set("Origin", "U", str(u)) + self.configReader.set("Origin", "V", str(v)) + self.configReader.set("Origin", "W", str(w)) + self.configReader.write(open(Constant.set_ini, "w", encoding="utf-8")) + log.log_message(logging.INFO, f"设置原点:{x},{y},{z}") except: - log.log_message(logging.ERROR, f'设置原点失败') + log.log_message(logging.ERROR, f"设置原点失败") self.show_infomessage_box("设置原点失败") - - def updateUI_Photo_Set(self): self.lineEdit_x1.setText(str(self.robotClient.photo_locs[0][0])) self.lineEdit_y1.setText(str(self.robotClient.photo_locs[0][1])) @@ -2036,6 +2545,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.lineEdit_v5.setText(str(self.robotClient.photo_locs[4][4])) self.lineEdit_w5.setText(str(self.robotClient.photo_locs[4][5])) pass + def updateUI_Base_Set(self): self.lineEdit_speed_run.setText(str(self.robotClient.feed_speed)) self.lineEdit_speed_debug.setText(str(self.robotClient.debug_speed)) @@ -2049,15 +2559,16 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.lineEdit_timedelay_shake.setText(str(self.robotClient.time_delay_shake)) self.lineEdit_setting_height.setText(str(self.robotClient.dynamic_height)) - def show_infomessage_box(self,message): - print('显示弹窗') + def show_infomessage_box(self, message): + # print("显示弹窗") msg_box = QMessageBox(self) msg_box.setWindowTitle("提示") msg_box.setText(message) msg_box.setIcon(QMessageBox.Icon.Information) msg_box.setStandardButtons(QMessageBox.StandardButton.Ok) msg_box.show() - def send_click_change_stackView(self,index): + + def send_click_change_stackView(self, index): self.stackedWidget_view.setCurrentIndex(index) if index == 0: self.updateUI_Select_Line() @@ -2067,7 +2578,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): if index == 3: self.updateUI_Base_Set() - #修改点2:设置一键投料的目标袋数从999设置到40袋 + # 修改点2:设置一键投料的目标袋数从999设置到40袋 def send_onekeyfeed_button_click(self): # if self.feeding.feedStatus != FeedStatus.FNone: # self.show_infomessage_box("正在执行") @@ -2088,42 +2599,63 @@ class MainWindow(QMainWindow, Ui_MainWindow): def send_click_sysmeuExpand(self): self.frame_sys_seting.setVisible(not self.frame_sys_seting.isVisible()) if self.frame_2.sizePolicy().verticalPolicy() == QSizePolicy.Policy.Fixed: - self.frame_2.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding) + self.frame_2.setSizePolicy( + QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding + ) else: - self.frame_2.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed) + self.frame_2.setSizePolicy( + QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed + ) - def send_IO_Point_OpenOrClose(self,index,togger): + def send_IO_Point_OpenOrClose(self, index, togger): if togger: - self.feeding.sendIOControl(index-1, 1) - log.log_message(logging.INFO, f'打开IO{index}') + self.feeding.sendIOControl(index - 1, 1) + log.log_message(logging.INFO, f"打开IO{index}") else: - self.feeding.sendIOControl(index-1, 0) - log.log_message(logging.INFO, f'关闭IO{index}') + self.feeding.sendIOControl(index - 1, 0) + log.log_message(logging.INFO, f"关闭IO{index}") def showEvent(self, event): super().showEvent(event) QTimer.singleShot(2000, self.check_continue) - def closeEvent(self, event): + self.hide() # 用户体验,先将窗口隐藏掉 + print("self.record_remain_num") self.record_remain_num() - # self.feeding.is_detected = False - # self.feeding.detect_thread.join() + print("self.feeding.close_feed()") self.feeding.close_feed() self.thread_signal = False + print("self.detect_person_thread.join()") + self.detect_person_thread.join() + print("self.main_UI_threading.join()") + self.main_UI_threading.join() + print("self.main_threading.join()") + self.main_threading.join() + print("after self.main_threading.join()") self.robotClient.close() + print("self.robot_connect_threading.join()") + self.robot_connect_threading.join() log.log_message(logging.INFO, Constant.str_sys_exit) + print(" closeEvent over") + # os._exit(0) - #记录投料袋数 + # 记录投料袋数 def record_remain_num(self): - try: - self.configReader = configparser.ConfigParser() - self.configReader.read(Constant.set_ini) - self.configReader.set('Robot_Feed', 'remain_linename', str(self.feeding.feedConfig.feedLine.id)) - self.configReader.set('Robot_Feed', 'remain_count', str(self.feeding.feedConfig.num)) - self.configReader.write(open(Constant.set_ini, 'w', encoding='utf-8')) - except: - log.log_message(logging.ERROR, Constant.str_sys_log_feedNum) + try: + self.configReader = configparser.ConfigParser() + self.configReader.read(Constant.set_ini) + self.configReader.set( + "Robot_Feed", + "remain_linename", + str(self.feeding.feedConfig.feedLine.id), + ) + self.configReader.set( + "Robot_Feed", "remain_count", str(self.feeding.feedConfig.num) + ) + self.configReader.write(open(Constant.set_ini, "w", encoding="utf-8")) + except: + log.log_message(logging.ERROR, Constant.str_sys_log_feedNum) class MyApplication(QApplication): @@ -2138,6 +2670,7 @@ class MyApplication(QApplication): # 可以在这里记录日志或者执行其他操作 return False + def handle_exception(exc_type, exc_value, exc_tb): if issubclass(exc_type, KeyboardInterrupt): log.log_message(logging.INFO, "用户主动退出程序") @@ -2145,14 +2678,14 @@ def handle_exception(exc_type, exc_value, exc_tb): log.log_message(logging.ERROR, exc_value) traceback.print_exception(exc_type, exc_value, exc_tb) + if __name__ == "__main__": app = MyApplication(sys.argv) window = MainWindow() window.show() sys.excepthook = handle_exception - try : + try: sys.exit(app.exec()) + print("after sys.exit(app.exec())") except Exception as e: log.log_message(logging.ERROR, e) - -