Exemplo n.º 1
0
 def cruising():
     time.sleep(1)
     PressKey(keys.W)
     time.sleep(3)
     ReleaseKey(keys.W)
     PressKey(keys.C)
     ReleaseKey(keys.C)
Exemplo n.º 2
0
def none():
    ReleaseKey(D)
    ReleaseKey(S)
    ReleaseKey(W)
    ReleaseKey(A)
    PressKey(S)
    time.sleep(0.4)
    ReleaseKey(S)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
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")
Exemplo n.º 7
0
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])
Exemplo n.º 8
0
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")
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
 def right(delay=0):
     PressKey(keys.D)
     time.sleep(delay)
     ReleaseKey(keys.D)
Exemplo n.º 12
0
def backward():
    PressKey(S)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(W)
Exemplo n.º 13
0
def right():
    ReleaseKey(A)
    ReleaseKey(S)
    ReleaseKey(W)
    PressKey(D)
Exemplo n.º 14
0
def left():
    PressKey(A)
    PressKey(W)
    ReleaseKey(D)
    time.sleep(t_time)
    ReleaseKey(A)
Exemplo n.º 15
0
def right():
    PressKey(W)
    PressKey(D)
    ReleaseKey(A)
    time.sleep(t_time)
    ReleaseKey(D)
Exemplo n.º 16
0
def reverse():
    ReleaseKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
    PressKey(S)
Exemplo n.º 17
0
def forward():
    PressKey(W)
    ReleaseKey(A)
    ReleaseKey(D)
    ReleaseKey(S)
Exemplo n.º 18
0
 def left(delay=0):
     PressKey(keys.A)
     time.sleep(delay)
     ReleaseKey(keys.A)
Exemplo n.º 19
0
def straight_left():
    ReleaseKey(D)
    ReleaseKey(S)
    PressKey(W)
    PressKey(A)
Exemplo n.º 20
0
    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()
Exemplo n.º 21
0
def no_key():
    ReleaseKey(A)
    ReleaseKey(W)
    ReleaseKey(D)
    ReleaseKey(S)
Exemplo n.º 22
0
def reverse_right():
    ReleaseKey(A)
    ReleaseKey(W)
    PressKey(S)
    PressKey(D)
Exemplo n.º 23
0
def reverse_left():
    ReleaseKey(D)
    ReleaseKey(W)
    PressKey(S)
    PressKey(A)
Exemplo n.º 24
0
def straight_right():
    ReleaseKey(A)
    ReleaseKey(S)
    PressKey(W)
    PressKey(D)
Exemplo n.º 25
0
        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
Exemplo n.º 26
0
def left():
    ReleaseKey(D)
    ReleaseKey(S)
    ReleaseKey(W)
    PressKey(A)