View Single Post
  #24  
Old 12-03-2007, 08:39 AM
Elittdogg Elittdogg is offline
Member
 
Join Date: Aug 2007
Posts: 77
This is what I did:

Code:
class SampleRate(viz.EventClass):
	def __init__(self,rate,func):
		viz.EventClass.__init__(self)
		self.rate = rate
		self.func = func
		self.lastSampleTime = 0.0
		self.callback(viz.TIMER_EVENT,self.onTimer)

	def onTimer(self,num):
		now = clock()
		while (now - self.lastSampleTime) >= self.rate:
			self.func()
			self.lastSampleTime += self.rate
			
	def start(self):
		self.killtimer(0)
		self.lastSampleTime = clock()
		self.starttimer(0,0,viz.FOREVER)
		
	def stop(self):
		self.killtimer(0)
		

def DoSample():
	#Collect sample here
	flock1 = viz.add('flockofbirds.dls')	
	flock1.command(6)
	tracking_data = open('openloopsine_complex/headdata_'+ str(Subnum) + " " + str(Trial) + '.txt', 'a')
	def mytimer(num):
		if num == 0:
			global AMPLITUDE, TOTALCYCLES, X1, X2, samples
			
			data1 = flock1.get()
			
			HEADLAT = data1[0]
			HEADVERT = data1[1]
			HEADAP = data1[2]
			HEADYAW = data1[3]
			HEADPITCH = data1[4]
			HEADROLL = data1[5]
			
			head_data = str(Subnum) + '\t' + str(Trial) + '\t' + str(HEADAP) + '\t' +str(HEADLAT) + '\t' +str(HEADVERT) + '\t' +str(HEADYAW) + '\t' +str(HEADPITCH) + '\t' +str(HEADROLL) +'\n'
			tracking_data.write(head_data)	
		if num == 4:
			xmove = AMPLITUDE*math.sin(2*(math.pi/180)*X1) + AMPLITUDE*math.sin(2.5*(math.pi/180)*X1) + AMPLITUDE*math.sin(3.5*(math.pi/180)*X1) + AMPLITUDE*math.sin(1.75*(math.pi/180)*X1) + AMPLITUDE*math.sin(2*(math.pi/180)*X1) + AMPLITUDE*math.sin(2.5*(math.pi/180)*X1) + AMPLITUDE*math.sin(3*(math.pi/180)*X1) + AMPLITUDE*math.sin(2.5*(math.pi/180)*X1)+ AMPLITUDE*math.sin(2*(math.pi/180)*X1) + AMPLITUDE*math.sin(1.75*(math.pi/180)*X1)
			X1 = X1 + 1	
			sphere.translate(0,1.8,.2+.3*xmove, viz.ABSOLUTE)
		if num == 3:
			viz.quit()	
	pass

#Call function 'DoSample' at rate of '1/30.0'
sampler = SampleRate(1/30.0,DoSample)

#Start the sampler
sampler.start()

viz.callback(viz.TIMER_EVENT,mytimer)

def onkeydown(key):
	if key == ' ':
		viz.starttimer(0,(1.0/30.0), viz.FOREVER)
		viz.starttimer(4,(1.0/30.0),viz.FOREVER)
		viz.starttimer(3,60,1)
viz.callback(viz.KEYDOWN_EVENT,onkeydown)

I think my problem is that I don't know how to embed "mytimer" within "dosample." Because even when I try different indentations it doesn't start at all. And it keeps giving me this error message:
Traceback (most recent call last):
File "<string>", line 12, in ?
File "openloopsine(complex)_adjusted timer3.py", line 111, in ?
viz.callback(viz.TIMER_EVENT,mytimer)
NameError: name 'mytimer' is not defined

So I'm assuming that it's not registering it as it's own identity/function but I'm a bit confused as to how that would work. Would I have to change "dosample" to a class? And if so, would the rest stay the same?
Reply With Quote