Map.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import math
  2. from dijkstra.dijkstra_algorithm import Graph
  3. import message_pb2 as message
  4. class Node(object):
  5. def __init__(self, id, x, y):
  6. self.id_ = id
  7. self.x_ = x
  8. self.y_ = y
  9. def distance(self, other):
  10. if not isinstance(other, (Node, StreetNode, SpaceNode)):
  11. print(" node must be Node,street_node,space_node")
  12. return -1
  13. return math.sqrt(math.pow(other.x_ - self.x_, 2) + math.pow(other.y_ - self.y_, 2))
  14. class StreetNode(Node):
  15. def __init__(self, id, x, y):
  16. Node.__init__(self, id, x, y)
  17. class SpaceNode(Node):
  18. def __init__(self, id, x, y, connect_nodeId):
  19. Node.__init__(self, id, x, y)
  20. self.connect_nodeId_ = connect_nodeId
  21. def singleton(cls):
  22. _instance = {}
  23. def inner():
  24. if cls not in _instance:
  25. _instance[cls] = cls()
  26. return _instance[cls]
  27. return inner
  28. '''
  29. map 采用单例
  30. '''
  31. @singleton
  32. class Map(object):
  33. def __init__(self):
  34. self.nodes_ = {}
  35. self.graph_ = Graph()
  36. def AddVertex(self, node):
  37. if isinstance(node, (StreetNode)):
  38. print("add street node :%s " % (node.id_))
  39. self.nodes_[node.id_] = node
  40. if isinstance(node, (SpaceNode)):
  41. if self.nodes_.get(node.connect_nodeId_) == None:
  42. print("add space node failed ,connect street node not exist")
  43. return False
  44. print("add space node :%s connect_id:%s" % (node.id_, node.connect_nodeId_))
  45. self.nodes_[node.id_] = node
  46. self.graph_.AddVertex(node.id_, [node.x_, node.y_])
  47. return True
  48. def AddEdge(self, id1, id2, direct=False):
  49. if self.nodes_.get(id1) == None or self.nodes_.get(id2) == None:
  50. raise ("Add edge failed %s or %s node is not exist" % (id1, id2))
  51. print("Add Edge :%s-%s" % (id1, id2))
  52. self.graph_.AddEdge(id1, id2)
  53. if direct == False:
  54. self.graph_.AddEdge(id2, id1)
  55. def GetShortestPath(self, beg, end):
  56. [pathId, distance] = self.graph_.shortest_path(beg, end)
  57. print("distance:", distance)
  58. print("path:", pathId)
  59. path = []
  60. for nodeId in pathId:
  61. node = self.nodes_[nodeId]
  62. path.append(node)
  63. return path
  64. def CreateNavCmd(self, pose, path):
  65. if len(path) <= 1:
  66. return None
  67. cmd = message.NavCmd()
  68. cmd.action=0 # 新导航
  69. key = "12354-ahsbd-kkkk"
  70. cmd.key=(key)
  71. adjustdiff = message.Pose2d()
  72. node_mpcdiff = message.Pose2d()
  73. enddiff = message.Pose2d()
  74. # 目标点精度设置
  75. adjustdiff.x=(0.1)
  76. adjustdiff.y=(0.05)
  77. adjustdiff.theta=(0.5 * math.pi / 180.0)
  78. node_mpcdiff.x=(0.1)
  79. node_mpcdiff.y=(0.1)
  80. node_mpcdiff.theta=(2 * math.pi / 180.0)
  81. enddiff.x=(0.1)
  82. enddiff.y=(0.1)
  83. enddiff.theta=(5 * math.pi / 180.0)
  84. # 速度限制
  85. v_limit = message.SpeedLimit()
  86. angular_limit = message.SpeedLimit()
  87. horize_limit = message.SpeedLimit()
  88. v_limit.min=(0.1)
  89. v_limit.max=(0.2)
  90. horize_limit.min=(0.05)
  91. horize_limit.max=(0.2)
  92. angular_limit.min=(1)
  93. angular_limit.max=(25.0)
  94. # mpc速度限制
  95. mpc_x_limit = message.SpeedLimit()
  96. mpc_angular_limit = message.SpeedLimit()
  97. mpc_x_limit.min=(0.05)
  98. mpc_x_limit.max=(1.5)
  99. mpc_angular_limit.min=(0 * math.pi / 180.0)
  100. mpc_angular_limit.max=(3 * math.pi / 180.0)
  101. # 创建动作集----------------------
  102. last_node = None
  103. count = 0
  104. for node in path:
  105. if last_node == None:
  106. last_node = node
  107. count+=1
  108. continue
  109. # 运动到上一点
  110. vector = [node.x_ - last_node.x_, node.y_ - last_node.y_]
  111. dx = vector[0]
  112. dy = vector[1]
  113. yaw = math.asin(dy / math.sqrt(dx * dx + dy * dy))
  114. if yaw >= 0:
  115. if dx < 0:
  116. yaw = math.pi - yaw
  117. if yaw < 0:
  118. if dx < 0:
  119. yaw = -math.pi - yaw
  120. if isinstance(last_node, (SpaceNode)):
  121. yaw = yaw + math.pi
  122. # 添加调整动作
  123. act_adjust = message.Action()
  124. act_adjust.type=(1)
  125. act_adjust.target.x=(last_node.x_)
  126. act_adjust.target.y=(last_node.y_)
  127. act_adjust.target.theta=(yaw)
  128. act_adjust.target_diff.CopyFrom(adjustdiff)
  129. act_adjust.velocity_limit.CopyFrom(v_limit)
  130. act_adjust.horize_limit.CopyFrom(horize_limit)
  131. act_adjust.angular_limit.CopyFrom(angular_limit)
  132. cmd.actions.add().CopyFrom(act_adjust)
  133. # 添加mpc动作
  134. act_along = message.Action()
  135. act_along.type=(2)
  136. act_along.begin.x=(last_node.x_)
  137. act_along.begin.y=(last_node.y_)
  138. act_along.begin.theta=(yaw)
  139. act_along.target.x=(node.x_)
  140. act_along.target.y=(node.y_)
  141. act_along.target.theta=(yaw)
  142. if count==len(path)-1:
  143. act_along.target_diff.CopyFrom(enddiff)
  144. else:
  145. act_along.target_diff.CopyFrom(node_mpcdiff)
  146. act_along.velocity_limit.CopyFrom(mpc_x_limit)
  147. act_along.angular_limit.CopyFrom(mpc_angular_limit)
  148. cmd.actions.add().CopyFrom(act_along)
  149. last_node = node
  150. count+=1
  151. return cmd