Example #1
0
def raisestop(sign):
	print("stop flag raised")
	endflag.set()
	t1.join()
	t2.join()
#	t4.join()
	viz.quit()
Example #2
0
def raisestop(biggle):
	global stopevent1
	global stopevent2
	
	stopevent1.set()
	stopevent2.set()
	viz.quit()
def raisestop(sign):
	#the sign passed in doesn't do anything, I just didn't know how to make this work without passing something in...
	print("stop flag raised")
	endflag.set()
	t1.join(5)
	t2.join(5)
	t4.join(5)
	viz.quit()
Example #4
0
def raisestop(sign):
    #the sign passed in doesn't do anything, I just didn't know how to make this work without passing something in...
    print("stop flag raised")
    endflag.set()
    t1.join()
    t2.join()
    t3.join()
    t4.join()
    viz.quit()
Example #5
0
def experiment():
    for trialN in range(10):
        if trialN < 9:
            datafile = 'Trial0' + str(trialN + 1) + '.txt'
            respfile = 'RT_Trial0' + str(trialN + 1) + time.strftime(
                "_%d-%b-%y_%H-%M") + '.csv'
        else:
            datafile = 'Trial' + str(trialN + 1) + '.txt'
            respfile = 'RT_Trial' + str(trialN + 1) + time.strftime(
                "_%d-%b-%y_%H-%M") + '.csv'

        colors = []
        with open(datafile, 'rb') as csvfile:
            csvreader = csv.reader(csvfile, delimiter=' ')
            for row in csvreader:
                colors.append(row)
        colors = numpy.asarray(colors, dtype=uInt8)
        # colors = colors[0]

        strobes = Strobes(x, 100)
        leds = LEDs(colors, 0.05, 0.9403)
        vizact.onkeydown('.', leds.recordResp)

        sensor_Target = vizproximity.Sensor(
            vizproximity.RectangleArea([4, 0.1],
                                       center=[0, real_tg_dist[(-1)**trialN]]),
            None)
        manager.addSensor(sensor_Target)

        # Show where the target is and ask them to face to the target
        yield viztask.waitKeyDown('a')
        standBy = viztask.schedule(leds.standby(trialN % 2))
        # Turn of the LED to get the experiment ready to start
        yield viztask.waitKeyDown('s')
        standBy.kill()
        leds.all_off()
        leds.stop()
        monitor_record_Network.send('Start!')
        strobes.StartTask()
        f = viztask.schedule(leds.flashing())
        monitor_record_Network.send('Start!')
        yield vizproximity.waitEnter(
            sensor_Target)  # flash until they enter the target zone
        f.kill()
        # Once reach the target turn off both the LEDs and strobes
        strobes.StopTask()
        strobes.ClearTask()
        leds.all_off()
        leds.stop()
        leds.clear()
        # Save the data into a csv file
        leds.saveAsFile(respfile)
        manager.clearSensors()

    yield viztask.waitTime(10)
    viz.quit()
Example #6
0
def EXPERIMENT(ITI,runs):

	for r in range(runs):
		runNum = r+1
		context = ContextGen(runNum)
		yield StartRun(runNum,1,0)
		yield Replace()

	yield StartRun(runNum,0,1)
	viz.quit()
def CloseConnections(EYETRACKING):
	
	"""Shuts down EYETRACKING and wheel threads then quits viz"""		
	
	print ("Closing connections")
	if EYETRACKING: 
	 	comms.stop_trial() #closes recording			
	
	#kill automation
	viz.quit()
Example #8
0
def luigiStatusCheck():
    a = True
    while luigiact.alive():
        if luigi.getAnimationTime(4) >= 12 and a == True:
            import vizfx.postprocess
            from vizfx.postprocess.blur import FragmentBlurEffect
            effect = FragmentBlurEffect(distance=4)
            vizfx.postprocess.addEffect(effect)
            a = False
        elif luigi.getAnimationTime(4) >= 18:
            viz.quit()
Example #9
0
def MainTask():
	"""Top level task that controls the game"""
	# Display instructions and wait for key press to continue
	yield DisplayInstructionsTask()

	# Create panel to display trial results
	resultPanel = vizinfo.InfoPanel('',align=viz.ALIGN_CENTER,fontSize=25,icon=False,key=None)
	resultPanel.visible(False)

	while True:

		# Randomly choose hiding spots from list
		locations = random.sample(HIDING_SPOTS,TRIAL_COUNT)

		# Reset score
		score = 0
		UpdateScore(score)

		# Go through each position
		for pos in locations:

			# Perform a trial
			found = yield TrialTask(pos)

			# Update score and display status text
			if found:
				viz.playSound('sounds/pigeon_catch.wav')
				score += 1
				UpdateScore(score)
				tracker.runAction(vizact.spinTo(point=pos,time=0.8,interpolate=vizact.easeOutStrong))
				resultPanel.setText(TRIAL_SUCCESS)
			else:
				viz.playSound('sounds/pigeon_fly.wav')
				viztask.schedule(FadeToGrayTask())
				resultPanel.setText(TRIAL_FAIL)

			#Display success/failure message
			resultPanel.visible(True)

			# Add delay before starting next trial
			yield viztask.waitTime(TRIAL_DELAY)
			resultPanel.visible(False)

			# Disable gray effect
			gray_effect.setEnabled(False)
			viz.quit()


		#Display results and ask to quit or play again
		resultPanel.setText(RESULTS.format(score,TRIAL_COUNT))
		resultPanel.visible(True)
		yield viztask.waitKeyDown(' ')
		resultPanel.visible(False)
Example #10
0
    def onKeyDown(self, key):
        if self.win or self.lose:
            viz.quit()

        if not self.picking:
            if (key == viz.KEY_UP):
                #increase velocity
                self.scaleValue += .1
            elif (key == viz.KEY_DOWN):
                #decrease velocity
                self.scaleValue = self.scaleValue - .1 if self.scaleValue > 2 else 2
            elif key == 's':
                self.pitch += 5
                #incorporate +5 degree more pitch
                v = self.rotmatrix.preMultVec([1, 0, 0])
                self.rotmatrix.postAxisAngle(
                    v[0], v[1], v[2], self.rotateValue
                    if self.model.id == self.xWing.id else -self.rotateValue)
            elif key == 'w':
                self.pitch -= 5
                #incorporate -5 degree more pitch
                v = self.rotmatrix.preMultVec([1, 0, 0])
                self.rotmatrix.postAxisAngle(
                    v[0], v[1], v[2], -self.rotateValue
                    if self.model.id == self.xWing.id else self.rotateValue)
            elif key == 'd':
                self.roll -= 5
                #incorporate -5 degree more roll
                v = self.rotmatrix.preMultVec([0, 0, 1])
                self.rotmatrix.postAxisAngle(
                    v[0], v[1], v[2], -self.rotateValue
                    if self.model.id == self.xWing.id else self.rotateValue)
            elif key == 'a':
                self.roll += 5
                #incorporate +5 degree more roll
                v = self.rotmatrix.preMultVec([0, 0, 1])
                self.rotmatrix.postAxisAngle(
                    v[0], v[1], v[2], self.rotateValue
                    if self.model.id == self.xWing.id else -self.rotateValue)
            elif key == 'q':
                self.yaw -= 5
                #incorporate -5 degree more yaw
                v = self.rotmatrix.preMultVec([0, 1, 0])
                self.rotmatrix.postAxisAngle(v[0], v[1], v[2],
                                             -self.rotateValue)
            elif key == 'e':
                self.yaw += 5
                #incorporate +5 degree more yaw
                v = self.rotmatrix.preMultVec([0, 1, 0])
                self.rotmatrix.postAxisAngle(v[0], v[1], v[2],
                                             self.rotateValue)

            self.setOrientation()
Example #11
0
def experiment():
    yield viztask.waitNetwork(serverName)
    # the first baseline
    yield baseline(1)

    # following offset and baseline conditions
    yield testing()
    # finish the experiment
    info.setText('End of the Experiment.')
    info.visible(viz.ON)
    yield viztask.waitTime(45)
    viz.quit()
	def onButton(self,obj,state):
		if obj==self.myButton:
			if state == viz.DOWN:
				self.continu=True
				#print continu
				self.done.send(data=self.continu)
				
				
		elif obj==self.myquit:
			self.continu=False
			viz.quit()
			self.done.send(data=self.continu)
			
 def Next(self, train):
     print "Give Directions: ", self.runnum
     if self.dialog.filename:
         train.log("# Direction confidence following " +
                   self.dialog.filename)
         train.log(getEvaluation(1, 1))
     if len(self.directions) == 0:
         tkMessageBox.showinfo(
             "Finished!", "Finished with this part of the experiment.")
         viz.quit()
     else:
         self.popDirection(train)
         train.onFinish = self.Dialog
     self.runnum += 1
Example #14
0
    def CloseConnections(self):
        """Shuts down EYETRACKING and wheel threads then quits viz"""

        print("Closing connections")
        if self.EYETRACKING:
            self.comms.stop_trial()  #closes recording
            #self.pupil_killer()

        #kill automation
        if self.AUTOWHEEL:
            self.Wheel.thread_kill(
            )  #This one is mission critical - else the thread will keep going
            self.Wheel.shutdown()

        viz.quit()
 def Next(self, train):
     print "Direction set ", self.runnum
     if self.runnum > 0:
         if self.dialog.filename:
             train.log("# Direction confidence giving " +
                       self.dialog.filename)
         train.log(getEvaluation(1, 1))
     if len(self.QuizList) == 0:
         tkMessageBox.showinfo(
             "Finished!", "Finished with this part of the experiment.")
         viz.quit()
     else:
         (self.startPos, self.endPos) = self.QuizList.pop()
         train.MoveToNext(self.startPos)
         train.onFinish = self.Dialog
     self.runnum += 1
Example #16
0
def experiment():
    yield viztask.waitNetwork(serverName)

    yield Baseline(1)

    if Exp_Sequence == 'RP':
        yield Direction_Exp(1)
        yield Distance_Exp(4)
    else:
        yield Distance_Exp(1)
        yield Direction_Exp(3)

    info.setText('End of the Experiment.')
    info.visible(viz.ON)
    yield viztask.waitTime(60)
    viz.quit()
Example #17
0
def runtrials():

    for i, trialtype in enumerate(TRIALSEQ):

        print("Trial: ", str(i))
        print("TrialType: ", str(i))

        trial_targetoccurence_prob = ConditionList_targetoccurence_prob[
            trialtype]  #set occurence parameter for the trial.
        trial_targetnumber = ConditionList_targetnumber[
            trialtype]  #set target number for the trial.

        print(str([trial_targetoccurence_prob, trial_targetnumber]))

        Distractor.StartTrial(trial_targetoccurence_prob,
                              trial_targetnumber,
                              trialn=i)  #starts trial

        print("Called Start Trial, now waiting")

        #yield viztask.waitTime(TotalTrialTime+.5) #this should always wait a little longer than the TrialTime, allowing the EndOfTrial function to get called in Count_Adjustable.

        def MonitorDistactor():
            """will return true if it is the end of trial"""
            EoTFlag = Distractor.getFlag()  # True if it's the end of the trial
            return (EoTFlag)

        yield viztask.waitTrue(MonitorDistactor)

        ###interface with End of Trial Screen
        pressed = 0
        while pressed < trial_targetnumber:

            #keep looking for gearpad presses until pressed reaches trial_targetnumber
            print("waiting for gear press")
            d = yield viztask.waitAny([waitButton1, waitButton2])
            pressed += 1
            print('pressed ' + str(pressed))
            Distractor.gearpaddown(d.condition)  #call gearpaddown.

            yield viztask.waitTime(.5)
            #Distractor.EoTScreen_Visibility(viz.OFF)
        Distractor.RecordCounts()
        #Finished = True
    else:

        viz.quit()  ##otherwise keeps writting data onto last file untill ESC
def sensorEnterEnd(sensor):
    print("Enter end sensor")
    global appendagesItterator
    global start
    global timeEnd
    global timeA
    global trialTimeList
    global leftEarly
    global enterSensorCount

    enterSensorCount[7] = enterSensorCount[7] + 1

    if (
            manager.getActiveTargets() == manager.getActiveTargets(
                sensor=endSensor)
    ):  # Used to provent potential issues when trying to record data. If a tracker is inside of another sensor then there could be time or count issues.
        if (start == True
            ):  # used to protect from doubling the effects of the sensor
            print("End sensor only active")
            print(len(appendages))

            # End timers
            timeEnd = time.clock() - timeA
            print(timeEnd)

            # Add to the list
            trialTimeList.append(timeEnd)
            trialTimeList.append(leftEarly)

            print(trialTimeList)

            #~ we can either print to a text file here, or we can store the variables for later.
            #print to text = trialTimeList
            #print to text = enterSensorCount

            del trialTimeList[:]  # Restart list for use in next trial

            if (len(appendages) > 1):
                start = False

            else:
                viz.quit()

    else:
        print("Please check appendages")
	def tester2(self,name):
		
		print "*****Looking around*****"
		viz.waitTime(3)
		if None is self.first:
			self.first = "smith"
		print "\nAgent SMITH: I WILL KILL YOU ALLLLLL!!!\n"
		viz.waitTime(2)
		if(False == self.yo):
			self.flag = True
			viz.waitTime(2)
			viz.quit()
		else:
			viz.waitTime(2)
			print "Agent SMITH: Wait waaa!!"
			viz.waitTime(2)
			print "*****NEO beats up Agent SMITH and destroys him*****"
			viz.waitTime(2)
			print "Agent SMITH: Damn you NEOOOOO!!\nAgent SMITH: NOW Fall Agent SMITH"
Example #20
0
    def experimentProcedure(self):
        self.response = None
        self.STARTTIME = viz.tick()
        self.STATE = 'Start Vection'

        #Define the Vection
        def motion():
            # Create Fixation Dot
            viz.startLayer(viz.POINTS)
            viz.pointSize(self.POINTSIZE)
            viz.vertexColor(viz.GRAY)
            viz.vertex(0, 1.8, 4)
            points = viz.endLayer()
            points.disable(viz.CULLING)
            # Create the circles
            sphere = self.createCircles(26, 1, 30, -26)
            sphere2 = self.createCircles(22, 0.8, 30, -26)
            sphere3 = self.createCircles(18, 0.6, 30, -26)
            sphere4 = self.createCircles(14, 0.4, 30, -26)
            sphere5 = self.createCircles(10, 0.2, 30, -26)
            viz.MainView.move([0, 0, 3])

            def keydown(key):

                if key == 'f':
                    self.response = 'self motion'
                    self.keyPressTime = viz.tick()  # get time for keypress
                    print(self.response, self.keyPressTime)
                    self.STATE = 'State - self Motion'

                if key == 'j':
                    self.response = 'object motion'
                    self.keyPressTime = viz.tick()  # get time for keypress
                    print(self.response, self.keyPressTime)
                    self.STATE = 'State - Object Motion'

            viz.callback(viz.KEYDOWN_EVENT, keydown)

        # A function to record thfe responses
        yield motion()

        yield viztask.waitTime(10)
        viz.quit()
Example #21
0
	def __init__(self, xx = None, yy = None, zz = None):
		if xx != None and yy == None and zz == None:
			#xx is a vector, we create a new vector passing
			#xx values as arguments
			self.x = xx.x
			self.y = xx.y
			self.z = xx.z
		elif xx != None and yy != None and zz == None:
			#two points to create a vector from xx to yy
			self.x = yy.x-xx.x
			self.y = yy.y-xx.y
			self.z = yy.z-xx.z
		elif xx != None and yy != None and zz != None:
			self.x = xx
			self.y = yy
			self.z = zz
		else:
			print "Vector Creation Error!"
			viz.quit()
def onbutton(obj,state):
    #Use our starting time variable to find out how much
    #time has elapsed.
    elapsed_time = viz.tick() - start_time
    
    #Create string lines to put in the data file, depending on which
    #button was pushed.
    if obj == yes_button:
        data = 'Subject ' + str(subject) + ' saw a pigeon.\t'
    if obj == no_button:
        data = 'Subject ' + str(subject) + ' did not see a pigeon.\t'
    
    #add elapsed time to data
    data = data + 'Elapsed time was: ' + str(round(elapsed_time,2)) + ' seconds\n'
    
    #Write the data to our file. 
    question_data.write(data)
    #Flush the internal buffer.
    question_data.flush()
    #Close the world.
    viz.quit()
Example #23
0
def experiment():
	# -- Launch the vizinfo panel
	global subj, trigger
	subj = CrossSubject()
	yield subj.grab_info()
	
	# -- Where will the trigger pulses be coming from?
	yield get_trigger()
	
	# -- Load the timings for this experiment
	subj.get_experiment()
	
	# -- Start the experiment, waiting for a trigger
	for nblock,block in enumerate(subj.blocks):
		blockdata = yield cross_block(block,training=subj.is_training)
		subj.add_block_data(nblock,blockdata)
	
	# -- write the data we just collected to text
	subj.write_data()
	
	# -- Close the program
	viz.quit()
