import configparser import json import logging import queue import sys from multiprocessing import Process from PyQt5.uic.properties import QtWidgets from PySide6.QtCore import QThread, Signal, Slot, QObject, QEvent from PySide6.QtGui import QIntValidator, QStandardItemModel, QStandardItem, Qt, QMovie from PySide6.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QHeaderView, QTableWidget, \ QTableWidgetItem, QWidget, QHBoxLayout, QAbstractItemView, QMessageBox from datetime import datetime import Util.util_pic from Util.util_ini import writeFeedLine_to_ini import Constant from CU.Command import FeedCommand from CU.Feeding import FeedLine, FeedingConfig, Feeding, FeedStatus from Util.util_log import QTextEditLogger from Vision.camera_coordinate_dete import Detection from ui_untitled import Ui_MainWindow from COM.COM_Robot import RobotClient from Expection import Error_Code from queue import Queue from Model.RobotModel import * import time from queue import Queue from Model.Position import Real_Position, Detection_Position from threading import Thread from CU.Command import Status from Util.util_log import log class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self): super(MainWindow, self).__init__() self.setupUi(self) self.init_UI() self.init_Run() self.init_robot_info() self.init_FeedLine() self.start_Runing() self.init_log() self.init_qss() def init_qss(self): self.active_status_Qss = """ background-color: #A2EF4D; color: #ffffff; border-radius: 8px; border: 1px solid #A2EF4D; qproperty-alignment: 'AlignCenter'; """ self.noActive_status_Qss = """ background-color: #FD3251; color: #ffffff; border-radius: 8px; border: 1px solid #FD3251; qproperty-alignment: 'AlignCenter'; """ self.active_label_sign_Qss = """ background-color: rgba(255, 255, 255, 0); font: 700 12pt "Microsoft YaHei UI"; color: black; """ self.active_btn_sign_Qss = """ background-color: rgb(255, 0, 0); font: 9pt "楷体"; border: 1px solid #dcdfe6; border-radius: 5px; """ self.noactive_label_sign_Qss = """ background-color: rgba(255, 255, 255, 0); font: 700 12pt "Microsoft YaHei UI"; color: #7A7A7A; """ self.noactive_btn_sign_Qss = """ background-color: #7A7A7A; font: 9pt "楷体"; border: 1px solid #dcdfe6; border-radius: 5px; """ def init_log(self): log.init_log(self.textEdit_log_info, self.textEdit_log_error, Constant.log_file_path) def init_UI(self): self.pushButton_num1.clicked.connect(self.send_num_button_click) self.pushButton_num2.clicked.connect(self.send_num_button_click) self.pushButton_num3.clicked.connect(self.send_num_button_click) self.pushButton_num4.clicked.connect(self.send_num_button_click) self.pushButton_num5.clicked.connect(self.send_num_button_click) self.pushButton_num6.clicked.connect(self.send_num_button_click) self.pushButton_AddNum.clicked.connect(self.send_addNum_button_click) self.pushButton_SubNum.clicked.connect(self.send_subNum_button_click) self.pushButton_num_free.clicked.connect(self.send_num_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) self.lineEdit_j4.returnPressed.connect(self.send_position_returnPressed) self.lineEdit_j5.returnPressed.connect(self.send_position_returnPressed) self.lineEdit_j6.returnPressed.connect(self.send_position_returnPressed) self.pushButton_j1_sub.clicked.connect(self.send_subOneAsix_button_click) self.pushButton_j2_sub.clicked.connect(self.send_subOneAsix_button_click) self.pushButton_j3_sub.clicked.connect(self.send_subOneAsix_button_click) self.pushButton_j4_sub.clicked.connect(self.send_subOneAsix_button_click) self.pushButton_j5_sub.clicked.connect(self.send_subOneAsix_button_click) self.pushButton_j6_sub.clicked.connect(self.send_subOneAsix_button_click) self.pushButton_j1_add.clicked.connect(self.send_addOneAsix_button_click) self.pushButton_j2_add.clicked.connect(self.send_addOneAsix_button_click) self.pushButton_j3_add.clicked.connect(self.send_addOneAsix_button_click) self.pushButton_j4_add.clicked.connect(self.send_addOneAsix_button_click) self.pushButton_j5_add.clicked.connect(self.send_addOneAsix_button_click) self.pushButton_j6_add.clicked.connect(self.send_addOneAsix_button_click) self.pushButton_stopFeed.clicked.connect(self.send_stopFeed_button_click) self.pushButton_pauseFeed.clicked.connect(self.send_pauseFeed_button_click) self.pushButton_clearAlarm.clicked.connect(self.send_clear_alarm_command) self.pushButton_reset.clicked.connect(self.send_reset_button_click) self.pushButton_speed.setText(str(Constant.speed)) self.pushButton_speed.clicked.connect(self.send_setSpeed_label_doubelClick) self.lineEdit_speed.returnPressed.connect(self.send_setSpeed_lineEdit_returePressed) self.lineEdit_num.returnPressed.connect(self.send_custom_num_returnPressed) self.tabWidget_control.currentChanged.connect(self.send_tabWidget_control_change) int_validator = QIntValidator(0, 100, self.lineEdit_num) self.lineEdit_num.setValidator(int_validator) # self.horizontalSlider_J1.sliderReleased self.pushButton_startFeed.clicked.connect(self.send_startFeed_button_click) self.pushButton_stack_feedControl.clicked.connect( lambda _, index=0: self.send_stack_feedSet_button_click(index)) self.pushButton_stack_feedSet.clicked.connect(lambda _, index=1: self.send_stack_feedSet_button_click(index)) self.horizontalSlider_feedingNum.blockSignals(True) self.horizontalSlider_feedingNum.setMinimum(0) self.lineEdit_speed.hide() self.pushButton_stopFeed.hide() self.pushButton_pauseFeed.hide() def init_Run(self): self.robotClient = None self.configReader = configparser.ConfigParser() self.detection = Detection() self.command_position_quene = Queue() self.status_address = DataAddress() self.feedLine_dict = {} self.command_quene = Queue() self.main_threading = None self.configReader.read(Constant.set_ini) ip = self.configReader.get('Robot_Feed', 'IPAddress') port = int(self.configReader.get('Robot_Feed', 'Port')) self.robotClient = RobotClient(ip, port, self.command_position_quene, self.status_address) self.feeding = Feeding(self.robotClient, self.detection) # 临时 self.last_time = time.time() try: self.robotClient.CreatConnect() except: self.set_label_status_style(False) return Error_Code.SYS_NETERROR if self.robotClient.connected: return 0 else: return Error_Code.SYS_NETERROR def init_seting_frame(self): rows = len(self.feedLine_dict.keys()) + 1 self.tableWidget_feedSeting.setRowCount(rows) self.tableWidget_feedSeting.setColumnCount(20) # 设置第一重表头的合并 (三列一组) 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.itemChanged.connect(self.send_tabelFeedSet_itemChanged) btn_safe = QPushButton("获取安全位置") widget_safe = QWidget() layout_safe = QHBoxLayout() layout_safe.addWidget(btn_safe) # 调整布局的间距,使之更紧凑 layout_safe.setContentsMargins(0, 0, 0, 0) # 将布局设置到 QWidget 容器中 widget_safe.setLayout(layout_safe) btn_photo = QPushButton("获取拍照位置") widget_photo = QWidget() layout_photo = QHBoxLayout() layout_photo.addWidget(btn_photo) # 调整布局的间距,使之更紧凑 layout_photo.setContentsMargins(0, 0, 0, 0) # 将布局设置到 QWidget 容器中 widget_photo.setLayout(layout_photo) btn_feed = QPushButton("获取投料位置") widget_feed = QWidget() layout_feed = QHBoxLayout() layout_feed.addWidget(btn_feed) # 调整布局的间距,使之更紧凑 layout_feed.setContentsMargins(0, 0, 0, 0) # 将布局设置到 QWidget 容器中 widget_feed.setLayout(layout_feed) btn_safe.clicked.connect(self.send_get_safe_position_button_click) btn_photo.clicked.connect(self.send_get_photo_position_button_click) btn_feed.clicked.connect(self.send_get_feed_position_button_click) # 添加第一重表头项 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_photo) self.tableWidget_feedSeting.setCellWidget(0, 12 + self.tableWidget_feedSeting_addtional_col_num, widget_feed) # 设置在合并的第2组 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']) 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.set_position_to_tabel(row + 1, 0, feed_line.safe_position) self.set_position_to_tabel(row + 1, 1, feed_line.photo_position) self.set_position_to_tabel(row + 1, 2, feed_line.feed_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_save.clicked.connect(self.send_tabelFeedSet_save) def init_FeedLine(self): line_count = self.configReader.get('Robot_Feed', 'LineCount', fallback=0) self.configReader.read(Constant.feedLine_set_file, encoding='utf-8') for i in range(int(line_count)): line_str = f'FeedLine{i + 1}' safe_position = Real_Position() photo_position = Real_Position() mid_position = Real_Position() broken_position = Real_Position() feed_position = Real_Position() safe_position.X = float(self.configReader.get(line_str, 'SafePosition_x', fallback=0)) safe_position.Y = float(self.configReader.get(line_str, 'SafePosition_y', fallback=0)) safe_position.Z = float(self.configReader.get(line_str, 'SafePosition_z', fallback=0)) safe_position.U = float(self.configReader.get(line_str, 'SafePosition_u', fallback=0)) safe_position.V = float(self.configReader.get(line_str, 'SafePosition_v', fallback=0)) safe_position.W = float(self.configReader.get(line_str, 'SafePosition_w', fallback=0)) photo_position.X = float(self.configReader.get(line_str, 'PhotoPosition_x', fallback=0)) photo_position.Y = float(self.configReader.get(line_str, 'PhotoPosition_y', fallback=0)) photo_position.Z = float(self.configReader.get(line_str, 'PhotoPosition_z', fallback=0)) photo_position.U = float(self.configReader.get(line_str, 'PhotoPosition_u', fallback=0)) photo_position.V = float(self.configReader.get(line_str, 'PhotoPosition_v', fallback=0)) photo_position.W = float(self.configReader.get(line_str, 'PhotoPosition_w', fallback=0)) mid_position.X = float(self.configReader.get(line_str, 'MidPosition_x', fallback=0)) mid_position.Y = float(self.configReader.get(line_str, 'MidPosition_y', fallback=0)) mid_position.Z = float(self.configReader.get(line_str, 'MidPosition_z', fallback=0)) mid_position.U = float(self.configReader.get(line_str, 'MidPosition_u', fallback=0)) mid_position.V = float(self.configReader.get(line_str, 'MidPosition_v', fallback=0)) mid_position.W = float(self.configReader.get(line_str, 'MidPosition_w', fallback=0)) broken_position.X = float(self.configReader.get(line_str, 'BrokenPosition_x', fallback=0)) broken_position.Y = float(self.configReader.get(line_str, 'BrokenPosition_y', fallback=0)) broken_position.Z = float(self.configReader.get(line_str, 'BrokenPosition_z', fallback=0)) broken_position.U = float(self.configReader.get(line_str, 'BrokenPosition_u', fallback=0)) broken_position.V = float(self.configReader.get(line_str, 'BrokenPosition_v', fallback=0)) broken_position.W = float(self.configReader.get(line_str, 'BrokenPosition_w', fallback=0)) feed_position.X = float(self.configReader.get(line_str, 'FeedPosition_x', fallback=0)) feed_position.Y = float(self.configReader.get(line_str, 'FeedPosition_y', fallback=0)) feed_position.Z = float(self.configReader.get(line_str, 'FeedPosition_z', fallback=0)) feed_position.U = float(self.configReader.get(line_str, 'FeedPosition_u', fallback=0)) feed_position.V = float(self.configReader.get(line_str, 'FeedPosition_v', fallback=0)) feed_position.W = float(self.configReader.get(line_str, 'FeedPosition_w', fallback=0)) name = self.configReader.get(line_str, 'Name', fallback='未命名') self.feedLine_dict[f'{Constant.feedLine_set_section}{i + 1}'] = FeedLine(name, safe_position, photo_position, mid_position, broken_position, feed_position) self.init_seting_frame() 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')) self.horizontalSlider_J1.setMinimum(j1_min) self.horizontalSlider_J1.setMaximum(j1_max) self.horizontalSlider_J2.setMinimum(j2_min) self.horizontalSlider_J2.setMaximum(j2_max) self.horizontalSlider_J3.setMinimum(j3_min) self.horizontalSlider_J3.setMaximum(j3_max) self.horizontalSlider_J4.setMinimum(j4_min) self.horizontalSlider_J4.setMaximum(j4_max) self.horizontalSlider_J5.setMinimum(j5_min) self.horizontalSlider_J5.setMaximum(j5_max) self.horizontalSlider_J6.setMinimum(j6_min) self.horizontalSlider_J6.setMaximum(j6_max) self.label_j1_min.setText(j1_min.__str__()) self.label_j1_max.setText(j1_max.__str__()) self.label_j2_min.setText(j2_min.__str__()) self.label_j2_max.setText(j2_max.__str__()) self.label_j3_min.setText(j3_min.__str__()) self.label_j3_max.setText(j3_max.__str__()) self.label_j4_min.setText(j4_min.__str__()) self.label_j4_max.setText(j4_max.__str__()) self.label_j5_min.setText(j5_min.__str__()) self.label_j5_max.setText(j5_max.__str__()) self.label_j6_min.setText(j6_min.__str__()) self.label_j6_max.setText(str(j6_max)) def start_Runing(self): self.main_threading = Thread(target=self.run) self.robot_connect_threading = Thread(target=self.robotClient.run) self.main_threading.start() self.robot_connect_threading.start() pass def send_startFeed_button_click(self): # 触发自动运行 if self.robotClient.status_model.curMode != 7: self.send_switch_tool_command() log.log_message(logging.INFO, Constant.str_sys_switch_tool) self.send_start_tool_command() log.log_message(logging.INFO, Constant.str_sys_start_tool) num = self.horizontalSlider_feedingNum.maximum() line_head = self.comboBox_lineIndex.currentData() self.command_quene.put(FeedCommand(FeedingConfig(num, self.feedLine_dict[line_head]))) self.stackedWidget_num.setCurrentIndex(1) self.set_run_status_button(True) 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() if button.text() != "自定义": num = int(button.text()) self.horizontalSlider_feedingNum.setMaximum(num) self.label_maxNum.setText(str(num)) self.horizontalSlider_feedingNum.setValue(0) else: self.pushButton_num_free.hide() self.lineEdit_num.show() def send_subOneAsix_button_click(self): btn_str = self.sender().objectName() Constant.manual_adjust_accuracy = float(self.lineEdit_manual_adjust_accuracy.text()) x1 = self.horizontalSlider_J1.value() x2 = self.horizontalSlider_J2.value() x3 = self.horizontalSlider_J3.value() x4 = self.horizontalSlider_J4.value() x5 = self.horizontalSlider_J5.value() x6 = self.horizontalSlider_J6.value() if 'j1' in btn_str: x1 = x1 - Constant.manual_adjust_accuracy elif 'j2' in btn_str: x2 = x2 - Constant.manual_adjust_accuracy pass elif 'j3' in btn_str: x3 = x3 - Constant.manual_adjust_accuracy pass elif 'j4' in btn_str: x4 = x4 - Constant.manual_adjust_accuracy pass elif 'j5' in btn_str: x5 = x5 - Constant.manual_adjust_accuracy pass 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()) x1 = self.horizontalSlider_J1.value() x2 = self.horizontalSlider_J2.value() x3 = self.horizontalSlider_J3.value() x4 = self.horizontalSlider_J4.value() x5 = self.horizontalSlider_J5.value() x6 = self.horizontalSlider_J6.value() if 'j1' in btn_str: x1 = x1 + Constant.manual_adjust_accuracy elif 'j2' in btn_str: x2 = x2 + Constant.manual_adjust_accuracy pass elif 'j3' in btn_str: x3 = x3 + Constant.manual_adjust_accuracy pass elif 'j4' in btn_str: x4 = x4 + Constant.manual_adjust_accuracy pass elif 'j5' in btn_str: x5 = x5 + Constant.manual_adjust_accuracy pass 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) def send_addNum_button_click(self): self.feeding.feedConfig.num += 1 log.log_message(logging.INFO, Constant.str_sys_feedNum_add) max_num = int(self.label_maxNum.text()) + 1 self.horizontalSlider_feedingNum.setMaximum(max_num) self.label_maxNum.setText(str(max_num)) def send_subNum_button_click(self): if self.feeding.feedConfig.num <= 1: #self.send_stopFeed_button_click() self.feeding.feedStatus = FeedStatus.FNone # 清空运行命令 self.send_clear_auto_command() log.log_message(logging.INFO, Constant.str_feed_stop) return self.feeding.feedConfig.num -= 1 log.log_message(logging.INFO, Constant.str_sys_feedNum_sub) 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): self.pushButton_num_free.show() self.lineEdit_num.hide() 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()} ') def send_stack_feedSet_button_click(self, index): # self.logger.info("This is an info message") self.stackedWidget_feed.setCurrentIndex(index) if index == 0: self.updateUI_Select_Line() def on_button_click(self): self.button.setText("Clicked!") def send_position_button_click(self): # if True: # cmd_command = CMDRequest() # cmd_command.cmdData = ['rewriteData', '800', f'{position_instruction.m0}', 0] # request_command = vars(cmd_command) # self.robotClient.add_sendQuene(request_command) return def send_position_returnPressed(self): log.log_message(logging.INFO, Constant.str_sys_manualPosition) 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())) def send_setSpeed_label_doubelClick(self): self.pushButton_speed.hide() self.lineEdit_speed.show() pass def send_setSpeed_lineEdit_returePressed(self): Constant.speed = int(self.lineEdit_speed.text()) self.lineEdit_speed.hide() self.pushButton_speed.setText(str(Constant.speed)) self.pushButton_speed.show() log.log_message(logging.INFO, Constant.str_sys_setSpeed + str(Constant.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.set_position_to_tabel(row_i, 0, real_position) def send_get_photo_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].photo_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_feed_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].feed_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_tabelFeedSet_addRow(self): for i in range(1, 20): 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) safe_position = Real_Position() photo_position = Real_Position() feed_position = Real_Position() self.feedLine_dict[head] = FeedLine('新建', safe_position, photo_position, feed_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, photo_position) self.set_position_to_tabel(self.tableWidget_feedSeting.rowCount() - 1, 2, feed_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(): self.feedLine_dict.pop(feed_line_key) self.tableWidget_feedSeting.removeRow(selectRow) break else: QMessageBox.information(self, "提示", "请选择要删除的行") 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)) writeFeedLine_to_ini(self.feedLine_dict, Constant.feedLine_set_file) pass def send_stopFeed_button_click(self): # 清空状态 self.feeding.feedStatus = FeedStatus.FNone # 清空运行命令 self.send_clear_auto_command() log.log_message(logging.INFO, Constant.str_feed_stop) def send_pauseFeed_button_click(self): if self.pushButton_pauseFeed.text() == '暂停': self.pushButton_pauseFeed.setText('继续') log.log_message(logging.INFO, Constant.str_feed_pause) self.send_pause_command(True) self.feeding.pause = True else: self.pushButton_pauseFeed.setText('暂停') self.send_pause_command(False) log.log_message(logging.INFO, Constant.str_feed_continue) self.send_start_tool_command() log.log_message(logging.INFO, Constant.str_sys_start_tool) self.feeding.pause = False pass def send_tabWidget_control_change(self): if self.robotClient.status_model.curMode != 7: self.send_switch_tool_command() log.log_message(logging.INFO, Constant.str_sys_switch_tool) self.send_start_tool_command() def send_reset_button_click(self): line_head = self.comboBox_lineIndex.currentData() 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) def send_tabelFeedSet_itemChanged(self, item): row = item.row() column = item.column() value = item.text() head = self.tableWidget_feedSeting.item(row, 0).text() if column == 1: self.feedLine_dict[head].name = value elif column == (0 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].safe_position.X = float(value) elif column == (1 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].safe_position.Y = float(value) elif column == (2 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].safe_position.Z = float(value) elif column == (3 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].safe_position.U = float(value) elif column == (4 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].safe_position.V = float(value) elif column == (5 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].safe_position.W = float(value) # elif column == 7: elif column == (6 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].photo_position.X = float(value) elif column == (7 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].photo_position.Y = float(value) elif column == (8 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].photo_position.Z = float(value) elif column == (9 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].photo_position.U = float(value) elif column == (10 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].photo_position.V = float(value) elif column == (11 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].photo_position.W = float(value) elif column == (12 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].feed_position.X = float(value) elif column == (13 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].feed_position.Y = float(value) elif column == (14 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].feed_position.Z = float(value) elif column == (15 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].feed_position.U = float(value) elif column == (16 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].feed_position.V = float(value) elif column == (17 + self.tableWidget_feedSeting_addtional_col_num): self.feedLine_dict[head].feed_position.W = float(value) def run(self): while True: time.sleep(0.1) if not self.command_quene.empty(): command = self.command_quene.get() if isinstance(command, FeedCommand) and command.status == Status.Prepareing: if self.feeding.feedStatus == FeedStatus.FNone: self.feeding.feedConfig = command.feed_config self.feeding.feedStatus = FeedStatus.FStart command.status = Status.Runing try: self.feeding.run() except: print(Error_Code.SYS_NONEPoint) self.updateUI() # pass #主线程 def updateUI(self): if self.robotClient.connected: self.set_label_status_style(True) else: self.set_label_status_style(False) if self.feeding.feedStatus != FeedStatus.FNone: self.horizontalSlider_feedingNum.setValue( self.horizontalSlider_feedingNum.maximum() - self.feeding.feedConfig.num) else: self.set_run_status_button(False) if self.feeding.feedStatus == FeedStatus.FNone: self.stackedWidget_num.setCurrentIndex(0) else: self.stackedWidget_num.setCurrentIndex(1) if self.feeding.feedConfig.num==2: self.feeding.pause=True self.send_pause_command(True) self.show_messagebox_of_person() log.log_message(logging.ERROR, '人员进入安全区') self.label_date.setText(datetime.now().strftime("%Y-%m-%d")) self.label_time.setText(datetime.now().strftime("%H:%M:%S")) self.updateUI_Position() self.updateUI_label_detection() self.updateUI_label_status() self.updateUI_frame_sign(self.feeding.feedStatus) def updateUI_label_detection(self): backgroud_img = Util.util_pic.cv2_to_qpixmap(self.feeding.detection_image) self.label_showDetection.setPixmap(backgroud_img) 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: self.label_move_sign.show() if self.label_move_sign.text() == '正在移动.': self.label_move_sign.setText('正在移动..') else: 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]) except: self.label_status_model.setText('未知模式') self.label_status_remoteCmdLen.setText(str(self.robotClient.status_model.RemoteCmdLen)) def updateUI_Position(self): self.horizontalSlider_J1.setValue(self.status_address.axis_0) self.horizontalSlider_J2.setValue(self.status_address.axis_1) self.horizontalSlider_J3.setValue(self.status_address.axis_2) self.horizontalSlider_J4.setValue(self.status_address.axis_3) self.horizontalSlider_J5.setValue(self.status_address.axis_4) self.horizontalSlider_J6.setValue(self.status_address.axis_5) self.label_j1.setText(str(self.status_address.axis_0)) self.label_j2.setText(str(self.status_address.axis_1)) self.label_j3.setText(str(self.status_address.axis_2)) self.label_j4.setText(str(self.status_address.axis_3)) self.label_j5.setText(str(self.status_address.axis_4)) self.label_j6.setText(str(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): # 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_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_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_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_take, self.pushButton_sign_take, True) self.set_sign_status(self.label_sign_feed, self.pushButton_sign_feed, False) return if sign <= FeedStatus.FBroken: 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_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): if connected: if self.label_connect_status.styleSheet().strip() != self.active_status_Qss: self.label_connect_status.setStyleSheet(self.active_status_Qss) self.active_status_Qss = self.label_connect_status.styleSheet().strip() else: if self.label_connect_status.styleSheet().strip() != self.noActive_status_Qss: self.label_connect_status.setStyleSheet(self.noActive_status_Qss) self.noActive_status_Qss = self.label_connect_status.styleSheet().strip() def set_sign_status(self,label,btn,signed:bool): try : if signed: if label.styleSheet().strip() != self.active_label_sign_Qss: label.setStyleSheet(self.active_label_sign_Qss) self.active_label_sign_Qss = label.styleSheet().strip() if btn.styleSheet().strip() != self.active_btn_sign_Qss: btn.setStyleSheet(self.active_btn_sign_Qss) self.active_btn_sign_Qss = btn.styleSheet().strip() else: if label.styleSheet().strip() != self.noactive_label_sign_Qss: label.setStyleSheet(self.noactive_label_sign_Qss) self.noactive_label_sign_Qss = label.styleSheet().strip() if btn.styleSheet().strip() != self.noactive_btn_sign_Qss: btn.setStyleSheet(self.noactive_btn_sign_Qss) self.noactive_btn_sign_Qss = btn.styleSheet().strip() except Exception as e: print(e) def set_run_status_button(self, isRuning: bool): self.pushButton_pauseFeed.setText("暂停") if isRuning: self.pushButton_pauseFeed.show() self.pushButton_stopFeed.show() self.pushButton_startFeed.hide() else: self.pushButton_pauseFeed.hide() self.pushButton_stopFeed.hide() self.pushButton_startFeed.show() def send_clear_auto_command(self): clear_command = CMDInstructRequest() request_command = clear_command.toString() log.log_message(logging.INFO, Constant.str_sys_clearAlarm) self.command_quene.put(request_command) def send_position_command(self, x1, x2, x3, x4, x5, x6, move_type: MoveType = MoveType.WORLD): position_instruction = Instruction() position_instruction.m0 = float(x1) position_instruction.m1 = float(x2) position_instruction.m2 = float(x3) position_instruction.m3 = float(x4) position_instruction.m4 = float(x5) position_instruction.m5 = float(x6) position_instruction.action = move_type.value 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}-' \ f'm5:{position_instruction.m4}-' \ f'm6:{position_instruction.m5}' log.log_message(logging.INFO,log_str) self.robotClient.add_sendQuene(request_command) def send_pause_command(self, pause: bool): pause_command = CMDRequest() pause_command.cmdData.append("actionPause") if pause: pause_command.cmdData.append("1") else: pause_command.cmdData.append("0") request_command = pause_command.toString() print(request_command) self.robotClient.add_sendQuene(request_command) def send_clear_alarm_command(self, pause: bool): pause_command = CMDRequest() pause_command.cmdData.append("clearAlarmContinue") pause_command.cmdData.append("1") request_command = pause_command.toString() print(request_command) log_str = f'暂停:{pause}' self.robotClient.add_sendQuene(request_command) def send_switch_tool_command(self): switch_command = CMDRequest() switch_command.cmdData.append("switchTool") switch_command.cmdData.append("2") request_command = switch_command.toString() print(request_command) self.robotClient.add_sendQuene(request_command) def send_start_tool_command(self): switch_command = CMDRequest() switch_command.cmdData.append("startButton") switch_command.cmdData.append("1") request_command = switch_command.toString() print(request_command) self.robotClient.add_sendQuene(request_command) def show_messagebox_of_person(self): msg_box = QMessageBox(self) msg_box.setWindowTitle("提示") msg_box.setText("当前有人是否继续") msg_box.setIcon(QMessageBox.Question) 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) if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec())