test.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # ===============================================================================
  4. #
  5. # test.py
  6. #
  7. # Test program for the simple GL Viewer.
  8. #
  9. # Copyright (c) 2011, Arne Schmitz <arne.schmitz@gmx.net>
  10. # All rights reserved.
  11. #
  12. # Redistribution and use in source and binary forms, with or without
  13. # modification, are permitted provided that the following conditions are met:
  14. # * Redistributions of source code must retain the above copyright
  15. # notice, this list of conditions and the following disclaimer.
  16. # * Redistributions in binary form must reproduce the above copyright
  17. # notice, this list of conditions and the following disclaimer in the
  18. # documentation and/or other materials provided with the distribution.
  19. # * Neither the name of the <organization> nor the
  20. # names of its contributors may be used to endorse or promote products
  21. # derived from this software without specific prior written permission.
  22. #
  23. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  27. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #
  34. # ===============================================================================
  35. import math
  36. import time
  37. import sys
  38. from PyQt5.QtGui import *
  39. from PyQt5.QtWidgets import *
  40. from PyQt5.QtCore import *
  41. from MapGLWidget import MapGLWidget
  42. import json
  43. import dijkstra.Map as mp
  44. import ControllWidget
  45. import JointContrallerWidget
  46. import ManualOperationWidget
  47. import RobotData as RD
  48. import message_pb2 as message
  49. import google.protobuf.json_format as jtf
  50. import uuid
  51. from custom_define import RobotName
  52. # ===============================================================================
  53. class MainWindow(QMainWindow):
  54. """docstring for Mainwindow"""
  55. djks_map_ = mp.DijikstraMap()
  56. ui_nodes = {}
  57. ui_nodes["street_nodes"] = []
  58. ui_nodes["space_nodes"] = []
  59. def __init__(self, parent=None):
  60. super(MainWindow, self).__init__(parent)
  61. self.basic()
  62. self.count_frame_ = ControllWidget.CountFrame()
  63. self.LoadMap()
  64. self.isLocalTest = True
  65. self.isAutoDispatch = False
  66. rpc_1 = "192.168.0.70:9090"
  67. mqtt_1 = ["pyui1", "192.168.0.70", 1883, "admin", "zx123456"]
  68. rpc_2 = "192.168.0.71:9090"
  69. mqtt_2 = ["pyui2", "192.168.0.71", 1883, "admin", "zx123456"]
  70. if self.isLocalTest:
  71. rpc_1 = "127.0.0.1:9090"
  72. mqtt_1 = ["pyui-main", "127.0.0.1", 1883, "admin", "zx123456"]
  73. rpc_2 = "127.0.0.1:9091"
  74. mqtt_2 = ["pyui-child", "127.0.0.1", 1883, "admin", "zx123456"]
  75. config1 = {"label": RobotName.strAGVMain,
  76. "rpc": rpc_1,
  77. "street_nodes": self.ui_nodes["street_nodes"],
  78. "space_nodes": self.ui_nodes["space_nodes"],
  79. "mqtt": mqtt_1,
  80. "subTopics": {"agv_main/agv_statu": message.RobotStatu.__name__,
  81. "agv_main/nav_statu": message.NavStatu.__name__},
  82. "cmdTopic": "agv_main/nav_cmd",
  83. "robotColor": [0.7, 0.2, 0.3]}
  84. config2 = {"label": RobotName.strAGV2,
  85. "rpc": rpc_2,
  86. # "rpc": "127.0.0.1:9091",
  87. "street_nodes": self.ui_nodes["street_nodes"],
  88. "space_nodes": self.ui_nodes["space_nodes"],
  89. "mqtt": mqtt_2,
  90. "subTopics": {"agv_child/agv_statu": message.RobotStatu.__name__,
  91. "agv_child/nav_statu": message.NavStatu.__name__},
  92. "cmdTopic": "agv_child/nav_cmd",
  93. "robotColor": [0.4, 0.2, 0.8]}
  94. self.Controller1 = ControllWidget.ControlFrame(config1)
  95. self.Controller2 = ControllWidget.ControlFrame(config2)
  96. robot_dict = {self.Controller1.robot_.name_: self.Controller1.robot_,
  97. self.Controller2.robot_.name_: self.Controller2.robot_}
  98. self.ManualOperationWidget = ManualOperationWidget.ManualOperationWidget(robot_dict)
  99. splitter_main = self.split_()
  100. self.setCentralWidget(splitter_main)
  101. self.gl.SetRobot1Instance(self.Controller1.robot_)
  102. self.gl.SetRobot2Instance(self.Controller2.robot_)
  103. self.timer_ = QTimer()
  104. self.timer_.timeout.connect(self.Update)
  105. self.timer_.start(100)
  106. def LoadMap(self):
  107. self.gl = MapGLWidget() # 将opengl例子嵌入GUI
  108. map = self.load_map("./map.json")
  109. for node in map['street_nodes'].items():
  110. [id, point] = node
  111. street_node = mp.StreetNode(id, point[0], point[1])
  112. self.djks_map_.AddVertex(street_node)
  113. self.gl.AddNode([id, "street_node", point])
  114. self.ui_nodes["street_nodes"].append(id)
  115. for node in map['space_nodes'].items():
  116. [id, point] = node
  117. [x, y, yaw] = point
  118. space_node = mp.SpaceNode(id, point[0], point[1], yaw)
  119. self.djks_map_.AddVertex(space_node)
  120. glNode = [id, "space_node", [x, y]]
  121. self.gl.AddNode(glNode)
  122. self.ui_nodes["space_nodes"].append(id)
  123. for road in map['roads'].items():
  124. self.gl.AddRoad(road)
  125. [_, points] = road
  126. for pt1 in points:
  127. for pt2 in points:
  128. if not pt1 == pt2:
  129. self.djks_map_.AddEdge(pt1, pt2)
  130. def load_map(self, file):
  131. with open(file, 'r', encoding='utf-8') as fp:
  132. map = json.load(fp)
  133. return map
  134. def Update(self):
  135. self.gl.update()
  136. if self.isAutoDispatch:
  137. self.Controller1.setVisible(False)
  138. self.Controller2.setVisible(False)
  139. else:
  140. self.Controller1.setVisible(True)
  141. self.Controller2.setVisible(True)
  142. # 窗口基础属性
  143. def basic(self):
  144. # 设置标题,大小,图标
  145. self.setWindowTitle("GT")
  146. self.resize(1100, 650)
  147. self.setWindowIcon(QIcon("./image/Gt.png"))
  148. # 居中显示
  149. screen = QDesktopWidget().geometry()
  150. self_size = self.geometry()
  151. self.move(int((screen.width() - self_size.width()) / 2), int((screen.height() - self_size.height()) / 2))
  152. def closeEvent(self, QCloseEvent):
  153. self.gl.close()
  154. # 分割窗口
  155. def split_(self):
  156. splitter_main = QSplitter(Qt.Horizontal)
  157. splitter = QSplitter(Qt.Vertical)
  158. splitter.addWidget(self.count_frame_)
  159. splitter.addWidget(self.Controller1)
  160. splitter.addWidget(self.Controller2)
  161. splitter.addWidget(self.ManualOperationWidget)
  162. splitter.setStretchFactor(0, 1)
  163. splitter.setStretchFactor(1, 3)
  164. splitter.setStretchFactor(2, 3)
  165. splitter.setStretchFactor(3, 1)
  166. splitter_main.addWidget(splitter)
  167. splitter_main.addWidget(self.gl)
  168. splitter_main.setStretchFactor(0, 1)
  169. splitter_main.setStretchFactor(2, 4)
  170. return splitter_main
  171. if __name__ == "__main__":
  172. app = QApplication(sys.argv)
  173. win = MainWindow()
  174. win.show()
  175. sys.exit(app.exec_())
  176. # ===============================================================================
  177. # Main
  178. # ===============================================================================
  179. '''app = QApplication(sys.argv)
  180. mainWindow = MapGLWidget()
  181. mainWindow.show()
  182. mainWindow.raise_() # Need this at least on OS X, otherwise the window ends up in background
  183. sys.exit(app.exec_())'''
  184. # ===============================================================================
  185. #
  186. # Local Variables:
  187. # mode: Python
  188. # indent-tabs-mode: nil
  189. # End:
  190. #
  191. # ===============================================================================