def __init__(self): pi2go.init() # Main body of code - this detects your key press and changes direction depending on it. try: while True: keyp = self.readkey if keyp == 'w' or keyp == UP: pi2go.forward(SPEED) print 'Forward', SPEED elif keyp == 's' or keyp == DOWN: pi2go.reverse(SPEED) print 'Backward', SPEED elif keyp == 'd' or keyp == RIGHT: pi2go.spinRight(SPEED) print 'Spin Right', SPEED elif keyp == 'a' or keyp == LEFT: pi2go.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 == ' ': pi2go.stop() print 'Stop' elif ord(keyp) == 3: break # When you want to exit - press ctrl+c and it will generate a keyboard interrupt - this is handled nicely here! except KeyboardInterrupt: pi2go.cleanup()
def moveAround(): while True: while not (pi2go.irLeft() or pi2go.irRight() or pi2go.irCentre()): if pi2go.getDistance() <= 2.0: pi2go.spinLeft(speed) time.sleep(1.5) else: pi2go.forward(speed) pi2go.stop() if pi2go.irLeft(): while pi2go.irLeft(): pi2go.spinRight(speed) time.sleep(0.5) pi2go.stop() if pi2go.irRight(): while pi2go.irRight(): pi2go.spinLeft(speed) time.sleep(0.5) pi2go.stop() if pi2go.irCentre(): while pi2go.irCentre(): pi2go.reverse(speed) time.sleep(2) pi2go.spinLeft(speed) time.sleep(1) pi2go.stop()
def line_follower(): while True: if left == right: # If both sensors are the same (either on or off) --> forward pi2go.forward(speed) elif left == True: # If the left sensor is on --> move right pi2go.turnForward(speed + change, speed - change) elif right == True: #If the right sensor is on --> move left pi2go.turnForward(speed - change, speed + change)
def line_follower(): while True: if left == right: # If both sensors are the same (either on or off) --> forward pi2go.forward(speed) elif left == True: # If the left sensor is on --> move right pi2go.turnForward(speed+change, speed-change) elif right == True: #If the right sensor is on --> move left pi2go.turnForward(speed-change, speed+change)
def manual(): """Manual control. Use for straight_line if autonomous mode not working. Use for obstacle course if autonomous mode not working. Use for joust and skittles.""" BTN_LEFT = 80 BTN_RIGHT = 79 BTN_DOWN = 81 BTN_UP = 82 BTN_FAST = 28 # Y BTN_SLOW = 17 # N BTN_STOP = 44 # Space BTN_EXIT = 41 # ESC #explorerhat.light.green.on() global leftspeed, rightspeed while not pi2go.getSwitch(): control = None try: control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT) #print(control) except: pass if control != None: logging.debug('control={}'.format(control)) if BTN_DOWN in control: pi2go.reverse(max(leftspeed, rightspeed)) if BTN_UP in control: pi2go.forward(max(leftspeed, rightspeed)) if BTN_LEFT in control: pi2go.spinLeft(max(leftspeed, rightspeed)) if BTN_RIGHT in control: pi2go.spinRight(max(leftspeed, rightspeed)) if BTN_FAST in control: leftspeed = min(leftspeed + 10, 100) rightspeed = min(rightspeed + 10, 100) logging.debug('leftspeed={} rightspeed={}'.format(leftspeed, rightspeed)) if BTN_SLOW in control: leftspeed = max(leftspeed - 10, 0) rightspeed = max(rightspeed -10, 0) logging.debug('leftspeed={} rightspeed={}'.format(leftspeed, rightspeed)) if BTN_STOP in control: pi2go.stop() if BTN_EXIT in control: break time.sleep(0.02)
def line(speed): left = pi2go.irLeftLine() right = pi2go.irRightLine() if left == False and right == True: pi2go.forward(speed) elif right == True and left == True: pi2go.spinLeft(speed - 20) # speed-20 to smoothen the line follow elif left == True and right == False: pi2go.spinRight(speed - 20) # speed-20 to smoothen the line follow elif left == False and right == False: pi2go.forward(speed)
def linefollow(speed): left = pi2go.irLeftLine() right = pi2go.irRightLine() if left == False and right == True: pi2go.forward(speed) elif right == True and left == True: pi2go.spinLeft(speed - 30) elif left == True and right == False: pi2go.spinRight(speed - 30) elif left == False and right == False: pi2go.forward(speed)
def pi2goMove(): #print "pi2go Move" if not piStop: if pi2go.irLeftLine() == False: #print "Left" pi2go.turnForward(0.3*speed, speed) elif pi2go.irRightLine() == False: #print "Right" pi2go.turnForward(speed, 0.3*speed) else: #print "Straight" pi2go.forward(speed) else: pi2go.forward(0)
def calibrate_all(): global timespincircle global timeturncircle global time10cm logging.info('Calibrating all') #timespincircle = calibrate(pi2go.spinLeft(100)) logging.debug('Time spin'.format(timespincircle)) #timeturncircle = calibrate(pi2go.turnForward(50,100)) logging.debug('Time turn'.format(timeturncircle)) time10cm = calibrate(pi2go.forward(100)) logging.debug('Time for 10cm='.format(time10cm))
def mainLoop(): global globalDistance, globalStop, state, finished global slowspeed, fastspeed while finished == False: if globalStop==1 or globalDistance<5: pi2go.stop() else: if state==1: # Standard Line Follower if pi2go.irLeftLine() and pi2go.irRightLine(): pi2go.forward(40) elif pi2go.irRightLine()==False: pi2go.spinRight(fastspeed) elif pi2go.irLeftLine()==False: pi2go.spinLeft(fastspeed) elif state==2: # Obstacle avoider (reverses then spins when near object) if globalDistance>15: pi2go.forward(50) else: pi2go.reverse(30) time.sleep(0.5) pi2go.turnReverse(30,50) time.sleep(3) elif state==3: # Obstacle avoider (spins when near object) if globalDistance>15: pi2go.forward(50) else: pi2go.spinLeft(50) elif state==4: # Avoids objects using IR sensors only if pi2go.irAll()==False: pi2go.forward(50) else: ir=pi2go.irRight() pi2go.reverse(30) time.sleep(0.5) if ir: pi2go.turnReverse(50,30) else: pi2go.turnReverse(30,50) time.sleep(3)
print "Tests the motors by using the arrow keys to control" print "Use , or < to slow down" print "Use . or > to speed up" print "Speed changes take effect when the next arrow key is pressed" print "Press Ctrl-C to end" print pi2go.init() # main loop try: while True: keyp = readkey() if keyp == 'w' or ord(keyp) == 16: pi2go.forward(speed) print 'Forward', speed elif keyp == 'z' or ord(keyp) == 17: pi2go.reverse(speed) print 'Reverse', speed elif keyp == 's' or ord(keyp) == 18: pi2go.spinRight(speed) print 'Spin Right', speed elif keyp == 'a' or ord(keyp) == 19: pi2go.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)
import pi2go as p2 from time import sleep p2.init() a = p2.getDistance() b = p2.irAll() while True: if p2.getSwitch() == True: while a > 30 and b != True: p2.forward(75) p2.LsetLED(1,1) sleep(0.5) p2.LsetLED(1,0) a = p2.getDistance() b = p2.irAll() else: p2.spinRight(75) p2.LsetLED(1,1) sleep(0.7) p2.LsetLED(1,0) a = p2.getDistance() b = p2.irAll()
def intersection(): centerx = 317 center_y = 404 radius = 370 global prev_dist[]= {1,2,3,4,5,6} init_dist[] = {270,270,270,270,270,270} tty.setcbreak(sys.stdin.fileno()) speed = 50 try: while 1: linefollow(speed) print 'linefollow 1' # ultrasonic sensor's obstacle detection obs = ult_dist() if (obs <= 4): pi2go.stop() print 'stop when bot is in front' time.sleep(1.2) elif (obs == 5): speed = 35 #print 'speed = 45' # manual input to control bot if isData(): c = sys.stdin.read(1) if c == '\x6c': # lane change 'l' print 'left-lane change' # ard.write('2') left() elif c == '\x72': # lane change 'r' print 'right-lanechange' # ard.write('3') right() elif c == '\x20': #spacebar print 'stop' # ard.write('1') #ard.write('4') pi2go.stop() time.sleep(5) # sys.exit(0) elif c == '\x77': # 'w' print 'forward' pi2go.forward(speed) time.sleep(3) elif c == '\x73': #'s' print 'reverse' # 's' pi2go.reverse(speed) time.sleep(3) elif c == '\x61': # 'a' print 'left' pi2go.spinLeft(speed) time.sleep(3) elif c == '\x64': # 'd' print 'right' pi2go.spinRight(speed) time.sleep(3) elif c == '\x2e': #period print 'increase speed' speed = max(0, speed + 10) elif c == '\x2c': # comma print 'decrease speed' speed = min(100, speed-10) elif c == '\x65': #e to exit from program print 'exiting...' sys.exit(0) id_1, x_1, y_1 = gps.gps_client(1) id_2, x_2, y_2 = gps.gps_client(2) id_3, x_3, y_3 = gps.gps_client(3) id_4, x_4, y_4 = gps.gps_client(4) id_5, x_5, y_5 = gps.gps_client(5) id_6, x_6, y_6 = gps.gps_client(6) print id_6, x_6, y_6 if id_6 == 6: if (x_6 == 0 and y_6 == 0) or (x_6 == -1 and y_6 == -1) : # not valid or GPS values 0 or -1 print 'no id' continue else: dx = abs(x_6 - centerx) dy = abs(y_6 - centery) if ((dx + dy) <= radius): print 'true' distance_6 = math.sqrt((dx * dx) + (dy * dy)) # 200 - to stop at intersection quadrant_6 = dir(x_6, y_6) if (int_dist[5] > distance_6 or (distance_6+ 5 <= init_dist[5] or init_dist[5] >= distance_6 - 5)): print 'ID %d arriving Intersection' %id_6 if id_1 == 1: init_dist[0] = find(id_1, x_1, y_1, init_dist[0], quadrant_6, distance_6) if id_2 == 2: init_dist[1] = find(id_2, x_2, y_2, init_dist[1], quadrant_6, distance_6) if id_3 == 3: prev_dist_3 = find(id_3, x_3, y_3, init_dist[2], quadrant_6, distance_6) if id_4 == 4: init_dist[3] = find(id_4, x_4, y_4, init_dist[3], quadrant_6, distance_6) if id_5 == 5: init_dist[4] = find(id_4, x_4, y_4, init_dist[4], quadrant_6, distance_6) init_dist[5] = distance_6 else: print 'ID %away from Intersection' % id_6 init_dist[5] = distance_6 else: print 'Id %d not at Intersection' %id_6 finally: termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings) pi2go.cleanup()
#!/usr/bin/python # irNav.py # navigate using ir obstacle detectors with pi2go library # Author : Zachary Igielman import time import pi2go pi2go.init() fast=50 slow=30 while True: if pi2go.irAll()==False: pi2go.forward(fast) pi2go.setAllLEDs(4095, 4095, 4095) else: ir=pi2go.irRight() pi2go.setAllLEDs(4095, 0, 0) pi2go.reverse(slow) time.sleep(0.5) if ir: pi2go.setAllLEDs(0, 0, 4095) pi2go.turnReverse(fast,slow) time.sleep(3) else: pi2go.setAllLEDs(0, 4095, 0) pi2go.turnReverse(slow,fast) time.sleep(3) pi2go.cleanup()
try: while True: key=raw_input("Where do you want to go (W, A, S, D or other)?") if key=="d": pi2go.setAllLEDs(0, 4095, 0) print "right" pi2go.spinRight(100) elif key=="a": pi2go.setAllLEDs(0, 0, 4095) print "left" pi2go.spinLeft(100) elif key=="s": pi2go.setAllLEDs(4095, 0, 0) print "back" pi2go.reverse(100) elif key=="w": pi2go.setAllLEDs(4095, 4095, 4095) print "up" pi2go.forward(100) else: pi2go.setAllLEDs(0, 0, 0) pi2go.stop() except KeyboardInterrupt: print pi2go.cleanup()
def on_message(self, message): #Messages are of the form: "MessageType/Instruction" hence each message #from scratch needs to be separated into is consistuent parts. print message msg= message.split("/") #MOTOR FUNCTIONS if msg[0]== 'stop': pi2go.stop() elif msg[0]== 'forward': pi2go.forward(float(msg[1])) elif msg[0]== 'reverse': pi2go.reverse(float(msg[1])) elif msg[0]== 'spinLeft': pi2go.spinLeft(float(msg[1])) elif msg[0]== 'spinRight': pi2go.spinRight(float(msg[1])) elif msg[0]== 'turnForward': pi2go.turnForward(float(msg[1]), float(msg[2])) elif msg[0]== 'turnReverse': pi2go.turnReverse(float(msg[1]), float(msg[2])) elif msg[0]== 'goM': pi2go.go(float(msg[1]), float(msg[2])) elif msg[0]== 'go': pi2go.go(float(msg[1])) # SERVO FUNCTIONS #elif msg[0]== 'startServos': #pi2go.startServos() #elif msg[0]== 'stopServos': #pi2go.stopServos() #elif msg[0]== 'setServos': #pi2go.setServo(msg[1],float(msg[2])) # LED FUNCTIONS #elif msg[0]== 'setLED': #pi2go.setLED(msg[1], msg[2], msg[3], msg[4]) #elif msg[0]== 'setAllLEDs': #pi2go.setAllLEDs(msg[1], msg[2], msg[3]) elif msg[0]== 'LsetLED': pi2go.LsetLED(msg[1], msg[2]) # IR FUNCTIONS elif msg[0]== 'irLeft': val = pi2go.irLeft() self.write_message(str(val)) elif msg[0]== 'irRight': val = pi2go.irRight() self.write_message(str(val)) elif msg[0]== 'irLeftLine': val =pi2go.irLeftLine() self.write_message(str(val)) elif msg[0]== 'irRightLine': val= pi2go.irRightLine() self.write_message(str(val)) # ULTRASONIC FUNCTION elif msg[0]== 'ultraSonic': val=pi2go.getDistance() self.write_message(str(val))
try: while True: inp = raw_input("}}") globalstop=1 pi2go.setAllLEDs(4095, 0, 0) if inp=="": globalstop=0 if state==4: state=1 else: state=state+1 if state==1: pi2go.setAllLEDs(0, 4095, 0) elif state==1: pi2go.forward(60) pi2go.setAllLEDs(4095, 4095, 0) elif state==2: pi2go.setAllLEDs(0, 4095, 4095) elif state==3: pi2go.setAllLEDs(4095, 0, 4095) else: pi2go.setAllLEDs(0, 0, 4095) elif inp=="w": pi2go.forward(60) elif inp=="a": pi2go.spinLeft(60) elif inp=="d": pi2go.spinRight(60) elif inp=="s": pi2go.reverse(60)
# speedy line follower with LEDs using library # Author : Zachary Igielman import sys, time import pi2go pi2go.init() slowspeed = 20 fastspeed = 100 lastleft = 0 lastright = 0 # Let's get going pi2go.forward(fastspeed) # main loop try: while True: left = pi2go.irLeftLine() right = pi2go.irRightLine() if left==0 and right==0: pi2go.stop() if left == 0 and lastleft == 1: pi2go.turnForward(slowspeed,fastspeed) pi2go.setAllLEDs(0, 4095, 4095) elif right == 0 and lastright == 1: pi2go.turnForward(fastspeed,slowspeed) pi2go.setAllLEDs(4095, 0, 4095) lastleft = left
def line_follower(): """ State: L R 1 1 - Both White - Depends on P 1 0 - Left White - Turn Left 0 1 - Right White - Turn Right 0 0 - Both Black - Go Forward P - previous State ------------------ 0 - Left 1 - Right :return: """ dist = 0 speed = 70 change = 20 start = 0 STATE = 00 prev_STATE = 11 STOP = False stop = False prev_stop = True while True: #print "line follower %f" %time.time() # print 'get dist: %f' % time.time() left = pi2go.irLeftLine() right = pi2go.irRightLine() # print 'get ir: %f' % time.time() if not left and not right: # If both sensors are the on --> forward STATE = 00 elif left and not right: # If the left sensor is Off --> move right STATE = 10 elif right and not left: # If the right sensor is off --> move left STATE = 01 else: STATE = 11 if time.time() - start > 0.15: dist = (int(pi2go.getDistance()*10))/10.0 print dist while dist < 25: dist = (int(pi2go.getDistance()*10))/10.0 pi2go.stop() STATE = 69 time.sleep(0.15) if stop == False: pi2go.setAllLEDs(4095,0,0) stop = True start = time.time() stop = False if stop == prev_stop: pass elif stop == False: pi2go.setAllLEDs(0,4095,0) if STATE == prev_STATE: pass elif STATE == 00: pi2go.forward(speed) elif STATE == 10: pi2go.turnForward(speed + change, speed - change) elif STATE == 01: pi2go.turnForward(speed - change, speed + change) elif STATE == 11: pi2go.stop() prev_stop = stop prev_STATE = STATE
left = pi2go.irLeftLine() right = pi2go.irRightLine() ntime = time.time() # timecheck for distance if ntime > (stime + 0.1): #print "%.5f" %time.time() dist = (int(pi2go.getDistance()*10))/10.0 #print "%.5f" %time.time() stime = ntime # distance groups if dist < 10: pi2go.setAllLEDs(2000, 0, 0) elif dist > 20: pi2go.setAllLEDs(0, 0, 2000) else: pi2go.setAllLEDs(0, 2000, 0) # line follower if left == right: # If both sensors are the same (either on or off) --> forward pi2go.forward(speed) elif left == True: # If the left sensor is on --> move right pi2go.turnForward(speed+change, speed-change) elif right == True: #If the right sensor is on --> move left pi2go.turnForward(speed-change, speed+change) finally: # Even if there was an error, cleanup pi2go.cleanup()
def line_follower(): """ State: L R 1 1 - Both White - Depends on P 1 0 - Left White - Turn Left 0 1 - Right White - Turn Right 0 0 - Both Black - Go Forward P - previous State ------------------ 0 - Left 1 - Right :return: """ dist = 0 speed = 70 change = 20 start = 0 STATE = 00 prev_STATE = 11 STOP = False while True: #print "line follower %f" %time.time() # we don't need to start a thread if theres only one..... # print 'get dist: %f' % time.time() left = pi2go.irLeftLine() right = pi2go.irRightLine() # print 'get ir: %f' % time.time() if not left and not right: # If both sensors are the on --> forward # pi2go.forward(speed) STATE = 00 elif left and not right: # If the left sensor is Off --> move right # pi2go.turnForward(speed+change, speed-change) STATE = 10 elif right and not left: # If the right sensor is off --> move left # pi2go.turnForward(speed - change, speed + change) STATE = 01 else: # stop # pi2go.stop() STATE = 11 if time.time() - start > 0.2: dist = (int(pi2go.getDistance() * 10)) / 10.0 #out_q.put(dist) start = time.time() if dist > 10: STOP = False STATE = 69 else: STOP = True if STOP: pi2go.stop() if STATE == prev_STATE: pass elif STATE == 00: pi2go.forward(speed) # pi2go.stop() elif STATE == 10: pi2go.turnForward(speed + change, speed - change) elif STATE == 01: pi2go.turnForward(speed - change, speed + change) elif STATE == 11: # pi2go.forward(speed) pi2go.stop() prev_STATE = STATE