MapGLWidget.py 8.5 KB

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