MapGLWidget.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import math
  2. import random
  3. import time
  4. from PyGLWidget import PyGLWidget
  5. from OpenGL.GL import *
  6. import message_pb2 as message
  7. class TimeStatu:
  8. def __init__(self,statu=None,timeout=3):
  9. self.statu=statu
  10. self.time=time.time()
  11. self.timeout_ms=timeout
  12. def timeout(self):
  13. tm=time.time()
  14. return tm-self.time>self.timeout_ms or self.statu==None
  15. class MapGLWidget(PyGLWidget):
  16. colors={"agv1":[1,0,0],
  17. "agv2":[0,1,0],
  18. "agv":[0,1,1]
  19. }
  20. nodes={}
  21. roads={}
  22. paths={}
  23. timedAgv1=TimeStatu(None,0.5)
  24. timedAgv2=TimeStatu(None,0.5)
  25. def ResetAgv1(self,statu):
  26. self.timedAgv1=TimeStatu(statu,0.5)
  27. self.update()
  28. def ResetAgv2(self,statu):
  29. self.timedAgv2=TimeStatu(statu,0.5)
  30. self.update()
  31. def AddNode(self,node):
  32. [id,type,point]=node
  33. self.nodes[id]=[type,point]
  34. def AddRoad(self,road):
  35. [key,value]=road
  36. self.roads[key]=value
  37. def AddTraj(self,name,path):
  38. self.paths[name]=path
  39. def drawPath(self,poses,width,color):
  40. glColor3f(color[0],color[1],color[2])
  41. glLineWidth(2)
  42. glBegin(GL_LINES)
  43. for pose in poses:
  44. [x,y,yaw]=pose
  45. point1=[x,y]
  46. [dx,dy]=self.RotatePoint([-width/2,-width/2],yaw)
  47. point2=[x+dx,y+dy]
  48. [dx,dy]=self.RotatePoint([-width/2,width/2],yaw)
  49. point3=[x+dx,y+dy]
  50. glVertex2d(point1[0],point1[1])
  51. glVertex2d(point3[0],point3[1])
  52. glVertex2d(point1[0],point1[1])
  53. glVertex2d(point2[0],point2[1])
  54. glEnd()
  55. def drawEdge(self,pt1,pt2,width,color):
  56. glColor4f(color[0],color[1],color[2],0)
  57. glLineWidth(width)
  58. glBegin(GL_LINES)
  59. glVertex2d(pt1[0],pt1[1])
  60. glVertex2d(pt2[0],pt2[1])
  61. glEnd()
  62. @staticmethod
  63. def RotatePoint(point,yaw):
  64. [x,y]=point
  65. nx=math.cos(yaw)*x-y*math.sin(yaw)
  66. ny=x*math.sin(yaw)+y*math.cos(yaw)
  67. return [nx,ny]
  68. def drawAxis(self,pose,len,width):
  69. [x,y,yaw]=pose
  70. x_source=[x+len*math.cos(yaw),y+math.sin(yaw)*len]
  71. y_source=[x-math.sin(yaw)*len,y+len*math.cos(yaw)]
  72. glDepthMask(GL_FALSE)
  73. glColor3f(1,0,0)
  74. glLineWidth(width)
  75. glBegin(GL_LINES)
  76. glVertex2d(x,y)
  77. glVertex2d(x_source[0],x_source[1])
  78. glEnd()
  79. glColor3f(0,1,0)
  80. glBegin(GL_LINES)
  81. glVertex2d(x,y)
  82. glVertex2d(y_source[0],y_source[1])
  83. glEnd()
  84. def drawNode(self,node,size,color):
  85. [x,y]=node
  86. glPointSize(size)
  87. glDepthMask(GL_FALSE)
  88. glBegin(GL_POINTS)
  89. glColor4f(color[0],color[1],color[2],0.5)
  90. glVertex2d(x,y)
  91. glEnd()
  92. '''glColor3f(1,1,1);
  93. #glBegin(GL_TEXTURE)
  94. glText'''
  95. def paintGL(self):
  96. PyGLWidget.paintGL(self)
  97. self.drawAxis([0,0,0],5,5)
  98. for node in self.nodes.items():
  99. [_,[type,point]]=node
  100. if type=="street_node":
  101. self.drawNode(point,8,[0,0,1])
  102. if type=="space_node":
  103. self.drawNode(point,8,[1,1,0])
  104. for road in self.roads.items():
  105. [_,value]=road
  106. for nodeId in value:
  107. for nextId in value:
  108. if not nodeId==nextId:
  109. [_,point1]=self.nodes[nodeId]
  110. [_,point2]=self.nodes[nextId]
  111. self.drawEdge(point1,point2,3.,[0,0.5,0.5])
  112. for [id,path] in self.paths.items():
  113. rgb=self.colors.get(id)
  114. if rgb is not None:
  115. for traj in path:
  116. self.drawPath(traj,0.5,rgb)
  117. #绘制agv
  118. if self.timedAgv1.timeout()==False:
  119. x=self.timedAgv1.statu.x
  120. y=self.timedAgv1.statu.y
  121. theta=self.timedAgv1.statu.theta
  122. self.drawAxis([x,y,theta],3,5)