Exemplo n.º 1
0
def multicycle( device, fio1, fio2, rate, duration ):
    sleepTime = 1.0/rate;
    #device.setFIOState(fio,0);
    device.getFeedback(u3.BitStateWrite(fio1, 0),u3.BitStateWrite(fio2, 0),u3.BitStateWrite(6, 0))
    start = time.time()
    current = start
    emgSig = 1
    DAC0_VALUE = d.voltageToDACBits(3.5, dacNumber = 0, is16Bits = False)
    d.getFeedback(u3.DAC0_8(DAC0_VALUE))        # Set DAC0 to 1.5 V
    while( (current - start) < duration):
        print current - start
        #device.setFIOState(fio,1);
        device.getFeedback(u3.BitStateWrite(fio1, 1),u3.BitStateWrite(fio2, 1),u3.BitStateWrite(6, 1) )
	emgSig = 0
        sleep( sleepTime );
        #device.setFIOState(fio,0);
        device.getFeedback(u3.BitStateWrite(fio1, 0),u3.BitStateWrite(fio2, 0),u3.BitStateWrite(6, 0) )
        current = time.time()
    DAC0_VALUE = d.voltageToDACBits(0, dacNumber = 0, is16Bits = False)
    d.getFeedback(u3.DAC0_8(DAC0_VALUE))        # Set DAC0 to 1.5 V
Exemplo n.º 2
0
    def valve1(self,toggle_v1):
      
      if toggle_v1:
          print "valve1 ON"
          #device.setFIOState(v1_lj, 1)
          DAC0_VALUE = device.voltageToDACBits(5.5, dacNumber = 0, is16Bits = False)
          device.getFeedback(u3.DAC0_8(DAC0_VALUE))        # Set DAC0 to 5 V
          if self.Valve2.isChecked() : #if valve2 activated
              self.odo.setValue(2) #odor 2
              print "odor: 2"
          else :
              self.odo.setValue(1) #odor 1
              print "odor: 1"


      else :
          print "valve1 OFF"
          print "odor: 0"
          self.odo.setValue(0) #no odors: odor 0
          DAC0_VALUE = device.voltageToDACBits(0, dacNumber = 0, is16Bits = False)
          device.getFeedback(u3.DAC0_8(DAC0_VALUE))        # Set DAC0 to 5 V
Exemplo n.º 3
0
def setOdor(device,odor):
    if odor == 0:
        #device.setFIOState(v1_lj, 0)
        DAC0_VALUE = device.voltageToDACBits(0, dacNumber = 0, is16Bits = False)
        device.getFeedback(u3.DAC0_8(DAC0_VALUE))   
    else :
        if odor == 1 :
            #device.setFIOState(v2_lj, 0)
            DAC1_VALUE = device.voltageToDACBits(0, dacNumber = 1, is16Bits = False)
            device.getFeedback(u3.DAC1_8(DAC1_VALUE))   
            #device.setFIOState(v1_lj, 1)    
            DAC0_VALUE = device.voltageToDACBits(5.5, dacNumber = 0, is16Bits = False)
            device.getFeedback(u3.DAC0_8(DAC0_VALUE))  
        else:
            #device.setFIOState(v2_lj, 1)
            DAC1_VALUE = device.voltageToDACBits(5.5, dacNumber = 1, is16Bits = False)
            device.getFeedback(u3.DAC1_8(DAC1_VALUE))  
            #device.setFIOState(v1_lj, 1)
            DAC0_VALUE = device.voltageToDACBits(5.5, dacNumber =0, is16Bits = False)
            device.getFeedback(u3.DAC0_8(DAC0_VALUE))  
    print "set odor to: "+str(odor)
Exemplo n.º 4
0
def take_focal_stack(device):
    ''' documentation for this method 
    
    '''

    print('setting nominal voltage: 2.5V')
    DACO_val = device.voltageToDACBits(2.5, 0, False)
    device.getFeedback(u3.DAC0_8(DACO_val))
    input('adjust focus, press enter when finished')
    focus_points = np.arange(2.0, 3.25, 0.25)
    print('focus points: ' + str(focus_points))

    print('starting voltage ramp')
    for voltage in focus_points:
        print('voltage: ' + str(voltage))
        DACO_val = device.voltageToDACBits(voltage, 0, False)
        device.getFeedback(u3.DAC0_8(DACO_val))
        time.sleep(0.1)
        #input('waiting for you to choose focus, press enter when done')
        p = sp.Popen(
            capture_img_exe_path, stdin=sp.PIPE, universal_newlines=True
        )  #universal_newlines means stdin can be a string instead of bytes
        p.communicate(IMG_STACK_SAVE_PATH + "stack-test_" + str(voltage) +
                      ".bmp")
Exemplo n.º 5
0
 def closeEvent(self, event):   # to clean up, closes labjack and windows
   #global device
   self.win.close()
   device.setFIOState(green_lj, 0)
   device.setFIOState(red_lj, 0)
   #device.setFIOState(v1_lj, 0)
   #device.setFIOState(v2_lj, 0)
   DAC0_VALUE = device.voltageToDACBits(0, dacNumber = 0, is16Bits = False)
   device.getFeedback(u3.DAC0_8(DAC0_VALUE))        
   DAC1_VALUE = device.voltageToDACBits(0, dacNumber = 1, is16Bits = False)
   device.getFeedback(u3.DAC1_8(DAC1_VALUE))   
   device.close()
   
   print "closing device"
   ser.close() #arduino serial
   mmc.unloadAllDevices() 
         
   event.accept()   
Exemplo n.º 6
0
    def setDACVoltage(self, dac_channel, voltage):
        '''Set the voltage on the DAC0'''

        # Set DAC using Modbus
        #if dac_channel == 0:
        #    DAC0_REGISTER = 5000
        #    self.lj.writeRegister(DAC0_REGISTER, voltage)
        #elif dac_channel == 1:
        #    DAC1_REGISTER = 5002
        #    self.lj.writeRegister(DAC1_REGISTER, voltage)
        #else:
        #    print 'Error: Not a channel'

        # Set DAC using 8 bit low level command
        dac_value = int(voltage * 255 / 4.95)
        if dac_channel == 0:
            self.lj.getFeedback(u3.DAC0_8(dac_value))
        elif dac_channel == 1:
            self.lj.getFeedback(u3.DAC1_8(dac_value))
        else:
            print 'Error: Not a channel'
Exemplo n.º 7
0
    def generate_command_lists(self, states):
        command_lists = []
        previous_state = self.default
        for state in states:
            command_list = []
            #  FIOs
            for i in range(4, 8):
                if previous_state[i - 4] != state[i - 4]:
                    command_list.append(u3.BitStateWrite(i, state[i - 4]))

            # DACs
            if previous_state[4] != state[4]:
                voltage_in_bits = self._device.voltageToDACBits(volts=state[4],
                                                                dacNumber=0)
                command_list.append(u3.DAC0_8(voltage_in_bits))

            if previous_state[5] != state[5]:
                voltage_in_bits = self._device.voltageToDACBits(volts=state[5],
                                                                dacNumber=1)
                command_list.append(u3.DAC1_8(voltage_in_bits))

            command_lists.append(command_list)
            previous_state = state
        return command_lists
Exemplo n.º 8
0
d = u3.U3()

try:
    #Configure the IOs before the test starts

    FIOEIOAnalog = (2**numChannels) - 1
    fios = FIOEIOAnalog & (0xFF)
    eios = FIOEIOAnalog / 256

    d.configIO(FIOAnalog=fios, EIOAnalog=eios)

    d.getFeedback(u3.PortDirWrite(Direction=[0, 0, 0], WriteMask=[0, 0, 15]))

    feedbackArguments = []

    feedbackArguments.append(u3.DAC0_8(Value=125))
    feedbackArguments.append(u3.PortStateRead())

    #Check if the U3 is an HV
    if d.configU3()['VersionInfo'] & 18 == 18:
        isHV = True
    else:
        isHV = False

    for i in range(numChannels):
        feedbackArguments.append(
            u3.AIN(i, 31, QuickSample=quickSample, LongSettling=longSettling))

    #print feedbackArguments

    start = datetime.now()
Exemplo n.º 9
0
def dac0out8(d,v=0.0):    # v is the floating point voltage.  Range is 0 to 4.95 
   DAC0_VALUE = d.voltageToDACBits(v, dacNumber = 0, is16Bits = False)
   d.getFeedback(u3.DAC0_8(DAC0_VALUE))        # Set DAC0 to v Volts
Exemplo n.º 10
0
def runEpoch(mmc,device,rep):
    import time
    
    global epoch_seq #epochs items as list (name,exp,led,odor,duration,noRec)
    global meta_index # list: epoch name, start frame, end frame
    
    namep=window.path.text()
    print "create hdf5" 
    hdfpath = createHdf5andClose(namep,savepath)
    
    
    n_epoch=len(epoch_seq)
    total =0
    epoch_times =[]
    
    eps = 0.005 #delay... error in time passed
    lj_delay = 0#0.012
    count = 1 #epoch 0 effectuated before loop
    m=0 #tiff saving index
    namep=window.path.text()
    print "namep..."+str(namep)

    window.progressBar.setMinimum(0)
    
    window.progressBar.setMaximum(n_epoch+(rep-1)*n_epoch)

    print str(n_epoch)
    for i in range(n_epoch):
        total += epoch_seq[i][4]
        epoch_times.append(total)
    print str(n_epoch)+" epochs"
    print "should take "+str(total)+"s and repeated "+str(rep)+" time(s)"

    mmc.prepareSequenceAcquisition('Zyla')
    #print "preparing sequence acquisition took", time.clock()-start, "seconds"                                         
    
#    cv2.namedWindow('Video')
    
    text_file_globalTime = open(savepath+"/globalTime"+namep+".txt", 'w')
    #text_file_globalTime.write(str(time.clock()-start)+",  "+ser.readline()+"\n")
    
    
    for i in range(rep): #LOOP FOR TRIALS
            print "trial "+str(i+1)
            text_file = open(savepath+"/respi"+namep+"_trial_"+str(i+1)+".txt", 'w')
            text_file_globalTime.write( str(time.time())+",  "+str(i+1)+"\n")
            frame_i=0
                         
            #mmc.setProperty(DEVICE[0], 'Exposure', epoch_seq[0][1])
            setLed(device,epoch_seq[0][2])
            setOdor(device,epoch_seq[0][3])
            mmc.initializeCircularBuffer()                          
            mmc.startContinuousSequenceAcquisition(10)
            
            start=time.clock()  

            while time.clock()-start < total:
                if  mmc.getRemainingImageCount()>0 :
                    im = mmc.popNextImage()
                    #im = mmc.getLastImage()
                    #cv2.imshow('Video', im)  
                    #raw_input("pause")   
                    frames.append(im)
                    timestamps.append(time.clock()-start) #0.6s from start !!!
                    meta.append( (epoch_seq[count-1], i)) 
                
                if  (count < n_epoch) and (time.clock()-start+eps > epoch_times[count-1]) :
                    print "count "+str(count)
                    print epoch_seq[count-1][0]

                    print frame_i
                    print len(frames)-1
                    #mmc.setProperty(DEVICE[0], 'Exposure', epoch_seq[count][1])
                    setLed(device,epoch_seq[count][2])
                    setOdor(device,epoch_seq[count][3])
                    window.progressBar.setValue(count+i*n_epoch)
                    
#                 #saving index of previous epoch (name,start frame, end frame)
#                    frame_f=len(frames)-1
#                    meta_index.append((epoch_seq[count-1][0],frame_i,frame_f))
#                    frame_i=len(frames)
                    
                    if epoch_seq[count-1][5] == 0 :
                        frame_f=len(frames)-1
                        meta_index.append((epoch_seq[count-1][0],frame_i,frame_f))
                        frame_i=len(frames)
#                        nrec = 0
#                        mmc.stopSequenceAcquisition()
#                        mmc.clearCircularBuffer() 
                    if (epoch_seq[count][5] == 1):
                        print "stop recording "+str(epoch_seq[count][4])+ "sec"
                        time.sleep(epoch_seq[count][4])
                        mmc.clearCircularBuffer()
                        #meta_index.pop()

#                        if epoch_seq[count-1][5] == 1:
#                            mmc.initializeCircularBuffer()   
#                            mmc.startContinuousSequenceAcquisition(10)
#                            nrec=1                    
                    count += 1
                if ser.inWaiting() :
                            text_file.write(str(time.clock()-start)+",  "+ser.readline()+"\n")
#                if cv2.waitKey(1) >= 0:
#                    break
            frame_f=len(frames)-1
            meta_index.append((epoch_seq[count-1][0],frame_i,frame_f))
            frame_i=len(frames)
            
            m_i_trial.append(meta_index)
            meta_index=[]
            
            window.progressBar.setValue(n_epoch+i*n_epoch)
            #reset
            device.setFIOState(green_lj, 0)
            device.setFIOState(red_lj, 1)
            #device.setFIOState(v1_lj, 0)
            #device.setFIOState(v2_lj, 0)
            DAC0_VALUE = device.voltageToDACBits(0, dacNumber = 0, is16Bits = False)
            device.getFeedback(u3.DAC0_8(DAC0_VALUE))        
            DAC1_VALUE = device.voltageToDACBits(0, dacNumber = 1, is16Bits = False)
            device.getFeedback(u3.DAC1_8(DAC1_VALUE))       
                      
            print str(total)+"s acquisition took", time.clock()-start, "seconds"
            
            mmc.stopSequenceAcquisition()
            mmc.clearCircularBuffer() 
            if i < rep: #end of an epoch
                    #time spent savong files
                    savingTime=time.time()
                    #print "saving tiff.."+str(len(frames))+" frames"
                    #namep=window.path.text()
                    framesnp = np.asarray(frames)
                    
                    #display avg green
                    print "save green"+str(i)+" in "+savepath
                    scipy.misc.imsave(savepath+'/green'+str(i)+'.jpg', np.mean(framesnp[0:20],axis=0))
                    #plt.imshow(np.mean(framesnp[0:20],axis=0))
                    
                    #saveAsMultipageTif(framesnp,savepath,namep,meta,k=512)#
                    #m = saveAsMultipageTif_inter(framesnp,savepath,namep,m)
                    #print "number of tiff files :"+str(m)
                    print "save trial hdf5"
                    print m_i_trial[-1]
                    print i+1
                    hdfs=saveTrialHdf5andClose(hdfpath,framesnp,m_i_trial[-1],i+1,epoch_seq) #modified added epoch
                    print "closing hdfs"
                    hdfs.close()
                    print "closed"
                    #print "save respiratory signal"
                    # text_file = open("respi"+".txt", 'w')
                    print "empty frames list.."
                    frames[:]=[]
                    text_file.close()
                    timeSpentSaving=time.time()-savingTime
                    newDelay=window.dur_2.value()-timeSpentSaving
                    print "in total wating for "+str(window.dur_2.value())+" s"
                    print "after time spent saving wating for "+str(newDelay)+" s"
                    if (newDelay>0):
                        time.sleep(newDelay)
            count =1
    
    #cv2.destroyAllWindows()
                                

    
    #print "nOfFrames :"+str(len(frames)) 
    print "exposure was", mmc.getProperty('Zyla','Exposure')                                               
    print "Framerate was", mmc.getProperty('Zyla','FrameRate')             
    print "saving metadata, timestamps and comments"
    
    if not os.path.exists(savepath+"/"+namep):
        os.makedirs(savepath+"/"+namep)
    
    with open(savepath+"/"+namep+"/epoch.p", "wb") as fp:   #Pickling
        pickle.dump(epoch_seq, fp)
    with open(savepath+"/"+namep+"/comment.p", "wb") as fpp:   #Pickling
        pickle.dump(window.comment.toPlainText(), fpp)
    with open(savepath+"/"+namep+"/timestamps.p", "wb") as fppp:   #Pickling
        pickle.dump(timestamps, fppp)
    with open(savepath+"/"+namep+"/meta.p", "wb") as fppp:   #Pickling
        pickle.dump(meta, fppp)
    with open(savepath+"/"+namep+"/m_i_trial.p", "wb") as fppp:   #Pickling
        pickle.dump(m_i_trial, fppp)

    print "restart kernel to be safe"
    
    text_file_globalTime.close()
    print "global time txt closed"

    
    window.progressBar.setValue(0)       
    ################################################################################
    ## Measurement Loop
    ################################################################################
    #LabJack and SA
    dig_offset = 0.024
    DAC1_VALUE = lj.voltageToDACBits(
        3 + dig_offset, dacNumber=1,
        is16Bits=False)  #CAL voltage value is set to 3V
    lj.getFeedback(u3.DAC1_8(DAC1_VALUE))

    # Get data when NGEN is OFF

    DAC0_VALUE = lj.voltageToDACBits(0 - dig_offset + 0.010,
                                     dacNumber=1,
                                     is16Bits=False)  #NGEN is OFF
    lj.getFeedback(u3.DAC0_8(DAC0_VALUE))
    k = 0
    with GracefulInterruptHandler() as h:
        while True:
            for i in range(n_avg):
                a0 = lj.getAIN(0)
                a1 = lj.getAIN(1)
                a2 = lj.getAIN(2)
                a3 = lj.getAIN(3)
                a4 = lj.getAIN(4)
                a5 = lj.getAIN(5)
                a6 = lj.getAIN(6)
                a7 = lj.getAIN(7)
                a8 = lj.getAIN(8)
                a9 = lj.getAIN(9)
                a10 = lj.getAIN(10)
Exemplo n.º 12
0
 def set_DAC0(self, voltage):
     voltage_in_bits = self._device.voltageToDACBits(volts=voltage,
                                                     dacNumber=0)
     return self._try_command('getFeedback', u3.DAC0_8(voltage_in_bits))