# A simple program that wanders and avoids obstacles from lib.finch import Finch from time import sleep # Instantiate the Finch object and connect to Finch tweety = Finch() # Get the Z-Axis acceleration zAccel = tweety.acceleration()[2] # Do the following while the Finch is not upside down (z value in gees above -0.7) while zAccel > -0.7: left_obstacle, right_obstacle = tweety.obstacle() # If there's an obstacle on the left, back up and arc if left_obstacle: tweety.led(255,0,0) tweety.wheels(-0.3,-1.0) sleep(1.0) # Back up and arc in the opposite direction if there's something on the right elif right_obstacle: tweety.led(255,255,0) tweety.wheels(-1.0, -0.3) sleep(1.0) # Else just go straight else: tweety.wheels(1.0, 1.0) tweety.led(0,255,0) # Keep reading in the Z acceleration zAccel = tweety.acceleration()[2]
while laps <= 0: laps = int(input('Enter number of laps: ')) if laps < 0: print('Cannot swim a negative number of laps!') elif laps == 0: print('Zero laps? I want to swim!') # Move forward until an obstacle is present and measure the time: start = time() finch.wheels(0.5, 0.5) sleep(0.1) while True: left_obstacle, right_obstacle = finch.obstacle() if left_obstacle or right_obstacle: half_lap_time = time() - start finch.wheels(0, 0) break print('Obstacle found, backing up') # Move backwards for the same amount of time spent moving forward finch.wheels(-0.5, -0.5) sleep(half_lap_time) laps -= 1 # Now lapswim!
# A simple program that randomly changes the LED when the Finch is tapped or shaken # Try it by setting the Finch on a table and tapping the top from lib.finch import Finch from random import randint # Instantiate the Finch object and connect to Finch tweety = Finch() left, right = tweety.obstacle() # Do the following while no obstacles are detected by Finch while not left and not right: # Get the accelerations x, y, z, tap, shake = tweety.acceleration() # Print the acceleration data print("X is %.2f gees, Y is %.2f gees, Z is %.2f gees, tap is %r shake is %r" % (x, y, z, tap, shake)); # If a tap or shake has been detected recently, set the LED to a random color if tap or shake: tweety.led(randint(0,255), randint(0, 255), randint(0,255)) # Get obstacles to use to exit loop left, right = tweety.obstacle() tweety.close()