1

I am currently building a robot that I would like to control with a pair of nice joysticks I bought. Up until now I've been using pygame.joystick with no problems at all with my PS4 controller. However the new joysticks have a problem. They output the correct values for like twenty or thirty seconds and function properly, but then they just freeze on a value. I can't think of any reasons they would work fine for a certain amount time and then all of a sudden just freeze.

My code is as simple as:

pygame.init()
leftjoy = pygame.Joystick.joystick(0)
leftjoy.init()
while True:
    print(leftjoy.get_axis(0))
    pygame.event.pump()

Update:

I tried this same code on Windows instead of raspberry pi and it worked perfectly. I plan on using ssh when controlling the robot so it should work well for that application. But I would still like to know how to fix this problem for testing.

  • What's the brand and model of the joysticks? – Isma Jul 13 '18 at 14:22
  • @Isma The question says *with my PS4 controller*. – iBug Jul 13 '18 at 14:29
  • 1
    The question says it works with his PS4 controller but fails with the new joysticks he bought but he doesn't specify which joysticks they are so it's a bit hard to find the problem. – Isma Jul 13 '18 at 14:30

1 Answers1

1

A couple shots in the dark, without access to your hardware...

  1. Since the PS4 controller works, I'm guessing the code and the RasPi are in good health, so the joysticks themselves might be somewhat buggy. The problem could be that the joysticks momentarily disconnect (power issue, faulty cable, just faulty hardware), and upon reconnect are assigned a new address such as 1. I don't recall that being a common problem in RasPi in general, but PyGame might have issues. Here's another post with some code that might help you detect and debug joystick disconnects in software. To much more quickly test that possibility in hardware, intentionally do a momentary disconnect-connect early, while your game is "working" and see if the same result happens.

  2. Finally, maybe the RasPi is polling too quickly and causes the joystick to freeze. To debug this, you could try putting a delay in your loop.