# Lap Swimmer, an example program for the Finch # Tell the Finch how many laps to do, set it down on the ground # Finch goes forward until it sees an obstacle # Then goes back the same distance. It'll then repeat this lap as many times # as the user requested from lib.finch import Finch from time import sleep, time # Main function for the lap swimmer example program finch = Finch() # Initialize the finch finch.led(255, 255, 0) laps = 0 # Get the number of laps Finch will swim: 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)
# 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]
# A simple Finch dance in Python from lib.finch import Finch from time import sleep print("Finch's First Python program.") # Instantiate the Finch object snakyFinch = Finch() # Do a six step dance snakyFinch.led(255,0,0) snakyFinch.wheels(1,1) sleep(1) snakyFinch.led(0,255,0) snakyFinch.wheels(0,1) sleep(1) snakyFinch.led(0,0,255) snakyFinch.wheels(1,0) sleep(1) snakyFinch.led(255,0,255) snakyFinch.wheels(-1,-1) sleep(0.5) snakyFinch.led(0,255,255) snakyFinch.wheels(0.2,-1) sleep(1)
# Car alarm # The finch sounds an alarm, alternating high pitch sounds and # flashing red abd blue lights, until its nose is turned up from time import sleep from lib.finch import Finch finch = Finch() x = 0 while x > -0.5: x, y, z, tap, shake = finch.acceleration() finch.led("#FF0000") # set the led to red finch.buzzer(1.0, 250) sleep(1.05) finch.led("#0000FF") # set the led to blue finch.buzzer(1.0, 400) sleep(1.05) finch.halt() finch.close()
# Race track driver, an example program for the Finch # Watch the Finch navigate a square race track from lib.finch import Finch from time import sleep #Main function for the race track driver example program.""" #Initialize the finch finch = Finch() #Set both wheels to one-half forward throttle for 1.5s finch.wheels(0.5,0.5) finch.led(0, 255, 255) sleep(1.5) # Now set the left wheel at half throttle, right wheel off to turn finch.wheels(0.5,0) finch.led(0, 255, 0) sleep(1.28) finch.wheels(0.5,0.5) finch.led(0, 255, 255) sleep(1.5) finch.wheels(0.5,0) finch.led(0, 255, 0) sleep(1.28)
# 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()
from lib.finch import Finch import curses from time import sleep #Initalize finch myFinch = Finch() """ Control Finch with keys """ # Initialize the library. Return a WindowObject which represents the whole screen. window = curses.initscr() curses.cbreak() window.keypad(1) window.addstr(0, 0, "Hit 'q' to quit") window.move(2, 0) window.refresh() key = '' while key != ord('q'): key = window.getch() window.addch(2, 0, key) window.refresh() if key == curses.KEY_UP: window.clrtobot() window.addstr(2, 0, "Finch goes forward!") myFinch.wheels(1, 1) sleep(0.1)
# A simple program that changes the Finch LED based on orientation. 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)); # Use the acceleration data to set the LED: # beak up if x < -0.7 and y > -0.3 and y < 0.3 and z > -0.3 and z < 0.3: tweety.led(255,0,0); # beak down elif x > 0.7 and y > -0.3 and y < 0.3 and z > -0.3 and z < 0.3: tweety.led(0,255,0); # level elif x > -0.5 and x < 0.5 and y > -0.5 and y < 0.5 and z > 0.7: tweety.led(0,0,255); # upside down elif x > -0.5 and x < 0.5 and y > -0.5 and y < 0.5 and z < -0.7: tweety.led(0,255,255);