Psychopy IO游戏手柄 [英] Psychopy IO Gamepad

查看:178
本文介绍了Psychopy IO游戏手柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows的独立PsychoPy 1.80.07中使用Builder.我试图弄清楚如何创建代码组件来接收Logitech F310游戏手柄的响应.理想情况下,我只想使用触发器.运行实验时没有收到任何错误代码(这是Stroop键盘IO演示,其中IO键盘代码组件代码替换为以下代码).看起来运行良好,但它只是显示了第一个试用版,并以为未听到游戏手柄为由等待主体回应.另外,我能够运行XInput Gamepad Coder Demo,并且一切正常.我是编码方面的新手,因此非常感谢您指出我在哪里犯了错误!

I am using Builder in standalone PsychoPy 1.80.07 in Windows. I'm trying to figure out how to create a Code Component to receive responses from a Logitech F310 Gamepad. Ideally, I would like to just use the triggers. I do not get any error code when I run the experiment (which is the Stroop keyboard IO demo with the IO keyboard code component code replaced with the below code). It seems to run fine, but it just shows the first trial and waits for a subject response as thought it isn't hearing the gamepad. Also, I am able to run the XInput Gamepad Coder Demo, and everything works just fine. I'm a novice at coding, so any help in pointing out where I've made mistakes would be much appreciated!

这是我的代码组件中的内容:

Here is what I have in my Code Component:

#Begin Experiment

try:
    from psychopy import visual, core
    from psychopy.iohub import launchHubServer, EventConstants
    from psychopy.data import getDateStr

    kwargs={'psychopy_monitor_name':'default','xinput.Gamepad':{}}
    io=launchHubServer(**kwargs)
    gamepad=io.devices.gamepad
except Exception, e:
    import sys
    print "!! Error starting ioHub: ",e," Exiting..."
    sys.exit(1)

#Begin Routine

response_event=None
trial_start=0
io.clearEvents()

#Each Frame

if frameN == 0:
    io.clearEvents('all')
    trial_start=core.getTime()
else:
    gamepadState=gamepad.getEvents()
    for anyState in gamepadState:
        if anyState in [u'left_trigger',u'right_trigger']:
            response_event=gamepadState
            continueRoutine = False 
            break

#End Routine

trials.addData("trial_start_time", trial_start)
if response_event:
    trials.addData("resp.time", response_event.time)
    trials.addData("resp.rt", response_event.time-trial_start)
    trials.addData("resp.duration", response_event.duration)
    trials.addData('resp.keys',response_event.key)
    trials.addData('resp.corr', response_event.key.lower()==corrAns)
else:
    trials.addData("resp.time",-1.0)
    trials.addData("resp.rt", -1.0)
    trials.addData("resp.duration", -1.0)
    trials.addData('resp.keys','None')
    trials.addData('resp.corr', False)

#End Experiment

io.quit()

推荐答案

如果要获取游戏手柄上左右触发器的最新值,则可以直接使用getTriggers()读取这些值,而不使用getEvents ()都没有.

If you want to get the latest value of the left and right triggers on the gamepad, you can read those values directly with getTriggers(), and not use getEvents() at all.

getTriggers()将返回一个dict,其中包含iohub的触发器的最后读取状态以及读取值的时间.

getTriggers() will return a dict containing the last read state of the triggers by iohub and the time the values were read.

例如,假设"gamepad"是一个变量,其中包含您的iohub xinput gamepad设备实例:

For example, assuming 'gamepad' is a variable holding your iohub xinput gamepad device instance:

gp_triggers = gamepad.getTriggers()
# psychopy time that the trigger values were actually read (in sec.msec)
trig_time = gp_triggers['time']
# values will be between 0.0 and 1.0. 0.0 = Not pressed at all; 1.0 = fully pressed.
left_val, right_val = gp_triggers['left_trigger'], gp_triggers['right_trigger'] 

然后在需要接受游戏手柄触发状态作为响应的条件下使用left_val和right_val.例如,如果您想在任一触发器的按下率超过50%时接受响应:

Then use left_val and right_val in a conditional that makes sense for when you want to accept the gamepad triggers state as a response. For example, if you want to accept a response when either trigger is more than 50% pressed:

if left_val > 0.5 and right_val > 0.5:
    # set some response vars like you were
    response = gp_triggers
    continueRoutine = False 
    break        

然后在代码后面,您可以再次访问response dict的值:

Then later in your code you can access the values of the response dict again:

resp_time = response['time']
left_val, right_val = response['left_trigger'], response['right_trigger'] 

这篇关于Psychopy IO游戏手柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