XBox One使用Analogstick和evdev进行伺服的无线控制

时间:2018-07-27 19:50:48

标签: python xbox servo evdev

我正在使用Evdev读取A,B,X,Y的XBOX ONE输入,获得了巨大的成功。但是,我正在努力接听模拟摇杆输入。谁能帮助我使用Python代码吗?

我正在尝试控制伺服器。

到目前为止,这是我的代码,它可以“完美地”工作。我需要知道如何读取(xbox)游戏杆的输出,这样我才能为伺服器使用“ GPIO.PWM”。

    from evdev import InputDevice, categorize, ecodes, KeyEvent
    import RPi.GPIO as GPIO
    from time import sleep

    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(7, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(11, GPIO.OUT, initial=GPIO.LOW)
    GPIO.setup(12, GPIO.OUT, initial=GPIO.LOW)


    gamepad = InputDevice('/dev/input/event2')


    #evdev takes care of polling the controller in a loop
    for event in gamepad.read_loop():
    if event.type == ecodes.EV_KEY:
    keyevent = categorize(event)
    if keyevent.keystate == KeyEvent.key_down:
        if keyevent.keycode[0] == "BTN_A":
            print "Button A Pressed"
            GPIO.output (8, GPIO.HIGH)
        elif keyevent.keycode[0] == "BTN_B":
            print "Button B Pressed"
            GPIO.output (7, GPIO.HIGH)
        elif keyevent.keycode[0] == "BTN_WEST":
            print "Button Y Pressed"
            GPIO.output (11, GPIO.HIGH)
        elif keyevent.keycode[0] == "BTN_NORTH":
            print "Button X Pressed"
            GPIO.output (12, GPIO.HIGH)
    if keyevent.keystate == KeyEvent.key_up:
        if keyevent.keycode[0] == "BTN_A":
            print "Button A Released"
            GPIO.output (8, GPIO.LOW)
        elif keyevent.keycode[0] == "BTN_B":
            print "Button B Released"
            GPIO.output (7, GPIO.LOW)
        elif keyevent.keycode[0] == "BTN_WEST":
            print "Button Y Released"
            GPIO.output (11, GPIO.LOW)
        elif keyevent.keycode[0] == "BTN_NORTH":
            print "Button X Released"
            GPIO.output (12, GPIO.LOW)

1 个答案:

答案 0 :(得分:0)

我正在一个类似的项目中,用XBox One Wireless控制器控制坦克式机器人,我已经绘制出了绝对轴,并以左摇杆向前/向后移动和右摇杆为例转。

Python不是我最喜欢的语言,可能有一种更好的校准方法,但我将其一起使用。希望对您有所帮助。

from evdev import list_devices, InputDevice, categorize, ecodes

CENTER_TOLERANCE = 350
STICK_MAX = 65536

dev = InputDevice( list_devices()[0] )
axis = {
    ecodes.ABS_X: 'ls_x', # 0 - 65,536   the middle is 32768
    ecodes.ABS_Y: 'ls_y',
    ecodes.ABS_Z: 'rs_x',
    ecodes.ABS_RZ: 'rs_y',
    ecodes.ABS_BRAKE: 'lt', # 0 - 1023
    ecodes.ABS_GAS: 'rt',

    ecodes.ABS_HAT0X: 'dpad_x', # -1 - 1
    ecodes.ABS_HAT0Y: 'dpad_y'
}

center = {
    'ls_x': STICK_MAX/2,
    'ls_y': STICK_MAX/2,
    'rs_x': STICK_MAX/2,
    'rs_y': STICK_MAX/2
}

last = {
    'ls_x': STICK_MAX/2,
    'ls_y': STICK_MAX/2,
    'rs_x': STICK_MAX/2,
    'rs_y': STICK_MAX/2
}

for event in dev.read_loop():

    # calibrate zero on Y button
    if event.type == ecodes.EV_KEY:
        if categorize(event).keycode[0] == "BTN_WEST":
            center['ls_x'] = last['ls_x']
            center['ls_y'] = last['ls_y']
            center['rs_x'] = last['rs_x']
            center['rs_y'] = last['rs_y']
            print( 'calibrated' )

    #read stick axis movement
    elif event.type == ecodes.EV_ABS:
        if axis[ event.code ] in [ 'ls_x', 'ls_y', 'rs_x', 'rs_y' ]:
            last[ axis[ event.code ] ] = event.value

            value = event.value - center[ axis[ event.code ] ]

            if abs( value ) <= CENTER_TOLERANCE:
                value = 0

            if axis[ event.code ] == 'rs_x':
                if value < 0:
                    print('left')
                else:
                    print('right')
                print( value )

            elif axis[ event.code ] == 'ls_y':
                if value < 0:
                    print('foreward')
                else:
                    print('backward')
                print( value )