MapGLWidget.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import math
  2. import random
  3. import time
  4. from PyGLWidget import PyGLWidget
  5. from OpenGL.GL import *
  6. import RobotData as RD
  7. import message_pb2 as message
  8. from OpenGL.GLUT import glutStrokeCharacter
  9. from OpenGL import GLUT
  10. class MapGLWidget(PyGLWidget):
  11. nodes = {}
  12. roads = {}
  13. robot1_ = None
  14. robot2_ = None
  15. robotMain_ = None
  16. def __init__(self):
  17. PyGLWidget.__init__(self)
  18. GLUT.glutInit()
  19. def SetRobot1Instance(self, robot):
  20. self.robot1_ = robot
  21. def SetRobot2Instance(self, robot):
  22. self.robot2_ = robot
  23. def SetRobotMainInstance(self, robot):
  24. self.robotMain_ = robot
  25. def AddNode(self, node):
  26. [id, type, point] = node
  27. self.nodes[id] = [type, point]
  28. def AddRoad(self, road):
  29. [key, value] = road
  30. self.roads[key] = value
  31. def drawTraj(self, poses, width, color):
  32. glColor3f(color[0], color[1], color[2])
  33. glLineWidth(2)
  34. glBegin(GL_LINES)
  35. for pose in poses:
  36. [x, y, yaw] = pose
  37. point1 = [x, y]
  38. [dx, dy] = self.RotatePoint([-width / 2, -width / 2], yaw)
  39. point2 = [x + dx, y + dy]
  40. [dx, dy] = self.RotatePoint([-width / 2, width / 2], yaw)
  41. point3 = [x + dx, y + dy]
  42. glVertex2d(point1[0], point1[1])
  43. glVertex2d(point3[0], point3[1])
  44. glVertex2d(point1[0], point1[1])
  45. glVertex2d(point2[0], point2[1])
  46. glEnd()
  47. def drawTrajWithPoint(self, poses, ptsize, color):
  48. glPointSize(ptsize)
  49. glDepthMask(GL_FALSE)
  50. glBegin(GL_POINTS)
  51. glColor3f(color[0], color[1], color[2])
  52. for pt in poses:
  53. [x, y, _] = pt
  54. glVertex2d(x, y)
  55. glEnd()
  56. def drawEdge(self, pt1, pt2, width, color):
  57. glColor4f(color[0], color[1], color[2], 0)
  58. glLineWidth(width)
  59. glBegin(GL_LINES)
  60. glVertex2d(pt1[0], pt1[1])
  61. glVertex2d(pt2[0], pt2[1])
  62. glEnd()
  63. @staticmethod
  64. def RotatePoint(point, yaw):
  65. [x, y] = point
  66. nx = math.cos(yaw) * x - y * math.sin(yaw)
  67. ny = x * math.sin(yaw) + y * math.cos(yaw)
  68. return [nx, ny]
  69. @staticmethod
  70. def Transform(point,yaw,dt):
  71. [x,y]=point
  72. [dx,dy]=dt
  73. nx = math.cos(yaw) * x - y * math.sin(yaw)+dx
  74. ny = x * math.sin(yaw) + y * math.cos(yaw)+dy
  75. return [nx, ny]
  76. def DrawText(self,pt,text,width,size,rgb):
  77. glDisable(GL_TEXTURE_2D)
  78. glLineWidth(width)
  79. glPointSize(width)
  80. r, g, b = rgb
  81. glPushMatrix()
  82. glColor3f(r, g, b)
  83. glTranslatef(pt[0],pt[1],0)
  84. s = size*0.005
  85. glScalef(s, s, s)
  86. for char in text:
  87. GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(char))
  88. glPopMatrix()
  89. glEnable(GL_TEXTURE_2D)
  90. def drawAxis(self, pose, len, width):
  91. [x, y, yaw] = pose
  92. x_source = [x + len * math.cos(yaw), y + math.sin(yaw) * len]
  93. y_source = [x - math.sin(yaw) * len, y + len * math.cos(yaw)]
  94. glDepthMask(GL_FALSE)
  95. glColor3f(1, 0, 0)
  96. glLineWidth(width)
  97. glBegin(GL_LINES)
  98. glVertex2d(x, y)
  99. glVertex2d(x_source[0], x_source[1])
  100. glEnd()
  101. glColor3f(0, 1, 0)
  102. glBegin(GL_LINES)
  103. glVertex2d(x, y)
  104. glVertex2d(y_source[0], y_source[1])
  105. glEnd()
  106. def DrawAGV(self,pose,rgb,label=""):
  107. [x,y,yaw]=pose
  108. l=0.8 #轮长
  109. L=1.3 #轴距
  110. W=2.5 #宽
  111. pt1=self.Transform([-(L+l)/2,W/2],yaw,[x,y])
  112. pt2=self.Transform([-(L-l)/2,W/2],yaw,[x,y])
  113. pt3=self.Transform([(L-l)/2,W/2],yaw,[x,y])
  114. pt4=self.Transform([(L+l)/2,W/2],yaw,[x,y])
  115. pt5=self.Transform([-(L+l)/2,-W/2],yaw,[x,y])
  116. pt6=self.Transform([-(L-l)/2,-W/2],yaw,[x,y])
  117. pt7=self.Transform([(L-l)/2,-W/2],yaw,[x,y])
  118. pt8=self.Transform([(L+l)/2,-W/2],yaw,[x,y])
  119. glDepthMask(GL_FALSE)
  120. glColor3f(rgb[0],rgb[1],rgb[2])
  121. glLineWidth(20)
  122. glBegin(GL_LINES)
  123. glVertex2d(pt1[0],pt1[1])
  124. glVertex2d(pt2[0],pt2[1])
  125. glVertex2d(pt3[0],pt3[1])
  126. glVertex2d(pt4[0],pt4[1])
  127. glVertex2d(pt5[0],pt5[1])
  128. glVertex2d(pt6[0],pt6[1])
  129. glVertex2d(pt7[0],pt7[1])
  130. glVertex2d(pt8[0],pt8[1])
  131. glEnd()
  132. glLineWidth(5)
  133. glBegin(GL_LINES)
  134. glVertex2d(pt2[0],pt2[1])
  135. glVertex2d(pt6[0],pt6[1])
  136. glVertex2d(pt3[0],pt3[1])
  137. glVertex2d(pt7[0],pt7[1])
  138. glVertex2d(pt2[0],pt2[1])
  139. glVertex2d(pt3[0],pt3[1])
  140. glVertex2d(pt6[0],pt6[1])
  141. glVertex2d(pt7[0],pt7[1])
  142. glEnd()
  143. #绘制方向
  144. self.drawAxis(pose,1.2,5)
  145. color=[1,1,1]
  146. self.DrawText([x-W/2,y-l/2],label,3,1.5,color)
  147. def drawNode(self, pt, size, color,label=""):
  148. [x, y] = pt
  149. glPointSize(size)
  150. glDepthMask(GL_FALSE)
  151. glBegin(GL_POINTS)
  152. glColor4f(color[0], color[1], color[2], 0.5)
  153. glVertex2d(x, y)
  154. glEnd()
  155. l=len(label)
  156. self.DrawText([x-l/8,y],label,size/10,1,[1,1,1])
  157. '''glColor3f(1,1,1);
  158. #glBegin(GL_TEXTURE)
  159. glText'''
  160. def DrawRobotData(self,robot):
  161. if robot is not None:
  162. if robot.currentNavPath_ is not None:
  163. for traj in robot.currentNavPath_:
  164. self.drawTraj(traj, 0.5, robot.pathColor_)
  165. # 绘制agv
  166. if robot is not None:
  167. if robot.timedPose_.timeout() == False:
  168. agv_statu = robot.timedPose_.statu
  169. x = agv_statu.x
  170. y = agv_statu.y
  171. theta = agv_statu.theta
  172. self.DrawAGV([x, y, theta], robot.robotColor_,robot.name_)
  173. if robot.timedNavStatu_.timeout() == False:
  174. select_traj = robot.MpcSelectTraj()
  175. predict_traj = robot.MpcPredictTraj()
  176. self.drawTrajWithPoint(select_traj, 8, [1, 0, 1])
  177. self.drawTrajWithPoint(predict_traj, 10, [0, 1, 1])
  178. def paintGL(self):
  179. PyGLWidget.paintGL(self)
  180. self.drawAxis([0, 0, 0], 3, 2)
  181. #绘制地图
  182. for road in self.roads.items():
  183. [_, value] = road
  184. for nodeId in value:
  185. for nextId in value:
  186. if not nodeId == nextId:
  187. [_, point1] = self.nodes[nodeId]
  188. [_, point2] = self.nodes[nextId]
  189. self.drawEdge(point1, point2, 7, [1,1,0])
  190. for node in self.nodes.items():
  191. [name, [type, point]] = node
  192. if type == "street_node":
  193. self.drawNode(point, 30, [0, 0.5, 0.5],name)
  194. if type == "space_node":
  195. self.drawNode(point, 20, [0.3, 0.8, 0.7],name)
  196. #绘制agv相关数据,路径、轨迹、位姿
  197. self.DrawRobotData(self.robot1_)
  198. self.DrawRobotData(self.robot2_)
  199. self.DrawRobotData(self.robotMain_)