def wallAvoid(): print('Starting wall avoid') speed = 100 time_end = time.time() + 4 # main loop try: for i in range(2): time_end = time.time() + 6 while time.time() < time_end: rightSensor = robohat.irRight() leftSensor = robohat.irLeft() if rightSensor and leftSensor: robohat.spinLeft(speed) time.sleep(1) elif rightSensor: robohat.spinLeft(speed) time.sleep(0.2) elif leftSensor == True: robohat.spinRight(speed) time.sleep(0.2) else: robohat.forward(speed) robohat.forward(0) time.sleep(3) robohat.forward(speed) time.sleep(0.2) robohat.spinLeft(speed) time.sleep(4) except KeyboardInterrupt: robohat.stop() robohat.cleanup()
import robohat, time speed = 80 robohat.init() # main loop try: while True: rightSensor = robohat.irRight() leftSensor = robohat.irLeft() print "right:{0}, left:{1}".format(rightSensor, leftSensor) if rightSensor and leftSensor: robohat.spinLeft(speed) time.sleep(1) elif rightSensor: robohat.spinLeft(speed) time.sleep(0.2) print('Turn Left') elif leftSensor == True: robohat.spinRight(speed) time.sleep(0.2) print('Turn Right') else: robohat.forward(speed) except KeyboardInterrupt: robohat.stop() robohat.cleanup()
if keyp == 'w' or ord(keyp) == 16: robohat.forward(speed) print ('Forward', speed) elif keyp == 'z' or ord(keyp) == 17: robohat.reverse(speed) print ('Reverse', speed) elif keyp == 's' or ord(keyp) == 18: robohat.spinRight(speed) print ('Spin Right', speed) elif keyp == 'a' or ord(keyp) == 19: robohat.spinLeft(speed) print ('Spin Left', speed) elif keyp == '.' or keyp == '>': speed = min(100, speed+10) print ('Speed+', speed) elif keyp == ',' or keyp == '<': speed = max (0, speed-10) print ('Speed-', speed) elif keyp == ' ': robohat.stop() print ('Stop') elif ord(keyp) == 3: break except KeyboardInterrupt: print finally: robohat.cleanup()