Example #24
0
def Instruction(objName,ITI,endMessage):

		viz.MainWindow.setScene(4)

		if endMessage!=1:
			collectMessage = 'Replace ' + objName
			#info
			info = viz.addText(collectMessage,viz.SCREEN,scene=4)
			info.fontSize(36)
			info.color(viz.WHITE)
			info.setPosition([0.4,0.5,0])
			info.visible(1)

			#wait for iti
			yield viztask.waitTime(ITI)
			yield viztask.waitTime(numpy.random.randint(4)) #add random jitter, eventually add a wait for t here as well
			info.visible(0)
		else:
			error = objName
			threshold = [3,5,7,9]
			if error < threshold[0]:
				feedback = 1
			elif error > threshold[0] and error < threshold[1]:
				feedback = 2
			elif error > threshold[1] and error < threshold[2]:
				feedback = 3
			elif error > threshold[2] and error < threshold[3]:
				feedback = 4
			elif error > threshold[3]:
				feedback = 5
			smile = viz.add('smile' + str(feedback) + '.tif')
			feed = viz.addTexQuad(parent=viz.SCREEN,scene=4,size=[500,400])
			feed.setPosition([0.5, 0.5, 0]) #put quad in view 
			feed.texture(smile)
			yield viztask.waitTime(ITI+2)
			viz.quit()
Example #25
0
def raisestop(biggle):
	global endflag
	endflag.set()
	viz.quit()
Example #26
0
def CloseConnections(wheel):
	
	wheel.thread_kill()
	wheel.shutdown()
	viz.quit()
Example #27
0
def raiseandquit(warn):
	
	print(warn.upper())
	viz.quit()
Example #28
0
##OPEN DATA FILE ##
###################

global runNum
subject = vizinput.input('What is the sub number?') 
runNum = vizinput.input('What is the run rumber?') 
dpath = '..\Data\TestingData\\'
runNum = int(runNum)
runLength = 8.075*60 #in seconds

fname = dpath + 'TEST_tracking_'+str(subject)+'_'+ str(runNum)+'.txt'

###check for existing data file <-uncomment for actual testing, along with input function above
if os.path.isfile(fname):
	print('file name already exists')
	viz.quit()

#open data file
tracking_data = open(fname, 'a')



###############
## VARIABLES ##
###############

#tuple of test objects
TestObjects = ['cone', 'beachball', 'plant','pumpkin']
TestObjectLocs = [[(-4,0.07,9),(2,0.29,5),(-7,0.07,-4),(10,0.05,-5)] , [(4,0.07,9),(-2,0.29,5),(7,0.07,-4),(-10,0.05,-5)]]
scaleFactors = [0.15,1.8,1.25,1.8]
Example #29
0
def raisestop(sign):
	print("stop flag raised")
	endflag.set()
	t1.join()
	t2.join()
	viz.quit()
Example #30
0
def raisestop(biggle):
    global endflag
    endflag.set()
    viz.quit()
Example #31
0
	def onTimer(self, num):
		# Main game loop
		if num == 1:
			# Check for balls colliding with the window borders
			for i in range(0, len(self.ballList)):
				if self.ballList[i].x + self.ballList[i].radius >= 100:
					self.ballList[i].vx = abs(self.ballList[i].vx) * -1
					if not self.mute:
						viz.playSound(self.pickSound(1))
				elif self.ballList[i].x - self.ballList[i].radius <= -100:
					self.ballList[i].vx = abs(self.ballList[i].vx)
					if not self.mute:
						viz.playSound(self.pickSound(1))
				elif self.ballList[i].y + self.ballList[i].radius >= 100:
					self.ballList[i].vy = abs(self.ballList[i].vy) * -1
					if not self.mute:
						viz.playSound(self.pickSound(1))
				elif self.ballList[i].y <= -100 - self.ballList[i].radius:
					self.ballList.pop(i)
					self.ballsOnScreen -= 1
					# Shift all the blocks on screen down
					if self.ballsOnScreen == 0:
						for i in range (0, len(self.blockList)):
							localRow = self.blockList[i].getRow()
							localCol = self.blockList[i].getCol()
							self.blockPositions[localRow][localCol] = 0
							self.blockList[i].shiftDown()
							self.blockPositions[localRow + 1][localCol] = 1
							
							# Check if a block has reached the bottom of the window
							for z in range(0, len(self.blockList)):
								if self.blockList[z].getRow() == 9:
									self.failState = True
									# Prompt for restart or quit
									break
									print("You lost!")
									
						# Give the player another ball if they broke twice the amount of blocks as their number of balls
						if self.contactCount >= self.clickCount * 2:
							self.clickCount += 1
						self.contactCount = 0
						if not self.failState:
							self.addBlocks()
							self.roundCount += 1
					break
				# Check for balls colliding with blocks
				else:
					contact = False
					for j in range(0, len(self.blockList)):
						if self.blockList[j].contains(self.ballList[i].getX() + self.ballList[i].radius, self.ballList[i].getY()):
							self.ballList[i].vx = abs(self.ballList[i].vx) * -1
							contact = True
						elif self.blockList[j].contains(self.ballList[i].getX() - self.ballList[i].radius, self.ballList[i].getY()):
							self.ballList[i].vx = abs(self.ballList[i].vx)
							contact = True
						elif self.blockList[j].contains(self.ballList[i].getX(), self.ballList[i].getY() + self.ballList[i].radius):
							self.ballList[i].vy = abs(self.ballList[i].vy) * -1
							contact = True
						elif self.blockList[j].contains(self.ballList[i].getX(), self.ballList[i].getY() - self.ballList[i].radius):
							self.ballList[i].vy = abs(self.ballList[i].vy)
							contact = True
						
						# Remove block from position and block lists if hit
						if contact:
							self.contactCount += 1
							self.blockPositions[self.blockList[j].getRow()][self.blockList[j].getCol()] = 0
							self.blockList[j].vertices.remove()
							self.blockList.pop(j)
							viz.playSound(self.pickSound(2))
							break
		
				self.ballList[i].x += self.ballList[i].vx
				self.ballList[i].y += self.ballList[i].vy
		
				self.ballList[i].setXY(self.ballList[i].x, self.ballList[i].y)
		# Handle the pause between launching balls
		elif num == 2:
				ball = Ball(self.launcher.getAngle())
				ball.setXY(self.launcher.getX(), self.launcher.getY())
				self.ballList.append(ball)
				ball.setVXVY(ball.getVX() * self.worldMultiplier, ball.getVY() * self.worldMultiplier)
				self.ballsOnScreen += 1
				viz.playSound(self.pickSound(6))
		# Charge launcher
		elif num == 3:
			if self.charging:
				self.worldMultiplier += .25
				self.chargeCount += 1
				if self.chargeCount == 9:
					viz.playSound(self.pickSound(5))
					self.chargeCount = 0
				else:
					viz.playSound(self.pickSound(4))
		# Prompt player if they failed
		if self.failState:
			self.killtimer(1)
			response = vizinput.ask("You survived {} rounds. Play again?".format(self.roundCount))
			if response == 1:
				self.reset()
			else:
				viz.quit()
Example #32
0
def runtrials():

    global trialtype, trialtype_signed, groundplane, radiiPool, out

    #yield viztask.waitTime(5.0) #allow me to get into the seat.

    setStage()  # texture setting. #likely to have to be expanded.
    driver.reset()  # initialization of driver
    [leftbends, rightbends] = BendMaker(radiiPool)
    viz.MainScene.visible(viz.ON, viz.WORLD)

    #add text to denote conditons.
    txtCondt = viz.addText("Condition", parent=viz.SCREEN)
    txtCondt.setPosition(.7, .2)
    txtCondt.fontSize(36)

    out = ""

    def updatePositionLabel():
        global driver, trialtype_signed, trialtype
        ##WHAT DO I NEED TO SAVE?

        # get head position(x, y, z)
        pos = viz.get(viz.HEAD_POS)
        pos[1] = 0.0  # (x, 0, z)
        # get body orientation
        ori = viz.get(viz.BODY_ORI)
        steeringWheel = driver.getPos()

        #what data do we want? RoadVisibility Flag. SWA. Time, TrialType. x,z of that trial These can be reset in processing by subtracting the initial position and reorienting.
        SaveData(pos[0], pos[2], ori, steeringWheel)  ##.

    vizact.ontimer((1.0 / 60.0), updatePositionLabel)

    for j in range(0, TotalN):
        #import vizjoy

        trialtype = abs(TRIALSEQ_signed[j])
        trialtype_signed = TRIALSEQ_signed[j]

        txtDir = ""

        # Define a function that saves data
        def SaveData(pos_x, pos_z, ori, steer):
            global out

            #what data do we want? RoadVisibility Flag. SWA. Time, TrialType. x,z of that trial These can be reset in processing by subtracting the initial position and reorienting.
            if out != '-1':
                # Create the output string
                currTime = viz.tick()
                out = out + str(float(
                    (currTime))) + '\t' + str(trialtype_signed) + '\t' + str(
                        pos_x) + '\t' + str(pos_z) + '\t' + str(
                            ori) + '\t' + str(steer) + '\t' + str(
                                radius) + '\t' + str(occlusion) + '\t' + str(
                                    int(trialbend.getVisible())) + '\n'

        radiipick = 1
        occlpick = 1
        L = len(radiiPool)
        L2 = L * 2
        print trialtype, L, L2
        if trialtype > L and trialtype <= L2:
            print 'here'
            radiipick = trialtype - L  #reset trialtype and occl index
            occlpick = 2
        elif trialtype > L2:
            print 'here too'
            radiipick = trialtype - L2
            occlpick = 3

        print radiipick
        #pick correct object
        if trialtype_signed > 0:  #right bend
            trialbend = rightbends[radiipick - 1]
            txtDir = "R"
        else:
            trialbend = leftbends[radiipick - 1]
            txtDir = "L"

        radius = radiiPool[radiipick - 1]
        occlusion = occlPool[occlpick - 1]
        if radius > 0:
            msg = "Radius: " + str(radius) + txtDir + '_' + str(occlusion)
        else:
            msg = "Radius: Straight" + txtDir + '_' + str(occlusion)
        txtCondt.message(msg)

        #translate bend to driver position.
        driverpos = viz.MainView.getPosition()
        print driverpos
        trialbend.setPosition(driverpos[0], 0, driverpos[2])

        #now need to set orientation
        driverEuler = viz.MainView.getEuler()
        trialbend.setEuler(driverEuler, viz.ABS_GLOBAL)

        #will need to save initial vertex for line origin, and Euler. Is there a nifty way to save the relative position to the road?
        driver.setSWA_invisible()

        yield viztask.waitTime(occlusion)  #wait an occlusion period

        trialbend.visible(1)

        yield viztask.waitTime(
            2.5 - occlusion
        )  #after the occlusion add the road again. 2.5s to avoid ceiling effects.

        trialbend.visible(0)

        #driver.setSWA_visible()

        def checkCentred():

            centred = False
            while not centred:
                x = driver.getPos()
                if abs(x) < .5:
                    centred = True
                    break


#		centred = False
#		while not centred:
#			x = driver.getPos()
#			print x

##wait a while

        print "waiting"
        yield viztask.waitDirector(checkCentred)
        print "waited"

        driver.setSWA_visible()
        yield viztask.waitTime(2)  #wait for input .

    else:
        #print file after looped through all trials.
        fileproper = ('Pilot_CDM.dat')
        # Opens nominated file in write mode
        path = viz.getOption('viz.publish.path/')
        file = open(path + fileproper, 'w')
        file.write(out)
        # Makes sure the file data is really written to the harddrive
        file.flush()
        #print out
        file.close()

        #exit vizard

        viz.quit()  ##otherwise keeps writting data onto last file untill ESC
def TrackTask():
    prevPos = None
    allData = []
    tRegained = 0
    stage = 0  # 0: wait for track loss, 1: wait for track regained, 2: wait for trigger pull, 3: trigger pulled. check optical heading position ok, 4: trigger released, ready for next trigger pull that will start actual data collection
    t = 0.
    opticalHeadScrewed = False
    tCount = -1
    while True:
        # wait for track regained
        stage = 0
        while True:
            pos = setup.trackerDataFun()
            if prevPos is not None and pos['vive'] == prevPos['vive']:
                # position didn't change at all, not tracking
                if stage is not 1:
                    setup.sounds.trialStart.play()
                    print 'track lost'
                    stage = 1
            else:
                # get here only once vive position/orientation starts changing again
                if stage == 1:
                    setup.sounds.trialCompleted.play()
                    print 'track regained'
                    tRegained = t
                    stage = 2 if qCheckOptHeading else 4  # if not qCheckOptHeading, skip the check

                if stage == 2 and t - tRegained > nSecWait and (
                        steamvr.getControllerList()[0].isButtonDown(2)
                        or viz.key.isDown('c', immediate=True)):
                    # first press, check if optical heading position is ok
                    stage = 3

                if stage == 3 and not (
                        steamvr.getControllerList()[0].isButtonDown(2)
                        or viz.key.isDown('c', immediate=True)):
                    # button/trigger released
                    stage = 4

                if stage == 4 and (
                        steamvr.getControllerList()[0].isButtonDown(2)
                        or viz.key.isDown('c', immediate=True)):
                    # second press: time to record some data
                    tCount += 1
                    break
            prevPos = pos

            # check position of optical heading markers is within expected range. Otherwise markers probably switched
            if qCheckOptHeading and stage in [3, 4] and any([
                    abs(p - m) > l for p, m, l in zip(
                        pos['optical_heading'][:3], optHeadingPos, optHeadLims)
            ]):
                # optical markers screwed, notify
                if not opticalHeadScrewed:
                    print 'optical heading error. current pose: ' + str(
                        pos['optical_heading'])
                    setup.sounds.alarm.play()
                    opticalHeadScrewed = True
            else:
                opticalHeadScrewed = False

            # limit to update rate
            d = yield viztask.waitDraw()
            t = d.time

        # report position
        print 'capturing track data'
        setup.sounds.trialStart.play()
        count = 0
        data = []
        while count < trackRate * nSec:
            ti = setup.trackerDataFun()
            ti['timeStamp'] = d.time
            data.append(ti)
            count += 1

            d = yield viztask.waitDraw()

        # store for later
        allData.append(data)
        setup.sounds.trialCompleted.play()

        # make filename
        if tCount + 1 == nTrial:
            time = datetime.datetime.now()
            fname = ''.join(
                (str(time.year) + str(time.month).zfill(2) +
                 str(time.day).zfill(2) + str(time.hour).zfill(2) +
                 str(time.minute).zfill(2) + str(time.second).zfill(2)))

            saveData = {}
            saveData['data'] = allData
            scio.savemat('data/' + fname,
                         saveData,
                         long_field_names=True,
                         do_compression=True,
                         oned_as='column')

            # done
            viz.quit()
