window_screen_pyqt.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. from PyQt5.QtWidgets import QWidget, QApplication,QMainWindow,QLabel
  2. from PyQt5.QtGui import QPixmap,QPainter,QResizeEvent,QCloseEvent,QPaintEvent,QFont
  3. from PyQt5.QtCore import QSize,QTimer,QRect,Qt
  4. import async_communication as CM
  5. import message_pb2 as message
  6. import google.protobuf.text_format as tf
  7. import projector_control as PJC
  8. MeasureStatu={"ok":0,"无数据":1,"噪声":2,"超界":3}
  9. ArrowType={0:"正确图片",0x01:"向后调整",0x02:"向前调整",0x04:"向右调整",0x08:"向左调整"
  10. ,0x10:"左前调整",0x06:"右前调整",0x09:"右后调整",0x05:"左后调整"}
  11. class PicLabel(QLabel):
  12. def __init__(self,parent):
  13. super(PicLabel,self).__init__(parent=parent)
  14. self.image=None
  15. self.show = False
  16. self.setStyleSheet("border-width:1px;border-style:solid;border-color:rgb(150,150,150)")
  17. self.timer=QTimer()
  18. self.timer.timeout.connect(self.OnTimer)
  19. def FlashImg(self,pixmap):
  20. self.image=pixmap
  21. if self.timer.isActive()==False:
  22. self.timer.start(500)
  23. def ShowImg(self,pixmap):
  24. self.timer.stop()
  25. self.show=True
  26. self.image=pixmap
  27. self.repaint()
  28. def OnTimer(self):
  29. self.show=not self.show
  30. self.repaint()
  31. def paintEvent(self, a0: QPaintEvent) -> None:
  32. if self.show==True:
  33. if not self.image == None:
  34. w, h = self.size().width(),self.size().height()
  35. iw, ih = self.image.width(), self.image.height()
  36. painter=QPainter(self)
  37. painter.drawPixmap(QRect(0,0,w,h),self.image,QRect(0,0,iw,ih))
  38. class Frame(QMainWindow):
  39. def __init__(self,images,prj_parameter,second_screen=False):
  40. super(Frame, self).__init__()
  41. self.images=images
  42. self.images = {}
  43. self.icpu_statu = CM.TimeStatu(timeout=0.1)
  44. self.measure_statu = CM.TimeStatu(timeout=0.1)
  45. self.terminal_statu = CM.TimeStatu(timeout=0.1)
  46. for key, file in images.items():
  47. self.images[key] = pix = QPixmap(file)
  48. self.InitUI()
  49. self.last_door_statu=None
  50. self.last_moving_statu = None
  51. self.last_show = None
  52. if second_screen == True:
  53. desktop = QApplication.desktop()
  54. screen_count = desktop.screenCount()
  55. displays = [desktop.availableGeometry(i) for i in range(desktop.screenCount())]
  56. second_offx = 0
  57. second_offy = 0
  58. if len(displays) == 2:
  59. second_offx = displays[1].width()
  60. second_offy = displays[1].height()
  61. self.setGeometry(second_offx,second_offy,200,500)
  62. if prj_parameter is not None:
  63. prj_ip=prj_parameter['ip']
  64. prj_port=prj_parameter['port']
  65. prj_type=prj_parameter['type']
  66. self.pjc_=PJC.ProjectorControl(prj_ip,prj_port,prj_type)
  67. self.pjc_.start()
  68. else:
  69. self.pjc_=None
  70. self.timer = QTimer()
  71. self.timer.timeout.connect(self.Switch)
  72. self.timer.start(200)
  73. def InitUI(self):
  74. self.measure_info_txt=QLabel(self)
  75. self.car_number_txt = QLabel(self)
  76. self.car_number_txt.setStyleSheet("border-width:1px;border-style:solid;border-color:rgb(150,150,150)")
  77. self.car_number_txt.setAlignment(Qt.AlignCenter)
  78. self.measure_info_txt.setStyleSheet("color:blue")
  79. self.panel_txt=PicLabel(self)
  80. self.panel_arrow=PicLabel(self)
  81. def receive_icpu(self,statu):
  82. self.icpu_statu=statu
  83. def receive_measureInfo(self,statu):
  84. self.measure_statu=statu
  85. def receive_terminal(self,statu):
  86. self.terminal_statu=statu
  87. def closeEvent(self, a0: QCloseEvent) -> None:
  88. if self.pjc_ is not None:
  89. self.pjc_.lightOff()
  90. self.pjc_.exit()
  91. self.pjc_.join()
  92. def resizeEvent(self, a0: QResizeEvent) -> None:
  93. w, h = a0.size().width(), a0.size().height()
  94. w, h = self.size().width(), self.size().height()
  95. top_w, top_h = w, int(h * 0.15)
  96. car_number_w,car_number_h=w, int(h * 0.10)
  97. txt_w, txt_h = w, int(h * 0.20)
  98. arrow_w, arrow_h = w, int(h * 0.55)
  99. font = QFont()
  100. font.setBold(True)
  101. font.setPixelSize(int(w / 40))
  102. self.measure_info_txt.setFont(font)
  103. self.measure_info_txt.setGeometry(0, 0, top_w, top_h)
  104. font.setPixelSize(int(w / 20))
  105. self.car_number_txt.setFont(font)
  106. self.car_number_txt.setGeometry(0, top_h, car_number_w, car_number_h)
  107. self.panel_txt.setGeometry(0, top_h+car_number_h, txt_w, txt_h)
  108. self.panel_arrow.setGeometry(0, top_h +car_number_h+ txt_h, arrow_w, arrow_h)
  109. def Switch(self):
  110. if self.terminal_statu.timeout() is False:
  111. terminal_statu = message.terminal_node_statu()
  112. tf.Parse(self.terminal_statu.statu, terminal_statu)
  113. print(terminal_statu.car_number)
  114. self.car_number_txt.setText(terminal_statu.car_number)
  115. self.car_number_txt.setStyleSheet("border-width:1px;border-style:solid;border-color:rgb(150,150,150);color:green")
  116. else:
  117. self.car_number_txt.setText("车牌号为空!")
  118. self.car_number_txt.setStyleSheet("border-width:1px;border-style:solid;border-color:rgb(150,150,150);color:red")
  119. #先判断连接状态
  120. if self.icpu_statu.timeout():
  121. self.panel_txt.ShowImg(self.images['传感器超时'])
  122. self.panel_arrow.FlashImg(self.images['传感器超时'])
  123. self.last_show="超时"
  124. return
  125. if self.measure_statu.timeout():
  126. self.panel_txt.ShowImg(self.images['测绘超时'])
  127. self.panel_arrow.FlashImg(self.images['测绘超时'])
  128. self.last_show="超时"
  129. return
  130. #有车才显示
  131. icpu_statu=message.in_mcpu_statu()
  132. tf.Parse(self.icpu_statu.statu,icpu_statu)
  133. if icpu_statu.is_occupy != 2:
  134. self.panel_txt.ShowImg(self.images["空闲"])
  135. self.panel_arrow.ShowImg(self.images["空闲"])
  136. self.last_show = "空闲"
  137. return
  138. measure_info=message.measure_info()
  139. tf.Parse(self.measure_statu.statu,measure_info)
  140. #外门状态控制投影仪
  141. if self.pjc_ is not None:
  142. if icpu_statu.door_statu==3: #关门状态
  143. if not self.last_door_statu=="closed":
  144. print("close")
  145. self.pjc_.lightOff()
  146. self.last_door_statu="closed"
  147. elif icpu_statu.door_statu==2 or icpu_statu.door_statu==4:
  148. if not self.last_door_statu=="opened":
  149. self.pjc_.lightOn()
  150. print("open")
  151. self.last_door_statu="opened"
  152. #以下是有车的处理
  153. #1,显示测量数据(实时和静态)
  154. realtime_info_txt=''
  155. static_info_txt=''
  156. realtime_info_txt="(实时) 中心(%.3f,%.3f) 角度:%.2f° 宽:%.3f 轴距:%.3f 前轮角:%.2f°"%\
  157. (measure_info.cx,measure_info.cy,measure_info.theta,measure_info.width,
  158. measure_info.wheelbase,measure_info.front_theta)
  159. if not self.last_moving_statu==None:
  160. static_info_txt="(静止) 中心(%.3f,%.3f) 角度:%.2f° 宽:%.3f 轴距:%.3f 前轮角:%.2f°"%\
  161. (self.last_moving_statu.cx,self.last_moving_statu.cy,
  162. self.last_moving_statu.theta,self.last_moving_statu.width,
  163. self.last_moving_statu.wheelbase,self.last_moving_statu.front_theta)
  164. self.measure_info_txt.setText(realtime_info_txt+"\n"+static_info_txt)
  165. #2,获取车辆运动状态(静止or动态)
  166. border_statu=measure_info.border_statu
  167. is_moving = ((border_statu >> 11) & 0x01) == 1
  168. lidar_statu=measure_info.ground_status # 测量状态(正常,无数据、噪声、超界)
  169. if is_moving:
  170. self.last_moving_statu=None
  171. else:
  172. #当前静止
  173. if lidar_statu==MeasureStatu["ok"]:
  174. self.last_moving_statu=measure_info
  175. elif lidar_statu==MeasureStatu["超界"]:
  176. if not self.last_moving_statu ==None:
  177. #上一刻静止且数据正确,当前静止,不可能出现超界,将超界清除
  178. new_border=border_statu
  179. new_border = (new_border & (~(0x01 << 0)))
  180. new_border = (new_border & (~(0x01 << 1)))
  181. new_border = (new_border & (~(0x01 << 2)))
  182. new_border = (new_border & (~(0x01 << 3)))
  183. new_border = (new_border & (~(0x01 << 6)))
  184. new_border = (new_border & (~(0x01 << 7)))
  185. new_border = (new_border & (~(0x01 << 8)))
  186. new_border = (new_border & (~(0x01 << 9)))
  187. border_statu=new_border
  188. elif lidar_statu==MeasureStatu["无数据"]:
  189. #当前静止无车,清除上一时刻数据
  190. self.last_moving_statu=None
  191. elif lidar_statu==MeasureStatu["噪声"]:
  192. if not self.last_moving_statu==None:
  193. #上一时刻静止且正确,当前噪声,不显示当前数据,显示上一正确数据
  194. measure_info.CopyFrom(self.last_moving_statu)
  195. #先判断光电
  196. if is_moving:
  197. if icpu_statu.back_io==1:
  198. self.panel_txt.ShowImg(self.images["请调整"])
  199. self.panel_arrow.FlashImg(self.images["向前调整"])
  200. self.last_show="调整"
  201. return
  202. #光电正常
  203. if lidar_statu==MeasureStatu["无数据"]:
  204. self.panel_txt.ShowImg(self.images["空闲"])
  205. self.panel_arrow.ShowImg(self.images["空闲"])
  206. self.last_show="空闲"
  207. return
  208. elif lidar_statu==MeasureStatu["噪声"]:
  209. if self.last_show=="超时":
  210. self.panel_txt.ShowImg(self.images["空闲"])
  211. self.panel_arrow.ShowImg(self.images["空闲"])
  212. self.last_show="空闲"
  213. return
  214. elif lidar_statu==MeasureStatu["ok"]:
  215. self.panel_txt.ShowImg(self.images['正确文字'])
  216. self.panel_arrow.ShowImg(self.images['正确图片'])
  217. return
  218. elif lidar_statu==MeasureStatu["超界"]:
  219. if (border_statu>>7)&0x01==1:
  220. self.panel_txt.ShowImg(self.images['轴距超差'])
  221. self.panel_arrow.ShowImg(self.images['轴距超差'])
  222. self.last_show="轴距超差"
  223. return
  224. if (border_statu>>9)&0x01==1:
  225. self.panel_txt.ShowImg(self.images['请调整'])
  226. self.panel_arrow.FlashImg(self.images['向左旋转'])
  227. self.last_show="请调整"
  228. return
  229. if (border_statu>>8)&0x01==1:
  230. self.panel_txt.ShowImg(self.images['请调整'])
  231. self.panel_arrow.FlashImg(self.images['向右旋转'])
  232. self.last_show="请调整"
  233. return
  234. if (border_statu>>10)&0x01==1:
  235. self.panel_txt.ShowImg(self.images['请调整'])
  236. self.panel_arrow.FlashImg(self.images['回正方向盘'])
  237. self.last_show="请调整"
  238. return
  239. if (border_statu>>6)&0x01==1:
  240. self.panel_txt.ShowImg(self.images['超宽车辆'])
  241. self.panel_arrow.ShowImg(self.images['超宽车辆'])
  242. self.last_show="超宽"
  243. return
  244. border=border_statu&0x0f
  245. if (border in ArrowType.keys()) is False:
  246. self.panel_txt.ShowImg(self.images['测绘超时'])
  247. self.panel_arrow.FlashImg(self.images['测绘超时'])
  248. self.last_show = "超时"
  249. else:
  250. if ArrowType[border] == "正确图片":
  251. self.panel_arrow.ShowImg(self.images[ArrowType[border]])
  252. self.panel_txt.ShowImg(self.images['正确文字'])
  253. self.last_show="正确"
  254. else:
  255. self.panel_arrow.FlashImg(self.images[ArrowType[border]])
  256. self.panel_txt.ShowImg(self.images['请调整'])
  257. self.last_show="请调整"