Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame Joystick.get_axis() always returns zero

Tags:

I bought a Logitech Gamepad F310 to remotely control robotics. I am using Pygame (version 1.9.1, Python version 2.7.11+) on Linux Mint (release 18, Sarah).

As a simple test to check the joystick's functionality, I wrote this short script that outputs the values for the different axes:

import pygame

pygame.display.init()
pygame.joystick.init()
pygame.joystick.Joystick(0).init()

# Get the name of the joystick and print it
JoyName = pygame.joystick.Joystick(0).get_name()
print "Name of the joystick:"
print JoyName

# Get the number of axes
JoyAx = pygame.joystick.Joystick(0).get_numaxes()
print "Number of axis:"
print JoyAx

# Print the values for the axes
pygame.event.pump()
print pygame.joystick.Joystick(0).get_axis(0)
print pygame.joystick.Joystick(0).get_axis(1)
print pygame.joystick.Joystick(0).get_axis(2)
print pygame.joystick.Joystick(0).get_axis(3)

Regardless of the position the axes are in when I run the script, I always get the following output:

Name of the joystick:
Logitech Gamepad F310
Number of axis:
6
SDL_JoystickGetAxis value:0:
0.0
SDL_JoystickGetAxis value:0:
0.0
SDL_JoystickGetAxis value:0:
0.0
SDL_JoystickGetAxis value:0:
0.0

I understand that the output "SDL_joystick.Joystick(0).getAxis value" is due to the fact that the developers kept the debug flag when compiling the source code. But I don't understand why the script returns 0 for all axis values while they're not in the 0 position.

I have tested the joystick with the jstest-gtk package. Using this package, it works smoothly. Is there an error in my code?

I ran the code in windows, there it works smoothly. Any idea why it doesn't work under the Linux distro?

like image 960
SGolt Avatar asked Oct 02 '16 13:10

SGolt


1 Answers

Extracted from an edit to the question.

In the initial code (not posted in the above) I had a while loop but I made the rookie mistake of putting the pygame.event.pump() line outside of the while loop. The code I have now to learn how to work with this module works perfect.

import pygame

pygame.display.init()
pygame.joystick.init()
pygame.joystick.Joystick(0).init()

# Prints the joystick's name
JoyName = pygame.joystick.Joystick(0).get_name()
print "Name of the joystick:"
print JoyName
# Gets the number of axes
JoyAx = pygame.joystick.Joystick(0).get_numaxes()
print "Number of axis:"
print JoyAx

# Prints the values for axis0
while True:
        pygame.event.pump()
        print pygame.joystick.Joystick(0).get_axis(0)

I did have to spend some time to adjust and recompile the joystick.c source code to get rid of the "SDL_joystick.Joystick(0).getAxis value:0:" print-outs. Apparently the issue has been known for some years now but not fixed for Ubuntu/Mint.

like image 162
gre_gor Avatar answered Sep 24 '22 16:09

gre_gor