test.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 asyncio
  36. import math
  37. import threading
  38. import time
  39. import sys
  40. from PyQt5.QtGui import *
  41. from PyQt5.QtWidgets import *
  42. from PyQt5.QtCore import *
  43. from MapGLWidget import MapGLWidget
  44. import json
  45. import dijkstra.Map as mp
  46. import ControllWidget
  47. import JointContrallerWidget
  48. import ManualOperationWidget
  49. import RobotData as RD
  50. import message_pb2 as message
  51. import google.protobuf.json_format as jtf
  52. import google.protobuf.text_format as tf
  53. from concurrent.futures import ThreadPoolExecutor
  54. import uuid
  55. from mytool.txt_helper.txt_operation import TXTOperation
  56. from custom_define import RobotName
  57. from mytool.RabbitMq_helper import async_communication as rabitmq
  58. import concurrent.futures
  59. import TaskTable
  60. from queue import Queue
  61. # ===============================================================================
  62. class MainWindow(QMainWindow):
  63. """docstring for Mainwindow"""
  64. mapManager = mp.MapManager()
  65. ui_nodes = {}
  66. ui_nodes["street_nodes"] = []
  67. ui_nodes["space_nodes"] = []
  68. taskTable = TaskTable.TaskTable()
  69. def __init__(self, parent=None):
  70. super(MainWindow, self).__init__(parent)
  71. self.basic()
  72. self.showMaximized()
  73. self.count_frame_ = ControllWidget.CountFrame()
  74. self.isLocalTest = False
  75. self.isAutoDispatchOnline = True
  76. self.Cancel_ = False
  77. task_thread = threading.Thread(target=self.execTask)
  78. task_thread.start()
  79. self.mapManager.LoadJson("./map.json")
  80. for node_id in self.mapManager.VertexDict("Base"):
  81. if node_id.find("S_") >= 0:
  82. self.ui_nodes["space_nodes"].append(node_id)
  83. if node_id.find("R_") >= 0:
  84. self.ui_nodes["street_nodes"].append(node_id)
  85. rpc_1 = "192.168.0.70:9090"
  86. mqtt_1 = ["pyui1", "192.168.0.70", 1883, "admin", "zx123456"]
  87. rpc_2 = "192.168.0.71:9090"
  88. mqtt_2 = ["pyui2", "192.168.0.71", 1883, "admin", "zx123456"]
  89. if self.isLocalTest:
  90. rpc_1 = "127.0.0.1:9090"
  91. mqtt_1 = ["pyui-main", "127.0.0.1", 1883, "admin", "zx123456"]
  92. rpc_2 = "127.0.0.1:9091"
  93. mqtt_2 = ["pyui-child", "127.0.0.1", 1883, "admin", "zx123456"]
  94. config1 = {"label": RobotName.strAGVMain,
  95. "rpc": rpc_1,
  96. "street_nodes": self.ui_nodes["street_nodes"],
  97. "space_nodes": self.ui_nodes["space_nodes"],
  98. "mqtt": mqtt_1,
  99. "subTopics": {"agv_main/agv_statu": message.RobotStatu.__name__,
  100. "agv_main/nav_statu": message.NavStatu.__name__},
  101. "cmdTopic": "agv_main/nav_cmd",
  102. "robotColor": [0.7, 0.2, 0.3]}
  103. config2 = {"label": RobotName.strAGV2,
  104. "rpc": rpc_2,
  105. # "rpc": "127.0.0.1:9091",
  106. "street_nodes": self.ui_nodes["street_nodes"],
  107. "space_nodes": self.ui_nodes["space_nodes"],
  108. "mqtt": mqtt_2,
  109. "subTopics": {"agv_child/agv_statu": message.RobotStatu.__name__,
  110. "agv_child/nav_statu": message.NavStatu.__name__},
  111. "cmdTopic": "agv_child/nav_cmd",
  112. "robotColor": [0.4, 0.2, 0.8]}
  113. self.gl = MapGLWidget() # 将opengl例子嵌入GUI
  114. self.Controller1 = ControllWidget.ControlFrame(config1)
  115. self.Controller2 = ControllWidget.ControlFrame(config2)
  116. robot_dict = {self.Controller1.robot_.name_: self.Controller1.robot_,
  117. self.Controller2.robot_.name_: self.Controller2.robot_}
  118. self.ManualOperationWidget = ManualOperationWidget.ManualOperationWidget(robot_dict)
  119. splitter_main = self.split_()
  120. self.setCentralWidget(splitter_main)
  121. self.gl.SetRobot1Instance(self.Controller1.robot_)
  122. self.gl.SetRobot2Instance(self.Controller2.robot_)
  123. self.gl.SetMaps(self.mapManager.maps)
  124. self.timer_ = QTimer()
  125. self.timer_.timeout.connect(self.Update)
  126. self.timer_.start(100)
  127. # 启动RabbitMQ
  128. self.g_rabbitmq = None
  129. if self.isAutoDispatchOnline:
  130. self.g_rabbitmq = rabitmq.RabbitAsyncCommunicator("192.168.0.201", 5672, "zx", "123456")
  131. calbbacks = [["agv_park_command_request_queue", self.recv_park_request],
  132. ["agv_pick_command_request_queue", self.recv_pick_request]]
  133. self.g_rabbitmq.Init(calbbacks, None)
  134. self.g_rabbitmq.setDaemon(True) # 守护线程随主线程结束
  135. self.g_rabbitmq.start()
  136. def execTask(self):
  137. while self.Cancel_ == False:
  138. task = self.taskTable.GetTask()
  139. if task != None:
  140. if isinstance(task, (message.park_table)):
  141. print(" exec park task :%s" % (task.primary_key))
  142. ret = self.AutoParkTask(task)
  143. self.taskTable.UpdateResult(task.primary_key, ret)
  144. if isinstance(task, (message.pick_table)):
  145. print(" exec pick task :%s" % (task.primary_key))
  146. ret = self.AutoPickTask(task)
  147. self.taskTable.UpdateResult(task.primary_key, ret)
  148. time.sleep(0.5)
  149. async def recv_park_request(self, msg):
  150. print("Recv_park_request------------------\n", msg)
  151. park_table = message.park_table()
  152. try:
  153. tf.Parse(msg, park_table)
  154. except Exception as e:
  155. print("Parse error\n")
  156. return False
  157. if self.isAutoDispatchOnline:
  158. return await self.PushParkTask(park_table)
  159. return False
  160. async def recv_pick_request(self, msg):
  161. print("Recv_pick_request------------------\n", msg)
  162. pick_table = message.pick_table()
  163. try:
  164. tf.Parse(msg, pick_table)
  165. except Exception as e:
  166. print("Parse error\n")
  167. return False
  168. if self.isAutoDispatchOnline:
  169. return await self.PushPickTask(pick_table)
  170. return False
  171. def updateMap_park(self, entrance_id, pose, wheelbase): # 0:前车轨迹,1:后车轨迹,2:添加整车轨迹
  172. # 修改Front地图
  173. self.mapManager.ResetMap("Front", 0, wheelbase / 2)
  174. [trans_x, trans_y, trans_a] = pose
  175. self.mapManager.maps["Front"].ResetVertexValue(mp.SpaceNode(entrance_id, trans_x, trans_y, trans_a))
  176. [f_id, f_x, f_y] = self.ComputeStreetNode("Front", entrance_id, trans_x, trans_y, trans_a)
  177. # 修改Back地图
  178. self.mapManager.ResetMap("Back", 0, -wheelbase / 2)
  179. [trans_x, trans_y, trans_a] = pose
  180. self.mapManager.maps["Back"].ResetVertexValue(mp.SpaceNode(entrance_id, trans_x, trans_y, trans_a))
  181. [b_id, b_x, b_y] = self.ComputeStreetNode("Back", entrance_id, trans_x, trans_y, trans_a)
  182. # 修正两个地图巷道点
  183. fff = False # 是否是整车空载进 出入口
  184. if fff == True:
  185. f_x = (f_x + b_x) / 2
  186. b_x = (f_x + b_x) / 2
  187. self.mapManager.maps["Front"].ResetVertexValue(mp.StreetNode(f_id, f_x, f_y))
  188. self.mapManager.maps["Back"].ResetVertexValue(mp.StreetNode(b_id, b_x, b_y))
  189. def asyncExecute(self, tasks):
  190. results = [feature.result() for feature in concurrent.futures.as_completed(tasks)]
  191. print("==========================\n")
  192. print(results)
  193. print("==========================\n")
  194. ret = True
  195. for result in results:
  196. ret = ret and result
  197. return ret
  198. def GetMainFrontBackAGV(self):
  199. controlMain = self.Controller1
  200. nagtive = False
  201. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  202. nagtive = True
  203. control1 = None
  204. control2 = None
  205. if nagtive == False:
  206. control1 = self.Controller1
  207. control2 = self.Controller2
  208. else:
  209. control2 = self.Controller1
  210. control1 = self.Controller2
  211. return [controlMain, control1, control2]
  212. async def PushParkTask(self, park_table: message.park_table = None):
  213. pushed = False
  214. while pushed == False and self.Cancel_ == False:
  215. pushed = self.taskTable.PushTask(park_table.primary_key, park_table)
  216. await asyncio.sleep(0.3)
  217. if pushed != True or self.Cancel_ != False:
  218. return False
  219. ret = None
  220. while pushed and self.Cancel_ == False:
  221. ret = self.taskTable.GetResult(park_table.primary_key)
  222. if ret == None:
  223. await asyncio.sleep(1)
  224. else:
  225. break
  226. return (ret != None)
  227. async def PushPickTask(self, pick_table: message.pick_table = None):
  228. pushed = False
  229. while pushed == False and self.Cancel_ == False:
  230. pushed = self.taskTable.PushTask(pick_table.primary_key, pick_table)
  231. await asyncio.sleep(0.3)
  232. if pushed != True or self.Cancel_ != False:
  233. return False
  234. ret = None
  235. while pushed and self.Cancel_ == False:
  236. ret = self.taskTable.GetResult(pick_table.primary_key)
  237. if ret == None:
  238. await asyncio.sleep(1)
  239. else:
  240. break
  241. return (ret != None)
  242. def AutoParkTask(self, park_table: message.park_table = None):
  243. beg_time = time.time()
  244. print(beg_time, "AutoParkTask:---------------------\n")
  245. task_flag = 1 # 存车
  246. [controlMain, control1, control2] = self.GetMainFrontBackAGV()
  247. mainMap, frontMap, backMap = ["Base", "Front", "Back"]
  248. entrance_id = "S_" + str(park_table.terminal_id) # "S1101"
  249. entrance_x, entrance_y, entrance_theta = [park_table.entrance_measure_info.measure_info_to_plc_forward.cx,
  250. park_table.entrance_measure_info.measure_info_to_plc_forward.cy,
  251. (
  252. 90 + park_table.entrance_measure_info.measure_info_to_plc_forward.theta) / 180 * math.pi]
  253. # 变换到地图坐标系
  254. # [dx, dy, da] = [-0.184040126204, -0.647539079189, 178.9302736990612 / 180 * math.pi]
  255. # trans_x = -0.999825716019 * entrance_x - 0.018668506294 * entrance_y + dx
  256. # trans_y = 0.018668506294 * entrance_x - 0.999825716019 * entrance_y + dy
  257. [dx, dy, da] = [-0.184040126204, -0.647539079189, 178.86625855601127 / 180 * math.pi]
  258. trans_x = -0.9998042333928334 * entrance_x - 0.01978622980177775 * entrance_y + dx
  259. trans_y = 0.01978622980177775 * entrance_x - 0.9998042333928334 * entrance_y + dy
  260. trans_a = entrance_theta + da - math.pi
  261. while trans_a < 0:
  262. trans_a += math.pi
  263. print("entrance:", entrance_id, trans_x, trans_y, trans_a / math.pi * 180)
  264. target_id = "S_" + str(park_table.allocated_space_info.table_id) # "S147"
  265. print("target:", target_id)
  266. # 轴距
  267. wheel_base = park_table.entrance_measure_info.measure_info_to_plc_forward.wheelbase
  268. control1.WheelBaseEdit.setText(str(wheel_base))
  269. control2.WheelBaseEdit.setText(str(wheel_base))
  270. trans_x += wheel_base / 2 * math.cos(trans_a)
  271. trans_y += wheel_base / 2 * math.sin(trans_a)
  272. # # 双单车入库-----------------------------------------------------
  273. # if (controlMain.robot_.SwitchMoveMode(0, wheel_base) == False):
  274. # return False
  275. # autoDirect = True
  276. # self.updateMap_park(entrance_id, [trans_x, trans_y, trans_a], wheel_base)
  277. #
  278. # cur_pose1 = control1.robot_.timedRobotStatu_.statu
  279. # [label, pt] = self.mapManager.findNeastNode(frontMap, [cur_pose1.x, cur_pose1.y])
  280. # control1.robot_.GeneratePath(label, entrance_id, frontMap)
  281. # print("Front: begin:%s target:%s wheelBase:%f" % (label, entrance_id, wheel_base))
  282. #
  283. # cur_pose2 = control2.robot_.timedRobotStatu_.statu
  284. # [label, pt] = self.mapManager.findNeastNode(backMap, [cur_pose2.x, cur_pose2.y])
  285. # control2.robot_.GeneratePath(label, entrance_id, backMap)
  286. # print("Back: begin:%s target:%s wheelBase:%f" % (label, entrance_id, wheel_base))
  287. #
  288. # threadPool = ThreadPoolExecutor(2)
  289. # features = [
  290. # threadPool.submit(control1.robot_.Navigatting, label, entrance_id, autoDirect, wheel_base, task_flag),
  291. # threadPool.submit(control2.robot_.Navigatting, label, entrance_id, autoDirect, wheel_base, task_flag)
  292. # ]
  293. # if self.asyncExecute(features) == False:
  294. # print(" Park Failed ")
  295. # return False
  296. # # ------------------------------------------
  297. # # 双单车驶至入口前巷道点
  298. self.updateMap_park(entrance_id, [trans_x, trans_y, trans_a], wheel_base)
  299. entrance_streetNode = entrance_id.replace("S_", "R_")
  300. if (controlMain.robot_.SwitchMoveMode(0, wheel_base) == False):
  301. return False
  302. cur_pose1 = control1.robot_.timedRobotStatu_.statu
  303. [label, pt] = self.mapManager.findNeastNode(frontMap, [cur_pose1.x, cur_pose1.y])
  304. control1.robot_.GeneratePath(label, entrance_streetNode, frontMap)
  305. print("Front: begin:%s target:%s wheelBase:%f" % (label, entrance_streetNode, wheel_base))
  306. cur_pose2 = control2.robot_.timedRobotStatu_.statu
  307. [label, pt] = self.mapManager.findNeastNode(backMap, [cur_pose2.x, cur_pose2.y])
  308. control2.robot_.GeneratePath(label, entrance_streetNode, backMap)
  309. print("Back: begin:%s target:%s wheelBase:%f" % (label, entrance_streetNode, wheel_base))
  310. autoDirect = True
  311. threadPool = ThreadPoolExecutor(2)
  312. features = [
  313. threadPool.submit(control1.robot_.Navigatting, label, entrance_id, autoDirect, wheel_base, task_flag),
  314. threadPool.submit(control2.robot_.Navigatting, label, entrance_id, autoDirect, wheel_base, task_flag)
  315. ]
  316. if self.asyncExecute(features) == False:
  317. print(" Park Failed ")
  318. return False
  319. # 整车驶进入口---------------------------------
  320. if (controlMain.robot_.SwitchMoveMode(1, wheel_base) == False):
  321. return False
  322. map_name = "Front"
  323. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  324. map_name = "Back"
  325. controlMain.robot_.GeneratePath(entrance_streetNode, entrance_id, map_name)
  326. if controlMain.robot_.Navigatting(entrance_streetNode, entrance_id, True, wheel_base, task_flag) == False:
  327. return False
  328. # ---------------------------------
  329. print(" input entrance completed!!!!")
  330. # 整车夹持
  331. if (controlMain.robot_.SwitchMoveMode(1, wheel_base) == False):
  332. return False
  333. controlMain.robot_.ClampClose()
  334. # 整车去车位---------------------------------
  335. # map_name = "Front"
  336. # if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  337. # map_name = "Back"
  338. # controlMain.robot_.GeneratePath(entrance_id, target_id, map_name)
  339. # print("begin:%s target:%s wheelBase:%f" % (entrance_id, target_id, wheel_base))
  340. # controlMain.robot_.Navigatting(entrance_id, target_id, autoDirect, wheel_base, task_flag)
  341. # 整车驶离入口到入口航道点
  342. entrance_streetNode = entrance_id.replace("S_", "R_")
  343. map_name = "Front"
  344. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  345. map_name = "Back"
  346. controlMain.robot_.GeneratePath(entrance_id, entrance_streetNode, map_name)
  347. print("begin:%s target:%s wheelBase:%f" % (entrance_id, entrance_streetNode, wheel_base))
  348. controlMain.robot_.Navigatting(entrance_id, entrance_streetNode, autoDirect, wheel_base, task_flag)
  349. # 整车从入口航道点到车位
  350. map_name = "Front"
  351. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  352. map_name = "Back"
  353. controlMain.robot_.GeneratePath(entrance_streetNode, target_id, map_name)
  354. print("begin:%s target:%s wheelBase:%f" % (entrance_streetNode, target_id, wheel_base))
  355. controlMain.robot_.Navigatting(entrance_streetNode, target_id, autoDirect, wheel_base, task_flag)
  356. print("-------------------------")
  357. # 整车松夹持------------------------------------
  358. if controlMain.robot_.ClampOpen() == False:
  359. return False
  360. map_name = "Front"
  361. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  362. map_name = "Back"
  363. # 整车出库---------------------------------
  364. # 限定轴距2.6m
  365. temp_wheel_base = self.mapManager.default_wheel_base
  366. self.updateMap_park(entrance_id, [trans_x, trans_y, trans_a], temp_wheel_base)
  367. if (controlMain.robot_.SwitchMoveMode(1, temp_wheel_base) == False):
  368. return False
  369. entrance_streetNode = target_id.replace("S_", "R_")
  370. controlMain.robot_.GeneratePath(target_id, entrance_streetNode, map_name)
  371. if controlMain.robot_.Navigatting(target_id, entrance_streetNode, True, temp_wheel_base, task_flag) == False:
  372. return False
  373. #
  374. # if (controlMain.robot_.SwitchMoveMode(0, wheel_base) == False):
  375. # return False
  376. #
  377. # # 双单车出库
  378. # controlMain, control1, control2 = self.GetMainFrontBackAGV()
  379. # end_street = target_id.replace("S_", "R_")
  380. # control1.robot_.GeneratePath(target_id, end_street, frontMap)
  381. # control2.robot_.GeneratePath(target_id, end_street, backMap)
  382. #
  383. # threadPool = ThreadPoolExecutor(2)
  384. # threadPool.submit(control1.robot_.Navigatting,
  385. # target_id, end_street, autoDirect, wheel_base, task_flag)
  386. # threadPool.submit(control2.robot_.Navigatting,
  387. # target_id, end_street, autoDirect, wheel_base, task_flag)
  388. # threadPool.shutdown(wait=True)
  389. # 反馈
  390. self.g_rabbitmq.publish("command_ex", "agv_park_command_response_port",
  391. tf.MessageToString(park_table, as_utf8=True))
  392. end_time = time.time()
  393. print(end_time, "Park time=", end_time - beg_time, "Publish_park_response------------------\n", park_table)
  394. return True
  395. def updateMap_pick(self, wheelBase): # 轴距
  396. self.mapManager.ResetMap("Front", 0, wheelBase / 2)
  397. self.mapManager.ResetMap("Back", 0, -wheelBase / 2)
  398. def AutoPickTask(self, pick_table: message.pick_table = None):
  399. beg_time = time.time()
  400. print(beg_time,"AutoPickTask:---------------------\n")
  401. task_flag = 2 # 取车
  402. controlMain, control1, control2 = self.GetMainFrontBackAGV()
  403. mainMap, frontMap, backMap = ["Main", "Front", "Back"]
  404. space_id = "S_" + str(pick_table.actually_space_info.table_id) # "S147"
  405. export_id = "S_" + str(pick_table.terminal_id) # "S1101"
  406. print(space_id, export_id)
  407. # 轴距
  408. wheel_base = pick_table.actually_measure_info.measure_info_to_plc_forward.wheelbase
  409. control1.WheelBaseEdit.setText(str(wheel_base))
  410. control2.WheelBaseEdit.setText(str(wheel_base))
  411. # # 双单车入库 -------------------------
  412. # self.updateMap_pick(wheel_base)
  413. # if (controlMain.robot_.SwitchMoveMode(0, wheel_base) == False):
  414. # return False
  415. # cur_pose1 = control1.robot_.timedRobotStatu_.statu
  416. # [label, pt] = self.mapManager.findNeastNode(frontMap, [cur_pose1.x, cur_pose1.y])
  417. # control1.robot_.GeneratePath(label, space_id, frontMap)
  418. # print("Front: begin:%s target:%s wheelBase:%f" % (label, space_id, wheel_base))
  419. #
  420. # cur_pose2 = control2.robot_.timedRobotStatu_.statu
  421. # [label, pt] = self.mapManager.findNeastNode(backMap, [cur_pose2.x, cur_pose2.y])
  422. # control2.robot_.GeneratePath(label, space_id, backMap)
  423. # print("Back: begin:%s target:%s wheelBase:%f" % (label, space_id, wheel_base))
  424. #
  425. # autoDirect = True
  426. # threadPool = ThreadPoolExecutor(2)
  427. # features = [threadPool.submit(control1.robot_.Navigatting, label, space_id, autoDirect, wheel_base, task_flag),
  428. # threadPool.submit(control2.robot_.Navigatting, label, space_id, autoDirect, wheel_base, task_flag)]
  429. # if self.asyncExecute(features) == False:
  430. # print(" Pick Failed ")
  431. # return False
  432. # ------------------------------------------
  433. # # 双单车驶至车位前巷道点
  434. self.updateMap_pick(wheel_base)
  435. space_streetNode = space_id.replace("S_", "R_")
  436. if (controlMain.robot_.SwitchMoveMode(0, wheel_base) == False):
  437. return False
  438. cur_pose1 = control1.robot_.timedRobotStatu_.statu
  439. [label, pt] = self.mapManager.findNeastNode(frontMap, [cur_pose1.x, cur_pose1.y])
  440. control1.robot_.GeneratePath(label, space_streetNode, frontMap)
  441. print("Front: begin:%s target:%s wheelBase:%f" % (label, space_streetNode, wheel_base))
  442. cur_pose2 = control2.robot_.timedRobotStatu_.statu
  443. [label, pt] = self.mapManager.findNeastNode(backMap, [cur_pose2.x, cur_pose2.y])
  444. control2.robot_.GeneratePath(label, space_streetNode, backMap)
  445. print("Back: begin:%s target:%s wheelBase:%f" % (label, space_streetNode, wheel_base))
  446. autoDirect = True
  447. threadPool = ThreadPoolExecutor(2)
  448. features = [
  449. threadPool.submit(control1.robot_.Navigatting, label, space_streetNode, autoDirect, wheel_base, task_flag),
  450. threadPool.submit(control2.robot_.Navigatting, label, space_streetNode, autoDirect, wheel_base, task_flag)
  451. ]
  452. if self.asyncExecute(features) == False:
  453. print(" Pick Failed ")
  454. return False
  455. # 整车驶进车位---------------------------------
  456. if (controlMain.robot_.SwitchMoveMode(1, wheel_base) == False):
  457. return False
  458. map_name = "Front"
  459. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  460. map_name = "Back"
  461. controlMain.robot_.GeneratePath(space_streetNode, space_id, map_name)
  462. if controlMain.robot_.Navigatting(space_streetNode, space_id, True, wheel_base, task_flag) == False:
  463. return False
  464. # ---------------------------------
  465. # 整车夹持------------------------------------
  466. print(" input space completed!!!!")
  467. if (controlMain.robot_.SwitchMoveMode(1, wheel_base) == False):
  468. return False
  469. if controlMain.robot_.ClampClose() == False:
  470. return False
  471. # 整车到出口---------------------------------
  472. map_name = "Front"
  473. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  474. map_name = "Back"
  475. controlMain.robot_.GeneratePath(space_id, export_id, map_name)
  476. print("Total: begin:%s target:%s wheelBase:%f" % (space_id, export_id, wheel_base))
  477. if controlMain.robot_.Navigatting(space_id, export_id, True, wheel_base, task_flag) == False:
  478. return False
  479. # 整车松夹池------------------------------------
  480. if controlMain.robot_.ClampOpen() == False:
  481. return False
  482. # 整车回巷道------------------------------------
  483. temp_wheel_base = self.mapManager.default_wheel_base
  484. self.updateMap_pick(temp_wheel_base)
  485. if (controlMain.robot_.SwitchMoveMode(1, temp_wheel_base) == False):
  486. return False
  487. map_name = "Front"
  488. if controlMain.robot_.timedRobotStatu_.statu.theta < 0:
  489. map_name = "Back"
  490. space_streetNode = export_id.replace("S_", "R_")
  491. controlMain.robot_.GeneratePath(export_id, space_streetNode, map_name)
  492. if controlMain.robot_.Navigatting(export_id, space_streetNode, True, temp_wheel_base, task_flag) == False:
  493. return False
  494. # 双单车出库
  495. # controlMain, control1, control2 = self.GetMainFrontBackAGV()
  496. # self.ReLoadMap(frontMap, "./map.json")
  497. # self.updateMap_pick(export_id, frontMap)
  498. # control1.robot_.GeneratePath(export_id, "F_exportLink", frontMap)
  499. #
  500. # self.ReLoadMap(backMap, "./map.json")
  501. # self.updateMap_pick(export_id, backMap)
  502. # control2.robot_.GeneratePath(export_id, "B_exportLink", backMap)
  503. #
  504. # threadPool = ThreadPoolExecutor(2)
  505. # features=[
  506. # threadPool.submit(control1.robot_.Navigatting,export_id, "F_exportLink", True, wheel_base, task_flag),
  507. # threadPool.submit(control2.robot_.Navigatting,export_id, "B_exportLink", True, wheel_base, task_flag)
  508. # ]
  509. # if self.asyncExecute(features) == False:
  510. # print(" Pick Failed ")
  511. # return False
  512. print(" over publish...")
  513. # 反馈
  514. self.g_rabbitmq.publish("command_ex", "agv_pick_command_response_port",
  515. tf.MessageToString(pick_table, as_utf8=True))
  516. end_time = time.time()
  517. print(end_time, "Pick time=", end_time - beg_time, "Publish_pick_response------------------\n", pick_table)
  518. return True
  519. def ComputeStreetNode(self, mapName, s_id, s_x, s_y, s_theta):
  520. """
  521. """
  522. id, n_x, n_y = "", 0, 0
  523. if s_id == "S_1101":
  524. id = "R_1101"
  525. n_y = self.mapManager.maps[mapName].graph_.points[id][1]
  526. k = math.tan(s_theta)
  527. n_x = (n_y - s_y) / k + s_x # 弧度
  528. print(id, n_x, n_y)
  529. return [id, n_x, n_y]
  530. def readJson(self, file):
  531. with open(file, 'r', encoding='utf-8') as fp:
  532. map = json.load(fp)
  533. return map
  534. def Update(self):
  535. self.gl.update()
  536. if self.isAutoDispatchOnline:
  537. # self.Controller1.setEnabled(False)
  538. # self.Controller2.setEnabled(False)
  539. pass
  540. else:
  541. self.Controller1.setVisible(True)
  542. self.Controller2.setVisible(True)
  543. # 窗口基础属性
  544. def basic(self):
  545. # 设置标题,大小,图标
  546. self.setWindowTitle("GT")
  547. self.resize(1100, 650)
  548. self.setWindowIcon(QIcon("./image/Gt.png"))
  549. # 居中显示
  550. screen = QDesktopWidget().geometry()
  551. self_size = self.geometry()
  552. self.move(int((screen.width() - self_size.width()) / 2), int((screen.height() - self_size.height()) / 2))
  553. def closeEvent(self, QCloseEvent):
  554. self.Cancel_ = True
  555. self.gl.close()
  556. print("close...")
  557. # 分割窗口
  558. def split_(self):
  559. splitter_main = QSplitter(Qt.Horizontal)
  560. splitter = QSplitter(Qt.Vertical)
  561. splitter.addWidget(self.count_frame_)
  562. splitter.addWidget(self.Controller1)
  563. splitter.addWidget(self.Controller2)
  564. splitter.addWidget(self.ManualOperationWidget)
  565. splitter.setStretchFactor(0, 1)
  566. splitter.setStretchFactor(1, 3)
  567. splitter.setStretchFactor(2, 3)
  568. splitter.setStretchFactor(3, 1)
  569. splitter_main.addWidget(splitter)
  570. splitter_main.addWidget(self.gl)
  571. splitter_main.setStretchFactor(0, 1)
  572. splitter_main.setStretchFactor(1, 10)
  573. return splitter_main
  574. if __name__ == "__main__":
  575. app = QApplication(sys.argv)
  576. win = MainWindow()
  577. win.show()
  578. sys.exit(app.exec_())
  579. # ===============================================================================
  580. #
  581. # Local Variables:
  582. # mode: Python
  583. # indent-tabs-mode: nil
  584. # End:
  585. #
  586. # ===============================================================================