Advertisement
eldieck

Pac Man Robot

May 9th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. from gopigo import *
  2. from time import sleep
  3.  
  4. #Node class for the maze
  5. class MazePoint:
  6.     def __init__(self, direction, distance):
  7.  
  8.         self.direction = direction
  9.         self.distance = distance
  10.  
  11.     def getDirectionInverse(self):
  12.         if(self.direction == 'right'):
  13.             return 'left'
  14.         elif(self.direction == 'left'):
  15.             return 'right'
  16.         else:
  17.             return 'none'
  18. #End of node class
  19.  
  20. #Stack object taken from:
  21. #http://interactivepython.org/runestone/static/pythonds/BasicDS/ImplementingaStackinPython.html
  22. class Stack:
  23.      def __init__(self):
  24.          self.items = []
  25.          self.size = 0
  26.  
  27.      def isEmpty(self):
  28.          return self.items == []
  29.  
  30.      def push(self, item):
  31.          self.items.append(item)
  32.          self.size += 1
  33.  
  34.      def pop(self):
  35.          return self.items.pop()
  36.          self.size -= 1
  37.  
  38.      def peek(self):
  39.          return self.items[len(self.items)-1]
  40.  
  41.      def size(self):
  42.          return self.size
  43. #End of stack object
  44.  
  45. #Stopping function
  46. def stopFunc():
  47.     stop()
  48.     servo(90)
  49.     disable_servo()
  50.  
  51. ROTATE_TIME = .10
  52. ROTATE_FACTOR = 1.5
  53. FORWARD_TIME = .10
  54. US_PIN = 15
  55. STOP_DISTANCE = 10
  56.  
  57. def main():
  58.     moves = Stack()
  59.  
  60.     goAgain = True
  61.  
  62.     while(goAgain):
  63.         leftDistance = 0
  64.         rightDistance = 0
  65.         forwardDistance = 0
  66.         maxAngle = 0
  67.  
  68.         #Get distances from the sides and front
  69.         sleep(0.6)
  70.         servo(90)
  71.         sleep(0.6)
  72.         forwardDistance = us_dist(US_PIN)
  73.         sleep(0.6)
  74.         servo(0)
  75.         sleep(0.6)
  76.         rightDistance = us_dist(US_PIN)
  77.         sleep(0.6)
  78.         servo(180)
  79.         sleep(0.6)
  80.         leftDistance = us_dist(US_PIN)
  81.         sleep(0.6)
  82.  
  83.         if(rightDistance > 10 or leftDistance > 10 or forwardDistance > 10): #Keep going
  84.  
  85.             #Look for the side furthest away from a wall
  86.             maxDistance = max(forwardDistance, leftDistance, rightDistance)
  87.  
  88.             if(maxDistance == forwardDistance):
  89.                 maxAngle = 90
  90.             elif(maxDistance == leftDistance):
  91.                 maxAngle = 180
  92.             else:
  93.                 maxAngle = 0
  94.  
  95.             servo(90)
  96.  
  97.             #Find the way to go and save it to the stack
  98.             if maxAngle < 60:
  99.                 right_rot()
  100.                 print("rotating right")
  101.                 sleep(ROTATE_FACTOR * float(60 - maxAngle) / 120.0)
  102.                 moves.push(MazePoint('right', us_dist(US_PIN)))
  103.             elif maxAngle > 120:
  104.                 left_rot()
  105.                 print('rotating left')
  106.                 sleep(ROTATE_FACTOR * float(maxAngle - 120) / 120.0)
  107.                 moves.push(MazePoint('left', us_dist(US_PIN)))
  108.             else:
  109.                 moves.push(MazePoint('forward', us_dist(US_PIN)))
  110.  
  111.             fwd()
  112.             print 'Going forward'
  113.             while us_dist(US_PIN) > STOP_DISTANCE:
  114.                 sleep(FORWARD_TIME)
  115.             stop()
  116.  
  117.         else: #Backtracking
  118.             if(moves.size > 0):
  119.                 lastMove = moves.pop()
  120.  
  121.                 #Rotate 180
  122.                 sleep(1)
  123.  
  124.                 fwd()
  125.                 while us_dist(US_PIN) > STOP_DISTANCE:
  126.                     sleep(FORWARD_TIME)
  127.                 stop()
  128.  
  129.                 #Rotate to the way contrary to the one already taken
  130.                 if(lastMove.getDirectionInverse == 'right'):
  131.                     right_rot()
  132.                     sleep(ROTATE_FACTOR * float(60 - 0) / 120.0)
  133.  
  134.                 elif(lastMove.getDirectionInverse == 'left'):
  135.                     left_rot()
  136.                     sleep(ROTATE_FACTOR * float(60 - 180) / 120.0)
  137.  
  138.  
  139. if __name__ == '__main__':
  140.     set_left_speed(100)
  141.     set_right_speed(100)
  142.     enable_servo()
  143.     main()
  144.     stopFunc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement