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! while laps > 0: finch.wheels(0.5, 0.5) sleep(half_lap_time) finch.wheels(0, 0) sleep(0.1) finch.wheels(-0.5, -0.5) sleep(half_lap_time) laps -= 1 finch.close()
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] tweety.close()
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) snakyFinch.led(255,255,255) snakyFinch.wheels(0.3,0.3) sleep(2.5) snakyFinch.close();
window.addstr(2, 0, "Finch goes forward!") myFinch.wheels(1, 1) sleep(0.1) myFinch.halt() elif key == curses.KEY_DOWN: window.clrtobot() window.addstr(2, 0, "Finch goes backwards!") myFinch.wheels(-1, -1) sleep(0.1) myFinch.halt() elif key == curses.KEY_LEFT: window.clrtobot() window.addstr(2, 0, "Finch goes left!") myFinch.wheels(0, 1) sleep(0.1) myFinch.halt() elif key == curses.KEY_RIGHT: window.clrtobot() window.addstr(2, 0, "Finch goes right!") myFinch.wheels(1, 0) sleep(0.1) myFinch.halt() curses.endwin() #Close connection with finch myFinch.close()