MapGLWidget.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 dijkstra import Map
  9. from OpenGL.GLUT import glutStrokeCharacter
  10. from OpenGL import GLUT
  11. def singleton(cls):
  12. _instance = {}
  13. def inner():
  14. if cls not in _instance:
  15. _instance[cls] = cls()
  16. return _instance[cls]
  17. return inner
  18. @singleton
  19. class MapGLWidget(PyGLWidget):
  20. maps_={}
  21. pointColors={"Front":[0,0.5,0.5],
  22. "Back":[0.5,0,0],
  23. "Main":[0.5,0,0.5],
  24. "Base":[0,0.5,0]}
  25. edgeColors = {"Front": [0, 0.5, 0.5],
  26. "Back": [0.5, 0, 0],
  27. "Main": [0.5, 0, 0.5],
  28. "Base": [0, 0.5, 0]}
  29. pointSizes={"Front":20,
  30. "Back":20,
  31. "Main":20,
  32. "Base":20
  33. }
  34. edgeLineWidth = {"Front": 5,
  35. "Back": 5,
  36. "Main": 5,
  37. "Base": 2
  38. }
  39. enableMaps=["Front","Back","Main"]
  40. robot1_ = None
  41. robot2_ = None
  42. def __init__(self):
  43. PyGLWidget.__init__(self)
  44. GLUT.glutInit()
  45. def SetMaps(self,maps):
  46. self.maps_=maps
  47. def SetMap(self,mapName,map:Map.DijikstraMap):
  48. self.maps_[mapName]=map
  49. def SetEnableMaps(self,mapNames):
  50. self.enableMaps=mapNames
  51. def SetRobot1Instance(self, robot):
  52. self.robot1_ = robot
  53. def SetRobot2Instance(self, robot):
  54. self.robot2_ = robot
  55. def drawTraj(self, poses, width, color):
  56. glColor3f(color[0], color[1], color[2])
  57. glLineWidth(2)
  58. glBegin(GL_LINES)
  59. for pose in poses:
  60. [x, y, yaw] = pose
  61. point1 = [x, y]
  62. [dx, dy] = self.RotatePoint([-width / 2, -width / 2], yaw)
  63. point2 = [x + dx, y + dy]
  64. [dx, dy] = self.RotatePoint([-width / 2, width / 2], yaw)
  65. point3 = [x + dx, y + dy]
  66. glVertex2d(point1[0], point1[1])
  67. glVertex2d(point3[0], point3[1])
  68. glVertex2d(point1[0], point1[1])
  69. glVertex2d(point2[0], point2[1])
  70. glEnd()
  71. def drawTrajWithPoint(self, poses, ptsize, color):
  72. glPointSize(ptsize)
  73. glDepthMask(GL_FALSE)
  74. glBegin(GL_POINTS)
  75. glColor3f(color[0], color[1], color[2])
  76. for pt in poses:
  77. [x, y, _] = pt
  78. glVertex2d(x, y)
  79. glEnd()
  80. def drawEdge(self, pt1, pt2, width, color):
  81. glColor4f(color[0], color[1], color[2], 0)
  82. glLineWidth(width)
  83. glBegin(GL_LINES)
  84. glVertex2d(pt1[0], pt1[1])
  85. glVertex2d(pt2[0], pt2[1])
  86. glEnd()
  87. @staticmethod
  88. def RotatePoint(point, yaw):
  89. [x, y] = point
  90. nx = math.cos(yaw) * x - y * math.sin(yaw)
  91. ny = x * math.sin(yaw) + y * math.cos(yaw)
  92. return [nx, ny]
  93. @staticmethod
  94. def Transform(point,yaw,dt=None):
  95. [x,y]=point
  96. nx = math.cos(yaw) * x - y * math.sin(yaw)
  97. ny = x * math.sin(yaw) + y * math.cos(yaw)
  98. if dt is not None:
  99. [dx,dy]=dt
  100. nx+=dx
  101. ny+=dy
  102. return [nx, ny]
  103. def DrawText(self,pt,text,width,size,rgb):
  104. glDisable(GL_TEXTURE_2D)
  105. glLineWidth(width)
  106. glPointSize(width)
  107. r, g, b = rgb
  108. glPushMatrix()
  109. glColor3f(r, g, b)
  110. glTranslatef(pt[0],pt[1],0)
  111. s = size*0.005
  112. glScalef(s, s, s)
  113. for char in text:
  114. GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(char))
  115. glPopMatrix()
  116. glEnable(GL_TEXTURE_2D)
  117. def drawAxis(self, pose, len, width):
  118. [x, y, yaw] = pose
  119. x_source = [x + len * math.cos(yaw), y + math.sin(yaw) * len]
  120. y_source = [x - math.sin(yaw) * len, y + len * math.cos(yaw)]
  121. glDepthMask(GL_FALSE)
  122. glColor3f(1, 0, 0)
  123. glLineWidth(width)
  124. glBegin(GL_LINES)
  125. glVertex2d(x, y)
  126. glVertex2d(x_source[0], x_source[1])
  127. glEnd()
  128. glColor3f(0, 1, 0)
  129. glBegin(GL_LINES)
  130. glVertex2d(x, y)
  131. glVertex2d(y_source[0], y_source[1])
  132. glEnd()
  133. def DrawMainAGV(self,pose,rgb,label,running=False):
  134. l=0.8 #轮长
  135. L=1.3 #轴距
  136. W=2.5
  137. if running:
  138. l=1.6 #轮长
  139. L=3 #轴距
  140. W=2.5
  141. [x,y,yaw]=pose
  142. #宽
  143. pt1=self.Transform([-(L+l)/2,W/2],yaw,[x,y])
  144. pt2=self.Transform([-(L-l)/2,W/2],yaw,[x,y])
  145. pt3=self.Transform([(L-l)/2,W/2],yaw,[x,y])
  146. pt4=self.Transform([(L+l)/2,W/2],yaw,[x,y])
  147. pt5=self.Transform([-(L+l)/2,-W/2],yaw,[x,y])
  148. pt6=self.Transform([-(L-l)/2,-W/2],yaw,[x,y])
  149. pt7=self.Transform([(L-l)/2,-W/2],yaw,[x,y])
  150. pt8=self.Transform([(L+l)/2,-W/2],yaw,[x,y])
  151. glDepthMask(GL_FALSE)
  152. glColor3f(rgb[0],rgb[1],rgb[2])
  153. glLineWidth(20)
  154. glBegin(GL_LINES)
  155. glVertex2d(pt1[0],pt1[1])
  156. glVertex2d(pt2[0],pt2[1])
  157. glVertex2d(pt3[0],pt3[1])
  158. glVertex2d(pt4[0],pt4[1])
  159. glVertex2d(pt5[0],pt5[1])
  160. glVertex2d(pt6[0],pt6[1])
  161. glVertex2d(pt7[0],pt7[1])
  162. glVertex2d(pt8[0],pt8[1])
  163. glEnd()
  164. glLineWidth(2)
  165. glBegin(GL_LINES)
  166. glVertex2d(pt2[0],pt2[1])
  167. glVertex2d(pt6[0],pt6[1])
  168. glVertex2d(pt3[0],pt3[1])
  169. glVertex2d(pt7[0],pt7[1])
  170. glVertex2d(pt2[0],pt2[1])
  171. glVertex2d(pt3[0],pt3[1])
  172. glVertex2d(pt6[0],pt6[1])
  173. glVertex2d(pt7[0],pt7[1])
  174. glEnd()
  175. #绘制方向
  176. self.drawAxis(pose,L,5)
  177. self.DrawText([x-W/2,y-l/2],label,5,1.5,rgb)
  178. def DrawAGV(self,pose,rgb,label=""):
  179. [x,y,yaw]=pose
  180. l=0.8 #轮长
  181. L=1.3 #轴距
  182. W=2.5 #宽
  183. pt1=self.Transform([-(L+l)/2,W/2],yaw,[x,y])
  184. pt2=self.Transform([-(L-l)/2,W/2],yaw,[x,y])
  185. pt3=self.Transform([(L-l)/2,W/2],yaw,[x,y])
  186. pt4=self.Transform([(L+l)/2,W/2],yaw,[x,y])
  187. pt5=self.Transform([-(L+l)/2,-W/2],yaw,[x,y])
  188. pt6=self.Transform([-(L-l)/2,-W/2],yaw,[x,y])
  189. pt7=self.Transform([(L-l)/2,-W/2],yaw,[x,y])
  190. pt8=self.Transform([(L+l)/2,-W/2],yaw,[x,y])
  191. glDepthMask(GL_FALSE)
  192. glColor3f(rgb[0],rgb[1],rgb[2])
  193. glLineWidth(3)
  194. glBegin(GL_LINES)
  195. glVertex2d(pt1[0],pt1[1])
  196. glVertex2d(pt2[0],pt2[1])
  197. glVertex2d(pt3[0],pt3[1])
  198. glVertex2d(pt4[0],pt4[1])
  199. glVertex2d(pt5[0],pt5[1])
  200. glVertex2d(pt6[0],pt6[1])
  201. glVertex2d(pt7[0],pt7[1])
  202. glVertex2d(pt8[0],pt8[1])
  203. glEnd()
  204. glLineWidth(1)
  205. glBegin(GL_LINES)
  206. glVertex2d(pt2[0],pt2[1])
  207. glVertex2d(pt6[0],pt6[1])
  208. glVertex2d(pt3[0],pt3[1])
  209. glVertex2d(pt7[0],pt7[1])
  210. glVertex2d(pt2[0],pt2[1])
  211. glVertex2d(pt3[0],pt3[1])
  212. glVertex2d(pt6[0],pt6[1])
  213. glVertex2d(pt7[0],pt7[1])
  214. glEnd()
  215. #绘制方向
  216. self.drawAxis(pose,1.2,5)
  217. color=[1,1,1]
  218. self.DrawText([x-W/2,y-l/2],label,3,1.5,color)
  219. def drawNode(self, pt, size, color,label=""):
  220. [x, y] = pt
  221. glPointSize(size)
  222. glDepthMask(GL_FALSE)
  223. glBegin(GL_POINTS)
  224. glColor4f(color[0], color[1], color[2], 0.5)
  225. glVertex2d(x, y)
  226. glEnd()
  227. l=len(label)
  228. self.DrawText([x-l/8,y],label,size/200,0.5,[1,1,1])
  229. def drawSpaceNode(self,pose,size,color,label=''):
  230. [x, y,yaw] = pose
  231. self.drawNode([x,y],size,color,label)
  232. width=2.4
  233. length=6
  234. pt1=self.Transform([x-length/2,y+width/2],yaw)
  235. pt2=self.Transform([x+length/2,y+width/2],yaw)
  236. pt3=self.Transform([x-length/2,y-width/2],yaw)
  237. pt4=self.Transform([x+length/2,y-width/2],yaw)
  238. glDepthMask(GL_FALSE)
  239. glColor3f(1,1,1)
  240. glLineWidth(10)
  241. glBegin(GL_LINES)
  242. glVertex2d(pt1[0],pt1[1])
  243. glVertex2d(pt2[0],pt2[1])
  244. glVertex2d(pt2[0],pt2[1])
  245. glVertex2d(pt3[0],pt3[1])
  246. glVertex2d(pt3[0],pt3[1])
  247. glVertex2d(pt4[0],pt4[1])
  248. glVertex2d(pt4[0],pt4[1])
  249. glVertex2d(pt1[0],pt1[1])
  250. glEnd()
  251. def DrawRobotData(self,robot):
  252. if robot is not None:
  253. if robot.currentNavPath_ is not None:
  254. for traj in robot.currentNavPath_:
  255. self.drawTraj(traj, 0.5, robot.pathColor_)
  256. # 绘制agv
  257. if robot is not None:
  258. if robot.timedRobotStatu_.timeout() == False:
  259. agv_statu = robot.timedRobotStatu_.statu
  260. x = agv_statu.x
  261. y = agv_statu.y
  262. theta = agv_statu.theta
  263. if robot.IsMainAgv():
  264. if robot.IsMainModeStatu():
  265. self.DrawMainAGV([x, y, theta], robot.Color(),robot.name_,True)
  266. else:
  267. self.DrawMainAGV([x, y, theta], robot.Color(),robot.name_)
  268. else:
  269. self.DrawAGV([x, y, theta], robot.Color(),robot.name_)
  270. if robot.timedNavStatu_.timeout() == False:
  271. select_traj = robot.MpcSelectTraj()
  272. predict_traj = robot.MpcPredictTraj()
  273. self.drawTrajWithPoint(select_traj, 8, [1, 0, 1])
  274. self.drawTrajWithPoint(predict_traj, 10, [0, 1, 1])
  275. def paintGL(self):
  276. PyGLWidget.paintGL(self)
  277. self.drawAxis([0, 0, 0], 3, 2)
  278. #绘制地图
  279. pointSize = 10
  280. pointColor = [0.2, 0.3, 0.6]
  281. edgeWidth = 10
  282. edgeColor = [0.1, 0.6, 0.1]
  283. for item in self.maps_.items():
  284. key,map=item
  285. if self.enableMaps.count(key)<=0:
  286. continue
  287. for edge in map.Edges():
  288. [node1,node2,distance]=edge
  289. vertex1=map.GetVertex(node1)
  290. vertex2 = map.GetVertex(node2)
  291. self.drawEdge([vertex1.x_,vertex1.y_],[vertex2.x_,vertex2.y_],self.edgeLineWidth[key],self.edgeColors[key])
  292. for vertex in map.VertexDict().items():
  293. nodeID,node=vertex
  294. self.drawNode([node.x_,node.y_], self.pointSizes[key], self.pointColors[key], nodeID)
  295. #绘制agv相关数据,路径、轨迹、位姿
  296. self.DrawRobotData(self.robot1_)
  297. self.DrawRobotData(self.robot2_)