[visionegg] Re: timing of button responses in visionegg

  • From: Mason Smith <masonrs@xxxxxxxxx>
  • To: visionegg@xxxxxxxxxxxxx
  • Date: Wed, 8 Aug 2007 12:13:11 -0400

I don't know if you've already solved this problem, but here's some code to read input from a Cedrus response pad (in our case, the RB-830 model).  You need to install the pySerial package first (http://pyserial.sourceforge.net).

Mason Smith
Lab Manager
Language and Cognitive Architectures Lab
University of Michigan
Lab phone:  734-764-6827
Cell:  734-272-4883
import serial
import pygame
pygame.init()

BBOXMAPPING = {0:4,6:3,5:2,2:1,1:5,4:6,3:7,7:8}
def byteToKey(byte):
    return (BBOXMAPPING[ord(byte)/32],(ord(byte)/16)%2)

#Open a serial port at COM2 with baud rate 115200
#(or in this case, a USB port masquerading as a serial port)
ser=serial.Serial(2,115200)


#Record when we reset the bbox's timer
clock=pygame.time.Clock()
#Send a message to the bbox telling it to reset its master timer
ser.write("e1")
offset=pygame.time.get_ticks()
#Send a message to the bbox telling it to reset its RT sub-timer
ser.write("e5")
#Record how long it took to send the last message
error=pygame.time.get_ticks()-offset
print "Error: %d\n"%error

key=-1
#Loop around and collect responses until the '0' button is pressed
while key != 4:

   ser.flushInput()
   print "Waiting..."
   
   #Wait for a message from the bbox saying a button got pressed
   s=ser.read(6)
   print "Message received"
   #Record the time, according to the CPU, since the bbox's RT timer was last 
reset
   #This should equal the time from reset to the button press (the RT), PLUS 
the time
   #it took to get the message from the bbox to the computer (typically 10-20 
ms)
   elapsed=pygame.time.get_ticks()-offset

   #The first character of the message from the button box is always 'k' -- we 
can ignore it.
   #The second character of the message
   #indicates which button was pressed
   key,down=byteToKey(s[1])
   #The remaining four characters, converted to base-256 digits, indicate the 
RT.
   bboxtime=(256^3)*ord(s[5])+(256^2)*ord(s[4])+256*ord(s[3])+ord(s[2])

   #Compare the CPU's measurement to the bbox's measurement
   discrepancy=elapsed-bboxtime
   print "%d %d %d %d %d %d"%(key,down,bboxtime,elapsed,
      discrepancy,
      error)

   #Tell the bbox to reset its RT timer, and record the time we're sending the 
message,
   #and how long it takes to send the message.
   offset=pygame.time.get_ticks()
   ser.write("e5")
   error=pygame.time.get_ticks()-offset



On Aug 2, 2007, at 6:59 AM, Christophe Pallier wrote:

Hello,

On 8/2/07, alxwhite@xxxxxxxxxxxxxxxxx <alxwhite@xxxxxxxxxxxxxxxxx > wrote:
Hello visionegg experts,

I am starting an experiment programmed in Python in which I need to
precisely measure the time of a response made by the subject. We fear that
with the mouse button or keyboard button of our macintosh G4 powerbook,
timing variability will be too high. Has anyone struggled with this issue?
We have a Cedrus response pad but no idea how to integrate it with our
python program using the USB port. Any advice on that? We're also
considering using a joystick.

With pygame, you can detect when a button is pressed on a gamepad.
Small example:

### 
import sys, pygame
from pygame.locals import *

pygame.init()

size = 800, 600

screen = pygame.display.set_mode(size)

njoy = pygame.joystick.get_count()
print "Number of joysticks connected to the computer = ",njoy

for j in range(njoy):
    gamepad = pygame.joystick.Joystick(j)
    gamepad.init()
    print "Joystick #",j+1,"(", gamepad.get_name(),")"
    print " nb of buttons = ", gamepad.get_numbuttons()
    print " nb of mini joysticks = ", gamepad.get_numhats()
    print " nb of trackballs = ", gamepad.get_numballs()


while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
        if event.type == JOYBUTTONDOWN: print "clic"

pygame.quit()
##########

 

This should be an issue that many have dealt with when measuring RTs and
the like, so I hope someone out there knows the answer and that when it
comes it will be of use to the rest.


Indeed,you will find papers in Behavior Research Method Instruments and Computer about the precision of RT measure with keyboards and mouses. The accuracy depends on the maker, the drivers, the operating system, the program, the other programs running,...

In many cases, the human response time variance is much larger (e.g. ~150 ms in some detection tasks) than the variance due to errors of measurment. Therefore, you do not need millisecond precision to estimate the average RT of a human subject in an experimental condition.

Yet, if you really need/want to check the timing of your equipment, you may be interested in an apparatus like the blackbox toolkit (http://www.blackboxtoolkit.co.uk/ ).


Christophe


--
Christophe Pallier (http://www.pallier.org)

Other related posts: