RobotData.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. import copy
  2. import enum
  3. import time
  4. import message_pb2 as message
  5. import grpc
  6. import message_pb2_grpc as msg_rpc
  7. from ManualOperationWidget import ManualOperationType
  8. from custom_define import RobotName, ActType
  9. from mqtt_async import MqttAsync
  10. import google.protobuf.json_format as jtf
  11. import json
  12. from dijkstra.Map import DijikstraMap, SpaceNode, StreetNode
  13. import uuid
  14. import random
  15. import math
  16. import Count
  17. from sigmoid import sigmoid
  18. class TimeStatu:
  19. def __init__(self, statu=None, timeout=3):
  20. self.statu = statu
  21. self.time = time.time()
  22. self.timeout_s = timeout
  23. def timeout(self):
  24. tm = time.time()
  25. return tm - self.time > self.timeout_s or self.statu == None
  26. class Robot(MqttAsync):
  27. def __init__(self, rpc_ipport, name=""):
  28. MqttAsync.__init__(self)
  29. self.test_speed = 0
  30. self.timedRobotStatu_ = TimeStatu(message.RobotStatu, 0)
  31. self.timedNavStatu_ = TimeStatu(message.NavStatu, 0)
  32. self.dataTopic_ = {}
  33. self.rpc_ipport_ = rpc_ipport
  34. self.messageArrivedSignal = None
  35. self.currentNavData_ = None
  36. self.navCmdTopic_ = None
  37. if (name == RobotName.strAGVMain):
  38. self.speedCmdTopic = "monitor_child/speedcmd"
  39. else:
  40. self.speedCmdTopic = "monitor_child/speedcmd"
  41. self.currentNavPathNodes_ = None
  42. self.currentNavPath_ = None
  43. self.pathColor_ = [1, 1, 0]
  44. self.robotColor_ = [0.7, 0.2, 0.3] # agv当前颜色
  45. self.name_ = name
  46. self.begId_ = "------"
  47. self.targetId_ = "------"
  48. self.autoTest_ = False
  49. self.l_ = 0.8 # 轮长
  50. self.L_ = 1.3 # 轴距
  51. self.W_ = 2.5 # 宽
  52. self.heat_ = 0 # 心跳
  53. self.manual_status = ManualOperationType.eReady # 正在进行的手动操作
  54. def Color(self):
  55. if self.IsMainModeStatu():
  56. return [0, 0, 0]
  57. return self.robotColor_
  58. def IsMainAgv(self):
  59. if self.timedNavStatu_.timeout() == False:
  60. if self.timedNavStatu_.statu.main_agv == True:
  61. return True
  62. return False
  63. def IsMainModeStatu(self):
  64. if self.IsMainAgv():
  65. if self.timedNavStatu_.timeout() == False:
  66. if self.timedNavStatu_.statu.move_mode == 1:
  67. return True
  68. return False
  69. def SetSubDataTopic(self, match: dict, messageSignal=None):
  70. self.dataTopic_ = match
  71. self.messageArrivedSignal = messageSignal
  72. for item in match.items():
  73. [topic, _] = item
  74. self.subscribe(topic, self.on_message)
  75. def SetCmdTopic(self, topic):
  76. self.navCmdTopic_ = topic
  77. def SetColor(self, pathColor, robotColor):
  78. self.pathColor_ = pathColor
  79. self.robotColor_ = robotColor
  80. def ResetPose(self, agv: message.RobotStatu):
  81. self.timedRobotStatu_ = TimeStatu(agv, 2)
  82. def ResetNavStatu(self, statu: message.NavStatu):
  83. self.timedNavStatu_ = TimeStatu(statu, 2)
  84. def on_message(self, client, userdata, msg):
  85. topic = msg.topic
  86. if self.dataTopic_.get(topic) is not None:
  87. dtype = self.dataTopic_[topic]
  88. if dtype == message.RobotStatu.__name__:
  89. proto = message.RobotStatu()
  90. jtf.Parse(msg.payload.decode(), proto)
  91. self.ResetPose(proto)
  92. if dtype == message.NavStatu.__name__:
  93. self.last_running = self.IsNavigating()
  94. proto = message.NavStatu()
  95. jtf.Parse(msg.payload.decode(), proto)
  96. self.ResetNavStatu(proto)
  97. if self.last_running == True and self.IsNavigating() == False:
  98. self.currentNavPathNodes_ = None
  99. self.currentNavPath_ = None
  100. if self.messageArrivedSignal is not None:
  101. self.messageArrivedSignal()
  102. def MpcSelectTraj(self):
  103. traj = []
  104. if self.timedNavStatu_.timeout() == False:
  105. nav = self.timedNavStatu_.statu
  106. for pose2d in nav.selected_traj.poses:
  107. traj.append([pose2d.x, pose2d.y, pose2d.theta])
  108. return traj
  109. def MpcPredictTraj(self):
  110. traj = []
  111. if self.timedNavStatu_.timeout() == False:
  112. nav = self.timedNavStatu_.statu
  113. for pose2d in nav.predict_traj.poses:
  114. traj.append([pose2d.x, pose2d.y, pose2d.theta])
  115. return traj
  116. def Connected(self):
  117. return self.timedNavStatu_.timeout() == False and self.timedRobotStatu_.timeout() == False
  118. def IsNavigating(self):
  119. return not self.ActionType() == ActType.eReady
  120. '''if self.timedNavStatu_.timeout() == False:
  121. key = self.timedNavStatu_.statu.key
  122. if key == "" or key == None:
  123. return False
  124. return True'''
  125. def GeneratePath(self, begID, endID, task_type="None"):
  126. self.currentNavPathNodes_ = DijikstraMap().GetShortestPath(begID, endID)
  127. if task_type == "DoubleAGV":
  128. print("AdjustPath------------------")
  129. if self.IsMainModeStatu() is False:
  130. print("原始路径:")
  131. for node in self.currentNavPathNodes_:
  132. print(node.id_, node.y_)
  133. self.currentNavPathNodes_ = self.AdjustPath(self.currentNavPathNodes_)
  134. print("修改路径:")
  135. for node in self.currentNavPathNodes_:
  136. print(node.id_, node.y_)
  137. self.currentNavPath_ = DijikstraMap().CreatePath(self.currentNavPathNodes_, 0.5)
  138. if self.currentNavPathNodes_ is not None and self.currentNavPath_ is not None:
  139. self.begId_ = begID
  140. self.targetId_ = endID
  141. return True
  142. return False
  143. def PositionId(self):
  144. if self.timedRobotStatu_.timeout():
  145. return None
  146. x = self.timedRobotStatu_.statu.x
  147. y = self.timedRobotStatu_.statu.y
  148. djks_map = DijikstraMap()
  149. [label, pt] = djks_map.findNeastNode([x, y])
  150. return [label, pt]
  151. '''
  152. 阻塞直到导航完成
  153. '''
  154. def Navigatting(self, begId, targetId, autoDirect, wheelbase=0, task_type="None"):
  155. print("nav")
  156. self.GeneratePath(begId, targetId, task_type=task_type)
  157. # self.ExecPathNodes(autoDirect)
  158. self.ExecNavtask(autoDirect, wheelbase)
  159. if self.IsMainModeStatu():
  160. Count.TestCount().loadCountAdd()
  161. else:
  162. Count.TestCount().singleCountAdd()
  163. return True
  164. def AutoTestNavClamp(self, begId, targetId):
  165. beg = begId
  166. target = targetId
  167. while self.autoTest_:
  168. if self.TestNavClampOnce(beg, target) == False:
  169. print(" quit auto Test")
  170. break
  171. print("1111")
  172. posInfo = self.PositionId()
  173. if posInfo is not None:
  174. [label, pt] = posInfo
  175. beg = label
  176. print("beg", beg)
  177. node = DijikstraMap().GetVertex(beg)
  178. if node is not None:
  179. if isinstance(node, (SpaceNode)):
  180. target = "O"
  181. if isinstance(node, (StreetNode)):
  182. end_nodes = ["E", "B", "C", "D"]
  183. id = random.randint(0, 1000) % 4
  184. target = end_nodes[id]
  185. print("3333", target)
  186. print(" Next nav clamp cmd,%s to %s" % (beg, target))
  187. def ActionType(self):
  188. if self.timedNavStatu_.timeout() == False:
  189. runningStatu = self.timedNavStatu_.statu
  190. if runningStatu.statu == 0:
  191. return ActType.eReady
  192. if runningStatu.statu == 1:
  193. return ActType.eRotation
  194. if runningStatu.statu == 2:
  195. return ActType.eHorizon
  196. if runningStatu.statu == 3:
  197. return ActType.eVertical
  198. if runningStatu.statu == 4:
  199. return ActType.eMPC
  200. if runningStatu.statu == 5:
  201. return ActType.eClampClose
  202. if runningStatu.statu == 6:
  203. return ActType.eClampOpen
  204. if runningStatu.statu == 7:
  205. return ActType.eLifterRise
  206. if runningStatu.statu == 8:
  207. return ActType.eLifterDown
  208. else:
  209. return ActType.eReady
  210. @staticmethod
  211. def generateProtoNode(node):
  212. protoNode = message.PathNode()
  213. protoNode.x = node.x_
  214. protoNode.y = node.y_
  215. protoNode.id = node.id_
  216. return protoNode
  217. def ExecNavtask(self, autoDirect, wheelbase=0):
  218. cmd = message.NavCmd()
  219. key = str(uuid.uuid4())
  220. cmd.key = key
  221. cmd.action = 0
  222. actions = self.SplitPath(self.currentNavPathNodes_)
  223. for action in actions:
  224. [type, nodes] = action
  225. if type == "input":
  226. [street, space] = nodes
  227. protoSpace = self.generateProtoNode(space)
  228. protoStreet = self.generateProtoNode(street)
  229. act = message.NewAction()
  230. act.type = 1
  231. act.wheelbase = wheelbase
  232. act.spaceNode.CopyFrom(protoSpace)
  233. act.streetNode.CopyFrom(protoStreet)
  234. cmd.newActions.add().CopyFrom(act)
  235. if type == "output":
  236. [space, street] = nodes
  237. protoSpace = self.generateProtoNode(space)
  238. protoStreet = self.generateProtoNode(street)
  239. act = message.NewAction()
  240. act.type = 2
  241. act.wheelbase = wheelbase
  242. act.spaceNode.CopyFrom(protoSpace)
  243. act.streetNode.CopyFrom(protoStreet)
  244. cmd.newActions.add().CopyFrom(act)
  245. if type == "nav":
  246. action = self.CreateNavPathNodesAction(nodes, autoDirect)
  247. if action is None:
  248. print("Nav cmd is None")
  249. return False
  250. else:
  251. cmd.newActions.add().CopyFrom(action)
  252. print(cmd)
  253. if cmd is None:
  254. print("Nav cmd is None")
  255. return False
  256. channel = grpc.insecure_channel(self.rpc_ipport_)
  257. stub = msg_rpc.NavExcutorStub(channel)
  258. response = stub.Start(cmd)
  259. print("client received: ", response)
  260. if not response.ret == 0:
  261. print("nav failed :%s" % (response.info))
  262. else:
  263. print("nav completed !!!")
  264. return True
  265. def CreateNavPathNodesAction(self, path, autoDirect):
  266. # if path is not None and self.timedRobotStatu_.timeout() == False:
  267. if 1:
  268. for node in path:
  269. if isinstance(node, (SpaceNode)):
  270. print(" nodes must all street node")
  271. return None
  272. statu = self.timedRobotStatu_.statu
  273. # if statu is not None:
  274. if 1:
  275. newAction = message.NewAction()
  276. size = len(path)
  277. for i in range(size):
  278. node = path[i]
  279. pathNode = message.PathNode()
  280. pathNode.x = node.x_
  281. pathNode.y = node.y_
  282. newAction.type = 4
  283. if autoDirect:
  284. newAction.type = 3
  285. newAction.pathNodes.add().CopyFrom(pathNode)
  286. return newAction
  287. else:
  288. print("statu is None")
  289. else:
  290. print("current path is none")
  291. return None
  292. def ExecPathNodes(self, autoDirect):
  293. if self.navCmdTopic_ == None:
  294. print("Robot has not set nav cmd topic")
  295. return False
  296. if not self.ActionType() == ActType.eReady:
  297. print(" robot is navigating ,key:%s" % (self.timedNavStatu_.statu.key))
  298. return False
  299. cmd = message.NavCmd()
  300. key = str(uuid.uuid4())
  301. cmd.key = key
  302. cmd.action = 0
  303. action = self.CreateNavPathNodesAction(self.currentNavPathNodes_, autoDirect)
  304. if action is None:
  305. print("Nav cmd is None")
  306. return False
  307. cmd.newActions.add().CopyFrom(action)
  308. print(cmd)
  309. channel = grpc.insecure_channel(self.rpc_ipport_)
  310. stub = msg_rpc.NavExcutorStub(channel)
  311. response = stub.Start(cmd)
  312. return True
  313. '''
  314. 根据AGV单车的起始位置调整路径
  315. '''
  316. def AdjustPath(self, path):
  317. adjusted_path = copy.deepcopy(path)
  318. y_offset = 1.2
  319. dy = 0
  320. # if self.IsMainAgv() or self.name_ == "AgvMain":
  321. if self.name_ == RobotName.strAGVMain:
  322. dy = 1
  323. # if not self.IsMainAgv() or self == "AGV2":
  324. if self.name_ == RobotName.strAGV2:
  325. dy = -1
  326. print("dy:", dy)
  327. for node in adjusted_path:
  328. if isinstance(node, StreetNode):
  329. if dy > 0:
  330. node.y_ += y_offset
  331. elif dy < 0:
  332. node.y_ -= y_offset
  333. else:
  334. pass
  335. return adjusted_path
  336. '''
  337. 拆分路径,生成动作集,input:入库动作,nav:导航动作,output:出库动作
  338. '''
  339. def SplitPath(self, path):
  340. space_count = 0
  341. for i in range(len(path)):
  342. if isinstance(path[i], (SpaceNode)):
  343. space_count += 1
  344. if i == 0 or i == len(path) - 1:
  345. pass
  346. else:
  347. print("Error: space node is not first/last of path")
  348. return None
  349. actions = []
  350. lastNode = None
  351. navIndex = -1
  352. for node in path:
  353. if lastNode == None:
  354. lastNode = node
  355. continue
  356. if isinstance(node, (SpaceNode)): # 当前点是车位点
  357. if isinstance(lastNode, (SpaceNode)): # 上一点为车位点
  358. print("Error: last node and current node is space node ")
  359. lastNode = None
  360. continue
  361. elif isinstance(lastNode, (StreetNode)): # 上一点为马路点,则动作为入库
  362. actions.append(["input", [lastNode, node]])
  363. lastNode = None
  364. continue
  365. if isinstance(node, (StreetNode)):
  366. if isinstance(lastNode, (SpaceNode)): # 上一点为车位点, 出库
  367. actions.append(["output", [lastNode, node]])
  368. elif isinstance(lastNode, (StreetNode)):
  369. if navIndex < 0:
  370. actions.append(["nav", [lastNode]])
  371. navIndex = len(actions) - 1
  372. actions[navIndex][1].append(node)
  373. lastNode = node
  374. return actions
  375. def SwitchMoveMode(self, mode, wheelbase):
  376. if self.IsMainAgv() == False:
  377. print(" this agv is single can not switch mode")
  378. return False
  379. cmd = message.NavCmd()
  380. key = str(uuid.uuid4())
  381. cmd.key = key
  382. cmd.action = 0
  383. action = message.NewAction()
  384. action.type = 8
  385. action.changedMode = mode
  386. if mode == 1:
  387. action.wheelbase = wheelbase
  388. cmd.newActions.add().CopyFrom(action)
  389. print(cmd)
  390. channel = grpc.insecure_channel(self.rpc_ipport_)
  391. stub = msg_rpc.NavExcutorStub(channel)
  392. response = stub.Start(cmd)
  393. return False
  394. def IsClampClosed(self):
  395. if self.timedRobotStatu_.timeout() == False:
  396. if self.IsMainModeStatu():
  397. clamp = self.timedRobotStatu_.statu.agvStatu.clamp
  398. clamp_other = self.timedRobotStatu_.statu.agvStatu.clamp_other
  399. return (clamp == 1 and clamp_other == 1)
  400. else:
  401. return self.timedRobotStatu_.statu.agvStatu.clamp == 1
  402. return False
  403. def IsClampRunning(self):
  404. if self.timedRobotStatu_.timeout() == False:
  405. if self.IsMainModeStatu():
  406. clamp = self.timedRobotStatu_.statu.agvStatu.clamp
  407. clamp_other = self.timedRobotStatu_.statu.agvStatu.clamp_other
  408. return (clamp == 0 or clamp_other == 0)
  409. else:
  410. return self.timedRobotStatu_.statu.agvStatu.clamp == 0
  411. return False
  412. def IsClampOpened(self):
  413. if self.timedRobotStatu_.timeout() == False:
  414. if self.IsMainModeStatu():
  415. clamp = self.timedRobotStatu_.statu.agvStatu.clamp
  416. clamp_other = self.timedRobotStatu_.statu.agvStatu.clamp_other
  417. return (clamp == 2 and clamp_other == 2)
  418. else:
  419. return self.timedRobotStatu_.statu.agvStatu.clamp == 2
  420. return False
  421. def ClampClose(self):
  422. if self.IsClampClosed() == True:
  423. print("clamp closed")
  424. return True
  425. cmd = message.NavCmd()
  426. cmd.action = 0
  427. key = str(uuid.uuid4())
  428. cmd.key = (key)
  429. act = message.NewAction()
  430. act.type = 6
  431. cmd.newActions.add().CopyFrom(act)
  432. channel = grpc.insecure_channel(self.rpc_ipport_)
  433. stub = msg_rpc.NavExcutorStub(channel)
  434. response = stub.Start(cmd)
  435. return True
  436. def ClampOpen(self):
  437. if self.IsClampOpened() == True:
  438. print("clamp opended")
  439. return True
  440. cmd = message.NavCmd()
  441. cmd.action = 0
  442. key = str(uuid.uuid4())
  443. cmd.key = (key)
  444. act = message.NewAction()
  445. act.type = 7
  446. cmd.newActions.add().CopyFrom(act)
  447. channel = grpc.insecure_channel(self.rpc_ipport_)
  448. stub = msg_rpc.NavExcutorStub(channel)
  449. response = stub.Start(cmd)
  450. return True
  451. def IsLiferDowned(self):
  452. if self.timedRobotStatu_.timeout() == False:
  453. if self.IsMainModeStatu():
  454. lifter = self.timedRobotStatu_.statu.agvStatu.lifter
  455. lifter_other = self.timedRobotStatu_.statu.agvStatu.lifter_other
  456. return (lifter == 2 and lifter_other == 2)
  457. else:
  458. return self.timedRobotStatu_.statu.agvStatu.lifter == 2
  459. return False
  460. def IsLiferRunning(self):
  461. if self.timedRobotStatu_.timeout() == False:
  462. if self.IsMainModeStatu():
  463. lifter = self.timedRobotStatu_.statu.agvStatu.lifter
  464. lifter_other = self.timedRobotStatu_.statu.agvStatu.lifter_other
  465. return (lifter == 0 and lifter_other == 0)
  466. else:
  467. return self.timedRobotStatu_.statu.agvStatu.lifter == 0
  468. return False
  469. def IsLiferRose(self):
  470. if self.timedRobotStatu_.timeout() == False:
  471. if self.IsMainModeStatu():
  472. lifter = self.timedRobotStatu_.statu.agvStatu.lifter
  473. lifter_other = self.timedRobotStatu_.statu.agvStatu.lifter_other
  474. return (lifter == 1 and lifter_other == 1)
  475. else:
  476. return self.timedRobotStatu_.statu.agvStatu.lifter == 1
  477. return False
  478. def LiferRise(self):
  479. if self.IsLiferRose() == True:
  480. print("lifter has rose")
  481. return True
  482. cmd = message.NavCmd()
  483. cmd.action = 0
  484. key = str(uuid.uuid4())
  485. cmd.key = (key)
  486. act = message.NewAction()
  487. act.type = 9
  488. cmd.newActions.add().CopyFrom(act)
  489. channel = grpc.insecure_channel(self.rpc_ipport_)
  490. stub = msg_rpc.NavExcutorStub(channel)
  491. response = stub.Start(cmd)
  492. return True
  493. def LiferDown(self):
  494. if self.IsLiferDowned() == True:
  495. print("lifter has downed")
  496. return True
  497. cmd = message.NavCmd()
  498. cmd.action = 0
  499. key = str(uuid.uuid4())
  500. cmd.key = (key)
  501. act = message.NewAction()
  502. act.type = 10
  503. cmd.newActions.add().CopyFrom(act)
  504. channel = grpc.insecure_channel(self.rpc_ipport_)
  505. stub = msg_rpc.NavExcutorStub(channel)
  506. response = stub.Start(cmd)
  507. return True
  508. def CancelNavTask(self):
  509. cmd = message.NavCmd()
  510. cmd.action = 3
  511. channel = grpc.insecure_channel(self.rpc_ipport_)
  512. stub = msg_rpc.NavExcutorStub(channel)
  513. response = stub.Cancel(cmd)
  514. print("client received: ", response)
  515. print(" Cancel task completed!!!")
  516. return True
  517. def IsManualTaskRunning(self):
  518. return self.manual_status != ManualOperationType.eReady
  519. def ManualTask(self, current_operation, step_acc=0, speed=0):
  520. print("speed: ", speed)
  521. if self.IsNavigating():
  522. print("AGV is navigating!!!")
  523. return
  524. if self.IsManualTaskRunning():
  525. return print("ManualTask | ManualTask is running...")
  526. self.manual_status = current_operation
  527. if ManualOperationType.eReady < current_operation <= ManualOperationType.eRightMove:
  528. cmd = message.ToAgvCmd()
  529. self.heat_ += 1
  530. self.heat_ %= 255
  531. cmd.H = self.heat_
  532. if self.IsMainModeStatu():
  533. cmd.M = 1
  534. else:
  535. cmd.M = 0
  536. if current_operation == ManualOperationType.eCounterclockwiseRotate:
  537. cmd.T = 1
  538. cmd.V = 0
  539. cmd.W = speed / 180 * math.pi * sigmoid(step_acc, 0, 1)
  540. if current_operation == ManualOperationType.eClockwiseRotate:
  541. cmd.T = 1
  542. cmd.V = 0
  543. cmd.W = -speed / 180 * math.pi * sigmoid(step_acc, 0, 1)
  544. if current_operation == ManualOperationType.eForwardMove:
  545. cmd.T = 3
  546. cmd.V = speed * sigmoid(step_acc, 0, 1)
  547. cmd.W = 0
  548. if current_operation == ManualOperationType.eBackwardMove:
  549. cmd.T = 3
  550. cmd.V = -speed * sigmoid(step_acc, 0, 1)
  551. cmd.W = 0
  552. if current_operation == ManualOperationType.eLeftMove:
  553. cmd.T = 2
  554. cmd.V = speed * sigmoid(step_acc, 0, 1)
  555. cmd.W = 0
  556. if current_operation == ManualOperationType.eRightMove:
  557. cmd.T = 2
  558. cmd.V = -speed * sigmoid(step_acc, 0, 1)
  559. cmd.W = 0
  560. cmd.L = self.L_
  561. cmd.end = 1
  562. # 处理为json格式
  563. cmd_dict = {}
  564. for field in cmd.DESCRIPTOR.fields:
  565. cmd_dict[field.name] = field.default_value
  566. for field, value in cmd.ListFields():
  567. cmd_dict[field.name] = value
  568. cmd_json = json.dumps(cmd_dict, indent=None, separators=(',', ':'))
  569. self.publish(self.speedCmdTopic, cmd_json)
  570. print(cmd_json)
  571. # channel = grpc.insecure_channel(self.rpc_ipport_)
  572. # stub = msg_rpc.NavExcutorStub(channel)
  573. # response = stub.ManualOperation(cmd_0)
  574. # print("client received: ", response)
  575. self.manual_status = ManualOperationType.eReady
  576. # print("ManualOperation {op_type} task completed!!!".format(op_type=hex(current_operation)))
  577. return True
  578. elif ManualOperationType.eClampClose <= current_operation <= ManualOperationType.eClampOpen:
  579. self.Manual_Unit_Clamp(current_operation)
  580. return True
  581. elif ManualOperationType.eLifterRise <= current_operation <= ManualOperationType.eLifterDown:
  582. self.Manual_Unit_Lifter(current_operation)
  583. return True
  584. return False
  585. def Manual_Unit_Lifter(self, current_operation: ManualOperationType):
  586. print(current_operation)
  587. cmd = message.ToAgvCmd()
  588. self.heat_ += 1
  589. self.heat_ %= 255
  590. cmd.H = self.heat_
  591. if self.IsMainModeStatu():
  592. cmd.M = 1
  593. else:
  594. cmd.M = 0
  595. cmd.T = self.ManualOperationType2AgvCmdType(current_operation)
  596. # 处理为json格式 # jtf.MessageToJson(cmd)
  597. cmd_dict = {}
  598. for field in cmd.DESCRIPTOR.fields:
  599. cmd_dict[field.name] = field.default_value
  600. for field, value in cmd.ListFields():
  601. cmd_dict[field.name] = value
  602. cmd_json = json.dumps(cmd_dict, indent=None, separators=(',', ':'))
  603. self.publish(self.speedCmdTopic, cmd_json)
  604. print(cmd_json)
  605. self.manual_status = ManualOperationType.eReady
  606. return True
  607. def Manual_Unit_Clamp(self, current_operation: ManualOperationType):
  608. print(current_operation)
  609. cmd = message.ToAgvCmd()
  610. self.heat_ += 1
  611. self.heat_ %= 255
  612. cmd.H = self.heat_
  613. if self.IsMainModeStatu():
  614. cmd.M = 1
  615. else:
  616. cmd.M = 0
  617. cmd.T = self.ManualOperationType2AgvCmdType(current_operation)
  618. # 处理为json格式 # jtf.MessageToJson(cmd)
  619. cmd_dict = {}
  620. for field in cmd.DESCRIPTOR.fields:
  621. cmd_dict[field.name] = field.default_value
  622. for field, value in cmd.ListFields():
  623. cmd_dict[field.name] = value
  624. cmd_json = json.dumps(cmd_dict, indent=None, separators=(',', ':'))
  625. self.publish(self.speedCmdTopic, cmd_json)
  626. print(cmd_json)
  627. self.manual_status = ManualOperationType.eReady
  628. return True
  629. def ManualOperationType2AgvCmdType(self, mo_type: ManualOperationType):
  630. if mo_type == ManualOperationType.eLifterRise:
  631. return ActType.eLifterRise
  632. if mo_type == ManualOperationType.eLifterDown:
  633. return ActType.eLifterDown
  634. if mo_type == ManualOperationType.eClampClose:
  635. return ActType.eClampClose
  636. if mo_type == ManualOperationType.eClampOpen:
  637. return ActType.eClampOpen