123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- from PyQt5.QtWidgets import QWidget, QApplication,QMainWindow,QLabel,QTextEdit,QPushButton
- from PyQt5.QtGui import QPixmap,QPainter,QResizeEvent,QCloseEvent,QPaintEvent,QFont
- from PyQt5.QtCore import QSize,QTimer,QRect,Qt
- import mqtt_async as mt
- import message_pb2 as MSG
- import google.protobuf.text_format as tf
- from google.protobuf import json_format
- class DirectBtn(QPushButton):
- def __init__(self, parent,text,type ,action):
- super().__init__(text,parent=parent)
- self.actionCallback=action
- self.type=type
- self.show()
- def mouseMoveEvent(self, e):
- pass
- def mousePressEvent(self, e):
- self.actionCallback(self.type,"press")
- def mouseReleaseEvent(self, e) -> None:
- self.actionCallback(self.type,"stop")
- def enterEvent(self, a0):
- return super().enterEvent(a0)
- def leaveEvent(self, a0):
- #self.actionCallback(self.type,"stop")
- return super().leaveEvent(a0)
- class Frame(QMainWindow):
- def __init__(self):
- super(Frame, self).__init__()
- self.mtclient=mt.MqttAsync("pyTool")
- self.mtclient.connect("192.168.2.132",1883,"admin","zx123456")
- self.v=0
- self.vth=0
- self.InitUI()
- def InitUI(self):
- self.label_ip=QLabel(self)
- self.edit_ip=QTextEdit(self)
- self.label_port = QLabel(self)
- self.edit_port=QTextEdit(self)
- self.btnConnect=QPushButton(self)
- self.btnConnect.clicked.connect(self.ConnectAction)
- #self.car_number_txt.setStyleSheet("border-width:1px;border-style:solid;border-color:rgb(150,150,150)")
- self.label_ip.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
- self.label_port.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
- self.label_ip.setStyleSheet("color:blue")
- self.label_port.setStyleSheet("color:blue")
- self.label_ip.setText("IP:")
- self.edit_ip.setText("192.168.2.132")
- self.label_ip.setGeometry(10, 10, 50, 30)
- self.edit_ip.setGeometry(70, 10, 150, 30)
- self.label_port.setText("端口:")
- self.edit_port.setText("1883")
- self.label_port.setGeometry(10, 45, 50, 30)
- self.edit_port.setGeometry(70, 45, 150, 30)
- self.btnConnect.setGeometry(230, 10, 100, 65)
- self.btnConnect.setText("连接")
- self.label_v=QLabel(self)
- self.edit_v=QTextEdit(self)
- self.label_vth = QLabel(self)
- self.edit_vth=QTextEdit(self)
- self.btnChangeSpeed=QPushButton(self)
- self.btnChangeSpeed.clicked.connect(self.ChangeSpeedAction)
- #self.car_number_txt.setStyleSheet("border-width:1px;border-style:solid;border-color:rgb(150,150,150)")
- self.label_v.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
- self.label_vth.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
- self.label_v.setStyleSheet("color:blue")
- self.label_vth.setStyleSheet("color:blue")
- self.label_v.setText("线速度:")
- self.label_v.setGeometry(10, 210, 50, 30)
- self.edit_v.setGeometry(70, 210, 150, 30)
- self.label_vth.setText("角速度:")
- self.label_vth.setGeometry(10, 245, 50, 30)
- self.edit_vth.setGeometry(70, 245, 150, 30)
- self.btnChangeSpeed.setGeometry(230, 210, 100, 65)
- self.btnChangeSpeed.setText("变速")
- ###
- self.btnRotation=DirectBtn(self,"逆时针","LR",self.monitorAction)
- self.btnRotation.setGeometry(450, 150, 60, 60)
- self.btnRotation=DirectBtn(self,"顺时针","RR",self.monitorAction)
- self.btnRotation.setGeometry(600, 150, 60, 60)
- self.btnRotation=DirectBtn(self,"前进","UP",self.monitorAction)
- self.btnRotation.setGeometry(520, 150, 70, 60)
- self.btnRotation=DirectBtn(self,"后退","DOWN",self.monitorAction)
- self.btnRotation.setGeometry(520, 250, 70, 60)
- def ConnectAction(self):
- self.btnConnect.setStyleSheet("color:yellow")
- self.btnConnect.setText("连接中")
- self.btnConnect.setStyleSheet("color:green")
- def ChangeSpeedAction(self):
- self.btnChangeSpeed.setStyleSheet("color:yellow")
- self.btnChangeSpeed.setText("变速")
- self.btnChangeSpeed.setStyleSheet("color:green")
- def monitorAction(self,type,action):
- if type=="LR": #逆时针旋转
- if action=="press":
- self.v=0
- self.vth=0.1
- print("逆时针旋转")
- else:
- self.v=0
- self.vth=0
- print("停止旋转")
- if type=="RR": #顺时针旋转
- if action=="press":
- self.v=0
- self.vth=-0.1
- print("顺时针旋转")
- else:
- self.v=0
- self.vth=0
- print("停止旋转")
- if type=="UP":
- if action=="press":
- self.v=0.2
- self.vth=0
- print("前进")
- else:
- self.v=0
- self.vth=0
- print("停止前进")
- if type=="DOWN":
- if action=="press":
- self.v=-0.2
- self.vth=0
- print("后退")
- else:
- self.v=0
- self.vth=0
- print("停止后退")
- cmd=MSG.ChangeSpeedCmd()
- cmd.v=self.v
- cmd.vth=self.vth
- msg=json_format.MessageToJson(cmd,True)
- self.mtclient.publish("cmd1",msg)
- '''if type=="TL": #左转弯
- if action=="press":
- print("左转弯")
- else:
- print("停止左转弯")
- if type=="TR": #右转弯
- if action=="press":
- print("右转弯")
- else:
- print("停止右转弯")'''
- def closeEvent(self, a0: QCloseEvent) -> None:
- pass
- def resizeEvent(self, a0: QResizeEvent) -> None:
- pass
- '''w, h = a0.size().width(), a0.size().height()
- w, h = self.size().width(), self.size().height()
- top_w, top_h = w, int(h * 0.15)
- car_number_w,car_number_h=w, int(h * 0.10)
- txt_w, txt_h = w, int(h * 0.20)
- arrow_w, arrow_h = w, int(h * 0.55)
- font = QFont()
- font.setBold(True)
- font.setPixelSize(int(w / 40))
- self.measure_info_txt.setFont(font)
- self.measure_info_txt.setGeometry(0, 0, top_w, top_h)
- font.setPixelSize(int(w / 20))
- self.car_number_txt.setFont(font)
- self.car_number_txt.setGeometry(0, top_h, car_number_w, car_number_h)
- self.panel_txt.setGeometry(0, top_h+car_number_h, txt_w, txt_h)
- self.panel_arrow.setGeometry(0, top_h +car_number_h+ txt_h, arrow_w, arrow_h)'''
|