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