XBoxOne使用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)

我正在进行一个类似的项目,用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 )

相关内容

  • 没有找到相关文章

最新更新