View Single Post
  #16  
Old 11-29-2007, 09:25 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
I cannot run your code because it is not inside the [code][/code] tags. Python code depends on indentation, and posting code without the proper tag loses the indentation.

Either way, I've included a sample script that will manually maintain a proper sample rate. Here is the code:
Code:
import viz
from time import clock
viz.go()

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
	pass

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

#Start the sampler
sampler.start()
Reply With Quote