【问题标题】:XBox One Wireless control of Servo using Analogue Stick withevdevXbox One 使用 Analogue Stick withevdev 无线控制伺服
【发布时间】:2018-07-27 19:50:47
【问题描述】:

我正在使用 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)

【问题讨论】:

  • 请提供您当前的代码。
  • @pydude - 谢谢,刚刚添加,我将不胜感激。
  • 操纵杆通常是EV_ABS 轴,因此除了KeyEvent,您还必须管理AbsEvent 事件。
  • 谢谢!生病了现在研究!感谢您为我指明方向:)
  • 您好,我这样做并设法获得了一些不错的读数,但它们似乎没有任何模式,将继续在 gamepad.read_loop() 中寻找事件:如果 event.type == 编码.EV_ABS: adam = categorize(event) print adam.event

标签: python xbox servo evdev


【解决方案1】:

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

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 )

【讨论】:

    猜你喜欢
    • 2016-08-25
    • 2020-06-17
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2018-12-10
    • 2017-10-25
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多