psychopy.org | Reference | Downloads | Github

Joysticks do not respond to events in Mac OS X El Capitan

I hope someone can help us. I’ve written some PsychoPy code for a graduate psychology student who needs the code for her dissertation research. We chose to get everything working with simple keystrokes before adding joysticks in the final steps. The PsychoPy documentation describes how to use the joysticks, but as far as I can tell, PsychoPy code does not respond to events. It recognizes the joystick brand, number of buttons, degrees of freedom, but doesn’t respond to events.

Here’s our test code, running with Python 2.7.11, Mac OS X 10.11.1. If anyone can give us a simple solution to make the code respond to joystick events, we will be extremely grateful.

```
from psychopy import visual, core  # import some libraries from PsychoPy
from psychopy import event
from psychopy import data
from psychopy.tools.filetools import fromFile
import random
import glob, sys, os
from PIL import Image
from datetime import timedelta
from psychopy.hardware import joystick
import pyglet




#===========================================================================
# Test Joystick
#===========================================================================
joystick.backend='pyglet'  # must match the Window
joysticks = pyglet.input.get_joysticks()
mywin = visual.Window([800,600], monitor="testMonitor", units="deg", winType='pyglet')  #create a window

print "devices: ", pyglet.input.get_devices()

print "len of pyglet joysticks = ", len(joysticks)
if joysticks:
    joy = joysticks[0]
joy.open(mywin)
joy.backend = 'pyglet' #must match the Window
print "name: ", joy.device
print


text  = "  TESTING JOYSTICKS \n"
message = visual.TextStim(mywin, alignHoriz='center', alignVert='center', height=0.5, text=text)
message.setAutoDraw(True)  # automatically draw every frame

event.clearEvents()

mywin.flip()

#
#    To use a joystick, first call `open`, then in your game loop examine
#    the values of `x`, `y`, and so on.  These values are normalized to the
#    range [-1.0, 1.0].
#
#    To receive events when the value of an axis changes, attach an
#    on_joyaxis_motion event handler to the joystick.  The `Joystick` instance,
#    axis name, and current value are passed as parameters to this event.
#
#    To handle button events, you should attach on_joybutton_press and
#    on_joy_button_release event handlers to the joystick.  Both the `Joystick`
#    instance and the index of the changed button are passed as parameters to
#    these events.

while True:
    core.wait(2.0)
    buttons = joy.buttons
    mywin.flip()
    if joy.on_joybutton_press(joy,0):
        print "button pressed!!"
        exit()

    print "button values: ", buttons
    print "controller 1: x, y axes: ", joy.x, joy.y
    print "controller 2: x, y axes: ", joy.z, joy.rz
    print "-----"

    if (joy.buttons[0]==True or joy.buttons[1]==True or event.getKeys('q')):
        break
    #mywin.flip()
```


The loop runs forever without any of the values changing.

Thanks.

Hi, I just jumped in and edited your original post to properly format code. Hopefully it is now correct? Have deleted other posts.

From a pure code point of view, one issue could be the core.wait(2.0) inside your loop. That actually means that the check for button presses is only happening once every two seconds. I’m not sure how this works for presses that aren’t occurring at that exact moment (i.e. should it detect a previous key press in a buffer, or is it purely looking for something happening instantaneously?)

But although I’m not familiar with using joysticks (and don’t have one to hand to test with), the code here does not seem to be based on the PsychoPy API:
http://www.psychopy.org/api/hardware/joystick.html

What happens when you run the built-in joystick demo (i.e. from the Coder window in PsychoPy, go to the Demos menu and select input -> joystick_universal.py)? See if that works for you, and if so, adapt it to your needs.

Indeed joysticks under OSX using pyglet dont seem to be working. it used to be that you had to push events for the devices butthen they seemed to remove that as an option and I haven’t found out what the new approved way was to handle joysticks (without using the pyglet App class to handle all events because we don’t want that).

This has been hampered by:

  • me not having time
  • me not having a joystick to test on

If you work out how to get events pushed that would be great. If not, we might one day look into switching away from pyglet (e.g. to use https://github.com/FlorianRhiem/pyGLFW instead)

Hi Jon,

Thanks very much for the reply. I would gladly and be happy to easily fix your lack of a joystick for testing, but I doubt I can fix your lack of time!

It seems that PsychoPy really needs to update its documentation. We adopted PsychoPy specifically for experiments with joysticks on Macs. If this isn’t working, the docs are seriously misleading. This will set my student back months on her dissertation research, which is really unfortunate.

Regardless, I truly appreciate your quick response. We can now move to other solutions with a better understanding the issues.

Best regards,

Bill

Shucks. Always the same! :wink:

It seems that PsychoPy really needs to update its documentation. We adopted PsychoPy specifically for experiments with joysticks on Macs. If this isn’t working, the docs are seriously misleading.

Apologies. Docs do get out of date I’m afraid. Probably the whole site needs a thorough check :frowning: (problem #1 again)

This will set my student back months on her dissertation research, which is really unfortunate.

Well, hold fire, I’m not sure it should cost months of work to make this happen! In fact I think you should be able to use the pygame joystick even though the pyglet one is not currently working. Although we don’t use pygame any more for creating windows and getting keypresses (which are associated with a window), a joystick doesn’t need to be associated with the windowing backendas far as I know. So I think it should be entirely possible to create your psychopy window and still run commands like

from pygame import joystick
joystick.init()
joy1= joystick.Joystick(i)
joy1.init()

nAxes = joy. get_numaxes()
nButtons = joy.get_numbuttons()

# make some measeurements
for i in range( nAxes ):
    axis = joystick.get_axis( i )
    print(axis)   # value on this joystick axis
for i in range( nButtons ):
    button = joystick.get_button( i )
    print(button)  # value of this button

The above is untested (problem #2). I’ve just grabbed it from the pygame documentation

http://www.pygame.org/docs/ref/joystick.html

Hope it helps
Jon

1 Like

I’m also sorry to hear that parts of the documentation were apparently misleading here :frowning: Unfortunately many different components of PsychoPy require lots of work and constant overhaul, yet time of all contributors is limited. So naturally, a higher priority is assigned to urgent issues; and typically, docs are rather far down on the priorities list (this is, by the way, a problem of many Free Software projects, as long as nobody gets payed to actually do those more or less boring tasks).

I personally think it’s a bit of a pity that, although thousands of researchers world-wide are actively using PsychoPy (and don’t have to purchase Matlab licenses for their data acquisition/stimulus presentation anymore!!), it appears to be extremely difficult to acquire funding for the development of this (or any?) software. I’ve talked to several professors and inquired about funding opportunities, because I could really imagine working on PsychoPy (or any other science-related Free Software project) exclusively for a year or so: some things (e.g., staircase issues) have been on my to-do list for over a year now, I just never had the time to actually implement proper solutions, because I’m mostly busy in the lab myself, or supervising students. At the same time, I feel there’s a great need for certain improvements, or implementations of new features, esp. with the growing user base and all. The responses I got were basically that improving PsychoPy during the ordinary working hours would only be supported and tolerated if the lab/my personal projects would directly benefit from those contributions. Which would pretty much force me to continue working on most parts of PsychoPy in my spare time, where coding would compete with other recreational activities that are far more important to me, e.g., meeting up with friends, reading, exploring nature, or various sporting activities.

I’m afraid as long as there is no real paid (core) development team, or some sort of commercial support or the like, issues like the one you’re experiencing now will continue to happen in the future. But this is just my very personal opinion. :slight_smile:

1 Like

Linking a thread where I have been going through similar problems. In the last post I have a workaround, I wouldn’t call it a good one but it might work if you need this run urgently.

http://discourse.psychopy.org/t/joystick-refresh-rate-catch-22?source_topic_id=1351
1 Like

Addendum: I’ve been going through the docs and I can’t even see where they are misleading. Joystick does not appear in the PsychoPy Features list nor in the API as far as I can tell.

Addendum2: OK, I have found it in the hardware section of the API (not the top-level). I’ll put a flag at the top to say it isn’t working

1 Like