ControllWidget.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import datetime
  2. import math
  3. import random
  4. import time
  5. from concurrent.futures import ThreadPoolExecutor, as_completed, wait
  6. from PyQt5.QtWidgets import QWidget, QFrame, QLabel, QLineEdit, QPushButton, QComboBox, QCheckBox
  7. from PyQt5.QtGui import QPixmap, QPainter, QResizeEvent, QCloseEvent, QPaintEvent, QFont
  8. from PyQt5.QtCore import QSize, QTimer, Qt
  9. import Count
  10. import message_pb2
  11. from RobotData import Robot
  12. import dijkstra.Map as mp
  13. from custom_define import ControllerStatus
  14. import MapGLWidget as viewer
  15. import dijkstra.Map as Map
  16. class ControlFrame(QFrame):
  17. threadPool_ = ThreadPoolExecutor(5)
  18. last_beg = "**"
  19. begId_ = "------"
  20. targetId_ = "------"
  21. def __init__(self, config):
  22. QFrame.__init__(self)
  23. self.street_nodes_ = config['street_nodes']
  24. self.space_nodes_ = config['space_nodes']
  25. self.label_ = config['label']
  26. [id, ip, port, user, pawd] = config['mqtt']
  27. subtopics = config['subTopics']
  28. cmdTopic = config['cmdTopic']
  29. robotColor = config['robotColor']
  30. self.all_nodes_ = []
  31. for node in self.street_nodes_:
  32. self.all_nodes_.append(node)
  33. for node in self.space_nodes_:
  34. self.all_nodes_.append(node)
  35. self.select_map = None
  36. self.default_wheel_base = 2.6
  37. rpc = config['rpc']
  38. self.robot_ = Robot(rpc, self.label_)
  39. self.robot_.connect(id, ip, port, user, pawd)
  40. self.robot_.SetSubDataTopic(subtopics, self.messageArrived)
  41. self.robot_.SetCmdTopic(cmdTopic)
  42. self.robot_.loop_start()
  43. self.robot_.SetColor(robotColor, robotColor)
  44. self.setGeometry(0, 0, 500, 500)
  45. self.InitUI()
  46. self.setFrameShape(self.StyledPanel)
  47. self.timer_ = QTimer()
  48. self.timer_.timeout.connect(self.Update)
  49. self.timer_.start(100)
  50. def messageArrived(self):
  51. pass
  52. # self.gl.update()
  53. def InitUI(self):
  54. self.basestatic = QLabel(self)
  55. self.basestatic.setText(self.label_)
  56. self.basestatic.setGeometry(20, 20, 80, 30)
  57. self.begstatic = QLabel(self)
  58. self.begstatic.setText(" 起点")
  59. self.begstatic.setGeometry(90, 5, 80, 30)
  60. self.begQc = QComboBox(self)
  61. self.begQc.setGeometry(160, 5, 80, 30)
  62. self.endstatic = QLabel(self)
  63. self.endstatic.setText("去马路点")
  64. self.endstatic.setGeometry(90, 50, 80, 30)
  65. self.endStreetQc = QComboBox(self)
  66. self.endStreetQc.setGeometry(160, 50, 80, 30)
  67. self.endstatic = QLabel(self)
  68. self.endstatic.setText("去车位点")
  69. self.endstatic.setGeometry(90, 95, 80, 30)
  70. self.endSpaceQc = QComboBox(self)
  71. self.endSpaceQc.setGeometry(160, 95, 80, 30)
  72. self.btnSend = QPushButton(self)
  73. self.btnSend.setGeometry(150, 140, 100, 40)
  74. self.btnSend.setText(" 开始")
  75. self.btnSend.clicked.connect(self.btnSendClick)
  76. self.btnCancel = QPushButton(self)
  77. self.btnCancel.setGeometry(150, 190, 100, 40)
  78. self.btnCancel.setText(" 取消")
  79. self.btnCancel.clicked.connect(self.btnCancelClick)
  80. self.begQc.addItems(self.all_nodes_)
  81. self.endStreetQc.addItems(self.street_nodes_)
  82. self.endStreetQc.addItem("------")
  83. self.endSpaceQc.addItem("------")
  84. self.endSpaceQc.addItems(self.space_nodes_)
  85. self.endStreetQc.activated.connect(self.endStreetQCChanged)
  86. self.endSpaceQc.activated.connect(self.endSpaceQCChanged)
  87. self.posestatic = QLabel(self)
  88. self.posestatic.setGeometry(260, 20, 100, 90)
  89. self.LedLabel = QLabel(self)
  90. self.LedLabel.setGeometry(80, 160, 50, 50)
  91. # 发送轴距
  92. self.wheelbasestatic = QLabel(self)
  93. self.wheelbasestatic.setText("轴距:")
  94. self.wheelbasestatic.setGeometry(260, 100, 40, 30)
  95. self.WheelBaseEdit = QLineEdit(self)
  96. self.WheelBaseEdit.setText(str(self.default_wheel_base))
  97. self.WheelBaseEdit.setGeometry(300, 100, 50, 30)
  98. self.btnModelCheck = QCheckBox("整体协调", self)
  99. self.btnModelCheck.setGeometry(360, 130, 100, 40)
  100. self.btnModelCheck.clicked.connect(self.MainAgvchangecb)
  101. self.btnClampCheck = QCheckBox("夹持", self)
  102. self.btnClampCheck.setGeometry(260, 160, 60, 40)
  103. self.btnClampCheck.clicked.connect(self.ClampClick)
  104. self.btnLifterCheck = QCheckBox("提升", self)
  105. self.btnLifterCheck.setGeometry(360, 160, 60, 40)
  106. self.btnLifterCheck.clicked.connect(self.LifterClick)
  107. self.btnAuto = QCheckBox(self)
  108. self.btnAuto.setText("自动测试")
  109. self.btnAuto.setGeometry(260, 195, 100, 40)
  110. self.btnAuto.stateChanged.connect(self.AutoCheck)
  111. self.btnAutoDirect = QCheckBox(self)
  112. self.btnAutoDirect.setText("自由方向")
  113. self.btnAutoDirect.setGeometry(260, 130, 100, 40)
  114. self.btnAutoDirect.setCheckState(Qt.Checked)
  115. self.btnAutoBegChanged = QCheckBox(self)
  116. self.btnAutoBegChanged.setText("起点自动")
  117. self.btnAutoBegChanged.setGeometry(260, 10, 100, 20)
  118. self.btnAutoBegChanged.setCheckState(Qt.Checked)
  119. # 选择地图
  120. self.mapstatic = QLabel(self)
  121. self.mapstatic.setText("地图")
  122. self.mapstatic.setGeometry(360, 5, 80, 30)
  123. self.select_map_Qc = QComboBox(self)
  124. self.select_map_Qc.setGeometry(400, 10, 100, 20)
  125. maps = ["Front", "Back", "Base"]
  126. self.select_map_Qc.addItem("------")
  127. self.select_map_Qc.addItems(maps)
  128. self.select_map_Qc.activated.connect(self.select_map_QcChanged)
  129. # 选择流程
  130. self.seq_static = QLabel(self)
  131. self.seq_static.setText("流程")
  132. self.seq_static.setGeometry(360, 35, 80, 30)
  133. self.select_seq_Qc = QComboBox(self)
  134. self.select_seq_Qc.setGeometry(400, 40, 100, 20)
  135. sequence = ["Park", "Pick"]
  136. self.select_seq_Qc.addItem("------")
  137. self.select_seq_Qc.addItems(sequence)
  138. self.select_seq_Qc.activated.connect(self.select_seq_QcChanged)
  139. def Update(self):
  140. if self.robot_.timedRobotStatu_.timeout() == False:
  141. x = self.robot_.timedRobotStatu_.statu.x
  142. y = self.robot_.timedRobotStatu_.statu.y
  143. yaw = self.robot_.timedRobotStatu_.statu.theta
  144. self.posestatic.setText("x : %.3f\ny : %.3f\nΘ : %.2f°" % (x, y, yaw * 180 / math.pi))
  145. if self.btnAutoBegChanged.checkState() == Qt.Checked:
  146. [label, pt] = mp.MapManager().findNeastNode("Base", [x, y])
  147. self.begQc.setCurrentText(label)
  148. statu = "min-width: 32px; min-height: 32px;max-width:32px;\
  149. max-height: 32px;border-radius: 16px; border:1px solid black;background:green"
  150. if self.robot_.Connected() == False:
  151. statu = "min-width: 32px; min-height: 32px;max-width:32px;\
  152. max-height: 32px;border-radius: 16px; border:1px solid black;background:gray"
  153. # self.endQc.setEnabled(False)
  154. elif self.robot_.IsNavigating() == True:
  155. statu = "min-width: 32px; min-height: 32px;max-width:32px;\
  156. max-height: 32px;border-radius: 16px; border:1px solid black;background:yellow"
  157. self.endStreetQc.setEnabled(False)
  158. self.endSpaceQc.setEnabled(False)
  159. self.btnModelCheck.setEnabled(False)
  160. self.btnClampCheck.setEnabled(False)
  161. self.btnLifterCheck.setEnabled(False)
  162. else:
  163. self.endStreetQc.setEnabled(True)
  164. self.endSpaceQc.setEnabled(True)
  165. self.btnModelCheck.setEnabled(True)
  166. self.btnClampCheck.setEnabled(True)
  167. self.btnLifterCheck.setEnabled(True)
  168. self.LedLabel.setStyleSheet(statu)
  169. # self.wheelbasestatic.setVisible(self.robot_.IsMainAgv())
  170. # self.WheelBaseEdit.setVisible(self.robot_.IsMainAgv())
  171. self.btnModelCheck.setVisible(self.robot_.IsMainAgv())
  172. if self.robot_.IsMainModeStatu():
  173. self.btnModelCheck.setCheckState(Qt.Checked)
  174. else:
  175. self.btnModelCheck.setCheckState(Qt.Unchecked)
  176. if self.robot_.IsClampClosed():
  177. self.btnClampCheck.setCheckState(Qt.Checked)
  178. else:
  179. self.btnClampCheck.setCheckState(Qt.Unchecked)
  180. if self.robot_.IsLiferRose():
  181. self.btnLifterCheck.setCheckState(Qt.Checked)
  182. else:
  183. self.btnLifterCheck.setCheckState(Qt.Unchecked)
  184. if self.robot_.IsNavigating():
  185. self.begId_ = self.robot_.begId_
  186. self.targetId_ = self.robot_.targetId_
  187. djks_map = mp.MapManager()
  188. beg_node = djks_map.GetVertex("Base", self.begId_)
  189. end_node = djks_map.GetVertex("Base", self.targetId_)
  190. if beg_node is not None:
  191. self.begQc.setCurrentText(self.begId_)
  192. if end_node is not None:
  193. if isinstance(end_node, (mp.SpaceNode)):
  194. self.endStreetQc.setCurrentText("------")
  195. self.endSpaceQc.setCurrentText(self.targetId_)
  196. if isinstance(end_node, (mp.StreetNode)):
  197. self.endStreetQc.setCurrentText(self.targetId_)
  198. self.endSpaceQc.setCurrentText("------")
  199. # if self.robot_.IsMainModeStatu():
  200. # self.btnAutoDirect.setCheckState(Qt.Unchecked)
  201. def AutoCheck(self):
  202. if self.btnAuto.checkState() == Qt.Checked:
  203. self.robot_.autoTest_ = True
  204. else:
  205. self.robot_.autoTest_ = False
  206. def ClampClick(self):
  207. if self.robot_.IsClampClosed() == False:
  208. self.threadPool_.submit(self.robot_.ClampClose)
  209. else:
  210. self.threadPool_.submit(self.robot_.ClampOpen)
  211. def LifterClick(self):
  212. if self.robot_.IsLiferRose() == False:
  213. self.threadPool_.submit(self.robot_.LiferRise)
  214. else:
  215. self.threadPool_.submit(self.robot_.LiferDown)
  216. def MainAgvchangecb(self):
  217. if self.robot_.IsMainModeStatu() == False:
  218. leng = float(self.WheelBaseEdit.text())
  219. self.threadPool_.submit(self.robot_.SwitchMoveMode, 1, leng)
  220. self.robot_.L_ = leng
  221. else:
  222. self.threadPool_.submit(self.robot_.SwitchMoveMode, 0, 0)
  223. def endStreetQCChanged(self):
  224. id1 = self.begQc.currentText()
  225. id2 = self.endStreetQc.currentText()
  226. if not id1 == "------" and not id2 == "------":
  227. self.robot_.GeneratePath(id1, id2, self.select_map)
  228. self.begId_ = id1
  229. self.targetId_ = id2
  230. def endSpaceQCChanged(self):
  231. id1 = self.begQc.currentText()
  232. id2 = self.endSpaceQc.currentText()
  233. if not id1 == "------" and not id2 == "------":
  234. self.robot_.GeneratePath(id1, id2, self.select_map)
  235. self.begId_ = id1
  236. self.targetId_ = id2
  237. def btnSendClick(self):
  238. if self.btnAuto.checkState() == Qt.Checked:
  239. if self.robot_.IsMainModeStatu():
  240. self.threadPool_.submit(self.robot_.AutoTestNavClamp,
  241. self.begId_, self.targetId_)
  242. else:
  243. print("agv not in main statu")
  244. else:
  245. autoDirect = False
  246. if self.btnAutoDirect.checkState() == Qt.Checked:
  247. print("自由方向")
  248. autoDirect = True
  249. self.robot_.GeneratePath(self.begId_, self.targetId_, self.select_map)
  250. self.threadPool_.submit(self.robot_.Navigatting,
  251. self.begId_, self.targetId_, autoDirect, float(self.WheelBaseEdit.text()),
  252. self.robot_.current_sequence_)
  253. def btnCancelClick(self):
  254. self.controller_status = ControllerStatus.eCancel
  255. self.threadPool_.submit(self.robot_.CancelNavTask)
  256. self.btnAuto.setCheckState(Qt.Unchecked)
  257. def select_map_QcChanged(self):
  258. self.select_map = self.select_map_Qc.currentText()
  259. mp.MapManager().ResetMap("Front", 0, float(self.WheelBaseEdit.text()) / 2)
  260. mp.MapManager().ResetMap("Back", 0, -float(self.WheelBaseEdit.text()) / 2)
  261. print("changed selected map to" + self.select_map)
  262. def select_seq_QcChanged(self):
  263. print("当前流程:", self.select_seq_Qc.currentText())
  264. if self.select_seq_Qc.currentText() == "Park":
  265. self.robot_.SetSequence(1)
  266. elif self.select_seq_Qc.currentText() == "Pick":
  267. self.robot_.SetSequence(2)
  268. else:
  269. self.robot_.SetSequence(0)
  270. class CountFrame(QFrame):
  271. def __init__(self):
  272. QFrame.__init__(self)
  273. self.default_wheel_base = 2.6
  274. self.InitUI()
  275. self.timer_ = QTimer()
  276. self.timer_.timeout.connect(self.UpdateUI)
  277. self.timer_.start(200)
  278. def InitUI(self):
  279. self.static1 = QLabel(self)
  280. self.static1.setText("重载导航次数 : /")
  281. self.static1.setGeometry(5, 5, 200, 30)
  282. self.static2 = QLabel(self)
  283. self.static2.setText("单车导航次数 : /")
  284. self.static2.setGeometry(5, 40, 200, 30)
  285. self.btnFrontCheck = QCheckBox("前车地图", self)
  286. self.btnFrontCheck.setGeometry(210, 5, 100, 30)
  287. self.btnFrontCheck.clicked.connect(self.FrontChecked)
  288. self.btnBackCheck = QCheckBox("后车地图", self)
  289. self.btnBackCheck.setGeometry(310, 5, 100, 30)
  290. self.btnBackCheck.clicked.connect(self.FrontChecked)
  291. self.btnMainCheck = QCheckBox("整车地图", self)
  292. self.btnMainCheck.setGeometry(210, 40, 100, 30)
  293. self.btnMainCheck.clicked.connect(self.FrontChecked)
  294. self.btnBaseCheck = QCheckBox("原始地图", self)
  295. self.btnBaseCheck.setGeometry(310, 40, 100, 30)
  296. self.btnBaseCheck.clicked.connect(self.FrontChecked)
  297. def FrontChecked(self):
  298. # wheelBase = self.default_wheel_base
  299. maps = []
  300. if self.btnFrontCheck.checkState() == Qt.Checked:
  301. maps.append("Front")
  302. if self.btnBackCheck.checkState() == Qt.Checked:
  303. maps.append("Back")
  304. if self.btnMainCheck.checkState() == Qt.Checked:
  305. maps.append("Main")
  306. if self.btnBaseCheck.checkState() == Qt.Checked:
  307. maps.append("Base")
  308. view = viewer.MapGLWidget()
  309. view.SetEnableMaps(maps)
  310. def UpdateUI(self):
  311. load_str = "重载导航次数 : %d" % (Count.TestCount().loadCount())
  312. single_str = "单车导航次数 : %d" % (Count.TestCount().singleCount())
  313. self.static1.setText(load_str)
  314. self.static2.setText(single_str)