Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. """My fifth robott."""
  2. import time
  3. from PiBot import PiBot
  4. robot = PiBot()
  5.  
  6. def go_straight():
  7. """Go straight."""
  8. counter = 0
  9. distance = robot.get_front_middle_laser()
  10. distance2 = robot.get_front_left_laser()
  11. distance3 = robot.get_front_right_laser()
  12. while True:
  13. if distance > 0.1 and counter < 40 and distance2 > 0.1 and distance3 > 0.1:
  14. robot.set_wheels_speed(12)
  15. robot.sleep(0.05)
  16. counter += 1
  17. distance = robot.get_front_middle_laser()
  18. distance2 = robot.get_front_left_laser()
  19. distance3 = robot.get_front_right_laser()
  20. else:
  21. robot.set_wheels_speed(0)
  22. break
  23. robot.set_wheels_speed(0)
  24.  
  25.  
  26. def find_object():
  27. """Turn 360 degrees and get difference in sensors."""
  28. last_distance = robot.get_front_middle_laser()
  29. while True:
  30. distance = robot.get_front_middle_laser()
  31. if abs(last_distance - distance) > 0.7:
  32. robot.set_wheels_speed(0)
  33. print("heading to first object")
  34. go_straight()
  35. robot.sleep(0.05)
  36. break
  37. else:
  38. print("turning to find first object")
  39. robot.set_left_wheel_speed(-10)
  40. robot.set_right_wheel_speed(10)
  41. last_distance = distance
  42. robot.sleep(0.05)
  43. robot.set_wheels_speed(0)
  44.  
  45.  
  46. def turn_for_a_few_seconds():
  47. """Turn."""
  48. print("start")
  49. start_time = time.time()
  50. while True:
  51. print("turn for 0.1 seconds")
  52. if abs(start_time - time.time()) < 0.5:
  53. robot.set_left_wheel_speed(-11)
  54. robot.set_right_wheel_speed(11)
  55. else:
  56. robot.set_wheels_speed(0)
  57. break
  58. robot.sleep(0.05)
  59. robot.set_wheels_speed(0)
  60.  
  61.  
  62. def find_second_object():
  63. """Turn 360 degrees and get difference in sensors."""
  64. last_distance = robot.get_front_middle_laser()
  65. while True:
  66. distance = robot.get_front_middle_laser()
  67. if last_distance - distance > 0.4: # 0.3
  68. robot.set_wheels_speed(0)
  69. print(f"i found the distance: {distance}")
  70. return round(distance)
  71. break
  72. else:
  73. print("turning to find second object")
  74. robot.set_left_wheel_speed(-11)
  75. robot.set_right_wheel_speed(11)
  76. last_distance = distance
  77. robot.sleep(0.05)
  78. robot.set_wheels_speed(0)
  79.  
  80.  
  81. def go_in_between_objects():
  82. """Drive between objects."""
  83. desired_distance = (find_second_object() / 2) - 0.1
  84. counter = 0
  85. distance = robot.get_front_middle_laser()
  86. distance2 = robot.get_front_left_laser()
  87. distance3 = robot.get_front_right_laser()
  88. while True:
  89. if distance > desired_distance and counter < 40 and distance2 > desired_distance and distance3 > desired_distance:
  90. robot.set_wheels_speed(11)
  91. robot.sleep(0.05)
  92. counter += 1
  93. distance = robot.get_front_middle_laser()
  94. distance2 = robot.get_front_left_laser()
  95. distance3 = robot.get_front_right_laser()
  96. else:
  97. robot.set_wheels_speed(0)
  98. break
  99. robot.set_wheels_speed(0)
  100. start_rotation = robot.get_rotation()
  101. while True:
  102. print("turning 90 degrees")
  103. current_rotation = robot.get_rotation()
  104. if abs(start_rotation - current_rotation) < 90:
  105. robot.set_right_wheel_speed(11)
  106. robot.set_left_wheel_speed(-11)
  107. else:
  108. robot.set_wheels_speed(0)
  109. break
  110. robot.sleep(0.05)
  111. robot.set_wheels_speed(0)
  112. print("calculating triangle height")
  113. triangle_height = 0.5 * (((desired_distance * 2 + 0.35) ** 2) - ((desired_distance + 0.35) ** 2))
  114. distance = robot.get_front_middle_laser()
  115. distance2 = robot.get_front_left_laser()
  116. distance3 = robot.get_front_right_laser()
  117. counter = 0
  118. while True:
  119. print("driving until triangle height is reached")
  120. if distance > triangle_height and counter < 40 and distance2 > triangle_height and distance3 > triangle_height:
  121. robot.set_wheels_speed(11)
  122. robot.sleep(0.05)
  123. counter += 1
  124. distance = robot.get_front_middle_laser()
  125. distance2 = robot.get_front_left_laser()
  126. distance3 = robot.get_front_right_laser()
  127. else:
  128. robot.set_wheels_speed(0)
  129. break
  130. robot.sleep(0.05)
  131. robot.set_wheels_speed(0)
  132.  
  133.  
  134. print("find_object")
  135. find_object()
  136. print("turn for a few seconds")
  137. turn_for_a_few_seconds()
  138. print("find second object and drive between")
  139. go_in_between_objects()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement