Advertisement
Guest User

Test

a guest
Jun 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. # Importing the required modules for the program
  2. import time
  3. import easygopigo3 as easy
  4.  
  5. # Constructing variables to determine whether we need to ask the users input
  6. isBlockedLeft = False
  7. isBlockedRight = False
  8.  
  9. # Creating a reference to the robot
  10. GPG = easy.EasyGoPiGo3()
  11. # Creating a refernce to the distance sensor
  12. distSen = GPG.init_distance_sensor()
  13. # Getting the distance in cm
  14. distance = distSen.cm()
  15.  
  16. # Creating a function that will move forward until we are are certain distance away from the object
  17. def moveForwardUntil(distanceAwayFromObject):
  18. # Creating a variable to store the inital distance that the robot is away from the object
  19. initial = -1
  20. while distance > distanceAwayFromObject:
  21. # Updating the variable only when it is -1, so the first time
  22. if initial == -1:
  23. initial = distance
  24. else:
  25. # Move forward in a loop until is 'distanceAwayFromObject' from the object
  26. time.sleep(1)
  27. GPG.forward()
  28. else:
  29. # Once the robot has reached its destination we want to get the distance it is away from the object now
  30. # We can now calculate how far it is traveled, we are not using the 'distanceAwayFromObject' because this would not be the actual distance traveled
  31. final = distance
  32. findDistanceTraveled(initial, final)
  33. # Once we have printed the traveled distance we must decide where to move
  34. checkBlockedLeftRight()
  35.  
  36. def checkBlockedLeftRight():
  37. # Reset the Right and Left
  38. isBlockedLeft = False
  39. isBlockedRight = False
  40. # Firstly check right
  41. turnAmount(90)
  42. sleep(1)
  43. # Determing if it is blocked on the right
  44. if int(distance) <= 10:
  45. print("Blocked Right")
  46. isBlockedRight = True
  47.  
  48. if isBlockedRight:
  49. # Checking is blocked left only if it is blocked right
  50. turnAmount(180)
  51. sleep(1)
  52.  
  53. if int(distance) <= 10:
  54. print("Blocked Left")
  55. isBlockedLeft = True
  56.  
  57. # Checking if it is both blocked left and right
  58. if isBlockedLeft and isBlockedRight:
  59. print("Asking input...")
  60.  
  61. elif int(distance) >= 15:
  62. print("Not Blocked On Left So We Should Move Left")
  63. # Already in the direction we need to move forward
  64. moveForwardUntil(15)
  65. else int(distance) >= 15:
  66. print("Not Blocked On Right So We Should Move Right")
  67. # Already in the direction we need to move forward
  68. moveForwardUntil(15)
  69.  
  70.  
  71.  
  72. def findDistanceTraveled(initialDistance, finalDistance):
  73. # Calculating the distance traveled before it reaches an obstacle or needs to turn
  74. movedDistance = int(int(initialDistance) - int(finalDistance))
  75. print("The robot has moved ", movedDistance, "cm")
  76.  
  77. def turnAmount(degrees):
  78. # Move the amount of degrees specified
  79. GPG.drive_degrees(degrees)
  80.  
  81.  
  82. while True:
  83. # Continually update the distance
  84. distance = distSen.cm()
  85. moveForwardUntil(15)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement