def key_input(event): counter = 1 offStep = 34 ct.init() #print 'Key:', event.char key_press = event.char sleep_time = 0.030 # Define keys if key_press.lower() == 'w': t0 = time.time() t_end = t0 + running_time while time.time() < t_end: ct.init() if counter % offStep == 0: ct.turnLeft(sleep_time) else: ct.forward(sleep_time) sys.stdout.write("\rTime elapsed: %.2f" % round(time.time() - t0, 2)) sys.stdout.flush() counter = counter + 1 else: time.sleep(sleep_time)
def driveRoute(actionList): # Start C script to get MPU6050 running and wait for initialisation process = subprocess.Popen('../C_GyroReader/mstest') time.sleep(4) print( "------------------------------------------------------------------------" ) velocity = 20 # cm / sec sleep_time = 0.030 accel_bias = 0.2 try: for elem in actionList: angle = elem[0] # FIRST: ANGLE pathLen = elem[1] # SECOND: PATH # no bias if no distance if pathLen == 0: accel_bias = 0 # show/start rotating process print "Rotating " + str(angle) + " deg." if angle != 0: rotate(angle, process) print print "Rotating finished." # time = cm / (cm/sec) runtime = pathLen / velocity t0 = time.time() t_end = t0 + runtime + accel_bias # acceleration bias # counter drift to right counter = 1 offStep = 34 # show/start driving process print "Driving " + str(pathLen) + " cm." while time.time() < t_end: ctr.init() if counter % offStep == 0: ctr.turnLeft(sleep_time) else: ctr.forward(sleep_time) print "Driving done." finally: process.kill()
def key_input(event): ct.init() #print 'Key:', event.char key_press = event.char sleep_time = 0.040 # Define keys if key_press.lower() == 'w': ct.forward(sleep_time) elif key_press.lower() == 's': ct.backward(sleep_time) elif key_press.lower() == 'a': ct.turnLeft(sleep_time) elif key_press.lower() == 'd': ct.turnRight(sleep_time) elif key_press.lower() == 'q': ct.pivotLeft(sleep_time) elif key_press.lower() == 'e': ct.pivotRight(sleep_time) else: time.sleep(sleep_time)
def decisions(): avoidMonster = 300 datas = numberToColor() print(datas) ### BEHIND TO AVOID MONSTER if (datas[2] < 250 and datas[2] >= avoidMonster): # 320 > [behind][black] >= (300 or 50) reset() time.sleep( 0.2) # to avoid to reset() a lot of times and run "speed bonus" if (time.time() - timeStart >= timeToAvoidMonster): avoidMonster = 50 if (datas[2] >= 250): # [behind][black] if (datas[5] > 0): energy() time.sleep(2) ### FRONT if (datas[3][2] >= 100): # [front][black] if (datas[0][2] > datas[1][2]): # [left][black] > [right][black] while (datas[3][2] >= 50): # [front][black] != 50 turnRight() turnRight() turnRight() datas = numberToColor() # recalcul elif (datas[0][2] < datas[1][2]): # [left][black] < [right][black] while (datas[3][2] >= 50): # [front][black] != 50 turnLeft() turnLeft() turnLeft() datas = numberToColor() # recalcul elif (datas[0][2] == datas[1][2]): # [left][black] == [right][black] if (lastControl != "down"): while (datas[3][2] >= 50): # [front][black] != 50 pyautogui.keyDown(lastControl) pyautogui.keyUp(lastControl) pyautogui.keyDown(lastControl) pyautogui.keyUp(lastControl) datas = numberToColor() # recalcul else: while (datas[0][2] != datas[1][2]): turnLeft() turnLeft() ### LEFT AND RIGHT BLACK if (datas[0][2] >= avoid): # [left][black] if (datas[0][2] >= avoid and datas[3][2] >= 200): # [left][black] and [front][black] turnRight() turnRight() turnRight() turnRight() elif (datas[0][2] >= avoid and datas[1][2] >= 200): # [left][black] and [right][black] reset() time.sleep( 0.2 ) # to avoid to reset() a lot of times and run "speed bonus" else: turnRight() if (datas[1][2] >= avoid): # [right][black] if (datas[1][2] >= avoid and datas[3][2] >= 200): # [right][black] and [front][black] turnLeft() turnLeft() turnLeft() turnLeft() elif (datas[1][2] >= avoid and datas[0][2] >= 200): # [right][black] and [left][black] reset() time.sleep( 0.2 ) # to avoid to reset() a lot of times and run "speed bonus" else: turnLeft() turnLeft() ### GREEN if (datas[0][1] >= avoid and datas[1][1] >= avoid): # [left][green] and # [right][green] turnLeft() turnLeft() turnLeft() if (datas[0][1] >= avoid): # [left][green] turnLeft() if (datas[1][1] >= avoid): # [right][green] turnRight() if (datas[3][1] >= avoid): # [front][green] reset() if (datas[0][2] >= avoid and datas[1][2] >= avoid and datas[3][1] >= earn): # [left][black] and # [right][black] and # [front][green] reset() time.sleep( 0.2) # to avoid to reset() a lot of times and run "speed bonus" ### COMBO BLACK AND GREEN if (datas[0][2] >= avoid and datas[0][1] >= earn): # [left][black] and # [left][green] turnLeft() turnLeft() reset() if (datas[1][2] >= avoid and datas[1][1] >= earn): # [right][black] and # [right][green] turnRight() turnRight() reset() if (datas[0][2] >= avoid and datas[3][1] >= earn): # [left][black] and # [front][green] turnRight() turnRight() reset() if (datas[1][2] >= avoid and datas[3][1] >= earn): # [right][black] and # [front][green] turnLeft() turnLeft() reset() ### SNIPE BLACK AND SAND: ## SAND if (datas[4][0] >= 50): # [snipe][sand] if (datas[0][2] > datas[1][2]): # [left][black] > [right][black] while (datas[4][0] >= 50): # [snipe][sand] != 50 turnRight() turnRight() datas = numberToColor() # recalcul elif (datas[0][2] < datas[1][2]): # [left][black] < [right][black] while (datas[4][0] >= 50): # [snipe][sand] != 50 turnLeft() turnLeft() datas = numberToColor() # recalcul elif (datas[0][2] == datas[1][2]): # [left][black] == [right][black] if (lastControl != "down"): while (datas[4][0] >= 50): # [snipe][sand] != 50 pyautogui.keyDown(lastControl) pyautogui.keyUp(lastControl) pyautogui.keyDown(lastControl) pyautogui.keyUp(lastControl) datas = numberToColor() # recalcul else: while (datas[0][2] != datas[1][2]): turnLeft() turnLeft() if (datas[4][0] >= 50 and datas[3][2] >= avoid): # [snipe][sand] and [front][black] if (datas[0][2] >= avoid): # [left][black] turnRight() turnRight() time.sleep(0.2) elif (datas[1][2] >= avoid): # [right][black] turnLeft() turnLeft() time.sleep(0.2) ## BLACK if (datas[4][1] >= 50): # [snipe][black] if (datas[0][2] > datas[1][2]): # [left][black] > [right][black] while (datas[4][1] >= 50): # [snipe][black] != 50 turnRight() turnRight() datas = numberToColor() # recalcul elif (datas[0][2] < datas[1][2]): # [left][black] < [right][black] while (datas[4][1] >= 50): # [snipe][black] != 50 turnLeft() turnLeft() datas = numberToColor() # recalcul elif (datas[0][2] == datas[1][2]): # [left][black] == [right][black] if (lastControl != "down"): while (datas[4][1] >= 50): # [snipe][black] != 50 pyautogui.keyDown(lastControl) pyautogui.keyUp(lastControl) pyautogui.keyDown(lastControl) pyautogui.keyUp(lastControl) datas = numberToColor() # recalcul else: while (datas[0][2] != datas[1][2]): turnLeft() turnLeft() if (datas[4][1] >= avoid and datas[3][2] >= 100): # [snipe][black] and [front][black] if (datas[0][2] >= avoid): # [left][black] turnRight() turnRight() time.sleep(0.2) elif (datas[1][2] >= avoid): # [right][black] turnLeft() turnLeft() time.sleep(0.2)