Raspberry Pi + PS3 Controller = Major Headache

Getting the PS3 controller working was (so far) the worst part of the project! There seem to be LOADS of examples on t’internet of how to do this, but with a couple of problems:

  • They are quite old
  • They are based on the same example
  • They DON’T WORK!!! At least not with the current OS distro.

Getting the PS3 controller working was the worst part of the project!

The best example I found (that works) is here:  https://pythonhosted.org/triangula/sixaxis.html I have taken a pdf save and it’s here should it ever be deleted: Configuring Playstation 3 Controllers — Triangula 0.3 So, Top Bloke and many thanks!

The most important (and satisfying) point is the instruction:

pi@raspberrypi~$ls/dev/input

2016-08-21-102200_1400x1050_scrot

And then seeing “jso” in the response. This shows the bluetooth receiver is setup with the driver and the controller has connected.

coffee-png

I followed the example as far as the section about accessing the values from within python but it appears to be more complicated than necessary. So, other examples were found of the python code and the following was cobbled together (sorry I can’t remember where the code came from… please let me know if you know):

import pygame

# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)

# This is a simple class that will help us print to the screen
# It has nothing to do with the joysticks, just outputing the
# information.
class TextPrint:
 def __init__(self):
 self.reset()
 self.font = pygame.font.Font(None, 20)

def prt(self, screen, textString):
 textBitmap = self.font.render(textString, True, BLACK)
 screen.blit(textBitmap, [self.x, self.y])
 self.y += self.line_height
 
 def reset(self):
 self.x = 10
 self.y = 10
 self.line_height = 15
 
 def indent(self):
 self.x += 10
 
 def unindent(self):
 self.x -= 10


pygame.init()

size = [500, 800]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")

#Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# Initialize the joysticks
pygame.joystick.init()
 
# Get ready to print
textPrint = TextPrint()

# -------- Main Program Loop -----------
while done==False:
 # EVENT PROCESSING STEP
 for event in pygame.event.get(): # User did something
 if event.type == pygame.QUIT: # If user clicked close
 done=True # Flag that we are done so we exit this loop

screen.fill(WHITE)
 textPrint.reset()

joystick = pygame.joystick.Joystick(0)
 joystick.init()
 
 axes = 4 
 for i in range( axes ):
 axis = joystick.get_axis( i )
 aaa = axis + 1
 textPrint.prt(screen, "Axis {} value: {:>6.4f}".format(i, aaa) )
 textPrint.unindent()
 
 buttons = joystick.get_numbuttons()
 for i in range( buttons ):
 button = joystick.get_button( i )
 textPrint.prt(screen, "Button {:>2} value: {}".format(i,button) )
 textPrint.unindent()
 
 # Go ahead and update the screen with what we've drawn.
 pygame.display.flip()

# Limit to 20 frames per second
 clock.tick(20)
 
pygame.quit ()


When this runs (if the PS3 controller is correctly connected) the window will display the 4 values for the 2 joysticks and state for each of the buttons. I did initially use all the analogue values but with the accelerometers constantly moving it just made things too confusing and so I limited to just the 4 axis for the joysticks.

2016-08-21-102551_1400x1050_scrot

Leave a Reply

Your email address will not be published. Required fields are marked *