# 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 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()
# A simple program that changes the Finch buzzer based on x-axis orientation. from 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)) # Make it buzz - beak up yields higher frequencies, beak down yields lower tweety.buzzer(0.1, (880 - int(x * 770.0))) # Get obstacles to use to exit loop left, right = tweety.obstacle() tweety.close()
class myFinch: # Class Initializer # Synopsis - # self.left_wheel, is the speed of the left wheel, set to 0 # self.right_wheel, is the speed of the right wheel, set to 0 # self.tweety, is the Finch robot object which will be manipulated through this class # # Description - # Initialize the class members to a specific value def __init__(self): self.tweety = Finch() # Set obstacle sensors self.left_obst, self.right_obst = self.tweety.obstacle() # Setting initial lighting self.myLights = lighting(self.tweety) self.myLights.readValues() # Setting initial acceleration settings self.x, self.y, self.z, self.tap, self.shake = self.tweety.acceleration( ) # Setting initial wheel speed self.left_wheel = 0.0 self.right_wheel = 0.0 self.tweety.wheels(self.left_wheel, self.right_wheel) # [FUNCTION]Name - setWheels ( left, right ) # Synopsis - # def setWheels ( left, right ) : # left, an double value for the left wheel speed # right, an double value for the right wheel speed # # Description - # Accelerates each wheel at different speeds to obtain the new speed. This can be # adjusted by small increments and also gives each wheel independent ending # speeds. # # Return - # (none) def setWheels(self, left, right): self.left_wheel = left self.right_wheel = right self.tweety.wheels(self.left_wheel, self.right_wheel) self.printSpeed() return # setWheels ( left, right ) # # [FUNCTION]Name - def printSpeed ( ) # Synopsis - # def printSpeed ( ): # (no parameters) # Description - # Upon changing speed with setWheels this will print the individual wheel speed # or both to the console. # # Return - # (none) def printSpeed(self): if (self.left_wheel == self.right_wheel): print("Current speed : ", self.left_wheel) return print("Left speed : ", self.left_wheel) print("Right speed : ", self.right_wheel) return # printSpeed ( self ) # def isLight(self): current_left, current_right = self.tweety.light() return current_left, current_right def scurryTowardsLights(self): lspeed = 0.3 rspeed = 0.5 clspeed = 0.1 crspeed = 0.5 onCurve = 0 onMax = 0 maxleft, maxright = self.myLights.getMax() tmpmaxleft = maxleft + .25 tmpmaxright = maxright + .25 if (tmpmaxleft) > .75: tmpmaxleft = .75 if (tmpmaxright) > .75: tmpmaxright = .75 while (True): # Check lights, then obstacles left_light, right_light = self.myLights.lightStatus() self.left_obst, self.right_obst = self.tweety.obstacle() if (self.checkForObstacle()): #switch the speeds tmp = lspeed ctmp = clspeed lspeed = rspeed rspeed = tmp clspeed = crspeed clspeed = ctmp if left_light == 0 and right_light == 0: # Just keep swimming print("Just keep swimming") self.delay(0.3, lspeed, rspeed) onCurve += 1 if onCurve == 10: print("Small curve") self.setWheels(0.0, 0.0) self.delay(3.0, clspeed, crspeed) self.setWheels(lspeed, rspeed) # Natural curve speed (testing) onCurve = 0 elif left_light > 0 or right_light > 0: onCurve = 0 # Find "brightest" # stop ... move towards the light self.setWheels(0.0, 0.0) leftval, rightval = self.myLights.getComparison() if leftval > rightval: # Turn towards our left print("Going left") self.turnLeft() else: print("Going right") # Turn towards our right self.turnRight() if ((leftval + maxleft) > (tmpmaxleft)) and ((rightval + maxright) > tmpmaxright): print("Under bright area!") while (True): self.setWheels(0.0, 0.0) self.delay(0.3, 0.5, 0.5) self.setWheels(0.0, 0.0) left, right = self.isLight() print(left, " ", right) print(tmpmaxleft, " ", tmpmaxright) if (left > tmpmaxleft) or (right > tmpmaxright): tmpmaxleft = left tmpmaxright = right sleep(0.5) else: self.delay(0.3, -0.5, -0.5) self.setWheels(0.0, 0.0) break os._exit(0) elif right_light < 0 or right_light < 0: onCurve = 0 print("got darker") def checkForObstacle(self): if self.left_obst == True and self.right_obst == True: print("Obstacle straight ahead") self.setWheels(-(self.left_wheel), -(self.right_wheel)) sleep(0.5) self.setWheels(0.0, 0.0) sleep(0.1) self.setWheels(0.0, 0.5) sleep(0.5) return True elif self.right_obst == True: print("Obstacle on right") self.setWheels(-(self.left_wheel), -(self.right_wheel)) sleep(0.5) self.setWheels(0.0, 0.0) sleep(0.1) self.setWheels(0.5, 0.0) sleep(0.5) return True elif self.left_obst == True: print("Obstacle on left") self.setWheels(-(self.left_wheel), -(self.right_wheel)) sleep(0.5) self.setWheels(0.0, 0.0) sleep(0.1) self.setWheels(0.5, 0.0) sleep(0.5) return True else: print("No obstacle") return False return False def turnRight(self): self.setWheels(.5, .25) def turnLeft(self): self.setWheels(0.25, .5) def straight(self): self.setWheels(0.4, 0.4) def charging(self): self.tweety.led("#00FF00") self.tweety.buzzer(1.0, 800) def discharging(self): self.tweety.led("#FF0000") def delay(self, time, lspeed, rspeed): timer = 0 while (timer < time): self.setWheels(lspeed, rspeed) if (self.checkForObstacle() == True): tmp = lspeed lspeed = rspeed rspeed = tmp self.setWheels(lspeed, rspeed) continue else: timer = timer + 0.1 sleep(0.1) continue
finch = Finch() done = False while not done: # read current sensor values x, y, z, tap, shake = finch.acceleration() left_light, right_light = finch.light() left_obstacle, right_obstacle = finch.obstacle() temp = finch.temperature() # did it just get dark? if left_light < 0.35 or right_light < 0.35: done = True # RED: is something in the way? elif left_obstacle or right_obstacle: finch.led(255, 0, 0) finch.wheels(-0.75, -0.75) sleep(0.50) # GREEN: all clear, move ahead! else: finch.led(0, 255, 0) finch.wheels(0.75, 0.75) sleep(0.05) # grand finale finch.buzzer(1.5, 10000) finch.halt()
# Remember that we're already upside down for the next time # we check. was_upside_down_before = True else: # Based on x, y and z, we're *not* upside down. # Red LED (hints to the user that we're right-side up and not # ready to print an answer). tweety.led(255, 0, 0) # Remember that we turned right-side up again. was_upside_down_before = False if shake: # If we're not upside-down, and the user shook us, then # change which answer we're going to print. answer_index = randrange(0, len(answers)) # Beep to let the user know we sensed the "shake". tweety.buzzer(0.1, 440) # If we sense obstacles, then it's time to close. left_obstacle, right_obstacle = tweety.obstacle() # Let the user know the program has ended. print('Thanks for playing. Goodbye!') # Always the last step: Close the connection to our Finch to keep # it happy. tweety.close()
from finch import Finch finch = Finch() print('Temperature %5.2f' % finch.temperature()) print() finch.wheels(1.0, -1.0) sleep(0.5) finch.wheels(0.0, 0.0) for count in range(5): finch.led(0, 50*count, 0) x, y, z, tap, shake = finch.acceleration() print ('Acceleration %5.3f, %5.3f %5.3f %s %s' % (x, y, z, tap, shake)) left_light, right_light = finch.light() print ('Lights %5.3f, %5.3f' % (left_light, right_light)) left_obstacle, right_obstacle = finch.obstacle() print('Obstacles %s, %s' % (left_obstacle, right_obstacle)) print() finch.buzzer(0.8, 100*count) sleep(1) finch.led('#FF0000') finch.buzzer(5, 440) sleep(5) finch.halt() finch.close()
from time import sleep from finch import Finch finch = Finch() print('Temperature %5.2f' % finch.temperature()) print() finch.wheels(1.0, -1.0) sleep(0.5) finch.wheels(0.0, 0.0) for count in range(5): finch.led(0, 50 * count, 0) x, y, z, tap, shake = finch.acceleration() print('Acceleration %5.3f, %5.3f %5.3f %s %s' % (x, y, z, tap, shake)) left_light, right_light = finch.light() print('Lights %5.3f, %5.3f' % (left_light, right_light)) left_obstacle, right_obstacle = finch.obstacle() print('Obstacles %s, %s' % (left_obstacle, right_obstacle)) print() finch.buzzer(0.8, 100 * count) sleep(1) finch.led('#FF0000') finch.buzzer(5, 440) sleep(5) finch.halt() finch.close()
length = 1.2 # uppercase letters mean long notes else: length = 0.6 # lowercase are short #convert note to lowercase so that we can compare easily note = note.lower() if (note == 'a'): frequency = 880 elif (note == 'b'): frequency = 988 elif (note == 'c'): frequency = 523 elif (note == 'd'): frequency = 587 elif (note == 'e'): frequency = 659 elif (note == 'f'): frequency = 698 else: #no valid note, nothing to play frequency = 0 if frequency > 0: print("Playing note: ", note) #this is how we play the note on the robot #the robot can play only one note at a time robot.buzzer(length, frequency) # play the note! else: print("Please enter a valid note!")
# A simple Finch dance in Python from finch import Finch from time import sleep print("Finch's First Python program.") # Instantiate the Finch object snakyFinch = Finch() # Do a dance.. think of a wheelchair snakyFinch.buzzer(1.5, 450) sleep(1.0) snakyFinch.led(0, 255, 0) snakyFinch.wheels(0, 1) sleep(3.05) snakyFinch.led(0, 255, 255) snakyFinch.wheels(0.2, -0.2) sleep(1) snakyFinch.led(255, 0, 0) snakyFinch.wheels(1, 1) #moving forward sleep(1.5) snakyFinch.led(0, 255, 0) snakyFinch.wheels(0, 1) #more spinning sleep(1) snakyFinch.led(0, 0, 255) snakyFinch.wheels(1, 0) #spinning but in the other directions sleep(1)