Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. """Programming O1."""
  2.  
  3. from PiBot import PiBot
  4. import rospy
  5.  
  6. robot = PiBot() # The only global variable that this program will use.
  7.  
  8.  
  9. def noise_filter(value, value_buffer):
  10. av = 0
  11. for el in value_buffer:
  12. av += el / len(value_buffer)
  13. clean_value = av
  14. del value_buffer[0]
  15. value_buffer.append(value)
  16. return clean_value, value_buffer
  17.  
  18.  
  19. def create_list(length: int, value=None):
  20. """
  21. Create a list for keeping track of previous IR values and fill it with placeholder values.
  22.  
  23. If value is None; start tracking and fill the list with 'length' amount of front IR sensor values.
  24. Else, fill it with the value 'length' times.
  25. Returns a tuple in the form of: (index = 0, list)
  26. """
  27. if value is None:
  28. return (0, [robot.get_front_middle_ir() for _ in range(length)])
  29. return (0, [value for _ in range(length)])
  30.  
  31.  
  32. def update_previous_values(index: int, previous_values: list):
  33. """Update the values of front middle ir sensor and store it."""
  34. previous_values[index] = robot.get_front_middle_ir()
  35. index = (index + 1) % len(previous_values)
  36. return index, previous_values
  37.  
  38.  
  39. def set_speeds(left: int, right: int) -> None:
  40. """Set the wheels speed."""
  41. robot.set_left_wheel_speed(left)
  42. robot.set_right_wheel_speed(right)
  43.  
  44.  
  45. '''
  46. def fix_speeds(left_encoder_start: int, right_encoder_start: int) -> None:
  47. """Balance the speed of the wheels to be equal."""
  48. left_encoder = abs(robot.get_left_wheel_encoder()) - abs(left_encoder_start)
  49. right_encoder = abs(robot.get_right_wheel_encoder()) - abs(right_encoder_start)
  50. delta = left_encoder / right_encoder
  51. if delta == 1: # Wheels have moved in sync.
  52. left_speed = default_speed
  53. right_speed = default_speed
  54. elif delta < 1: # Right encoder has a higher value - needs to move a bit more to the right.
  55. left_speed = int(default_speed * delta)
  56. right_speed = int(default_speed * (2 - delta))
  57. else: # Left encoder has a higher value - needs to move a bit more to the left.
  58. left_speed = int(default_speed * (2 - delta))
  59. right_speed = int(default_speed * delta)
  60. set_speeds(left_speed, right_speed)
  61. '''
  62.  
  63.  
  64. def find_object(previous_values: list, deviation: float) -> None:
  65. """Find the object by spinning around until a sharp jump in the front IR distance is noticed when compared to the average of the last values."""
  66. while True:
  67. clean_value, previous_values = noise_filter(robot.get_front_middle_ir(), previous_values)
  68. result = abs(clean_value - previous_values[0])
  69. if result > deviation:
  70. print("Object found.")
  71. break
  72. #index, previous_values = update_previous_values(index, previous_values)
  73.  
  74.  
  75. def refind_object(left_speed: int, right_speed: int, average: int, attempts: int, deviation: int) -> bool:
  76. """
  77. Find the object, judged by a sharp jump in the front IR distance.
  78.  
  79. Each "attempt" occurs in 0.1 second intervals.
  80. """
  81. set_speeds(left_speed, right_speed)
  82. for attempt in range(attempts):
  83. rospy.sleep(0.1)
  84. value = robot.get_front_middle_ir()
  85. if abs(value - average) < deviation:
  86. print("Object FOUND.")
  87. return True
  88. print("Object NOT FOUND.")
  89. return False
  90.  
  91.  
  92. def drive_towards_object(left_speed: int, right_speed: int, index: int, previous_values: list, deviation: int) -> None:
  93. """
  94. Drive towards the object.
  95.  
  96. In case of driving forward unevenly (too much leaning to either side - front IR sensor loses object), refind the object.
  97. """
  98. set_speeds(left_speed, right_speed)
  99. while True:
  100. value = robot.get_front_middle_ir()
  101. if value < 0.16: # Break condition - robot has reached the object.
  102. print("Object reached.")
  103. break
  104. average = sum(previous_values) / len(previous_values)
  105. if abs(value - average) > deviation: # Steered away from the object?
  106. print("Object lost.")
  107. # See if the object is to the right.
  108. print("Seeing if the object is to the right...")
  109. if not refind_object(11, -11, average, 5, deviation):
  110. # See if the object is to the left.
  111. print("Seeing if the object is to the left...")
  112. if not refind_object(-11, 11, average, 10, deviation):
  113. set_speeds(0, 0)
  114. print("Couldn't refind the object. Exiting prematurely.")
  115. exit(1)
  116. set_speeds(left_speed, right_speed)
  117. index, previous_values = update_previous_values(index, previous_values)
  118.  
  119.  
  120. def reverse() -> None:
  121. """
  122. Reverse until the robot is close enough to the object to consider the objective completed.
  123.  
  124. This function will also refind the object automatically, in case it is lost.
  125. """
  126. while robot.get_rear_left_straight_ir() > 0.05: # While the robot is not close enough...
  127. while robot.get_rear_left_straight_ir() == 0.16: # If the object has been lost...
  128. set_speeds(11, -11) # Steer until the object is found again.
  129. set_speeds(-11, -11)
  130.  
  131.  
  132. def main():
  133. """Main function of the program."""
  134. default_speed = 20
  135. deviation = 0.2
  136. set_speeds(11, -11)
  137. index, previous_values = create_list(5)
  138.  
  139. # 1. Seek a "jump" in the front IR distance (object found?).
  140. print("Attempting to find object...")
  141. find_object(previous_values, deviation)
  142. # Overwrite the list with a shorter one.
  143. index, previous_values = create_list(5, robot.get_front_middle_ir())
  144. # 2. Drive towards the object.
  145. print("Driving towards the object...")
  146. drive_towards_object(default_speed, default_speed, index, previous_values, deviation)
  147. # 3. Move even closer using more precise back sensor.
  148. print("Reversing...")
  149. reverse()
  150.  
  151. print("Done")
  152. set_speeds(0, 0)
  153.  
  154.  
  155. if __name__ == "__main__":
  156. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement