def btnClicked(self): sender = self.sender() print(sender.text()) if sender.text() == r'伺服参数设置': self.sifucanshu_ui.port = self.port self.sifucanshu_ui.activateWindow() self.sifucanshu_ui.show() if sender.text() == r'压力参数设置': self.yalicanshu_ui.port = self.port self.yalicanshu_ui.askTimer.start() self.yalicanshu_ui.activateWindow() self.yalicanshu_ui.show() if sender.text() == r'调试参数设置': self.tiaoshicanshu_ui.activateWindow() self.tiaoshicanshu_ui.show() if sender.text() == r'工件参数设置': self.gongjiancanshu_ui.activateWindow() self.gongjiancanshu_ui.show() if sender.text() == r'相机位置计算': self.cameraPosition_ui.port = self.port self.cameraPosition_ui.activateWindow() #self.cameraPosition_ui.openCamera() self.cameraPosition_ui.show() if sender.text() == r'相机四孔识别': self.sikongshibie_ui.port = self.port self.sikongshibie_ui.activateWindow() #self.sikongshibie_ui.openCamera() self.sikongshibie_ui.show() if sender.text() == r'自动调校': self.ziDongTiaoJiao_ui.port = self.port self.ziDongTiaoJiao_ui.activateWindow() #todo if self.port: write_data_to_port(self.port, r'0001000000000005') #self.ziDongTiaoJiao_ui.openCamera() self.ziDongTiaoJiao_ui.show() if sender.text() == r'电压调试': self.dianyatiaoshi_ui.port = self.port self.dianyatiaoshi_ui.activateWindow() self.dianyatiaoshi_ui.show() if sender.text() == r'零点标定': if self.port: write_data_to_port(self.port, r'0001000000000001') if sender.text() == r'电压对比': self.adDuiBi_ui.activateWindow() self.adDuiBi_ui.show() if self.port: write_data_to_port(self.port, r'0001000000000002') if sender.text() == r'关机归位': if self.port: write_data_to_port(self.port, r'0001000000000003')
def startWorking(self): self.pos_x.setText(self.current_x.text()) self.pos_y.setText(self.current_y.text()) zuantou_high = math.floor(self.gongJian.zhuansu / 256) zuantou_low = int(self.gongJian.zhuansu % 256) if zuantou_low < 10: zuantou_low = '0' + str(zuantou_low) if zuantou_high < 10: zuantou_high = '0' + str(zuantou_high) move_up = r'00' move_down_quick = r'00' move_down_slow = r'00' if self.move_up_flag: move_up = r'01' if self.move_down_slow_flag: move_down_slow = r'01' if self.move_down_quick_flag: move_down_quick = r'01' if self.start_flag == False: start = r'01' cmd = r'0003' + str(zuantou_high) + str( zuantou_low ) + move_up + move_down_quick + move_down_slow + start if self.port: write_data_to_port(self.port, cmd) self.start_flag = True self.start.setText("停止") else: QMessageBox.information(self, '串口连接状态', '连接失败', QMessageBox.Yes) else: start = r'00' cmd = r'0003' + str(zuantou_high) + str( zuantou_low ) + move_up + move_down_quick + move_down_slow + start if self.port: write_data_to_port(self.port, cmd) self.start.setText("打孔") self.start_flag = False
def moveRightReleased(self): if self.port: write_data_to_port(self.port, r'0004000000000000')
def moveRight(self): if self.port: write_data_to_port(self.port, r'0004000000000001')
def moveLeft(self): if self.port: write_data_to_port(self.port, r'0004000000000100')
def startClicked(self): cmd = r'0005000000010000' #cmd2 = r'0006000000000001' if self.port: write_data_to_port(self.port, cmd)
def parse(self, data): data_splited = [] for i in range(0, 15, 2): data_splited.append(data[i:i + 2]) # 获取AD值和继电器状态 if data_splited[0] == b'55' and data_splited[1] == b'02': high = (data_splited[3][0] - 48) if data_splited[3][0] < 58 else data_splited[3][0] - 87 low = (data_splited[3][1] - 48) if data_splited[3][1] < 58 else data_splited[3][1] - 87 high_bin = bin(high)[-4:] low_bin = bin(low)[-4:] if len(high_bin) < 4: high_bin = '0' + high_bin[-1] if len(low_bin) is 3: low_bin = '000' + low_bin[-1] if len(low_bin) is 4: if low_bin.startswith('b'): low_bin = '0' + low_bin[-3:] if low_bin.startswith('0b'): low_bin = '00' + low_bin[-2:] # if len(low_bin) is 5: # low_bin = '0' + low_bin[-3:] # if len(low_bin) is 6: # low_bin = low_bin[-4:] status = (high_bin + low_bin)[-6:] self.update_relay_status(status) ad1_high = data_splited[4] ad1_low = data_splited[5] ad2_high = data_splited[6] ad2_low = data_splited[7] ad1_value = byte_to_oct(ad1_high, ad1_low) ad2_value = byte_to_oct(ad2_high, ad2_low) temp1 = float(self.yalicanshu_ui.ad1_value_2.text()) - float( self.yalicanshu_ui.ad1_value_1.text()) temp2 = float(self.yalicanshu_ui.ad1_pressure_2.text()) - float( self.yalicanshu_ui.ad1_pressure_1.text()) if not temp2 == 0: vp = temp1 / temp2 else: vp = 999 # AD1 平均滤波 self.ad1_value_list.append(ad1_value) self.ad1_count = self.ad1_count + 1 if self.ad1_count >= 4: ad1_value = np.mean(self.ad1_value_list) self.ad1_value_list = [] self.ad1_count = 0 self.yalicanshu_ui.ad1_display.setText(str(ad1_value)) # AD2 平均滤波 self.ad2_value_list.append(ad2_value) self.ad2_count = self.ad2_count + 1 if self.ad2_count >= 4: ad2_value = np.mean(self.ad2_value_list) self.ad2_value_list = [] self.ad2_count = 0 self.yalicanshu_ui.ad2_display.setText(str(ad2_value)) #todo 测试AD2 AD1 压力及电压更新 self.yalicanshu_ui.updateUI() # 向下位机发送相机XYZ坐标 if data_splited[0] == b'05' and data_splited[1] == b'01': gongjian = self.ziDongTiaoJiao_ui.gongjian camera_x = gongjian.c_x camera_y = gongjian.c_y camera_z = gongjian.c_z oct_c_x_high = math.floor(camera_x / 256) oct_c_x_low = math.floor(camera_x % 256) oct_c_y_high = math.floor(camera_y / 256) oct_c_y_low = math.floor(camera_y % 256) oct_c_z_high = math.floor(camera_z / 256) oct_c_z_low = math.floor(camera_z % 256) cmd = r'5001'+oct_to_bin(oct_c_x_high) + \ oct_to_bin(oct_c_x_low) + \ oct_to_bin(oct_c_y_high) + \ oct_to_bin(oct_c_y_low) + \ oct_to_bin(oct_c_z_high) + \ oct_to_bin(oct_c_z_low) if self.port: write_data_to_port(self.port, cmd) # 向下位机发送AD2压力和限压 if data_splited[0] == b'05' and data_splited[1] == b'02': gongjian = self.ziDongTiaoJiao_ui.gongjian pressure = gongjian.pressure ad2 = self.yalicanshu_ui.sensor xianya = ad2.ad2_max pressure_high = math.floor(pressure / 256) pressure_low = math.floor(pressure % 256) xianya_high = math.floor(xianya / 256) xianya_low = math.floor(xianya % 256) cmd = r'05020000' + oct_to_bin(pressure_high) + \ oct_to_bin(pressure_low) + \ oct_to_bin(xianya_high) + \ oct_to_bin(xianya_low) if self.port: write_data_to_port(self.port, cmd) #接收下位机完成步数 if data_splited[0] == b'05' and data_splited[1] == b'04': # 接收下位机的传感器判断结果,并显示 if data_splited[-1] == b'04': ad2_status = data_splited[-2] if ad2_status == b'01': self.yalicanshu_ui.ad2_pressure.setText(r'传感器错误') #向下位机发送第四步完成的信号 write_data_to_port(self.port, r'0006000000000004') # 第五步完成接第六步 if data_splited[-1] == b'05': cx1 = 0 cy1 = 0 cx2 = 0 cy2 = 0 cx3 = 0 cy3 = 0 try: srcImage = self.cap_image self.circle_detector.origin_Image = srcImage #todo gengxin recognizedImage, x1, y1, x2, y2, x3, y3 = self.circle_detector.match( ) if not x1 == -1 and not y1 == -1: cx1 = x1 cy1 = y1 if not x2 == -1 and not y2 == -1: cx2 = x2 cy2 = y2 if not x3 == -2 and not y3 == -1: cx3 = x3 cy3 = y3 except Exception as e: print(e) QMessageBox.information(self, '识别结果', '识别失败', QMessageBox.Yes) filePath = os.path.join( r'C:\Users\zyp\PycharmProjects\cv\data', 'test.xlsx') data_dict = {} data_dict['x1'] = cx1 data_dict['y1'] = cy1 data_dict['x2'] = cx2 data_dict['y2'] = cy2 data_dict['x3'] = cx3 data_dict['y3'] = cy3 data_dict['c_x'] = self.ziDongTiaoJiao_ui.gongjian.c_x data_dict['c_y'] = self.ziDongTiaoJiao_ui.gongjian.c_y data_dict['ad2_pressure'] = float( self.ziDongTiaoJiao_ui.ad2_pressure.text()) data_dict['angle'] = self.ziDongTiaoJiao_ui.gongjian.angle data_dict['vision'] = self.ziDongTiaoJiao_ui.gongjian.vision data_dict[ 'pressure'] = self.ziDongTiaoJiao_ui.gongjian.pressure writeDataForSixStep(filePath, data_dict) print("第6步 此处进行识别并写excel文件") write_data_to_port(self.port, r'0006000000000006') # 下位机第7步完成 if data_splited[-1] == b'07': cx1 = 0 cy1 = 0 cx2 = 0 cy2 = 0 cx3 = 0 cy3 = 0 try: srcImage = self.cap_image self.circle_detector.origin_Image = srcImage recognizedImage, x1, y1, x2, y2, x3, y3 = self.circle_detector.match( ) if not x1 == -1 and not y1 == -1: cx1 = x1 cy1 = y1 if not x2 == -1 and not y2 == -1: cx2 = x2 cy2 = y2 if not x3 == -2 and not y3 == -1: cx3 = x3 cy3 = y3 except Exception as e: print(e) QMessageBox.information(self, '识别结果', '识别失败', QMessageBox.Yes) filePath = os.path.join( r'C:\Users\zyp\PycharmProjects\cv\data', 'test.xlsx') data_dict = {} data_dict['x1'] = cx1 data_dict['y1'] = cy1 data_dict['x2'] = cx2 data_dict['y2'] = cy2 data_dict['x3'] = cx3 data_dict['y3'] = cy3 data_dict['ad2_pressure'] = float( self.ziDongTiaoJiao_ui.ad2_pressure.text()) writeDataForEightStep(filePath, data_dict) print("第8步 对工件进行识别 写excel文件") time.sleep(2) write_data_to_port(self.port, r'0006000000000008') #下位机第九步完成 if data_splited[-1] == b'09': write_data_to_port(self.port, r'5004000000000000') print(r"下位机第 9 步完成信号收到") #下位机第十步完成 if data_splited[-1] == b'0a': write_data_to_port(self.port, r'5004000000000000') print(r"下位机第 10 步完成信号收到") #下位机第 11 步完成 if data_splited[-1] == b'0b': write_data_to_port(self.port, r'5004000000000000') print(r"下位机第 11 步完成信号收到") #下位机第 12 步完成 if data_splited[-1] == b'0c': write_data_to_port(self.port, r'5004000000000000') print(r"下位机第 12 步完成信号收到") #下位机第 13 步完成 if data_splited[-1] == b'0d': time.sleep(2) print(r'识别工件 并写入excel') cx1 = 0 cy1 = 0 cx2 = 0 cy2 = 0 cx3 = 0 cy3 = 0 try: srcImage = self.cap_image self.circle_detector.origin_Image = srcImage recognizedImage, x1, y1, x2, y2, x3, y3 = self.circle_detector.match( ) if not x1 == -1 and not y1 == -1: cx1 = x1 cy1 = y1 if not x2 == -1 and not y2 == -1: cx2 = x2 cy2 = y2 if not x3 == -2 and not y3 == -1: cx3 = x3 cy3 = y3 except Exception as e: print(e) QMessageBox.information(self, '识别结果', '识别失败', QMessageBox.Yes) filePath = os.path.join( r'C:\Users\zyp\PycharmProjects\cv\data', 'test.xlsx') data_dict = {} data_dict['x1'] = cx1 data_dict['y1'] = cy1 data_dict['x2'] = cx2 data_dict['y2'] = cy2 data_dict['x3'] = cx3 data_dict['y3'] = cy3 writeDataForFourteenStep(filePath, data_dict) print(r"上位机 14 步识别工件 写入excel") #通知下位机 上位机的第14步已经完成 write_data_to_port(self.port, r'000600000000000e') print(r"上位机第14步 读取D26 判断是否正确") flag = int(readData(filePath, (25, 3))) unqualified_num = int( self.ziDongTiaoJiao_ui.unqualified.text()) qualified_num = int(self.ziDongTiaoJiao_ui.qualified.text()) if flag == 0: self.ziDongTiaoJiao_ui.img_box.setText("打孔位置正确") self.ziDongTiaoJiao_ui.unqualified.setText( str(qualified_num + 1)) print(r"打孔合格,读取D27 D28") ##通知下位机第十七步完成 write_data_to_port(self.port, r'0006000000000011') else: self.ziDongTiaoJiao_ui.img_box.setText("打孔位置错误") self.ziDongTiaoJiao_ui.unqualified.setText( str(unqualified_num + 1)) #通知下位机第十五步完成 write_data_to_port(self.port, r'000600000000000f') #下位机获取X轴和Y轴的坐标 if data_splited[0] == b'05' and data_splited[1] == b'05': filePath = os.path.join(r'C:\Users\zyp\PycharmProjects\cv\data', 'test.xlsx') x, y = readDataForNineStep(filePath) print("第九步从excel文件中读取数据") x_high = math.floor(x / 256) x_low = math.floor(x % 256) y_high = math.floor(y / 256) y_low = math.floor(y % 256) cmd = r'5005'+oct_to_bin(x_high) + \ oct_to_bin(x_low) + \ oct_to_bin(y_high) + \ oct_to_bin(y_low) + \ '0009' write_data_to_port(self.port, cmd) #获取转速 和起钻、止钻的位置 if data_splited[0] == b'05' and data_splited[1] == b'03': gongjian = self.ziDongTiaoJiao_ui.gongjian qizuan = gongjian.z_start zhizuan = gongjian.z_stop zhuansu = gongjian.zhuansu qizuan_high = math.floor(qizuan / 256) qizuan_low = math.floor(qizuan % 256) zhuansu_high = math.floor(zhuansu / 256) zhuansu_low = math.floor(zhuansu % 256) zhizuan_high = math.floor(zhizuan / 255) zhizuan_low = math.floor(zhizuan % 255) cmd = r'5003'+oct_to_bin(qizuan_high) + \ oct_to_bin(qizuan_low) + \ oct_to_bin(zhizuan_high) + \ oct_to_bin(zhizuan_low) + \ oct_to_bin(zhuansu_high) + \ oct_to_bin(zhuansu_low) if self.port: write_data_to_port(self.port, cmd) #获取游丝角度 if data_splited[0] == b'05' and data_splited[1] == b'07': gongjian = self.ziDongTiaoJiao_ui.gongjian yousi = gongjian.yousi_ang yousi_high = math.floor(yousi / 256) yousi_low = math.floor(yousi % 256) cmd = r'5007'+ oct_to_bin(yousi_high) + \ oct_to_bin(yousi_low)+'00000000' write_data_to_port(self.port, cmd) #获取钻速 if data_splited[0] == b'05' and data_splited[1] == b'02': gongjian = self.ziDongTiaoJiao_ui.gongjian zuansu = gongjian.zuansu zuansu_high = math.floor(zuansu / 256) zuansu_low = math.floor(zuansu % 256) ad2 = self.yalicanshu_ui.sensor xianya = ad2.ad2_max pressure_high = math.floor(pressure / 256) pressure_low = math.floor(pressure % 256) xianya_high = math.floor(xianya / 256) xianya_low = math.floor(xianya % 256) cmd = r'5002' + oct_to_bin(zuansu_high) + \ oct_to_bin(zuansu_low) + \ oct_to_bin(xianya_high) + \ oct_to_bin(xianya_low) + \ oct_to_bin(pressure_high) + \ oct_to_bin(pressure_low) write_data_to_port(self.port, cmd) #相机位置计算更新坐标 #TODO 待更新计算公式 if data_splited[0] == b'55' and data_splited[1] == b'04': x_high = data_splited[2] x_mid = data_splited[3] x_low = data_splited[4] y_high = data_splited[5] y_mid = data_splited[6] y_low = data_splited[7] # x = byte_to_oct(x_high,x_mid, x_low) high_b1 = x_high[0] high_b2 = x_high[1] high_b1 = high_b1 - 48 if high_b1 < 58 else high_b1 - 87 high_b2 = high_b2 - 48 if high_b2 < 58 else high_b2 - 87 mid_b1 = x_mid[0] mid_b2 = x_mid[1] mid_b1 = mid_b1 - 48 if mid_b1 < 58 else mid_b1 - 87 mid_b2 = mid_b2 - 48 if mid_b2 < 58 else mid_b2 - 87 low_b1 = x_low[0] low_b2 = x_low[1] low_b1 = low_b1 - 48 if low_b1 < 58 else low_b1 - 87 low_b2 = low_b2 - 48 if low_b2 < 58 else low_b2 - 87 x = (high_b1 * 16 + high_b2) * 65536 + ( mid_b1 * 16 + mid_b2) * 256 + (low_b1 * 16 + low_b2) #y = byte_to_oct(y_high, y_low) high_b1 = y_high[0] high_b2 = y_high[1] high_b1 = high_b1 - 48 if high_b1 < 58 else high_b1 - 87 high_b2 = high_b2 - 48 if high_b2 < 58 else high_b2 - 87 mid_b1 = y_mid[0] mid_b2 = y_mid[1] mid_b1 = mid_b1 - 48 if mid_b1 < 58 else mid_b1 - 87 mid_b2 = mid_b2 - 48 if mid_b2 < 58 else mid_b2 - 87 low_b1 = y_low[0] low_b2 = y_low[1] low_b1 = low_b1 - 48 if low_b1 < 58 else low_b1 - 87 low_b2 = low_b2 - 48 if low_b2 < 58 else low_b2 - 87 y = (high_b1 * 16 + high_b2) * 65536 + ( mid_b1 * 16 + mid_b2) * 256 + (low_b1 * 16 + low_b2) self.cameraPosition_ui.current_x.setText(str(round(x / 10000, 3))) self.cameraPosition_ui.current_y.setText(str(round(y / 10000, 3)))
def sliderMoveRight(self): if self.port: write_data_to_port(self.port, r'0004000000000001') self.sliderMoveRightFlag = True
def sliderMoveLeft(self): if self.port: write_data_to_port(self.port, r'0004000000000100') self.sliderMoveLeftFlag = True
def sliderMoveRightReleased(self): if self.port and self.sliderMoveRightFlag: write_data_to_port(self.port, r'0004000000000000') self.sliderMoveRightFlag = False
def moveToDownSlow(self): if self.port: self.move_down_slow_flag = True cmd = self.constructCMD() write_data_to_port(self.port, cmd) self.move_down_slow.setStyleSheet("*{background-color:red}")
def moveDownSlowReleased(self): if self.port and self.move_down_slow_flag: self.move_down_slow_flag = False write_data_to_port(self.port, self.constructCMD()) self.move_down_slow.setStyleSheet( "background-image: url(:/image/down.png);")
def moveUpRelease(self): if self.port and self.move_up_flag: self.move_up_flag = False write_data_to_port(self.port, self.constructCMD()) self.move_up.setStyleSheet( "background-image: url(:/image/up.png);")
def moveToUp(self): if self.port: self.move_up_flag = True write_data_to_port(self.port, self.constructCMD()) self.move_up.setStyleSheet("*{background-color:red}")