HTML

lapswimmer.py

Nov 18th, 2016
180
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. # Lap Swimmer, an example program for the Finch
  2. # Tell the Finch how many laps to do, set it down on the ground
  3. # Finch goes forward until it sees an obstacle
  4. # Then goes back the same distance. It'll then repeat this lap as many times
  5. # as the user requested
  6.  
  7. from finch import Finch
  8. from time import sleep, time
  9.  
  10. # Main function for the lap swimmer example program
  11.    
  12. finch = Finch() # Initialize the finch
  13.  
  14. finch.led(255, 255, 0)
  15. laps = 0
  16.  
  17. # Get the number of laps Finch will swim:
  18.    
  19. while laps <= 0:
  20.     laps = int(input('Enter number of laps: '))
  21.  
  22.     if laps < 0:
  23.         print('Cannot swim a negative number of laps!')
  24.     elif laps == 0:
  25.         print('Zero laps? I want to swim!')
  26.  
  27. # Move forward until an obstacle is present and measure the time:
  28.  
  29. start = time()
  30. finch.wheels(0.5, 0.5)
  31. sleep(0.1)
  32. while True:
  33.     left_obstacle, right_obstacle = finch.obstacle()
  34.     if left_obstacle or right_obstacle:
  35.         half_lap_time = time() - start
  36.         finch.wheels(0, 0)
  37.         break
  38.  
  39. print('Obstacle found, backing up')
  40.  
  41. # Move backwards for the same amount of time spent moving forward
  42.  
  43. finch.wheels(-0.5, -0.5)
  44. sleep(half_lap_time)
  45. laps -= 1
  46.  
  47. # Now lapswim!
  48.  
  49. while laps > 0:
  50.     finch.wheels(0.5, 0.5)
  51.     sleep(half_lap_time)
  52.     finch.wheels(0, 0)
  53.     sleep(0.1)
  54.     finch.wheels(-0.5, -0.5)
  55.     sleep(half_lap_time)
  56.     laps -= 1
  57.    
  58. finch.close()
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment