def cruising(): time.sleep(1) PressKey(keys.W) time.sleep(3) ReleaseKey(keys.W) PressKey(keys.C) ReleaseKey(keys.C)
def none(): ReleaseKey(D) ReleaseKey(S) ReleaseKey(W) ReleaseKey(A) PressKey(S) time.sleep(0.4) ReleaseKey(S)
def right(): ReleaseKey(A) ReleaseKey(S) ReleaseKey(W) #PressKey(S) #time.sleep(0.05) #ReleaseKey(S) PressKey(D) PressKey(W) time.sleep(0.09) ReleaseKey(D)
def left(): ReleaseKey(D) ReleaseKey(S) ReleaseKey(W) #PressKey(S) #time.sleep(0.05) #ReleaseKey(S) PressKey(A) PressKey(W) time.sleep(0.09) ReleaseKey(A)
def reset_race(): # Run keys pattern to reset race PressKey(ESC) t.sleep(keypress_pause) ReleaseKey(ESC) PressKey(ENTER) t.sleep(keypress_pause) ReleaseKey(ENTER) PressKey(DOWN) t.sleep(keypress_pause) ReleaseKey(DOWN) PressKey(ENTER) t.sleep(keypress_pause) ReleaseKey(ENTER) t.sleep(3 * keypress_pause)
def on_gyro(data): global z global pressed z += data[2] if (z > 0.5): if (pressed != 'left'): PressKey(LEFT) pressed = 'left' elif (z < -0.5): if (pressed != 'right'): PressKey(RIGHT) pressed = 'right' else: if (pressed == 'left'): ReleaseKey(LEFT) elif (pressed == 'right'): ReleaseKey(RIGHT) pressed = False print('z: ' + str(z) + "\n")
def control_car(chosen_action): for action in actions_array: ReleaseKey(action) # Simplified state-action with # turning on constant accelerating # TODO remove after experimenting PressKey(W) # Perform action PressKey(actions_array[chosen_action])
def on_acc(data): global pressed if (data[1] > 2): if (pressed == 'right'): ReleaseKey(RIGHT) if (pressed != 'left'): PressKey(LEFT) pressed = 'left' elif (data[1] < -2): if (pressed == 'left'): ReleaseKey(LEFT) if (pressed != 'right'): PressKey(RIGHT) pressed = 'right' else: if (pressed == 'left'): ReleaseKey(LEFT) elif (pressed == 'right'): ReleaseKey(RIGHT) pressed = False print('acc: ' + str(data) + "\n")
def on_released(data): print("\n" + 'released: ' + str(data) + "\n") if (data == "x"): ReleaseKey(X) elif (data == "y"): ReleaseKey(Y) elif (data == "a"): ReleaseKey(A) elif (data == "b"): ReleaseKey(B) elif (data == "up"): ReleaseKey(UP) elif (data == "down"): ReleaseKey(DOWN) elif (data == "left"): ReleaseKey(LEFT) elif (data == "right"): ReleaseKey(RIGHT)
def changeKeyState(newPressConfig): global speed, frame pressed = {up: False, down: False, left: False, right: False, nitro: False} if (speed < speedLimit * 8 / 10 and len(road) > 0 and abs(road[0]) < 2): # current speed is much lower than our desired speed, nitro up ! PressKey(up) PressKey(nitro) ReleaseKey(down) pressed[up] = True pressed[nitro] = True elif (speed < speedLimit): # push the pedal but no nitro PressKey(up) ReleaseKey(down) ReleaseKey(nitro) pressed[up] = True elif (speed < speedLimit * 12 / 10): # release both buttons, stop speeding ReleaseKey(up) ReleaseKey(down) ReleaseKey(nitro) else: # slow down ReleaseKey(up) PressKey(down) ReleaseKey(nitro) pressed[down] = True # lastKeypressConfig = what was pressed in the last frame for key in lastKeypressConfig: if not lastKeypressConfig[key] and key not in newPressConfig: continue # fresh press elif not lastKeypressConfig[key]: lastKeypressConfig[key] = newPressConfig[key] PressKey(key) pressed[key] = True continue # force delayBetweenInputs elif lastKeypressConfig[key] + delayBetweenInputs < time.time(): if key in newPressConfig and newPressConfig[key]: lastKeypressConfig[key] = newPressConfig[key] PressKey(key) pressed[key] = True else: ReleaseKey(key) lastKeypressConfig[key] = False continue # delayBetweenInputs ended, release elif lastKeypressConfig[key] < time.time(): if ReleaseKey(key): pass font = cv2.FONT_HERSHEY_SIMPLEX fontScale = 0.8 fontColor = (255, 255, 255) lineType = 3 # visualize what buttons are pressed visualButtons = [('up', (400, 450), pressed[up]), ('down', (400, 500), pressed[down]), ('left', (325, 500), pressed[left]), ('right', (500, 500), pressed[right]), ('nitro', (575, 475), pressed[nitro]), (str(speed) + ' km/h', (625, 450), False)] for visual in visualButtons: cv2.putText(frame, visual[0], visual[1], font, fontScale, (255, 0, 0) if visual[2] else (255, 255, 255), lineType)
def right(delay=0): PressKey(keys.D) time.sleep(delay) ReleaseKey(keys.D)
def backward(): PressKey(S) ReleaseKey(A) ReleaseKey(D) ReleaseKey(W)
def right(): ReleaseKey(A) ReleaseKey(S) ReleaseKey(W) PressKey(D)
def left(): PressKey(A) PressKey(W) ReleaseKey(D) time.sleep(t_time) ReleaseKey(A)
def right(): PressKey(W) PressKey(D) ReleaseKey(A) time.sleep(t_time) ReleaseKey(D)
def reverse(): ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) PressKey(S)
def forward(): PressKey(W) ReleaseKey(A) ReleaseKey(D) ReleaseKey(S)
def left(delay=0): PressKey(keys.A) time.sleep(delay) ReleaseKey(keys.A)
def straight_left(): ReleaseKey(D) ReleaseKey(S) PressKey(W) PressKey(A)
PressKey(ESC) t.sleep(keypress_pause) ReleaseKey(ESC) PressKey(ENTER) t.sleep(keypress_pause) ReleaseKey(ENTER) PressKey(DOWN) t.sleep(keypress_pause) ReleaseKey(DOWN) PressKey(ENTER) t.sleep(keypress_pause) ReleaseKey(ENTER) t.sleep(3 * keypress_pause) Q = np.loadtxt('qmatrix7006.out', delimiter=',') # print(Q) while (True): run_q_algorithm() print("Iteration" + str(iteration)) iteration += 1 key = cv2.waitKey(1) if key == 27: for action in actions_array: ReleaseKey(action) break cv2.destroyAllWindows()
def no_key(): ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) ReleaseKey(S)
def reverse_right(): ReleaseKey(A) ReleaseKey(W) PressKey(S) PressKey(D)
def reverse_left(): ReleaseKey(D) ReleaseKey(W) PressKey(S) PressKey(A)
def straight_right(): ReleaseKey(A) ReleaseKey(S) PressKey(W) PressKey(D)
prediction = model.predict([screen.reshape(WIDTH, HEIGHT, 1)])[0] print(prediction) turn_thresh = .75 fwd_thresh = 0.70 if prediction[0] > fwd_thresh: forward() elif prediction[1] > turn_thresh: left() elif prediction[3] > turn_thresh: right() elif prediction[2] > fwd_thresh: backward() else: forward() keys = key_check() if 'P' in keys: if paused: paused = False time.sleep(1) else: paused = True ReleaseKey(A) ReleaseKey(W) ReleaseKey(D) time.sleep(1) elif 'X' in keys: break
def left(): ReleaseKey(D) ReleaseKey(S) ReleaseKey(W) PressKey(A)