Example #34
0
	def exitGame(self):
		viz.quit()
		print 'Visual Anatomy Trainer has closed'
    def onTimer(self, num):

        if self.frameNumber < len(self.rawDataStruct['frameTime']) - 1:
            #pointCounter[calibrationCounter[self.frameNumber]] = pointCounter[calibrationCounter[self.frameNumber]] + 1
            self.frameNumber = self.frameNumber + 1
        else:
            #self.frameNumber = 0
            print 'Data = ', errorArray
            MatFile = {'Data':errorArray}
            MatFileName = 'ErrorData' 
            sio.savemat(MatFileName + '.mat', MatFile)
            viz.quit()
        if (self.rawDataStruct['eventFlag'][self.frameNumber] == 1):
            self.PD = 0.8
            self.BD = 0.3
            self.PBD = 0.7
            self.timer = 0
    #    StartIndex = TrialStartIndex[counter]
    #    EndIndex   = TrialEndIndex[counter]
    #    for self.frameNumber in range(StartIndex, EndIndex):
        #print 'F=', self.frameNumber,'P=', Ball_Pos_XYZ[:,self.frameNumber],'Q=',View_Quat_WXYZ[:,self.frameNumber], '\n'
        self.ball.setPosition(*self.rawDataStruct['ball_Pos_XYZ'][:,self.frameNumber]) # X[:,self.frameNumber])#
        self.Hand.setPosition(*self.rawDataStruct['paddle_Pos_XYZ'][:,self.frameNumber])
        self.Hand.setQuat(*self.rawDataStruct['paddle_Quat_WXYZ'][:,self.frameNumber])
        
        #self.headBone.setQuat(self.cyclopEyeNode.getQuat())
        #self.male.setEuler([0, 0, 0], viz.ABS_GLOBAL)

        
        self.V1 = self.rawDataStruct['ball_Pos_XYZ'][:,self.frameNumber] - self.rawDataStruct['view_Pos_XYZ'][:,self.frameNumber]
        self.V1 = np.hstack((self.V1,1))
        self.V1.reshape(4,1)



        self.cyclopEyeNode.setPosition(*self.rawDataStruct['view_Pos_XYZ'][:,self.frameNumber])
        self.cyclopEyeNode.setQuat(*self.rawDataStruct['view_Quat_WXYZ'][:,self.frameNumber])
        
        
        self.rightGazePoint_XYZ = [ -self.rawDataStruct['rightGazeDir_XYZ'][0,self.frameNumber], self.rawDataStruct['rightGazeDir_XYZ'][1,self.frameNumber], self.rawDataStruct['rightGazeDir_XYZ'][2,self.frameNumber]]
        self.rightScale = np.linalg.norm(self.rawDataStruct['ball_Pos_XYZ'][:,self.frameNumber] - self.rightEyeNode.getPosition(viz.ABS_GLOBAL))
        self.rightGazePoint_XYZ = np.multiply(self.rightGazePoint_XYZ, self.rightScale)
        self.rightGazeSphere.setPosition(self.rightGazePoint_XYZ[0], self.rightGazePoint_XYZ[1], self.rightGazePoint_XYZ[2], viz.ABS_PARENT)
        self.rightGazeVector.setVertex(0, self.rightEyeNode.getPosition(mode = viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        self.rightGazeVector.setVertex(1, self.rightGazeSphere.getPosition(viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        

        self.leftGazePoint_XYZ = [ -self.rawDataStruct['leftGazeDir_XYZ'][0,self.frameNumber], self.rawDataStruct['leftGazeDir_XYZ'][1,self.frameNumber], self.rawDataStruct['leftGazeDir_XYZ'][2,self.frameNumber]]
        self.leftScale = np.linalg.norm(self.rawDataStruct['ball_Pos_XYZ'][:,self.frameNumber] - self.leftEyeNode.getPosition(viz.ABS_GLOBAL))
        self.leftGazePoint_XYZ = np.multiply(self.leftGazePoint_XYZ, self.leftScale)
        self.leftGazeSphere.setPosition(self.leftGazePoint_XYZ[0], self.leftGazePoint_XYZ[1], self.leftGazePoint_XYZ[2], viz.ABS_PARENT)
        self.leftGazeVector.setVertex(0, self.leftEyeNode.getPosition(mode = viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        self.leftGazeVector.setVertex(1, self.leftGazeSphere.getPosition(viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        
        self.EyeBallLine.setVertex(0, self.rawDataStruct['view_Pos_XYZ'][0,self.frameNumber], self.rawDataStruct['view_Pos_XYZ'][1,self.frameNumber], self.rawDataStruct['view_Pos_XYZ'][2,self.frameNumber], viz.ABS_GLOBAL)
        self.EyeBallLine.setVertex(1, self.rawDataStruct['ball_Pos_XYZ'][0,self.frameNumber], self.rawDataStruct['ball_Pos_XYZ'][1,self.frameNumber], self.rawDataStruct['ball_Pos_XYZ'][2,self.frameNumber], viz.ABS_GLOBAL)
        
        self.ballPlane.setPosition(*self.rawDataStruct['ball_Pos_XYZ'][:,self.frameNumber])

        self.V1 = self.rawDataStruct['ball_Pos_XYZ'][:,self.frameNumber] - self.cyclopEyeNode.getPosition()
        self.V2 =  self.leftGazePoint_XYZ - self.leftEyeNode.getPosition(viz.ABS_GLOBAL) + self.rightGazePoint_XYZ - self.rightEyeNode.getPosition(viz.ABS_GLOBAL) 

        self.V3 =  self.leftGazePoint_XYZ  + self.rightGazePoint_XYZ
        self.eyeGazeVector.setVertex(0,0,0, viz.ABS_PARENT)
        self.eyeGazeVector.setVertex(1, self.V3[0]/2.0, self.V3[1]/2.0, self.V3[2]/2.0, viz.ABS_PARENT)
        self.eyeGazeSphere.setPosition(self.eyeGazeVector.getVertex(1), viz.ABS_PARENT)

        self.V1 = self.rawDataStruct['ball_Pos_XYZ'][:,self.frameNumber] - self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL)
        #V2 = eyeGazeSphere.getPosition(viz.ABS_GLOBAL) - cyclopEyeNode.getPosition(viz.ABS_GLOBAL)
        self.V2 = [a - b for a, b in zip(self.eyeGazeSphere.getPosition(viz.ABS_GLOBAL), self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL))]
        # XYZ = [l1[idx][0] - l2[idx][0] , l1[idx][1] - l2[idx][1], l1[idx][2] - l2[idx][2]  for idx in range(len(l1))]
        errorAngle = np.multiply(self.angle(self.V2, self.V1), 180/np.pi)
        #print 'Angle = ', errorAngle
        #if (calibrationStatus[self.frameNumber] == 1):
        #errorArray[calibrationCounter[self.frameNumber], pointCounter[calibrationCounter[self.frameNumber]]] = errorAngle
        #viz.MainView.setPosition(*Ball_Pos_XYZ[:,self.frameNumber] + [0.0, 0.4, -1.5])
        #if (calibrationStatus[self.frameNumber] == 0):
        string = 'PerForM Lab'#'Before Calibration'
        #else:
        #    string = 'Eye Tracking in VR'#'After Calibration'
        self.wall_text_object.message(string)
def check_steps():
	global stp, stp_counter, step_range, Lstp_flag, Rstp_flag, belt, belt_vec, ptb_max, ptb, elapse_time_R, elapse_time_L,start_L, start_R

	FPtemp = qtm_receive()
	GRF = [FPtemp[2], FPtemp[8]]	# ground reaction forces [left, right]
	
	if stp_counter==80:
		check_step_timing(start_L,start_R)
		
	if (stp_counter > stp):	# wait for the desired amount of steps
		
		# LEFT LEG
		if ( GRF[0]<=80 ):	# left swing phase
			if (Lstp_flag == 0):	# toe off event
				Lstp_flag = 1	# swing now
				#print "toe off left"
				
		elif ( 80<GRF[0]<2000 ):	# left stance phase
			Lstp_flag = 0
			if (belt == 'R'):	# accelerate right
					time.sleep(elapse_time)
#					labjack_impulse()					
					viz.director(perturbation)
					
					
			elif (belt == '0' and belt_vec[ptb-1] == 'R'):	# 2nd toe off // decelerate
					viz.director(perturbation)
					
					if (ptb > ptb_max):
						time.sleep(0.2)	# give time to the treadmill to stabilize
						out = serializepacket(0,0,100,100,0)	# stop treadmill
						s.sendall(out)
						print "THE END - STOPPING"
						time.sleep(0.3)	# give time to the treadmill to stabilize
						viz.quit()
					
					else:	# resetting
						belt = belt_vec[ptb-1]	# 0 < ptb <= ptb_max
#						stp = random.randint(step_range[0], step_range[1])
#						stp_counter = 0
						#print "stp", stp
		
		# RIGHT LEG
		if ( GRF[1]<=80 ):	# right swing phase
			if (Rstp_flag == 0):	# toe off event
				Rstp_flag = 1	# swing now
				#print "toe off right"
			
		elif ( 80<GRF[1]<2000 ):	# right stance phase
			Rstp_flag = 0
			if (belt == 'L'):	# accelerate left
					time.sleep(elapse_time-0.02)
					labjack_impulse()
					viz.director(perturbation)
					
					
			elif (belt == '0' and belt_vec[ptb-1] == 'L'):	# 2nd toe off // decelerate
					viz.director(perturbation)
					if (ptb > ptb_max):
						time.sleep(0.2)	# give time to the treadmill to stabilize
						out = serializepacket(0,0,100,100,0)	# stop treadmill
						s.sendall(out)
						print "THE END - STOPPING"
						time.sleep(0.3)	# give time to the treadmill to stabilize
						viz.quit()
					
					else:	# resetting
						belt = belt_vec[ptb-1]	# 0 < ptb <= ptb_max
#						stp = random.randint(step_range[0], step_range[1])
#						stp_counter = 0
						#print "stp", stp

	else:	# count steps
		# LEFT LEG
		if ( 80<GRF[0]<2000 ):	# left stance phase
			if (Lstp_flag == 1):	# one more step
				Lstp_flag = 0
				stp_counter += 1
				print stp-stp_counter+1
				#print "L", stp_counter
		elif ( GRF[0]<=80 ):	# left swing phase
			Lstp_flag = 1
		
		
		# RIGHT LEG
		if ( 80<GRF[1]<2000 ):	# right stance phase
			if(Rstp_flag == 1):	# one more step
				Rstp_flag = 0
				stp_counter += 1
				print stp-stp_counter+1
				#print "R", stp_counter
		elif ( GRF[1]<=80 ):	# left swing phase
			Rstp_flag = 1
Example #37
0
def EXPERIMENT(trials_per_object, initial_collect_per_object, TestObjects,
               TestObjectLocsALL, ITI, scaleFactors, runs):

    for r in range(runs):
        runNum = r + 1
        context = ContextGen(runNum)
        #test object locations for this world
        TestObjectLocs = TestObjectLocsALL[context - 1]
        yield StartRun(runNum, 1, 0)

        #initial training, just collects
        if runNum < 3:
            Init_trials = []
            for n in range(initial_collect_per_object):
                Init_trials = numpy.random.permutation(
                    len(TestObjects)).tolist() + Init_trials

            for nn in Init_trials:
                #get target object for collection
                objName = TestObjects[nn]
                objLoc = TestObjectLocs[nn]
                S = scaleFactors[nn]
                yield Instruction(objName, ITI, 1, 0)
                data = 'Start collect\n'
                tracking_data.write(data)
                yield Collect(objName, objLoc, S, 1)
                data = objName + ' Collected Initial' + '\n'
                tracking_data.write(data)

        #post-initial training, replace/collect phases
        Trials = []
        for n in range(trials_per_object):
            Trials = numpy.random.permutation(
                len(TestObjects)).tolist() + Trials

        replaceError = []
        for nn in Trials:
            #get target object for replace/collect
            objName = TestObjects[nn]
            objLoc = TestObjectLocs[nn]
            S = scaleFactors[nn]
            #show instructions
            yield Instruction(objName, ITI, 0, 0)
            data = 'Start replace\n'
            tracking_data.write(data)
            #replace phase
            yield Replace()
            error, feedback = computeError(viz.MainView.getPosition(), objLoc)
            data = 'Replaced ' + objName + ', ' + str(error) + '\n'
            tracking_data.write(data)
            replaceError.append(
                error
            )  #store replace error in a list, just to compute the mean at the end

            yield Instruction(objName, ITI, 1, feedback)
            data = 'Start collect\n'
            tracking_data.write(data)
            yield Collect(objName, objLoc, S,
                          1)  #have feedback every trial no matter what
            data = 'Collected ' + objName + '\n'
            tracking_data.write(data)

        #all done, shut down, printing mean error just for experimenter to check if needed
        print('Info: ' + str(numpy.mean(replaceError)))
        yield StartRun(runNum, 0, 0)

    yield StartRun(runNum, 0, 1)
    viz.quit()
    def onTimer(self, num):

        if self.frameNumber < len(self.rawDataStruct['frameTime']) - 1:
            #pointCounter[calibrationCounter[self.frameNumber]] = pointCounter[calibrationCounter[self.frameNumber]] + 1
            self.frameNumber = self.frameNumber + 1
            if(self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
                self.internalCounter = self.internalCounter + 1
            else:
                self.internalCounterBall = self.internalCounterBall + 1
        else:
            #self.frameNumber = 0
            #print 'Data = ', errorArray
            MatFile = {'Data':errorArray}
            self.rawDataStruct['truePOR_XY'] = self.truePOR_XY.T
            self.rawDataStruct['rightEyePOR_XY'] = self.rightEyePOR_XY.T
            self.rawDataStruct['leftEyePOR_XY'] = self.leftEyePOR_XY.T
            self.rawDataStruct['cyclopeanPOR_XY'] = self.cyclopeanPOR_XY.T
            
            MatFileName = 'preProcessedData'+ self.textFileName 
            sio.savemat(MatFileName + '.mat', self.rawDataStruct)
            print 'PreProcessed Data saved into mat file', MatFileName + '.mat'
            print 'Mat File Saving Quit!!'
            viz.quit()
        if (self.rawDataStruct['eventFlag'][self.frameNumber] == 1):
            self.PD = 0.8
            self.BD = 0.3
            self.PBD = 0.7
            self.timer = 0
    #    StartIndex = TrialStartIndex[counter]
    #    EndIndex   = TrialEndIndex[counter]
    #    for self.frameNumber in range(StartIndex, EndIndex):
        #print 'F=', self.frameNumber,'P=', Ball_Pos_XYZ[:,self.frameNumber],'Q=',View_Quat_WXYZ[:,self.frameNumber], '\n'
        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            self.ball.setPosition(*self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter])
        else:
            self.ball.setPosition(*self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall], mode = viz.ABS_GLOBAL) # X[:,self.frameNumber])#
        self.Hand.setPosition(*self.rawDataStruct['paddle_Pos_XYZ'][:,self.frameNumber])
        self.Hand.setQuat(*self.rawDataStruct['paddle_Quat_WXYZ'][:,self.frameNumber])
        
        #self.headBone.setQuat(self.cyclopEyeNode.getQuat())
        #self.male.setEuler([0, 0, 0], viz.ABS_GLOBAL)

        
        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            self.V1 = self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter] - self.rawDataStruct['view_Pos_XYZ'][:,self.frameNumber]
        else:
            self.V1 = self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall] - self.rawDataStruct['view_Pos_XYZ'][:,self.frameNumber]  
            
        self.V1 = np.hstack((self.V1,1))
        self.V1.reshape(4,1)

        viz.MainView.setPosition(*self.rawDataStruct['view_Pos_XYZ'][:,self.frameNumber])
        viz.MainView.setQuat(*self.rawDataStruct['view_Quat_WXYZ'][:,self.frameNumber])
        
        self.cyclopEyeNode.setPosition(*self.rawDataStruct['view_Pos_XYZ'][:,self.frameNumber])
        self.cyclopEyeNode.setQuat(*self.rawDataStruct['view_Quat_WXYZ'][:,self.frameNumber])

        x = self.rawDataStruct['eyePOR_XY'][0,self.frameNumber]
        y = self.rawDataStruct['eyePOR_XY'][1,self.frameNumber]
        x = (x /1920.0)*0.126 - 0.126/2.0
        y = -(y/1080.0)*0.071 + 0.071/2.0
        z = self.eyePOR.getPosition()[2]
        self.eyePOR.setPosition([x, y, z])
        self.cyclopeanPOR_XY = np.vstack((self.cyclopeanPOR_XY, [x, y]))

        
        rx = self.rawDataStruct['rightPOR_XY'][0,self.frameNumber]
        ry = self.rawDataStruct['rightPOR_XY'][1,self.frameNumber]
        rx = (rx /1920.0)*0.126 - 0.126/2.0
        ry = -(ry/1080.0)*0.071 + 0.071/2.0
        rz = self.rightPOR.getPosition()[2]
        self.rightPOR.setPosition([rx, ry, rz])
        self.rightEyePOR_XY = np.vstack((self.rightEyePOR_XY, [rx, ry]))

        lx = self.rawDataStruct['leftPOR_XY'][0,self.frameNumber]
        ly = self.rawDataStruct['leftPOR_XY'][1,self.frameNumber]
        lx = (lx /1920.0)*0.126 - 0.126/2.0
        ly = -(ly/1080.0)*0.071 + 0.071/2.0
        lz = self.leftPOR.getPosition()[2]
        self.leftPOR.setPosition([lx, ly, lz])
        self.leftEyePOR_XY = np.vstack((self.leftEyePOR_XY, [lx, ly]))
        
        self.rightGazePoint_XYZ = [ -self.rawDataStruct['rightGazeDir_XYZ'][0,self.frameNumber], self.rawDataStruct['rightGazeDir_XYZ'][1,self.frameNumber], self.rawDataStruct['rightGazeDir_XYZ'][2,self.frameNumber]]

        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):        
            self.rightScale = np.linalg.norm(self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter] - self.rightEyeNode.getPosition(viz.ABS_GLOBAL))
        else:
            self.rightScale = np.linalg.norm(self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall] - self.rightEyeNode.getPosition(viz.ABS_GLOBAL))
            
        self.rightGazePoint_XYZ = np.multiply(self.rightGazePoint_XYZ, self.rightScale)
        self.rightGazeSphere.setPosition(self.rightGazePoint_XYZ[0], self.rightGazePoint_XYZ[1], self.rightGazePoint_XYZ[2], viz.ABS_PARENT)
        self.rightGazeVector.setVertex(0, self.rightEyeNode.getPosition(mode = viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        # This was the method to use gaze direction vector to draw gaze vector
        #self.rightGazeVector.setVertex(1, self.rightGazeSphere.getPosition(viz.ABS_GLOBAL), viz.ABS_GLOBAL)

        # This was the method to use Screen Coordinates to draw gaze vector from Eye to screen
        #self.rightGazeVector.setVertex(1, self.rightPOR.getPosition( mode = viz.ABS_GLOBAL), viz.ABS_GLOBAL)

        rightEyeToScreenLine = createLine(self.rightPOR.getPosition( mode = viz.ABS_GLOBAL), self.rightEyeNode.getPosition( mode = viz.ABS_GLOBAL))
        pos = self.rightEyeNode.getPosition(mode = viz.ABS_GLOBAL) + np.multiply(rightEyeToScreenLine, self.rightScale)
        self.rightGazeVector.setVertex(1,[pos[0], pos[1], pos[2]] , viz.ABS_GLOBAL)
        
        point0 = np.array([0.0,0.0,0.0])
        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            line = self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter]# - self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL)
        else:
            line = self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall]# - self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL)            
        planeNormal = np.array([0.0,0.0,1.0])
        point1 = np.array([0.0,0.0,0.0725])
        pos = findLinePlaneIntersection( point0, line, planeNormal, point1)
        self.pixelatedBall.setPosition(pos[0], pos[1], 0)
        self.truePOR_XY = np.vstack((self.truePOR_XY, [pos[0], pos[1]]))

        self.leftGazePoint_XYZ = [ -self.rawDataStruct['leftGazeDir_XYZ'][0,self.frameNumber], self.rawDataStruct['leftGazeDir_XYZ'][1,self.frameNumber], self.rawDataStruct['leftGazeDir_XYZ'][2,self.frameNumber]]
        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            self.leftScale = np.linalg.norm(self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter] - self.leftEyeNode.getPosition(viz.ABS_GLOBAL))
        else:
            self.leftScale = np.linalg.norm(self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall] - self.leftEyeNode.getPosition(viz.ABS_GLOBAL))
            
        self.leftGazePoint_XYZ = np.multiply(self.leftGazePoint_XYZ, self.leftScale)
        self.leftGazeSphere.setPosition(self.leftGazePoint_XYZ[0], self.leftGazePoint_XYZ[1], self.leftGazePoint_XYZ[2], viz.ABS_PARENT)
        self.leftGazeVector.setVertex(0, self.leftEyeNode.getPosition(mode = viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        # This was the method to use gaze direction vector to draw gaze vector
        #self.leftGazeVector.setVertex(1, self.leftGazeSphere.getPosition(viz.ABS_GLOBAL), viz.ABS_GLOBAL)
        
        # This was the method to use Screen Coordinates to draw gaze vector from Eye to screen
        #self.leftGazeVector.setVertex(1, self.leftPOR.getPosition(mode = viz.ABS_GLOBAL), viz.ABS_GLOBAL)

        leftEyeToScreenLine = createLine(self.leftPOR.getPosition( mode = viz.ABS_GLOBAL), self.leftEyeNode.getPosition( mode = viz.ABS_GLOBAL))
        pos = self.leftEyeNode.getPosition(mode = viz.ABS_GLOBAL) + np.multiply(leftEyeToScreenLine, self.leftScale)
        self.leftGazeVector.setVertex(1,[pos[0], pos[1], pos[2]] , viz.ABS_GLOBAL)
        
        self.EyeBallLine.setVertex(0, self.rawDataStruct['view_Pos_XYZ'][0,self.frameNumber], self.rawDataStruct['view_Pos_XYZ'][1,self.frameNumber], self.rawDataStruct['view_Pos_XYZ'][2,self.frameNumber], viz.ABS_GLOBAL)

        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            self.EyeBallLine.setVertex(1, self.rawDataStruct['calibration_Pos_XYZ'][0,self.internalCounter], self.rawDataStruct['calibration_Pos_XYZ'][1,self.internalCounter], self.rawDataStruct['calibration_Pos_XYZ'][2,self.internalCounter], viz.ABS_GLOBAL)
        else:
            self.EyeBallLine.setVertex(1, self.rawDataStruct['ball_Pos_XYZ'][0,self.internalCounterBall], self.rawDataStruct['ball_Pos_XYZ'][1,self.internalCounterBall], self.rawDataStruct['ball_Pos_XYZ'][2,self.internalCounterBall], viz.ABS_GLOBAL)
            
        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            self.ballPlane.setPosition(*self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter])
        else:
            self.ballPlane.setPosition(*self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall])            

        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            self.V1 = self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter] - self.cyclopEyeNode.getPosition()
        else:
            self.V1 = self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall] - self.cyclopEyeNode.getPosition()
            
        self.V2 =  self.leftGazePoint_XYZ - self.leftEyeNode.getPosition(viz.ABS_GLOBAL) + self.rightGazePoint_XYZ - self.rightEyeNode.getPosition(viz.ABS_GLOBAL) 

        self.V3 =  self.leftGazePoint_XYZ  + self.rightGazePoint_XYZ
        self.eyeGazeVector.setVertex(0,0,0, viz.ABS_PARENT)

        # This was the method to use right and left Gaze sphere to draw the cyclopean eye
        #self.eyeGazeVector.setVertex(1, self.V3[0]/2.0, self.V3[1]/2.0, self.V3[2]/2.0, viz.ABS_PARENT)
        cyclopEyeToScreenLine = createLine(self.eyePOR.getPosition( mode = viz.ABS_GLOBAL), self.cyclopEyeNode.getPosition( mode = viz.ABS_GLOBAL))

        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            self.cyclopScale = np.linalg.norm(self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter] - self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL))
        else:
            self.cyclopScale = np.linalg.norm(self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall] - self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL))
            
        pos = self.cyclopEyeNode.getPosition(mode = viz.ABS_GLOBAL) + np.multiply(cyclopEyeToScreenLine, self.cyclopScale)
        self.eyeGazeVector.setVertex(1,[pos[0], pos[1], pos[2]] , viz.ABS_GLOBAL)
        self.eyeGazeSphere.setPosition(self.eyeGazeVector.getVertex(1), viz.ABS_PARENT)

        if (self.rawDataStruct['calibrationFlag'][self.frameNumber] == 1 ):
            self.V1 = self.rawDataStruct['calibration_Pos_XYZ'][:,self.internalCounter] - self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL)
        else:
            self.V1 = self.rawDataStruct['ball_Pos_XYZ'][:,self.internalCounterBall] - self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL)
            
        #V2 = eyeGazeSphere.getPosition(viz.ABS_GLOBAL) - cyclopEyeNode.getPosition(viz.ABS_GLOBAL)
        self.V2 = [a - b for a, b in zip(self.eyeGazeSphere.getPosition(viz.ABS_GLOBAL), self.cyclopEyeNode.getPosition(viz.ABS_GLOBAL))]
        # XYZ = [l1[idx][0] - l2[idx][0] , l1[idx][1] - l2[idx][1], l1[idx][2] - l2[idx][2]  for idx in range(len(l1))]
        errorAngle = np.multiply(angle(self.V2, self.V1), 180/np.pi)
        #print 'Angle = ', errorAngle
        #if (calibrationStatus[self.frameNumber] == 1):
        #errorArray[calibrationCounter[self.frameNumber], pointCounter[calibrationCounter[self.frameNumber]]] = errorAngle
        #viz.MainView.setPosition(*Ball_Pos_XYZ[:,self.internalCounterBall] + [0.0, 0.4, -1.5])
        #if (calibrationStatus[self.frameNumber] == 0):
        string = 'PerForM Lab'#'Before Calibration'
        #else:
        #    string = 'Eye Tracking in VR'#'After Calibration'
        self.wall_text_object.message(string)
