/
rodandframe.py
141 lines (116 loc) · 3.73 KB
/
rodandframe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
"""
This script shows moving cirecles made of dots and measures the
participants' sway
"""
import viz, viztask, vizact, vizinput, vizshape
import math
import random
import oculus
import time
import datetime
from datetime import date
from string import maketrans
import itertools, csv, time
import numpy
#Ask for mode
presentations = ['oculus','normal']
presentationMode = vizinput.choose('Presentation Mode', presentations)
#Prompt for the participant's demographic
def participantInfo():
subject = vizinput.input('What is the participant number?')
age = vizinput.input('age?')
sex = ['male','female']
sex_choice = vizinput.choose('Sex',sex)
handedness = ['right','left','both']
handedness_choice = vizinput.choose('Handedness',handedness)
ts = time.time()
##create demographic data file
demographic_data = open('demographic_data_rod_frame.csv','w')
with open('data/demographic_data_rod_frame.csv', 'a') as f:
wr = csv.writer(f, delimiter=';', lineterminator='\n', quoting=csv.QUOTE_ALL)
row_demo = [subject, age, sex[sex_choice] ,handedness[handedness_choice] , datetime.datetime.fromtimestamp(ts).strftime('%Y-%d-%m %H:%M:%S')]
wr.writerow(row_demo)
return subject,datetime.datetime.fromtimestamp(ts).strftime('%Y-%d-%m %H:%M:%S')
#-----------------------------------------------------
# Set the viewpoint
if presentations[presentationMode] == 'oculus':
hmd = oculus.Rift()
if not hmd.getSensor():
sys.exit('Oculus Rift not detected')
else:
pass
viz.setMultiSample(8)
viz.go()
viz.fov(90)
def experiment():
[subj, date] = participantInfo()
def block():
directions = ['left','right','none']
random.shuffle(directions)
shuffledDirections = numpy.repeat(directions,3)
def trial(direction,trialNumber):
def createFrame(direction):
viz.startLayer(viz.LINES)
viz.vertex(-.2,1.6,1)
viz.vertex(-.2,2,1)
viz.vertex(.2,1.6,1)
viz.vertex(.2,2,1)
viz.vertex(-.2,1.6,1)
viz.vertex(.2,1.6,1)
viz.vertex(-.2,2,1)
viz.vertex(.2,2,1)
poly = viz.endLayer()
poly.visible(viz.OFF)
poly.center(0,1.8,1)
if direction == 'left':
poly.setEuler([0,0,20])
elif direction == 'right':
poly.setEuler([0,0,-20])
elif direction == 'none':
poly.remove()
return poly
def createRod():
viz.startLayer(viz.POINTS)
viz.pointSize(4)
viz.vertex(0,1.65,1)
viz.vertex(0,1.70,1)
viz.vertex(0,1.75,1)
viz.vertex(0,1.80,1)
viz.vertex(0,1.85,1)
viz.vertex(0,1.90,1)
viz.vertex(0,1.95,1)
line = viz.endLayer()
line.visible(viz.OFF)
line.center(0,1.8,1)
initLinePos = 0 + 20 * random.uniform(-1,1)
line.setEuler([0,0,initLinePos])
class LineRotation(viz.EventClass):
def __init__(self):
viz.EventClass.__init__(self)
self.callback(viz.KEYDOWN_EVENT, self.keyboardAction)
def keyboardAction(self,key):
currentPosition = line.getEuler()
currentRoll = currentPosition[2]
if viz.key.isDown(viz.KEY_LEFT):
line.setEuler([0,0,currentRoll + 1])
elif viz.key.isDown(viz.KEY_RIGHT):
line.setEuler([0,0,currentRoll -1])
LineRotation()
return line, initLinePos
frame = createFrame(direction)
[line, initLinePos] = createRod()
line.visible(viz.ON)
frame.visible(viz.ON)
yield viztask.waitKeyDown(' ')
line.visible(viz.OFF)
frame.visible(viz.OFF)
with open('data_rf/subj' + '_' + subj + '.csv', 'a') as f:
wr = csv.writer(f, delimiter=';', lineterminator='\n', quoting=csv.QUOTE_ALL)
row = [direction, str(trialNumber + 1), initLinePos, line.getEuler()[2]]
wr.writerow(row)
for t in range(0,len(shuffledDirections)):
yield viztask.waitKeyDown(' ')
viztask.schedule(trial(shuffledDirections[t],t))
viztask.schedule(block())
experiment()