test.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 threading
  37. import time
  38. import sys
  39. from PyQt5.QtGui import *
  40. from PyQt5.QtWidgets import *
  41. from PyQt5.QtCore import *
  42. from MapGLWidget import MapGLWidget
  43. import json
  44. import dijkstra.Map as mp
  45. import ControllWidget
  46. import JointContrallerWidget
  47. import ManualOperationWidget
  48. import RobotData as RD
  49. import message_pb2 as message
  50. import google.protobuf.json_format as jtf
  51. import google.protobuf.text_format as tf
  52. from concurrent.futures import ThreadPoolExecutor
  53. import uuid
  54. from mytool.txt_helper.txt_operation import TXTOperation
  55. from custom_define import RobotName
  56. from mytool.RabbitMq_helper import async_communication as rabitmq
  57. # ===============================================================================
  58. class MainWindow(QMainWindow):
  59. """docstring for Mainwindow"""
  60. djks_map_ = mp.MapManager()
  61. ui_nodes = {}
  62. ui_nodes["street_nodes"] = []
  63. ui_nodes["space_nodes"] = []
  64. def __init__(self, parent=None):
  65. super(MainWindow, self).__init__(parent)
  66. self.basic()
  67. self.count_frame_ = ControllWidget.CountFrame()
  68. self.LoadMap()
  69. self.isLocalTest = False
  70. self.isAutoDispatchOnline = True
  71. rpc_1 = "192.168.0.70:9090"
  72. mqtt_1 = ["pyui1", "192.168.0.70", 1883, "admin", "zx123456"]
  73. rpc_2 = "192.168.0.71:9090"
  74. mqtt_2 = ["pyui2", "192.168.0.71", 1883, "admin", "zx123456"]
  75. if self.isLocalTest:
  76. rpc_1 = "127.0.0.1:9090"
  77. mqtt_1 = ["pyui-main", "127.0.0.1", 1883, "admin", "zx123456"]
  78. rpc_2 = "127.0.0.1:9091"
  79. mqtt_2 = ["pyui-child", "127.0.0.1", 1883, "admin", "zx123456"]
  80. config1 = {"label": RobotName.strAGVMain,
  81. "rpc": rpc_1,
  82. "street_nodes": self.ui_nodes["street_nodes"],
  83. "space_nodes": self.ui_nodes["space_nodes"],
  84. "mqtt": mqtt_1,
  85. "subTopics": {"agv_main/agv_statu": message.RobotStatu.__name__,
  86. "agv_main/nav_statu": message.NavStatu.__name__},
  87. "cmdTopic": "agv_main/nav_cmd",
  88. "robotColor": [0.7, 0.2, 0.3]}
  89. config2 = {"label": RobotName.strAGV2,
  90. "rpc": rpc_2,
  91. # "rpc": "127.0.0.1:9091",
  92. "street_nodes": self.ui_nodes["street_nodes"],
  93. "space_nodes": self.ui_nodes["space_nodes"],
  94. "mqtt": mqtt_2,
  95. "subTopics": {"agv_child/agv_statu": message.RobotStatu.__name__,
  96. "agv_child/nav_statu": message.NavStatu.__name__},
  97. "cmdTopic": "agv_child/nav_cmd",
  98. "robotColor": [0.4, 0.2, 0.8]}
  99. self.Controller1 = ControllWidget.ControlFrame(config1)
  100. self.Controller2 = ControllWidget.ControlFrame(config2)
  101. robot_dict = {self.Controller1.robot_.name_: self.Controller1.robot_,
  102. self.Controller2.robot_.name_: self.Controller2.robot_}
  103. self.ManualOperationWidget = ManualOperationWidget.ManualOperationWidget(robot_dict)
  104. splitter_main = self.split_()
  105. self.setCentralWidget(splitter_main)
  106. self.gl.SetRobot1Instance(self.Controller1.robot_)
  107. self.gl.SetRobot2Instance(self.Controller2.robot_)
  108. self.timer_ = QTimer()
  109. self.timer_.timeout.connect(self.Update)
  110. self.timer_.start(100)
  111. # 启动RabbitMQ
  112. self.g_rabbitmq = None
  113. if self.isAutoDispatchOnline:
  114. self.g_rabbitmq = rabitmq.RabbitAsyncCommunicator("192.168.0.201", 5672, "zx", "123456")
  115. calbbacks = [["agv_park_command_request_queue", self.recv_park_request],
  116. ["agv_pick_command_request_queue", self.recv_pick_request]]
  117. self.g_rabbitmq.Init(calbbacks, None)
  118. self.g_rabbitmq.setDaemon(True) # 守护线程随主线程结束
  119. self.g_rabbitmq.start()
  120. def recv_park_request(self, msg):
  121. print("Recv_park_request------------------\n", msg)
  122. park_table = message.park_table()
  123. try:
  124. tf.Parse(msg, park_table)
  125. except Exception as e:
  126. print("Parse error\n")
  127. if self.isAutoDispatchOnline:
  128. self.AutoParkTask(park_table)
  129. def recv_pick_request(self, msg):
  130. print("Recv_pick_request------------------\n", msg)
  131. pick_table = message.pick_table()
  132. try:
  133. tf.Parse(msg, pick_table)
  134. except Exception as e:
  135. print("Parse error\n")
  136. if self.isAutoDispatchOnline:
  137. self.AutoPickTask(pick_table)
  138. def updateMap_park(self, entrance_id, pose, type): # 0:前车轨迹,1:后车轨迹,2:添加整车轨迹
  139. self.djks_map_.Reset() # 重置地图
  140. trans_x, trans_y, trans_a = pose
  141. self.djks_map_.AddVertex_t(mp.SpaceNode(entrance_id, trans_x, trans_y, trans_a)) # 更新库位点
  142. entrance_street_nodes = self.ComputeStreetNode(entrance_id, trans_x, trans_y, trans_a)
  143. print("entrance_space pose: ", self.djks_map_.map_t.graph_.points[entrance_id])
  144. if type == 0:
  145. self.djks_map_.AddVertex_t(mp.StreetNode("FInput_R1101", entrance_street_nodes[0][0],
  146. entrance_street_nodes[0][1])) # 更新库位点对应马路点
  147. self.djks_map_.AddEdge_t(entrance_id, "FInput_R1101")
  148. # 加临时边
  149. for node_id, value in self.djks_map_.VertexDict().items():
  150. if node_id.find("FInput") >= 0:
  151. self.djks_map_.AddEdge_t("FInput_R1101", node_id)
  152. print("F entrance_street pose ", self.djks_map_.map_t.graph_.points["FInput_R1101"])
  153. elif type == 1:
  154. self.djks_map_.AddVertex_t(mp.StreetNode("BInput_R1101", entrance_street_nodes[2][0],
  155. entrance_street_nodes[2][1])) # 更新库位点对应马路点
  156. self.djks_map_.AddEdge_t(entrance_id, "BInput_R1101")
  157. # 加临时边
  158. for node_id, value in self.djks_map_.VertexDict().items():
  159. if node_id.find("BInput") >= 0:
  160. self.djks_map_.AddEdge_t("BInput_R1101", node_id)
  161. else:
  162. self.djks_map_.AddVertex_t(mp.StreetNode("CInput_R1101", entrance_street_nodes[1][0],
  163. entrance_street_nodes[1][1])) # 更新库位点对应马路点
  164. self.djks_map_.AddEdge_t(entrance_id, "CInput_R1101")
  165. for node_id, value in self.djks_map_.VertexDict().items():
  166. if node_id.find("CInput") >= 0:
  167. self.djks_map_.AddEdge_t("CInput_R1101", node_id)
  168. print("C entrance_street pose ", self.djks_map_.map_t.graph_.points["CInput_R1101"])
  169. def GenerateSpecifiedMap(self, map_type): # 0:前车地图,1:后车地图,2:整车地图
  170. '''map = self.load_map("./map.json")
  171. {
  172. for node in map['street_nodes'].items():
  173. [id, point] = node
  174. street_node = mp.StreetNode(id, point[0], point[1])
  175. self.djks_map_.AddVertex(street_node)
  176. self.gl.AddNode([id, "street_node", point])
  177. self.ui_nodes["street_nodes"].append(id)
  178. for node in map['space_nodes'].items():
  179. [id, point] = node
  180. [x, y, yaw] = point
  181. space_node = mp.SpaceNode(id, point[0], point[1], yaw)
  182. self.djks_map_.AddVertex(space_node)
  183. glNode = [id, "space_node", [x, y]]
  184. self.gl.AddNode(glNode)
  185. self.ui_nodes["space_nodes"].append(id)
  186. for road in map['roads'].items():
  187. self.gl.AddRoad(road)
  188. [_, points] = road
  189. for pt1 in points:
  190. for pt2 in points:
  191. if not pt1 == pt2:
  192. self.djks_map_.AddEdge(pt1, pt2)
  193. }
  194. if type == 0:
  195. pass
  196. elif type == 1:
  197. pass
  198. elif type == 2:
  199. pass
  200. else:
  201. print("type of specified map is wrong!\n")'''
  202. def GetMainFrontBackAGV(self):
  203. controlMain = self.Controller1
  204. nagtive = False
  205. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  206. nagtive = True
  207. control1 = None
  208. control2 = None
  209. if nagtive == False:
  210. control1 = self.Controller1
  211. control2 = self.Controller2
  212. else:
  213. control2 = self.Controller1
  214. control1 = self.Controller2
  215. return [controlMain, control1, control2]
  216. def AutoParkTask(self, park_table: message.park_table = None):
  217. print("AutoParkTask:---------------------\n")
  218. [controlMain, control1, control2] = self.GetMainFrontBackAGV()
  219. entrance_id = "S" + str(park_table.terminal_id) # "S1101"
  220. entrance_x, entrance_y, entrance_theta = [park_table.entrance_measure_info.measure_info_to_plc_forward.cx,
  221. park_table.entrance_measure_info.measure_info_to_plc_forward.cy,
  222. (
  223. 90 + park_table.entrance_measure_info.measure_info_to_plc_forward.theta) / 180 * math.pi]
  224. # 变换到地图坐标系
  225. # [dx, dy, da] = [-0.223411843181, -0.643030941486, 178.9478 / 180 * math.pi]
  226. # trans_x = -0.99983137 * entrance_x - 0.01836309 * entrance_y + dx
  227. # trans_y = 0.01836309 * entrance_x - 0.99983137 * entrance_y + dy
  228. [dx, dy, da] = [-0.234040126204, -0.647539079189, 178.9302736990612 / 180 * math.pi]
  229. trans_x = -0.999825716019 * entrance_x - 0.018668506294 * entrance_y + dx
  230. trans_y = 0.018668506294 * entrance_x - 0.999825716019 * entrance_y + dy
  231. trans_a = entrance_theta + da - math.pi
  232. while trans_a < 0:
  233. trans_a += math.pi
  234. print("entrance:", entrance_id, trans_x, trans_y, trans_a / math.pi * 180)
  235. target_id = "S" + str(park_table.allocated_space_info.table_id) # "S147"
  236. print("target:", target_id)
  237. # 轴距
  238. wheel_base = park_table.entrance_measure_info.measure_info_to_plc_forward.wheelbase
  239. control1.WheelBaseEdit.setText(str(wheel_base))
  240. control2.WheelBaseEdit.setText(str(wheel_base))
  241. trans_x += wheel_base / 2 * math.cos(trans_a)
  242. trans_y += wheel_base / 2 * math.sin(trans_a)
  243. # # 双单车入库-----------------------------------------------------
  244. threadPool = ThreadPoolExecutor(2)
  245. threadPool.submit(control1.robot_.SwitchMoveMode, 0, wheel_base)
  246. threadPool.submit(control2.robot_.SwitchMoveMode, 0, wheel_base)
  247. threadPool.shutdown(wait=True)
  248. self.updateMap_park(entrance_id, [trans_x, trans_y, trans_a], 0)
  249. cur_pose1 = control1.robot_.timedRobotStatu_.statu
  250. [label, pt] = mp.MapManager().findNeastNode([cur_pose1.x, cur_pose1.y])
  251. control1.robot_.GeneratePath(label, entrance_id)
  252. print("Main: begin:%s target:%s wheelBase:%f" % (label, entrance_id, wheel_base))
  253. # 后车 ------------------------------------------------------
  254. self.updateMap_park(entrance_id, [trans_x, trans_y, trans_a], 1)
  255. cur_pose2 = control2.robot_.timedRobotStatu_.statu
  256. [label, pt] = mp.MapManager().findNeastNode([cur_pose2.x, cur_pose2.y])
  257. control2.robot_.GeneratePath(label, entrance_id)
  258. print("Child: begin:%s target:%s wheelBase:%f" % (label, entrance_id, wheel_base))
  259. autoDirect = True
  260. threadPool = ThreadPoolExecutor(2)
  261. threadPool.submit(control1.robot_.Navigatting,
  262. label, entrance_id, autoDirect, wheel_base)
  263. threadPool.submit(control2.robot_.Navigatting,
  264. label, entrance_id, autoDirect, wheel_base)
  265. threadPool.shutdown(wait=True)
  266. print(" input entrance completed!!!!")
  267. threadPool = ThreadPoolExecutor(1)
  268. threadPool.submit(controlMain.robot_.SwitchMoveMode, 1, wheel_base)
  269. threadPool.shutdown(wait=True)
  270. threadPool = ThreadPoolExecutor(1)
  271. threadPool.submit(controlMain.robot_.ClampClose)
  272. threadPool.shutdown(wait=True)
  273. # 整车入库---------------------------------
  274. self.updateMap_park(entrance_id, [trans_x, trans_y, trans_a], 2)
  275. controlMain.robot_.GeneratePath(entrance_id, target_id)
  276. print("begin:%s target:%s wheelBase:%f" % (entrance_id, target_id, wheel_base))
  277. threadPool = ThreadPoolExecutor(1)
  278. threadPool.submit(controlMain.robot_.Navigatting,
  279. entrance_id, target_id, autoDirect, wheel_base)
  280. threadPool.shutdown(wait=True)
  281. # 整车松夹池------------------------------------
  282. threadPool = ThreadPoolExecutor(1)
  283. threadPool.submit(controlMain.robot_.ClampOpen)
  284. threadPool.shutdown(wait=True)
  285. threadPool = ThreadPoolExecutor(1)
  286. threadPool.submit(controlMain.robot_.SwitchMoveMode, 0, wheel_base)
  287. threadPool.shutdown(wait=True)
  288. # 双单车出库
  289. controlMain, control1, control2 = self.GetMainFrontBackAGV()
  290. self.djks_map_.Reset() # 重置地图
  291. control1.robot_.GeneratePath(target_id, "FInput_R147")
  292. control2.robot_.GeneratePath(target_id, "BInput_R147")
  293. threadPool = ThreadPoolExecutor(2)
  294. threadPool.submit(control1.robot_.Navigatting,
  295. target_id, "FInput_R147", autoDirect, wheel_base)
  296. threadPool.submit(control2.robot_.Navigatting,
  297. target_id, "BInput_R147", autoDirect, wheel_base)
  298. threadPool.shutdown(wait=True)
  299. # 反馈
  300. self.g_rabbitmq.publish("command_ex", "agv_park_command_response_port",
  301. tf.MessageToString(park_table, as_utf8=True))
  302. print("Publish_park_response------------------\n", park_table)
  303. return True
  304. def updateMap_pick(self, export_id, type): # 0:前车轨迹,1:后车轨迹,2:添加整车轨迹
  305. self.djks_map_.Reset() # 重置地图
  306. # export_id = "S1103"
  307. # 添加出口车位点
  308. self.djks_map_.AddVertex_t(mp.SpaceNode(export_id, self.djks_map_.map_t.graph_.points["S1100"][0],
  309. self.djks_map_.map_t.graph_.points["S1100"][1], 90)) # 更新库位点对应马路点
  310. if type == 0:
  311. self.djks_map_.AddEdge_t(export_id, "FInput_R1100")
  312. elif type == 1:
  313. self.djks_map_.AddEdge_t(export_id, "BInput_R1100")
  314. elif type == 2:
  315. export_street_node_x = self.djks_map_.map_t.graph_.points["CInput_R1100"][0]
  316. export_street_node_y = self.djks_map_.map_t.graph_.points["CInput_R1100"][1]
  317. self.djks_map_.AddVertex_t(mp.StreetNode("CInput_R1103", export_street_node_x,
  318. export_street_node_y)) # 更新库位点对应马路点
  319. self.djks_map_.AddEdge_t(export_id, "CInput_R1103")
  320. for node_id, value in self.djks_map_.VertexDict().items():
  321. if node_id.find("CInput") >= 0:
  322. self.djks_map_.AddEdge_t("CInput_R1103", node_id)
  323. print("C entrance_street pose ", self.djks_map_.map_t.graph_.points["CInput_R1103"])
  324. def AutoPickTask(self, pick_table: message.pick_table = None):
  325. print("AutoPickTask:---------------------\n")
  326. controlMain, control1, control2 = self.GetMainFrontBackAGV()
  327. self.djks_map_.Reset() # 重置地图
  328. space_id = "S" + str(pick_table.actually_space_info.table_id) # "S147"
  329. export_id = "S" + str(pick_table.terminal_id) # "S1103"
  330. print(space_id, export_id)
  331. # 轴距
  332. wheel_base = pick_table.actually_measure_info.measure_info_to_plc_forward.wheelbase
  333. control1.WheelBaseEdit.setText(str(wheel_base))
  334. control2.WheelBaseEdit.setText(str(wheel_base))
  335. # 双单车入库
  336. threadPool = ThreadPoolExecutor(2)
  337. threadPool.submit(control1.robot_.SwitchMoveMode, 0, wheel_base)
  338. threadPool.submit(control2.robot_.SwitchMoveMode, 0, wheel_base)
  339. threadPool.shutdown(wait=True)
  340. cur_pose1 = control1.robot_.timedRobotStatu_.statu
  341. [label, pt] = mp.MapManager().findNeastNode([cur_pose1.x, cur_pose1.y])
  342. control1.robot_.GeneratePath(label, space_id)
  343. print("Main: begin:%s target:%s wheelBase:%f" % (label, space_id, wheel_base))
  344. cur_pose2 = control2.robot_.timedRobotStatu_.statu
  345. [label, pt] = mp.MapManager().findNeastNode([cur_pose2.x, cur_pose2.y])
  346. control2.robot_.GeneratePath(label, space_id)
  347. print("Child: begin:%s target:%s wheelBase:%f" % (label, space_id, wheel_base))
  348. autoDirect = True
  349. threadPool = ThreadPoolExecutor(2)
  350. threadPool.submit(control1.robot_.Navigatting,
  351. label, space_id, autoDirect, wheel_base)
  352. threadPool.submit(control2.robot_.Navigatting,
  353. label, space_id, autoDirect, wheel_base)
  354. threadPool.shutdown(wait=True)
  355. # 整车夹持------------------------------------
  356. print(" input space completed!!!!")
  357. threadPool = ThreadPoolExecutor(1)
  358. threadPool.submit(controlMain.robot_.SwitchMoveMode, 1, wheel_base)
  359. threadPool.shutdown(wait=True)
  360. threadPool = ThreadPoolExecutor(1)
  361. threadPool.submit(controlMain.robot_.ClampClose)
  362. threadPool.shutdown(wait=True)
  363. # 整车到出口---------------------------------
  364. self.updateMap_pick(export_id, 2)
  365. controlMain.robot_.GeneratePath(space_id, export_id)
  366. print("Total: begin:%s target:%s wheelBase:%f" % (space_id, export_id, wheel_base))
  367. threadPool = ThreadPoolExecutor(1)
  368. threadPool.submit(controlMain.robot_.Navigatting,
  369. space_id, export_id, True, wheel_base)
  370. threadPool.shutdown(wait=True)
  371. # 整车松夹池------------------------------------
  372. threadPool = ThreadPoolExecutor(1)
  373. threadPool.submit(controlMain.robot_.ClampOpen)
  374. threadPool.shutdown(wait=True)
  375. threadPool = ThreadPoolExecutor(1)
  376. threadPool.submit(controlMain.robot_.SwitchMoveMode, 0, wheel_base)
  377. threadPool.shutdown(wait=True)
  378. # 双单车出库
  379. controlMain, control1, control2 = self.GetMainFrontBackAGV()
  380. self.updateMap_pick(export_id, 0)
  381. control1.robot_.GeneratePath(export_id, "FInput_R1100")
  382. self.updateMap_pick(export_id, 1)
  383. control2.robot_.GeneratePath(export_id, "BInput_R1100")
  384. threadPool = ThreadPoolExecutor(2)
  385. threadPool.submit(control1.robot_.Navigatting,
  386. export_id, "FInput_R1100", True, wheel_base)
  387. threadPool.submit(control2.robot_.Navigatting,
  388. export_id, "BInput_R1100", True, wheel_base)
  389. threadPool.shutdown(wait=True)
  390. # 反馈
  391. self.g_rabbitmq.publish("command_ex", "agv_pick_command_response_port",
  392. tf.MessageToString(pick_table, as_utf8=True))
  393. print("Publish_park_response------------------\n", pick_table)
  394. return True
  395. def ComputeStreetNode(self, s_id, s_x, s_y, s_theta):
  396. """
  397. """
  398. n_x, n_y = 0, 0
  399. if s_id == "S1101":
  400. n_y = self.djks_map_.map_t.graph_.points["CInput_R1100"][1]
  401. k = math.tan(s_theta)
  402. n_x = (n_y - s_y) / k + s_x # 弧度
  403. n_yF = self.djks_map_.map_t.graph_.points["FInput_R1100"][1]
  404. n_xF = (n_yF - s_y) / k + s_x # 弧度
  405. n_yB = self.djks_map_.map_t.graph_.points["BInput_R1100"][1]
  406. n_xB = (n_yB - s_y) / k + s_x # 弧度
  407. # print(n_x, n_y)
  408. return [[n_xF, n_yF], [n_x, n_y], [n_xB, n_yB]]
  409. def LoadMap(self):
  410. self.gl = MapGLWidget() # 将opengl例子嵌入GUI
  411. map = self.load_map("./map.json")
  412. for node in map['street_nodes'].items():
  413. [id, point] = node
  414. street_node = mp.StreetNode(id, point[0], point[1])
  415. self.djks_map_.AddVertex(street_node)
  416. self.gl.AddNode([id, "street_node", point])
  417. self.ui_nodes["street_nodes"].append(id)
  418. for node in map['space_nodes'].items():
  419. [id, point] = node
  420. [x, y, yaw] = point
  421. space_node = mp.SpaceNode(id, point[0], point[1], yaw)
  422. self.djks_map_.AddVertex(space_node)
  423. glNode = [id, "space_node", [x, y]]
  424. self.gl.AddNode(glNode)
  425. self.ui_nodes["space_nodes"].append(id)
  426. for road in map['roads'].items():
  427. self.gl.AddRoad(road)
  428. [_, points] = road
  429. for pt1 in points:
  430. for pt2 in points:
  431. if not pt1 == pt2:
  432. self.djks_map_.AddEdge(pt1, pt2)
  433. def load_map(self, file):
  434. with open(file, 'r', encoding='utf-8') as fp:
  435. map = json.load(fp)
  436. return map
  437. def Update(self):
  438. self.gl.update()
  439. if self.isAutoDispatchOnline:
  440. # self.Controller1.setEnabled(False)
  441. # self.Controller2.setEnabled(False)
  442. pass
  443. else:
  444. self.Controller1.setVisible(True)
  445. self.Controller2.setVisible(True)
  446. # 窗口基础属性
  447. def basic(self):
  448. # 设置标题,大小,图标
  449. self.setWindowTitle("GT")
  450. self.resize(1100, 650)
  451. self.setWindowIcon(QIcon("./image/Gt.png"))
  452. # 居中显示
  453. screen = QDesktopWidget().geometry()
  454. self_size = self.geometry()
  455. self.move(int((screen.width() - self_size.width()) / 2), int((screen.height() - self_size.height()) / 2))
  456. def closeEvent(self, QCloseEvent):
  457. self.gl.close()
  458. print("close...")
  459. # 分割窗口
  460. def split_(self):
  461. splitter_main = QSplitter(Qt.Horizontal)
  462. splitter = QSplitter(Qt.Vertical)
  463. splitter.addWidget(self.count_frame_)
  464. splitter.addWidget(self.Controller1)
  465. splitter.addWidget(self.Controller2)
  466. splitter.addWidget(self.ManualOperationWidget)
  467. splitter.setStretchFactor(0, 1)
  468. splitter.setStretchFactor(1, 3)
  469. splitter.setStretchFactor(2, 3)
  470. splitter.setStretchFactor(3, 1)
  471. splitter_main.addWidget(splitter)
  472. splitter_main.addWidget(self.gl)
  473. splitter_main.setStretchFactor(0, 1)
  474. splitter_main.setStretchFactor(2, 4)
  475. return splitter_main
  476. if __name__ == "__main__":
  477. app = QApplication(sys.argv)
  478. win = MainWindow()
  479. win.show()
  480. sys.exit(app.exec_())
  481. # ===============================================================================
  482. # Main
  483. # ===============================================================================
  484. '''app = QApplication(sys.argv)
  485. mainWindow = MapGLWidget()
  486. mainWindow.show()
  487. mainWindow.raise_() # Need this at least on OS X, otherwise the window ends up in background
  488. sys.exit(app.exec_())'''
  489. # ===============================================================================
  490. #
  491. # Local Variables:
  492. # mode: Python
  493. # indent-tabs-mode: nil
  494. # End:
  495. #
  496. # ===============================================================================