2013-10-05 93 views
0

我想要使用Raspberry Pi读取Logitech Logitech Extreme 3D Pro的值。我使用的是pygame library使用Python读取游戏杆值

脚本:

import pygame 
import sys 
import time 

pygame.joystick.init() 

print pygame.joystick.get_count() 

_joystick = pygame.joystick.Joystick(0) 
_joystick.init() 
print _joystick.get_init() 
print _joystick.get_id() 
print _joystick.get_name() 
print _joystick.get_numaxes() 
print _joystick.get_numballs() 
print _joystick.get_numbuttons() 
print _joystick.get_numhats() 
print _joystick.get_axis(0) 

的输出继电器:

1 
1 
0 
Logitech Logitech Extreme 3D Pro 
4 
0 
12 
SDL_JoystickNumHats value:1: 
1 
SDL_JoystickGetAxis value:0: 
0.0 

有4轴和我转过所有的人。

我找不到问题。我已经尝试过使用其他轴。

感谢您的帮助。

+0

什么是预期的输出? – kindall

+1

您是否尝试过循环'get_axis',或者处理'JOYAXISMOTION'事件而不是轮询?使用游戏杆可能会将初始位置视为0,因此在get_init后立即调用get_axis将始终返回0.0。 – abarnert

回答

2

如果问题是该值始终为0,则在读取值之前尝试执行pygame.event.pump()。我有类似的问题,这有助于。

2

我遇到了同样的问题。您必须编写pygame.event.get()才能从游戏杆读取信息。否则它永远不会更新。

0

我宁愿等待(在一个线程甚至更多) 例如:

axes = [ 0.0 ] * your_joystick.get_numaxes() 
    buttons = [ False ] * your_joystick.get_numbuttons() 

    while self.keep_alive: 
     event = pygame.event.wait() 
     if event.type == pygame.QUIT: 
      self.keep_alive = False 
     elif event.type == pygame.JOYAXISMOTION: 
      e = event.dict 
      axes[e['axis']] = e['value'] 
     elif event.type in [pygame.JOYBUTTONUP, pygame.JOYBUTTONDOWN ]: 
      e = event.dict 
      buttons[e['button']] ^= True