0

I've got this problem with Pygame that I cannot seem to find a solution too. I'm trying to use a PS4 controller to control an RC car. I've got connecting to the controller to the program and seeing how many buttons it has, but I'm unable to get the value of a pushed button in a useable form.

Basically, I want to be able to get all of the values of the buttons on the controller. When a button is pressed, the program updates the value of that button and allows me to execute code based on the value of that button.

Here's what I've got so far:

import os
import pprint
import pygame
from time import sleep

class PS4Controller(object):
    """Class representing the PS4 controller. Pretty straightforward functionality."""

controller = None
axis_data = None
button_data = None
hat_data = None

def init(self):
    """Initialize the joystick components"""

    pygame.init()
    pygame.joystick.init()
    self.controller = pygame.joystick.Joystick(0)
    self.controller.init()

def listen(self):
    """Listen for events to happen"""

    if not self.axis_data:
        self.axis_data = {}

    if not self.button_data:
        self.button_data = {}
        for i in range(self.controller.get_numbuttons()):
            self.button_data[i] = False

    if not self.hat_data:
        self.hat_data = {}
        for i in range(self.controller.get_numhats()):
            self.hat_data[i] = (0, 0)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.JOYAXISMOTION:
                self.axis_data[event.axis] = round(event.value,2)
                #print(self.axis_data)
            elif event.type == pygame.JOYBUTTONDOWN:
                self.button_data[event.button] = True
                print (self.button_data)
                print("\n")
            elif event.type == pygame.JOYBUTTONUP:
                self.button_data[event.button] = False
                #print(self.button_data)
            elif event.type == pygame.JOYHATMOTION:
                self.hat_data[event.hat] = event.value
                #print(self.hat_data)

            # Insert your code on what you would like to happen for each event here!
            # In the current setup, I have the state simply printing out to the screen.



            #os.system('clear')
            #pprint.pprint(self.button_data)
            #pprint.pprint(self.axis_data)
            #pprint.pprint(self.hat_data)


if __name__ == "__main__":
    ps4 = PS4Controller()
    ps4.init()
    ps4.listen()

Here's the output when I press the "X" button on the controller:

{0: False, 1: True, 2: False, 3: False, 4: False, 5: False, 6: False, 7: False, 8: False, 9: Falselse, 12: False, 13: False}

This is good, but I'm unable to use the data from this output when it's in this format.

Right now, if the code is run, it will output which button was pressed, but the button_data is not in a usable state. How can I run, for example:

if button3 == True:
      #execute code
 esif button4 == True:
     #execute some different code

Thanks!

Malachi Bazar
  • 1,695
  • 1
  • 19
  • 21
  • 1
    what means `button_data is not in a usable state` ? Show this data in question. Maybe there is simple method to make it usable. (BTW: I don't have controller to see it) – furas Jan 03 '17 at 05:24
  • Thank you for the input. I've updated the question. – Malachi Bazar Jan 03 '17 at 05:30
  • 1
    i don't understand your question. you mean you can't use `self.button_data[1] == True`? – gkso Jan 03 '17 at 05:35
  • What the heck was I thinking?! That's almost what I need, but that's close enough. If I use a "for i in self.button_data:" I can append all of the button values to a list and work from there! Thanks! – Malachi Bazar Jan 03 '17 at 05:39
  • Malachi, I'm borrowing your code because it works well for me. Not having used classes before, I'm wondering how I might monitor 2 controllers at the same time. Can you advise? – thescoop Sep 21 '18 at 14:05
  • Sorry mate, I used the above code to control an RC car, so I never had to use two controllers. Maybe if you make two variables in place of this variable: self.controller = pygame.joystick.Joystick(0) Something like self.controller1 = pygame.joystick.Joystick(0) and self.controller2 = pygame.joystick.Joystick(1) – Malachi Bazar Sep 21 '18 at 18:35
  • 1
    @thescoop I have the final code on my GitHub, maybe you could pull it apart and see what you can come up with: https://github.com/helloman1802/Self_Driving_RC_Car/blob/master/ps4_ctrl_car.py – Malachi Bazar Sep 21 '18 at 18:39
  • Many thanks, I'll give it a go! – thescoop Sep 22 '18 at 07:52

0 Answers0