5

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?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
SGolt
  • 51
  • 1
  • 5
  • Two diagnostic comments: Number of axes is 6 but you're only querying the value for 4. Also, there may be some zero-ing on the initial position when you start the script and since this isn't a game and just a script that runs instantaneously, you may want to query and print the values in a loop and see if you still get 0's in real time. – Blake O'Hare Oct 02 '16 at 19:13
  • Blake, thanks, you are right. I started to adjust the Pygame source code and compile it to get rid of the superfluous SDL_joystick.Joystick output that was printed with every call. Next, I started looking into what I did wrong in my initial while loop (which I didn't post in the above code). Problem was that I put the pygame.event.pump() function in the wrong position (outside of the while loop). Lesson learned, read the manual :) – SGolt Oct 02 '16 at 20:27
  • 2
    It'd be worth moving the update+solution into its own answer. It's perfectly fine to answer your questions, and then the answer can be voted on and marked as the solution. – Petr Nov 25 '17 at 13:02

1 Answers1

5

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.

gre_gor
  • 6,669
  • 9
  • 47
  • 52