Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. import noiseModel
  2. import maze
  3. reload(maze)
  4. import search
  5. reload(search)
  6. import checkoff
  7. reload(checkoff)
  8. from mazeGraphics import *
  9.  
  10. import lib601.util as util
  11. import lib601.sonarDist as sonarDist
  12. import lib601.markov as markov
  13. from soar.io import io
  14. import soar.outputs.simulator as sim
  15.  
  16. import time
  17. import math
  18. import random
  19.  
  20. robotDiameterMeters = 0.44
  21. ###### SETUP
  22.  
  23. NOISE_ON = False
  24.  
  25. sl13World = [0.15, util.Point(7.0, 1.0), (-0.5, 8.5, -0.5, 6.5)]
  26. bigFrustrationWorld = [0.2, util.Point(7.0, 1.0), (-0.5, 8.5, -0.5, 8.5)]
  27. frustrationWorld = [0.15, util.Point(3.5, 0.5), (-0.5, 5.5, -0.5, 5.5)]
  28. raceWorld = [0.1, util.Point(2.0, 5.5), (-0.5, 5.5, -0.5, 8.5)]
  29. bigPlanWorld = [0.25, util.Point(3.0, 1.0), (-0.5, 10.5, -0.5, 10.5)]
  30. realRobotWorld = [0.1, util.Point(1.5,0.0), (-2.0, 6.0, -2.0, 6.0)]
  31. robotRaceWorld = [0.1, util.Point(3.0,0.0), (-2.0, 6.0, -2.0, 6.0)]
  32.  
  33. THE_WORLD = sl13World
  34. (gridSquareSize, goalPoint, (xMin, xMax, yMin, yMax)) = THE_WORLD
  35.  
  36. # graphics options
  37. show_heatmap = False
  38. show_passable = False
  39. keep_old_windows = False
  40.  
  41. ###### SOAR CONTROL
  42.  
  43. # this function is called when the brain is (re)loaded
  44. def setup():
  45. #initialize robot's internal map
  46. width = int((xMax-xMin)/gridSquareSize)
  47. height = int((yMax-yMin)/gridSquareSize)
  48. print gridSquareSize
  49. print width, height
  50. robot.map = maze.DynamicRobotMaze(height,width,xMin,yMin,xMax,yMax)
  51. robot.goalIndices = robot.map.pointToIndices(goalPoint)
  52. sim.SONAR_VARIANCE = (lambda mean: 0.001) if NOISE_ON else (lambda mean: 0) #sonars are accurate to about 1 mm
  53. robot.plan = None
  54. robot.successors = search.makeMazeSuccessors(robot.map)
  55. #initialize graphics
  56. robot.windows = []
  57. if show_heatmap:
  58. robot.windows.append(HeatMapWindow(robot.map))
  59. robot.windows.append(PathWindow(robot.map, show_passable))
  60. for w in robot.windows:
  61. w.redrawWorld()
  62. w.render()
  63.  
  64. # this function is called when the start button is pushed
  65. def brainStart():
  66. robot.done = False
  67. robot.count = 0
  68. checkoff.getData(globals())
  69.  
  70. # this function is called 10 times per second
  71. def step():
  72. global inp
  73. robot.count += 1
  74. inp = io.SensorInput(cheat=True)
  75.  
  76.  
  77. # discretize sonar readings
  78. # each element in discreteSonars is a tuple (d, cells)
  79. # d is the distance measured by the sonar
  80. # cells is a list of grid cells (r,c) between the sonar and the point d meters away
  81. discreteSonars = []
  82. for (sonarPose, distance) in zip(sonarDist.sonarPoses,inp.sonars):
  83. if NOISE_ON:
  84. distance = noiseModel.noisify(distance, gridSquareSize)
  85. sensorIndices = robot.map.pointToIndices(inp.odometry.transformPose(sonarPose).point())
  86. hitIndices = robot.map.pointToIndices(sonarDist.sonarHit(distance, sonarPose, inp.odometry))
  87. ray = util.lineIndices(sensorIndices, hitIndices)
  88. discreteSonars.append((distance, ray))
  89.  
  90.  
  91. # figure out where robot is
  92. startPoint = inp.odometry.point()
  93. startCell = robot.map.pointToIndices(startPoint)
  94.  
  95.  
  96. # if necessary, make new plan
  97. replan = False
  98. if robot.plan is not None:
  99.  
  100. row = math.floor(robotDiameterMeters/robot.map.height/2)+1
  101. col = math.floor(robotDiameterMeters/robot.map.width/2)+1
  102. print row,col
  103. for cell in robot.plan:
  104. for i in range(int(row)):
  105. for j in range(int(col)):
  106. if not robot.map.isPassable((cell[0]+i,cell[1]+j)):
  107. replan = True
  108. if not robot.map.isPassable((cell[0]+i,cell[1]-j)):
  109. replan = True
  110. if not robot.map.isPassable((cell[0]-i,cell[1]+j)):
  111. replan = True
  112. if not robot.map.isPassable((cell[0]-i,cell[1]-j)):
  113. replan = True
  114. replan = False
  115.  
  116. if robot.plan is None or replan:
  117. print 'REPLANNING'
  118. robot.plan = search.ucSearch(robot.successors,
  119. robot.map.pointToIndices(inp.odometry.point()),
  120. lambda x: x == robot.goalIndices,
  121. lambda x: 0)
  122.  
  123. #print robot.plan
  124.  
  125.  
  126.  
  127. # graphics (draw robot's plan, robot's location, goalPoint)
  128. # do not change this block
  129. for w in robot.windows:
  130. w.redrawWorld()
  131. robot.windows[-1].markCells(robot.plan,'blue')
  132. robot.windows[-1].markCell(robot.map.pointToIndices(inp.odometry.point()),'gold')
  133. robot.windows[-1].markCell(robot.map.pointToIndices(goalPoint),'green')
  134.  
  135.  
  136. # update map
  137. for (d,cells) in discreteSonars:
  138. robot.map.sonarHit(cells[-1])
  139.  
  140.  
  141. # if we are within 0.1m of the goal point, we are done!
  142. if startPoint.distance(goalPoint) <= 0.1:
  143. io.Action(fvel=0,rvel=0).execute()
  144. code = checkoff.generate_code(globals())
  145. raise Exception('Goal Reached!\n\n%s' % code)
  146.  
  147. # otherwise, figure out where to go, and drive there
  148. destinationPoint = robot.map.indicesToPoint(robot.plan[0])
  149. while startPoint.isNear(destinationPoint,0.1) and len(robot.plan)>1:
  150. robot.plan.pop(0)
  151. destinationPoint = robot.map.indicesToPoint(robot.plan[0])
  152.  
  153. currentAngle = inp.odometry.theta
  154. angleDiff = startPoint.angleTo(destinationPoint)-currentAngle
  155. angleDiff = util.fixAnglePlusMinusPi(angleDiff)
  156. fv = rv = 0
  157. if startPoint.distance(destinationPoint) > 0.1:
  158. if abs(angleDiff) > 0.1:
  159. rv = angleDiff
  160. else:
  161. fv = 1.5*startPoint.distance(destinationPoint)
  162. io.setForward(fv)
  163. io.setRotational(rv)
  164.  
  165.  
  166. # render the drawing windows
  167. # do not change this block
  168. for w in robot.windows:
  169. w.render()
  170.  
  171. # called when the stop button is pushed
  172. def brainStop():
  173. stopTime = time.time()
  174. print 'Total steps:', robot.count
  175. print 'Elapsed time in seconds:', stopTime - robot.startTime
  176.  
  177. def shutdown():
  178. if not keep_old_windows:
  179. for w in robot.windows:
  180. w.window.destroy()
  181.  
  182.  
  183.  
  184.  
  185. ////////////
  186.  
  187.  
  188.  
  189. import math
  190. import lib601.util as util
  191.  
  192. robotDiameterMeters = 0.44
  193. sonarMax = 1.5
  194.  
  195. class DynamicRobotMaze:
  196. def __init__(self, height, width, x0, y0, x1, y1):
  197. self.width = width
  198. self.height = height
  199. self.x0,self.x1 = x0,x1
  200. self.y0,self.y1 = y0,y1
  201. self.grid = [[True for c in xrange(width)] for r in xrange(height)]
  202.  
  203. def pointToIndices(self, point):
  204. ix = int(math.floor((point.x-self.x0)*self.width/(self.x1-self.x0)))
  205. iix = min(max(0,ix),self.width-1)
  206. iy = int(math.floor((point.y-self.y0)*self.height/(self.y1-self.y0)))
  207. iiy = min(max(0,iy),self.height-1)
  208. return ((self.height-1-iiy,iix))
  209.  
  210. def indicesToPoint(self, (r,c)):
  211. x = self.x0 + (c+0.5)*(self.x1-self.x0)/self.width
  212. y = self.y0 + (self.height-r-0.5)*(self.y1-self.y0)/self.height
  213. return util.Point(x,y)
  214.  
  215. def isClear(self,(r,c)):
  216. if not (0 <= r < self.height and 0 <= c < self.width):
  217. return False
  218. return self.grid[r][c]
  219.  
  220. def isPassable(self,(r,c)):
  221. return self.isClear((r,c))
  222.  
  223. row = math.floor(robotDiameterMeters/self.height)+1
  224. col = math.floor(robotDiameterMeters/self.width)+1
  225. #print "(",row,",", col,")"
  226. for i in range(int(row)):
  227. for j in range(int(col)):
  228. if not self.isClear((r+i,c+j)):
  229. return False
  230. if not self.isClear((r+i,c-j)):
  231. return False
  232. if not self.isClear((r-i,c+j)):
  233. return False
  234. if not self.isClear((r-i,c-j)):
  235. return False
  236. return True
  237.  
  238.  
  239. def probOccupied(self,(r,c)):
  240. return float(not self.grid[r][c])
  241.  
  242. def sonarHit(self,(r,c)):
  243. self.grid[r][c] = False
  244.  
  245. def sonarPass(self,(r,c)):
  246. self.grid[r][c] = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement