class StimNorm: # amp range 10 - 60 % A_0 = 0.0 #10 #.33 #10. A_BAND = 1.0 #0.90 - A_0#50.#60. AMP = A_0 + A_BAND # const amp AMPWAVE = A_0 + A_BAND / 2 #180 # freq mod FREQ_0 = 0. #1. FREQ_BAND = 3. - FREQ_0 #1.5 CLICK_MOTOR = 0 MOTORS = [3, 4, 0, 5, 1, 2] N_MOTORS = len(MOTORS) LAST_MOTOR = N_MOTORS - 1 # motors -1 def __init__(self): self.brd = DriverBrd() #self.wb.setLRA(False) self.brd.setSequence(StimNorm.MOTORS) self.brd.setValueAll(0) self.brd.setEnable(True) def stimulate(self, level, method): self.brd.setValueAll(0, False) # S1 - amp #print method if method is Method.MODE_AMP: self.__playClick(level) # S2 - wave elif method is Method.MODE_WAVE: self.__playWave(level, StimNorm.AMPWAVE) # S3 - freq and amp vaiations # S4 - S3 random else: self.__playWave(level, None) def stopStim(self): self.brd.setWave(1, 1, 0) #redundant: #self.wb.setValueAll(0) def prepareTrial(self, method): if method is Method.MODE_RANDOM: seq = StimNorm.MOTORS random.shuffle(seq) self.brd.setSequence(seq) else: self.brd.setSequence(StimNorm.MOTORS) def __playClick(self, level): A = (StimNorm.A_0 + level * StimNorm.A_BAND) self.brd.setValue(StimNorm.CLICK_MOTOR, 100 * A) def __playWave(self, level, amp): if amp is None: A = (StimNorm.A_0 + level * StimNorm.A_BAND) else: A = (amp) d = 1 # 1 ms is the default toff time hardcoded in the arduino tOff = 0.001 freq = StimNorm.FREQ_0 + level * StimNorm.FREQ_BAND tau = 1. / freq / StimNorm.N_MOTORS # 1/f/6 [s] tOn = tau - tOff #print 'ton=%f amp=%f'%(tOn,A) self.brd.setWave(100 * A, 1000 * tOn, d)
class Stimuli: # amp range 10 - 60 % A0=33 #10. Arange=90-33#50.#60. AMP=A0+Arange # const amp AMPWAVE=A0+Arange/2#180 # freq mod FREQ_RANGE = 2.5#1.5 FREQ_0 = 1. CLICK_MOTOR=0 MOTORS=[3,4,0,5,1,2] LAST_MOTOR=len(MOTORS)-1 # motors -1 N_MOTORS=len(MOTORS) def __init__(self): self.wb = DriverBrd() #self.wb.setLRA(False) self.wb.setSequence(Stimuli.MOTORS) self.wb.setValueAll(0) self.wb.setEnable(True) def stimulate(self, level,method): self.wb.setValueAll(0,False) # S1 - amp if method is Method.MODE_AMP: self.__playClick(level) # S2 - wave elif method is Method.MODE_WAVE: self.__playWave(1.*level, Stimuli.AMPWAVE) # S3 - freq and amp vaiations # S4 - S3 random else: self.__playWave(1.*level,None) def stopStim(self): self.wb.setWave(1,1,0) self.wb.setValueAll(0) def prepareTrial(self,method): if method is Method.MODE_RANDOM: seq=Stimuli.MOTORS random.shuffle(seq) self.wb.setSequence(seq) else: self.wb.setSequence(Stimuli.MOTORS) def __playClick(self,level): A=int(Stimuli.A0+level/10.*Stimuli.Arange) self.wb.setValue(Stimuli.CLICK_MOTOR, A) def __playWave(self,level,amp): if amp is None: A=int(Stimuli.A0+level/10.*Stimuli.Arange) else: A=int(amp) d=1 # 1 ms is the default toff time hardcoded in the arduino tOff=0.001 freq= Stimuli.FREQ_0 + level / 10. * Stimuli.FREQ_RANGE tau=1./freq/Stimuli.N_MOTORS # 1/f/6 [s] tOn = tau-tOff self.wb.setWave(A,tOn,d)
class BrdThread(threading.Thread): """ abstract class, extend it and override the while loop """ def __init__(self, com=None): self.wb = DriverBrd(com) self.__shouldRun = True #print "thread constructed" threading.Thread.__init__(self) def cancel(self): """ call from main """ self.__shouldRun = False #print "thread canceled" def getWb(self): """ for internal use """ return self.wb def shouldRun(self): """ for internal use """ return self.__shouldRun def whileLoop(self): """ override this """ while self.shouldRun(): """ example """ # t=time.time() - self.tic # self.getWb().setValue(1,30) # time.sleep(.001) pass def __onFinish(self): """ private, don't call this """ time.sleep(.005) self.wb.setEnable(False) self.wb.close() def run(self): """ don't call this, use thread.start() """ try: self.wb.setEnable(True) self.wb.setValueAll(0) self.tic = time.time() #omega=100 #degrees/s self.whileLoop() except KeyboardInterrupt: print "Caught KeyboardInterrupt, terminating workers" except AttributeError as e: print "attr " + e.message except IOError as e: print "IO " + e.message except BaseException as e: print "Base " + e.message #print "thread finished" self.__onFinish()