View Single Post
  #10  
Old 10-22-2008, 05:51 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
I modified your script to randomly loom a new cube every iteration, and to handle the case if no reaction was registered for a certain amount of time. I also place the cube back to the original location once the reaction is entered.

Code:
import viz
import vizact
import viztask
import random

# Time limit for keyboard reaction
REACTION_TIME_LIMIT = 5.0

# Sets the speed of the rotation for all 9 cubes
ROTATE_SPEED = 50

viz.go()
viz.mouse(0)
viz.cursor(viz.OFF)

# Sets viewpoint at z meters away
viz.move(0,0,-10)

# Asks for the participant number
subject = viz.input('Enter the participant number?')

# List of cubes
cubes = []
for row in [0,2,4]:
	for col in [0,-3,3]:
		pos = (col,row,0)
		cube = viz.add('box.wrl',cache=viz.CACHE_CLONE,pos=pos)
		cube.originalPos = pos
		cubes.append(cube)

# Function that will update cube rotation every frame
def RotateCubes():
	increment = ROTATE_SPEED * viz.elapsed()
	for cube in cubes:
		cube.setAxisAngle([0, 5, 20, increment],viz.REL_PARENT)
	
vizact.ontimer(0,RotateCubes)

# Opens file 'response.txt' in write mode
file = open('reaction_time', 'a')

# Define a function that records reaction times in response to a looming cube
def SaveData():
	#Data for getting values from wait conditions
	d = viz.Data()
	while True:
		
		#Wait 2 seconds
		yield viztask.waitTime(2)
		
		# Randomly loom a cube
		randomCube = random.choice(cubes)
		backAndFourth = vizact.sequence([vizact.goto(0, 0, -1, 1), vizact.goto(randomCube.originalPos, 1)])
		randomCube.addAction(backAndFourth)
		
		#Wait for next frame to be drawn to screen and save display time
		yield viztask.waitDraw(d)
		displayTime = d.time
		
		#Wait for keyboard reaction or timeout
		waitKey = viztask.waitKeyDown(None,d)
		waitTimeout = viztask.waitTime(REACTION_TIME_LIMIT)
		
		yield viztask.waitAny([waitKey,waitTimeout],d)
		
		if d.condition is waitKey:
			
			reactionTime = d.time - displayTime
			
			# Create the output string
			out = str(reactionTime) + '\t''\n' 
			
			# Write the string to the output file
			file.write(out)
			print 'Reaction time:',reactionTime
			
		else:
			
			print 'Reaction timeout exceeded'

		# Restore cube to original location and stop looming action
		randomCube.setPosition(randomCube.originalPos)
		randomCube.clearActions()
		
# Defines a callback for functions
viztask.schedule(SaveData())
Reply With Quote