MapGLWidget.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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, pose] = node
  24. self.nodes[id] = [type, pose]
  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=None):
  68. [x,y]=point
  69. nx = math.cos(yaw) * x - y * math.sin(yaw)
  70. ny = x * math.sin(yaw) + y * math.cos(yaw)
  71. if dt is not None:
  72. [dx,dy]=dt
  73. nx+=dx
  74. ny+=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 DrawMainAGV(self,pose,rgb,label,running=False):
  107. l=0.8 #轮长
  108. L=1.3 #轴距
  109. W=2.5
  110. if running:
  111. l=1.6 #轮长
  112. L=3 #轴距
  113. W=2.5
  114. [x,y,yaw]=pose
  115. #宽
  116. pt1=self.Transform([-(L+l)/2,W/2],yaw,[x,y])
  117. pt2=self.Transform([-(L-l)/2,W/2],yaw,[x,y])
  118. pt3=self.Transform([(L-l)/2,W/2],yaw,[x,y])
  119. pt4=self.Transform([(L+l)/2,W/2],yaw,[x,y])
  120. pt5=self.Transform([-(L+l)/2,-W/2],yaw,[x,y])
  121. pt6=self.Transform([-(L-l)/2,-W/2],yaw,[x,y])
  122. pt7=self.Transform([(L-l)/2,-W/2],yaw,[x,y])
  123. pt8=self.Transform([(L+l)/2,-W/2],yaw,[x,y])
  124. glDepthMask(GL_FALSE)
  125. glColor3f(rgb[0],rgb[1],rgb[2])
  126. glLineWidth(20)
  127. glBegin(GL_LINES)
  128. glVertex2d(pt1[0],pt1[1])
  129. glVertex2d(pt2[0],pt2[1])
  130. glVertex2d(pt3[0],pt3[1])
  131. glVertex2d(pt4[0],pt4[1])
  132. glVertex2d(pt5[0],pt5[1])
  133. glVertex2d(pt6[0],pt6[1])
  134. glVertex2d(pt7[0],pt7[1])
  135. glVertex2d(pt8[0],pt8[1])
  136. glEnd()
  137. glLineWidth(2)
  138. glBegin(GL_LINES)
  139. glVertex2d(pt2[0],pt2[1])
  140. glVertex2d(pt6[0],pt6[1])
  141. glVertex2d(pt3[0],pt3[1])
  142. glVertex2d(pt7[0],pt7[1])
  143. glVertex2d(pt2[0],pt2[1])
  144. glVertex2d(pt3[0],pt3[1])
  145. glVertex2d(pt6[0],pt6[1])
  146. glVertex2d(pt7[0],pt7[1])
  147. glEnd()
  148. #绘制方向
  149. self.drawAxis(pose,L,5)
  150. self.DrawText([x-W/2,y-l/2],label,5,1.5,rgb)
  151. def DrawAGV(self,pose,rgb,label=""):
  152. [x,y,yaw]=pose
  153. l=0.8 #轮长
  154. L=1.3 #轴距
  155. W=2.5 #宽
  156. pt1=self.Transform([-(L+l)/2,W/2],yaw,[x,y])
  157. pt2=self.Transform([-(L-l)/2,W/2],yaw,[x,y])
  158. pt3=self.Transform([(L-l)/2,W/2],yaw,[x,y])
  159. pt4=self.Transform([(L+l)/2,W/2],yaw,[x,y])
  160. pt5=self.Transform([-(L+l)/2,-W/2],yaw,[x,y])
  161. pt6=self.Transform([-(L-l)/2,-W/2],yaw,[x,y])
  162. pt7=self.Transform([(L-l)/2,-W/2],yaw,[x,y])
  163. pt8=self.Transform([(L+l)/2,-W/2],yaw,[x,y])
  164. glDepthMask(GL_FALSE)
  165. glColor3f(rgb[0],rgb[1],rgb[2])
  166. glLineWidth(3)
  167. glBegin(GL_LINES)
  168. glVertex2d(pt1[0],pt1[1])
  169. glVertex2d(pt2[0],pt2[1])
  170. glVertex2d(pt3[0],pt3[1])
  171. glVertex2d(pt4[0],pt4[1])
  172. glVertex2d(pt5[0],pt5[1])
  173. glVertex2d(pt6[0],pt6[1])
  174. glVertex2d(pt7[0],pt7[1])
  175. glVertex2d(pt8[0],pt8[1])
  176. glEnd()
  177. glLineWidth(1)
  178. glBegin(GL_LINES)
  179. glVertex2d(pt2[0],pt2[1])
  180. glVertex2d(pt6[0],pt6[1])
  181. glVertex2d(pt3[0],pt3[1])
  182. glVertex2d(pt7[0],pt7[1])
  183. glVertex2d(pt2[0],pt2[1])
  184. glVertex2d(pt3[0],pt3[1])
  185. glVertex2d(pt6[0],pt6[1])
  186. glVertex2d(pt7[0],pt7[1])
  187. glEnd()
  188. #绘制方向
  189. self.drawAxis(pose,1.2,5)
  190. color=[1,1,1]
  191. self.DrawText([x-W/2,y-l/2],label,3,1.5,color)
  192. def drawNode(self, pt, size, color,label=""):
  193. [x, y] = pt
  194. glPointSize(size)
  195. glDepthMask(GL_FALSE)
  196. glBegin(GL_POINTS)
  197. glColor4f(color[0], color[1], color[2], 0.5)
  198. glVertex2d(x, y)
  199. glEnd()
  200. l=len(label)
  201. self.DrawText([x-l/8,y],label,size/10,1,[1,1,1])
  202. def drawSpaceNode(self,pose,size,color,label=''):
  203. [x, y,yaw] = pose
  204. self.drawNode([x,y],size,color,label)
  205. width=2.4
  206. length=6
  207. pt1=self.Transform([x-length/2,y+width/2],yaw)
  208. pt2=self.Transform([x+length/2,y+width/2],yaw)
  209. pt3=self.Transform([x-length/2,y-width/2],yaw)
  210. pt4=self.Transform([x+length/2,y-width/2],yaw)
  211. glDepthMask(GL_FALSE)
  212. glColor3f(1,1,1)
  213. glLineWidth(10)
  214. glBegin(GL_LINES)
  215. glVertex2d(pt1[0],pt1[1])
  216. glVertex2d(pt2[0],pt2[1])
  217. glVertex2d(pt2[0],pt2[1])
  218. glVertex2d(pt3[0],pt3[1])
  219. glVertex2d(pt3[0],pt3[1])
  220. glVertex2d(pt4[0],pt4[1])
  221. glVertex2d(pt4[0],pt4[1])
  222. glVertex2d(pt1[0],pt1[1])
  223. glEnd()
  224. def DrawRobotData(self,robot):
  225. if robot is not None:
  226. if robot.currentNavPath_ is not None:
  227. for traj in robot.currentNavPath_:
  228. self.drawTraj(traj, 0.5, robot.pathColor_)
  229. # 绘制agv
  230. if robot is not None:
  231. if robot.timedRobotStatu_.timeout() == False:
  232. agv_statu = robot.timedRobotStatu_.statu
  233. x = agv_statu.x
  234. y = agv_statu.y
  235. theta = agv_statu.theta
  236. if robot.IsMainAgv():
  237. if robot.IsMainModeStatu():
  238. self.DrawMainAGV([x, y, theta], robot.Color(),robot.name_,True)
  239. else:
  240. self.DrawMainAGV([x, y, theta], robot.Color(),robot.name_)
  241. else:
  242. self.DrawAGV([x, y, theta], robot.Color(),robot.name_)
  243. if robot.timedNavStatu_.timeout() == False:
  244. select_traj = robot.MpcSelectTraj()
  245. predict_traj = robot.MpcPredictTraj()
  246. self.drawTrajWithPoint(select_traj, 8, [1, 0, 1])
  247. self.drawTrajWithPoint(predict_traj, 10, [0, 1, 1])
  248. def paintGL(self):
  249. PyGLWidget.paintGL(self)
  250. self.drawAxis([0, 0, 0], 3, 2)
  251. #绘制地图
  252. for road in self.roads.items():
  253. [_, value] = road
  254. for nodeId in value:
  255. for nextId in value:
  256. if not nodeId == nextId:
  257. [_, point1] = self.nodes[nodeId]
  258. [_, point2] = self.nodes[nextId]
  259. self.drawEdge(point1, point2, 20, [1,1,0])
  260. for node in self.nodes.items():
  261. [name, [type, point]] = node
  262. if type == "street_node":
  263. self.drawNode(point, 30, [0, 0.5, 0.5],name)
  264. if type == "space_node":
  265. self.drawNode(point, 20, [0.3, 0.8, 0.7],name)
  266. #绘制agv相关数据,路径、轨迹、位姿
  267. self.DrawRobotData(self.robot1_)
  268. self.DrawRobotData(self.robot2_)