Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from ev3dev2.motor import MoveTank, OUTPUT_B, OUTPUT_C
  3. from ev3dev2.sensor.lego import ColorSensor, TouchSensor, UltrasonicSensor
  4. from ev3dev2.button import Button
  5. from ev3dev2.sound import Sound
  6.  
  7.  
  8. btn = Button()
  9. tank_pair = MoveTank(OUTPUT_B, OUTPUT_C)
  10. ts = TouchSensor()
  11. cl = ColorSensor()
  12. us = UltrasonicSensor()
  13. sound = Sound()
  14.  
  15.  
  16.  
  17.  
  18. def correct_angle(direction):
  19. # correct_angle reverses to then last the known black tile and then turns either left or right depending
  20. # the parameter direction
  21. tank_pair.on_for_rotations(-20, -20, 0.8)
  22.  
  23. if direction == 1:
  24. tank_pair.on_for_rotations(0, 5, 0.1)
  25. else:
  26. tank_pair.on_for_rotations(5, 0, 0.1)
  27.  
  28.  
  29.  
  30. def search():
  31. # search looks 0.8 rotations ahead for the next black tile
  32. tank_pair.on_for_rotations(20, 20, 0.8)
  33. if cl.reflected_light_intensity < 17:
  34. sound.beep()
  35. return True
  36. else:
  37. return False
  38.  
  39.  
  40.  
  41. def find_black():
  42. # find black first assumes robot is on correct path so calls search method straight away
  43. # if this fails to find the next black tile then it begins correcting its angle by 9 degrees
  44. # first to the right then back to the left
  45. if search():
  46. return True
  47. else:
  48. failed_attemps = 0
  49. # this means it didnt find a black tile where it expected to so it needs to correct its angle
  50. while failed_attemps < 2:
  51. correct_angle(1)
  52. if search():
  53. return True
  54. else:
  55. failed_attemps += 1
  56.  
  57. failed_attemps = 0
  58. while failed_attemps < 4:
  59. correct_angle(0)
  60. if search():
  61. return True
  62. else:
  63. failed_attemps += 1
  64.  
  65.  
  66.  
  67. def tower():
  68. tank_pair.on_for_rotations(15, 0, 1)
  69. us.mode = 'US-DIST-CM'
  70. tank_pair.on_for_rotations(20,20, 13)
  71.  
  72. distance = us.value()/10
  73.  
  74. while distance > 70:
  75. tank_pair.on_for_rotations(0,10,0.5)
  76. distance = us.value()/10
  77.  
  78.  
  79.  
  80. while not ts.value():
  81. tank_pair.on_for_rotations(20,20,1)
  82.  
  83. tank_pair.on_for_rotations(-10,-10,1)
  84.  
  85. while cl.reflected_light_intensity > 40:
  86. tank_pair.on_for_rotations(100,100,1)
  87.  
  88. """
  89. while not ts.value() and cl.reflected_light_intensity > 50:
  90. tank_pair.on_for_rotations(50,50,1)
  91. """
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. sound.beep("Finished")
  101.  
  102.  
  103.  
  104. def move_forward(tiles):
  105. count = 0
  106.  
  107. while count < tiles:
  108.  
  109. # While we are over black just go forward
  110. while cl.reflected_light_intensity < 17:
  111. tank_pair.on(10, 10)
  112.  
  113. # When we are no longer over a black tile we must search for the next one
  114. tank_pair.off()
  115. if find_black() == True:
  116. count += 1
  117.  
  118.  
  119.  
  120.  
  121. def turn_right():
  122. tank_pair.on_for_rotations(20, 0, 1)
  123.  
  124.  
  125.  
  126. move_forward(1)
  127. turn_right()
  128. move_forward(14)
  129. tank_pair.on_for_rotations(-15, -15, 0.4)
  130. tower()
  131. sound.speak("complete")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement