我在Windows中使用独立的PsychoPy 1.80.07中的Builder。我试图弄清楚如何创建一个代码组件来接收来自Logitech F310游戏手柄的响应。理想情况下,我想使用触发器。我运行实验时没有得到任何错误代码(这是Stroop键盘IO演示,IO键盘代码组件代码替换为下面的代码)。它似乎运行良好,但它只是显示第一次试验,并等待主题响应,因为它认为它没有听到游戏手柄。此外,我能够运行XInput游戏手柄编码器演示,一切正常。我是编码的新手,所以在指出我犯错误的地方有任何帮助都会非常感激!
以下是我的代码组件中的内容:
#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()
答案 0 :(得分:1)
如果你想在游戏手柄上获得左右触发器的最新值,你可以直接用getTriggers()读取这些值,而根本不使用getEvents()。
getTriggers()将返回一个dict,其中包含iohub触发器的最后读取状态以及读取值的时间。
例如,假设'游戏手柄'是一个包含你的iohub xinput游戏手柄设备实例的变量:
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%以上时接受响应:
if left_val > 0.5 and right_val > 0.5:
# set some response vars like you were
response = gp_triggers
continueRoutine = False
break
然后在您的代码中,您可以再次访问响应字典的值:
resp_time = response['time']
left_val, right_val = response['left_trigger'], response['right_trigger']