PyGame - 带有 ps3 控制器的 RaspberryPi 3b+

PyGame - RaspberryPi 3b+ with a ps3 controller

我正在尝试将 pygame 与 raspberry pi 结合使用,以将 PlayStation 3 控制器用作汽车的输入。 我已经用演示代码测试了控制器,一切正常。然后,当我尝试在我的程序中使用它时,当操纵杆移动时,它读取 0.0 作为输入。附件是我当前的代码:

import pygame

class controller:
        def __init__(self):
                pygame.init()
                pygame.joystick.init()
                global joystick
                joystick = pygame.joystick.Joystick(0)
                joystick.init()

        def get_value(self, axis):
                value = joystick.get_axis(axis)
                return value
control = controller()
val = control.get_value(0)
while True:
        print(val)

我知道这个测试只针对轴 0,但所有轴的输出仍然是 0.0。

下面我附上了演示代码,其中所有的值都被正确读取了。

import pygame, sys, time    #Imports Modules
from pygame.locals import *

pygame.init()#Initializes Pygame
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()#Initializes Joystick

# get count of joysticks=1, axes=27, buttons=19 for DualShock 3

joystick_count = pygame.joystick.get_count()
print("joystick_count")
print(joystick_count)
print("--------------")

numaxes = joystick.get_numaxes()
print("numaxes")
print(numaxes)
print("--------------")

numbuttons = joystick.get_numbuttons()
print("numbuttons")
print(numbuttons)
print("--------------")

loopQuit = False
while loopQuit == False:

    # test joystick axes and prints values
    outstr = ""
    for i in range(0,4):
        axis = joystick.get_axis(i)
        outstr = outstr + str(i) + ":" + str(axis) + "|"
        print(outstr)

    # test controller buttons
    outstr = ""
    for i in range(0,numbuttons):
           button = joystick.get_button(i)
           outstr = outstr + str(i) + ":" + str(button) + "|"
    print(outstr)

    for event in pygame.event.get():
       if event.type == QUIT:
           loopQuit = True
       elif event.type == pygame.KEYDOWN:
           if event.key == pygame.K_ESCAPE:
               loopQuit = True
             
       # Returns Joystick Button Motion
       if event.type == pygame.JOYBUTTONDOWN:
        print("joy button down")
       if event.type == pygame.JOYBUTTONUP:
        print("joy button up")
       if event.type == pygame.JOYBALLMOTION:
        print("joy ball motion")
       # axis motion is movement of controller
       # dominates events when used
       if event.type == pygame.JOYAXISMOTION:
           # print("joy axis motion")

    time.sleep(0.01)
pygame.quit()
sys.exit()

任何反馈将不胜感激。

代码丢失了对初始化操纵杆的引用。它需要维护一个内部 link 给它。请注意下面 class 中 self. 的使用。这将引用保留在 class 中,使“self.joystick”成为 member variable of the class. Python classes need the self. notation (unlike lots of (all?) other object orientated languages). While editing I changed some of the names to match the Python PEP-8 style guide,希望没问题 ;)

class Controller:
    def __init__( self, joy_index=0 ):
        pygame.joystick.init()                # is it OK to keep calling this?
        self.joystick = pygame.joystick.Joystick( joy_index )
        self.joystick.init()

    def getAxisValue( self, axis ):
        value = self.joystick.get_axis( axis )
        return value

也许您没有考虑额外的代码,但是没有事件循环的 PyGame 程序最终会锁定。

import pygame

# Window size
WINDOW_WIDTH    = 300
WINDOW_HEIGHT   = 300


class Controller:
    """ Class to interface with a Joystick """
    def __init__( self, joy_index=0 ):
        pygame.joystick.init()
        self.joystick = pygame.joystick.Joystick( joy_index )
        self.joystick.init()

    def getAxisValue( self, axis ):
        value = self.joystick.get_axis( axis )
        return value


### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
clock  = pygame.time.Clock()
pygame.display.set_caption( "Any Joy?" )    

# Talk to the Joystick
control = controller()

# Main loop
done = False
while not done:
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Query the Joystick
    val = control.getAxisValue( 0 )
    print( "Joystick Axis: " + str( val ) )

    # Update the window, but not more than 60fps
    window.fill( (0,0,0) )
    pygame.display.flip()
    clock.tick_busy_loop(60)

pygame.quit()