SGo*_*olt 5 python pygame zero joystick logitech
我买了一个罗技 Gamepad F310 来远程控制机器人。我在 Linux Mint(版本 18,Sarah)上使用 Pygame(版本 1.9.1,Python 版本 2.7.11+)。
作为检查操纵杆功能的简单测试,我编写了这个简短的脚本来输出不同轴的值:
import pygame
pygame.display.init()
pygame.joystick.init()
pygame.joystick.Joystick(0).init()
# Get the name of the joystick and print it
JoyName = pygame.joystick.Joystick(0).get_name()
print "Name of the joystick:"
print JoyName
# Get the number of axes
JoyAx = pygame.joystick.Joystick(0).get_numaxes()
print "Number of axis:"
print JoyAx
# Print the values for the axes
pygame.event.pump()
print pygame.joystick.Joystick(0).get_axis(0)
print pygame.joystick.Joystick(0).get_axis(1)
print pygame.joystick.Joystick(0).get_axis(2)
print pygame.joystick.Joystick(0).get_axis(3)
Run Code Online (Sandbox Code Playgroud)
无论我运行脚本时轴的位置如何,我总是得到以下输出:
import pygame
pygame.display.init()
pygame.joystick.init()
pygame.joystick.Joystick(0).init()
# Get the name of the joystick and print it
JoyName = pygame.joystick.Joystick(0).get_name()
print "Name of the joystick:"
print JoyName
# Get the number of axes
JoyAx = pygame.joystick.Joystick(0).get_numaxes()
print "Number of axis:"
print JoyAx
# Print the values for the axes
pygame.event.pump()
print pygame.joystick.Joystick(0).get_axis(0)
print pygame.joystick.Joystick(0).get_axis(1)
print pygame.joystick.Joystick(0).get_axis(2)
print pygame.joystick.Joystick(0).get_axis(3)
Run Code Online (Sandbox Code Playgroud)
我知道输出“ SDL_joystick.Joystick(0).getAxis value ”是由于开发人员在编译源代码时保留了调试标志。但我不明白为什么脚本为所有轴值返回 0 而它们不在 0 位置。
我已经使用 jstest-gtk 包测试了操纵杆。使用这个包,它工作顺利。我的代码中有错误吗?
我在 Windows 中运行了代码,在那里运行顺利。知道为什么它在 Linux 发行版下不起作用吗?
从对问题的编辑中提取。
在最初的代码(上面未发布)中,我有一个 while 循环,但我犯了一个菜鸟错误,将该行放在pygame.event.pump()while 循环之外。我现在学习如何使用这个模块的代码非常完美。
import pygame
pygame.display.init()
pygame.joystick.init()
pygame.joystick.Joystick(0).init()
# Prints the joystick's name
JoyName = pygame.joystick.Joystick(0).get_name()
print "Name of the joystick:"
print JoyName
# Gets the number of axes
JoyAx = pygame.joystick.Joystick(0).get_numaxes()
print "Number of axis:"
print JoyAx
# Prints the values for axis0
while True:
pygame.event.pump()
print pygame.joystick.Joystick(0).get_axis(0)
Run Code Online (Sandbox Code Playgroud)
我确实需要花一些时间来调整和重新编译 Joystick.c 源代码,以摆脱“ SDL_joystick.Joystick(0).getAxis value:0: ”打印输出。显然这个问题已经存在好几年了,但 Ubuntu/Mint 尚未得到解决。