Invalid Joystick Button

Forums:

Hi, I managed to connect the PS4 controller to the raspberry Pi. However, then the file Diddy2Claw.Py is run, I get the following error:

pi@raspberrypi:~/diddyborgv2 $ sudo python diddy2Claw.py
pygame 1.9.4.post1
Hello from the pygame community. https://www.pygame.org/contribute.html
Loading ThunderBorg on bus 1, address 15
Found ThunderBorg at 15
ThunderBorg loaded on bus 1
Loading UltraBorg on bus 1, address 36
Found UltraBorg at 36
UltraBorg loaded on bus 1
Waiting for joystick... (press CTRL+C to abort)
Joystick found
Press CTRL+C to quit

Traceback (most recent call last):
File "diddy2Claw.py", line 248, in
elif joystick.get_button(buttonMeArmDown):
pygame.error: Invalid joystick button

piborg's picture

The diddy2Claw.py script was written to work with a PS3 controller. Unfortunately the button and axis numbers are not the same for the PS3 and PS4 controllers.

You can find the typical PS4 button and axis numbers listed in our Gamepad library code here: https://github.com/piborg/Gamepad/blob/master/Controllers.py

The numbers the diddy2Claw.py script uses are listed towards the top in the Settings for the joystick section.

Thanks. But the instructions are slightly unclear. This is the code from diddy2Clay.py from the section you mentioned:

# Settings for the joystick
axisUpDown = 1 # Joystick axis to read for up / down position
axisUpDownInverted = False # Set this to True if up and down appear to be swapped
axisLeftRight = 2 # Joystick axis to read for left / right position
axisLeftRightInverted = False # Set this to True if left and right appear to be swapped
buttonSlow = 8 # Joystick button number for driving slowly whilst held (L2)
slowFactor = 0.5 # Speed to slow to when the drive slowly button is held, e.g. 0.5 would be half speed
buttonFastTurn = 9 # Joystick button number for turning fast (R2)
interval = 0.00 # Time between updates in seconds, smaller responds faster but uses more processor time
buttonMeArmClawOpen = 7 # Joystick button number to open the claw (D-Pad Left)
buttonMeArmClawClose = 5 # Joystick button number to close the claw (D-Pad Right)
buttonMeArmForward = 4 # Joystick button number to move the MeArm forwards (D-Pad Up)
buttonMeArmBackward = 6 # Joystick button number to move the MeArm backwards (D-Pad Down)
buttonMeArmUp = 12 # Joystick button number to move the MeArm upwards (Triangle)
buttonMeArmDown = 14 # Joystick button number to move the MeArm downwards (Cross)
buttonMeArmLeft = 15 # Joystick button number to move the MeArm left (Square)
buttonMeArmRight = 13 # Joystick button number to move the MeArm right (Circle)

This is from the link you posted:
class PS4(Gamepad):
fullName = 'PlayStation 4 controller'

def __init__(self, joystickNumber = 0):
Gamepad.__init__(self, joystickNumber)
self.axisNames = {
0: 'LEFT-X',
1: 'LEFT-Y',
2: 'L2',
3: 'RIGHT-X',
4: 'RIGHT-Y',
5: 'R2',
6: 'DPAD-X',
7: 'DPAD-Y'
}
self.buttonNames = {
0: 'CROSS',
1: 'CIRCLE',
2: 'TRIANGLE',
3: 'SQUARE',
4: 'L1',
5: 'R1',
6: 'L2',
7: 'R2',
8: 'SHARE',
9: 'OPTIONS',
10: 'PS',
11: 'L3',
12: 'R3'
}
self._setupReverseMaps()

I am unclear how to use the information in the new link to modify the existing part of the code. Could you please explain this in further detail? Thank you.

piborg's picture

I have highlighted the relevant sections from both bits of code in the image below.

The idea is to replace each of the axis numbers in diddy2Claw.py (circled in yellow) with one of the axis numbers listed in Controllers.py (boxed in red).

You also need to replace each of the button numbers in diddy2Claw.py (circled in blue) with one of the button numbers listed in Controllers.py (boxed in green).

For example the current code for buttonMeArmUp uses the triangle button on a PS3 controller:

buttonMeArmUp = 12                      # Joystick button number to move the MeArm upwards (Triangle)

If you want it to use triangle on the PS4 controller you need to find the button number for triangle in Controllers.py:

2:  'TRIANGLE',

and copy that number over:

buttonMeArmUp = 2                      # Joystick button number to move the MeArm upwards (PS4 Triangle)
Images: 
Subscribe to Comments for "Invalid Joystick Button"