Example #39
0
def ContextGen(runNum):

    if runNum % 2 == 0:
        if subject % 2 == 0:
            context = 1
        else:
            context = 2
    else:
        if subject % 2 == 0:
            context = 2
        else:
            context = 1

    fname = dpath + 'TRAIN_tracking_' + str(subject) + '_' + str(
        context) + '_' + str(runNum) + '.txt'

    #check for existing data file <-uncomment for actual testing, along with input function above
    if os.path.isfile(fname):
        print('file name already exists')
        viz.quit()

    #open data file
    global tracking_data
    tracking_data = open(fname, 'a')

    ##############################
    ## ENVIRONMENT AND CONTROLS ##
    ##############################

    ##distal cues: add some trees

    #distal cues
    if runNum < 2:
        global cue1  #sloppy making global but it works
        global cue2
        global cue3
        global cue4
        global cue5
        global cue6
        global cue7
        global cue8
        cue1 = viz.add('tree4.osgb')
        cue1.setPosition(-20, -10, 70)
        cue1.setScale([2, 2, 2])
        cue2 = viz.add('tree6.osgb')
        cue2.setPosition(20, -1, -50)
        cue2.setScale([3, 3, 3])
        cue3 = viz.add('tree2.osgb')
        cue3.setPosition(20, -2, 60)
        cue3.setScale([3, 3, 3])
        cue4 = viz.add('tree5.osgb')
        cue4.setPosition(-20, -1, -60)
        cue4.setScale([3, 3, 3])

        cue5 = viz.add('tree10.osgb')
        cue5.setPosition(60, 0, 20)
        cue5.setScale([2.5, 2.5, 2.5])
        cue6 = viz.add('tree11.osgb')
        cue6.setPosition(-60, 0, -20)
        cue6.setScale([5, 5, 5])
        cue7 = viz.add('tree12.osgb')
        cue7.setPosition(60, -5, -20)
        cue7.setScale([4, 4, 4])
        cue8 = viz.add('tree13.osgb')
        cue8.setPosition(-60, -5, 20)
        cue8.setScale([4, 4, 4])

    if context == 1:  #context 1 is circle world
        cue3.visible(0)
        cue4.visible(0)
        cue1.visible(1)
        cue2.visible(1)

        cue5.visible(1)
        cue6.visible(1)
        cue7.visible(0)
        cue8.visible(0)

    else:
        cue3.visible(1)
        cue4.visible(1)
        cue1.visible(0)
        cue2.visible(0)

        cue5.visible(0)
        cue6.visible(0)
        cue7.visible(1)
        cue8.visible(1)

    #boundary elements - may need to adjust x,y positions of Square boundaries slightly to get rid of line between segments (this seems monitor dependent)
    if runNum < 2:
        global boundary1
        global boundary2
        global boundary3
        global boundary4
        global boundary5
        global boundary6
        global boundary7
        global boundary8
        boundary1 = viz.add('CIRCLE.osgb')
        boundary1.setPosition(0.05, 0, -0.05)
        boundary2 = viz.add('CIRCLE.osgb')
        boundary2.setEuler([90, 0, 0])
        boundary2.setPosition(-0.05, 0, -0.05)
        boundary3 = viz.add('CIRCLE.osgb')
        boundary3.setEuler([-90, 0, 0])
        boundary3.setPosition(0.05, 0, 0.05)
        boundary4 = viz.add('CIRCLE.osgb')
        boundary4.setEuler([180, 0, 0])
        boundary4.setPosition(-0.05, 0, 0.05)
        boundary5 = viz.add('SQUARE.osgb')
        #		boundary5.setPosition(0.05,0,-0.05)
        boundary6 = viz.add('SQUARE.osgb')
        boundary6.setEuler([90, 0, 0])
        #		boundary6.setPosition(-0.05,0,-0.05)
        boundary7 = viz.add('SQUARE.osgb')
        boundary7.setEuler([-90, 0, 0])
        #		boundary7.setPosition(0.05,0,0.05)
        boundary8 = viz.add('SQUARE.osgb')
        boundary8.setEuler([180, 0, 0])
#		boundary8.setPosition(-0.05,0,0.05)

##add boundary
    if (context == 1):  #context 1 is circle world
        boundary1.visible(1)
        boundary2.visible(1)
        boundary3.visible(1)
        boundary4.visible(1)
        boundary5.visible(0)
        boundary6.visible(0)
        boundary7.visible(0)
        boundary8.visible(0)

    else:  #context 2 is square world:
        boundary5.visible(1)
        boundary6.visible(1)
        boundary7.visible(1)
        boundary8.visible(1)
        boundary1.visible(0)
        boundary2.visible(0)
        boundary3.visible(0)
        boundary4.visible(0)

    return context
Example #40
0
def task_choice():

    # ensure that the mouse is turned on
    viz.mouse(viz.ON)
    viz.mouse.setVisible(viz.ON)

    # load condition_box
    condition_box = vizinfo.add('')
    condition_box.title('SNAP')
    condition_box.scale(2, 2)
    condition_box.translate(0.85, 0.65)
    condition_box.drag(viz.OFF)

    # add different options
    condition_one = condition_box.add(viz.BUTTON, 'Condition 1')
    condition_two = condition_box.add(viz.BUTTON, 'Condition 2')
    practice = condition_box.add(viz.BUTTON, 'Practice Program')
    expertise = condition_box.add(viz.BUTTON, 'Visuomotor Expertise Program')
    maze_quit = condition_box.add(viz.BUTTON, 'Quit Maze Program')

    # add variables for user chocies
    condition_one_pressed = viztask.waitButtonDown(condition_one)
    condition_two_pressed = viztask.waitButtonDown(condition_two)
    practice_pressed = viztask.waitButtonDown(practice)
    expertise_pressed = viztask.waitButtonDown(expertise)
    maze_quit_pressed = viztask.waitButtonDown(maze_quit)

    # data variable to hold user choices
    data = viz.Data()

    # setup globals
    global condition
    global maze_root
    global start_coords
    global start_ori
    global end_coords

    # While statment that will run a different maze based on user choice.
    # Will keep looping until the user selects quit and ends the program
    while True:
        yield viztask.waitAny([
            condition_one_pressed, condition_two_pressed, practice_pressed,
            expertise_pressed, maze_quit_pressed
        ], data)
        condition_box.remove()
        if data.condition is condition_one_pressed:
            condition = 1
            yield maze_choice()
            yield run_condition_one()
            yield task_choice()
        elif data.condition is condition_two_pressed:
            condition = 2
            yield maze_choice()
            yield run_condition_two()
            yield task_choice()
        elif data.condition is practice_pressed:
            viz.mouse(viz.OFF)
            viz.mouse.setVisible(viz.OFF)
            maze_root = 'C:\Experiments\Bike_Extend_Pilot\SNAP\PMS'
            start_coords = (0, 40, 0)
            start_ori = (3.5, 0, 0)
            yield practice_maze()
            yield task_choice()
        elif data.condition is expertise_pressed:
            viz.mouse(viz.OFF)
            viz.mouse.setVisible(viz.OFF)
            maze_root = 'C:\Experiments\Bike_Extend_Pilot\SNAP\VMS'
            start_coords = (0, 40, 0)
            start_ori = (0, 0, 0)
            end_coords = [1150, 1275, -190, -100]
            yield expertise_maze()
            yield task_choice()
        elif data.condition is maze_quit_pressed:
            viz.quit()

    viz.quit()
def runtrials():
	
	global trialtype, trialtype_signed, groundplane, fixation_counter, inside_edge, outside_edge
	
	setStage() # texture setting
	addfix()   # fixation setting
	driver.reset() # initialization of driver


	def updatePositionLabel():
		global driver, trialtype_signed, fixation, fixation_counter, rdsize, outside_edge, inside_edge, trialtype, groundplane
		
		# get head position(x, y, z)
		pos = viz.get(viz.HEAD_POS)
		pos[1] = 0.0 # (x, 0, z)
		# get body orientation
		ori = viz.get(viz.BODY_ORI)
		steeringWheel = driver.getPos()

		
		######Fixation. This section makes sure the fixation is moved with the observer. 
		fpheight = .12  
		fixation.setEuler((ori, 0.0, 0.0),viz.ABS_GLOBAL) ##fixation point always faces observer
		
		
		if trialtype_signed > 0: ##fixations for right bends
			while fixation_counter < 3142:
				fix_dist = mt.sqrt( ( ( pos[0] - x_right_mid[fixation_counter] )**2 ) + ( ( pos[2] - z_right_mid[fixation_counter] )**2 ) )
				if ( (fix_dist < 16.0) | (fix_dist > 16.3) ):
					fixation_counter += 1
					continue
				elif ( (fix_dist > 16.0) and (fix_dist < 16.3) ):
					fpx = x_right_mid[fixation_counter]
					fpz = z_right_mid[fixation_counter]
					centre_x = x_right_mid[fixation_counter]
					centre_z = z_right_mid[fixation_counter]
					break
			else: ##if you move more than 16m away from any possible fixation, fixation goes back to 0,0,0
				fixation_counter = 0  ##if you end up finding your path again, there is a brand new fixation for you! 
				fpx = 0
				fpz = 0
				centre_x = 0
				centre_z = 0
		else: ##fixations for left bends
			while fixation_counter < 3142:
				fix_dist = mt.sqrt( ( ( pos[0] - x_left_mid[fixation_counter] )**2 ) + ( ( pos[2] - z_left_mid[fixation_counter] )**2 ) )
				if ( (fix_dist < 16.0) | (fix_dist > 16.3) ):
					fixation_counter += 1
					#compCount += 1
					continue
				elif ( (fix_dist > 16.0) and (fix_dist < 16.3) ):
					fpx = x_left_mid[fixation_counter]
					fpz = z_left_mid[fixation_counter]
					centre_x = x_left_mid[fixation_counter]
					centre_z = z_left_mid[fixation_counter]
					#print fix_dist
					break
			else:
				fixation_counter = 0
				fpx = 0
				fpz = 0
				centre_x = 0	
				centre_z = 0
		
		############################
		## added by Yuki
		# insert variables in driver class
		driver.function_insert(centre_x, centre_z, pos[0], pos[2], fix_dist)
		############################
		
		# fixation coordinate(X, eye height, Z)
		fixation.translate(fpx, fpheight, fpz)
		
		eyedata = 9999
		#SaveData(pos[0], pos[1], pos[2], ori, steeringWheel, eyedata) ##.

	# start action ontimer(rate, function)
	vizact.ontimer((1.0/60.0),updatePositionLabel)
	
	
	for j in range(0,TotalN):
		#import vizjoy
		global outside_edge, inside_edge, trialtype, trialtype_signed
		
		trialtype=abs(TRIALSEQ_signed[j])
		trialtype_signed = TRIALSEQ_signed[j]				
	
		viz.MainScene.visible(viz.OFF,viz.WORLD)
		
##		# Define a function that saves data
##		def SaveData(pos_x, pos_y, pos_z, ori, steer, eye):
##			# Create the output string
##			currTime = viz.tick()
##			#out = str(float((currTime))) + '\t' + str(pos_x) + '\t' + str(pos_z)+ '\t' + str(ori)+ '\t' + str(trialtype_signed) + '\n'
##			
##			out = str(float((currTime))) + '\t' + str(pos_x) + '\t' + str(pos_z)+ '\t' + str(ori)+ '\t' + str(steer) + '\t' + str(trialtype_signed) + '\n'
##			#out = str(float((currTime))) + '\t' + str(pos_x) + '\t' + str(pos_z)+ '\t' + str(ori)+ '\t' + str(steer) + '\t' + str(eye[0]) + '\t' + str(eye[1]) + '\t' + str(eye[2]) + '\t' + str(eye[3]) + '\t' + str(eye[4]) + '\t' + str(trialtype_signed) + '\n'
##			# Write the string to the output file
##			file.write(out)                                     
##			# Makes sure the file data is really written to the harddrive
##			file.flush()                                        
##			#print out
	
			
		roadEdges()	
		
		driver.reset()
		
		viz.MainScene.visible(viz.ON,viz.WORLD)
		
		yield viztask.waitTime(10) #Trial Time
		
		
		if edge == 1:
			inside_edge.remove()
			outside_edge.remove()
		
	else:
		viz.quit() ##otherwise keeps writting data onto last file untill ESC