def getTorque(): Gain = UL.BIP5VOLTS Chan = 0 DataValue = UL.cbAIn(BoardNum, Chan, Gain) #return (UL.cbToEngUnits(BoardNum, Gain, DataValue)) engUnits = UL.cbToEngUnits(BoardNum, Gain, DataValue) return (2.26*engUnits)
def sampleCurrent(self): Gain = UL.BIP5VOLTS Chan = 2 try: DataValue = UL.cbAIn(self.BoardNum, Chan, Gain) engUnits = UL.cbToEngUnits(self.BoardNum, Gain, DataValue) self.Current = (engUnits) / .02 - self.CurrentOffset except: pass
def sampleTorque(self): Gain = UL.BIP5VOLTS Chan = 0 try: DataValue = UL.cbAIn(self.BoardNum, Chan, Gain) engUnits = UL.cbToEngUnits(self.BoardNum, Gain, DataValue) self.Torque = 2.26 * engUnits # - self.TorqueOffset; except: pass
def sampleVoltage(self): Gain = UL.BIP5VOLTS Chan = 1 try: DataValue = UL.cbAIn(self.BoardNum, Chan, Gain) engUnits = UL.cbToEngUnits(self.BoardNum, Gain, DataValue) self.Voltage = -1 * ( (engUnits - 2.5) * 20.2030 - self.VoltageOffset) except: pass
def startdisplayclicked(self): """Starts the data display""" #start functions and update handles self.stopdisplayclicked() # Stop an existing timer if self.driftalert.isChecked(): DataValue = UL.cbAIn(BoardNum, Chan, Gain)#raw input data for drift check initialValue = UL.cbToEngUnits(BoardNum, Gain, DataValue)#convert raw data to volts self._displayloop = self.startdisplayloop() #start the display loop function self._timerId = self.startTimer(0) # idle timer
def get_some_data(npts): #acquire from the board BoardNum=0 Gain=ul.BIP5VOLTS Chan=0 v=zeros(npts) for i in arange(npts): d=ul.cbAIn(BoardNum,Chan,Gain) v[i]=ul.cbToEngUnits(BoardNum,Gain,d) return v
def lick_detection(SIDE): EngUnits = 0 DataValue = 0 if SIDE == 0: while ((EngUnits) * (EngUnits))**0.5 < 1.55: DataValue = UL.cbAIn(0, SIDE, UL.BIP5VOLTS) EngUnits = UL.cbToEngUnits(0, UL.BIP5VOLTS, DataValue) else: get_reward(UL.FIRSTPORTA)
def lick_detection_no_reward(SIDE): EngUnits = 0 DataValue = 0 if SIDE == 0: while ((EngUnits) * (EngUnits))**0.5 < 1.5: DataValue = UL.cbAIn(0, SIDE, UL.BIP5VOLTS) EngUnits = UL.cbToEngUnits(0, UL.BIP5VOLTS, DataValue) else: print 'nope'
def record_port(self, volt_interval=UL.BIP10VOLTS): """Reads the electrical voltage and transforms \ the value with the scale factor and the offset. :param volt_interval: Interval of the electrical voltage of the machine :type volt_interval: int. :returns: The value of the machine :rtype: float """ data = UL.cbAIn(self.board.id, self.id, volt_interval) volt = UL.cbToEngUnits(self.board.id, volt_interval, data) return self.scale_factor * volt + self.offset
def sampleAllFast(self): Count = 300 Rate = 5000 Gain = UL.BIP5VOLTS Options = UL.CONVERTDATA + UL.BACKGROUND + UL.SINGLEIO ADData = numpy.zeros((Count, ), dtype=numpy.int16) UL.cbAInScan(self.BoardNum, 0, 2, Count, Rate, Gain, ADData, Options) time.sleep(Count / Rate) torqueVolts = UL.cbToEngUnits(self.BoardNum, Gain, int(numpy.mean(ADData[0::3]))) voltageVolts = UL.cbToEngUnits(self.BoardNum, Gain, int(numpy.mean(ADData[1::3]))) currentVolts = UL.cbToEngUnits(self.BoardNum, Gain, int(numpy.mean(ADData[2::3]))) print(torqueVolts, voltageVolts, currentVolts) self.Torque = 2.26 * torqueVolts - self.TorqueOffset self.Voltage = -1 * ( (voltageVolts - 2.5) * 20.2030 - self.VoltageOffset) self.Current = (currentVolts) / .02 - self.CurrentOffset
def lick_detection(SIDE): EngUnits=0 DataValue=0 if SIDE==0: #threshold while ((EngUnits)*(EngUnits))**0.5<1.25: DataValue = UL.cbAIn(0, SIDE, UL.BIP5VOLTS) EngUnits = UL.cbToEngUnits(0, UL.BIP5VOLTS, DataValue) #print 1/(((EngUnits+1)*(EngUnits))**0.5) else: print EngUnits+1 print core.Clock() get_reward(UL.FIRSTPORTA)
def lick_detection(SIDE): EngUnits=0 DataValue=0 time.sleep(1.4) if SIDE==0: while ((EngUnits)*(EngUnits))**0.5<1.35: DataValue = UL.cbAIn(0, SIDE, UL.BIP5VOLTS) EngUnits = UL.cbToEngUnits(0, UL.BIP5VOLTS, DataValue) #print EngUnit else: print EngUnits print core.Clock() get_reward(UL.FIRSTPORTA)
def startdisplayloop(self): """Continuously updates graph and acquires data""" counter = 0 #runtime counter, counts number of iterations while 1: counter = counter + 1 #increment counter """DAQ Capture and storage""" DataValue = UL.cbAIn(BoardNum, Chan, Gain)#raw input data EngUnits = UL.cbToEngUnits(BoardNum, Gain, DataValue)#convert raw data to volts data.append( EngUnits ) #DAQ at high frequency but not displayed times.append( time.time()-tstart ) #time stamps at high frequency but not displayed if(len(data)>10000): #10 second capture interval, upper bound = (capture frequency)*10 data.pop(0) #remove first data point in list times.pop(0) #remove first time point in list """DAQ downsample and display""" if counter % 1000: #downsampling by (1/(modulo value)) for display smalldata.append( EngUnits ) #add plotted data point smalltimes.append( time.time()-tstart ) #add plotted time point if(len(smalldata)>100):#len(data)/modulo value, defines number of points plotted smalldata.pop(0) #remove first plotted data point in the list smalltimes.pop(0) #remove first plotted time point in the list if self.trapalert.isChecked(): #if checking for trapping is enabled trapCheck = (np.mean(smalldata[10:50])-np.mean(smalldata[50:100])) #calculate the difference between two averages trapCheck = (trapCheck/np.mean(smalldata[10:15])) if(trapCheck > 0.20): #put trapping threshold here self.stopdisplayclicked() self.sendemailalert() if self.driftalert.isChecked(): #if the voltage has depreciated by 75% if (np.mean(smalldata[10:100]) < (initialValue*0.60)): self.stopdisplayclicked() self.sendemailalert() # force an image redraw on every 3rd data capture if counter % 2000: #downsampled values are plotted as they are added self.displaywindow.canvas.ax.clear() #clear plot self.displaywindow.canvas.ax.plot(smalltimes,smalldata,'g') #plot downsampled values self.displaywindow.canvas.draw() #force image redraw """Delay and yield for proper capture frequency""" time.sleep(0.001)#1kHz yield
def startdisplayclicked(self): """Starts the data display""" #start functions and update handles self.stopdisplayclicked() # Stop an existing timer if self.driftalert.isChecked(): DataValue = UL.cbAIn(BoardNum, Chan, Gain)#raw input data for drift check initialValue = UL.cbToEngUnits(BoardNum, Gain, DataValue)#convert raw data to volts QdriftThreshold = self.driftpercent.text() #get text from UI SdriftThreshold = str(QdriftThreshold) #convert to string driftThreshold = int(SdriftThreshold) #convert to int driftThreshold = driftThreshold/100 #convert to percent if self.trapalert.isChecked(): QtrapThreshold = self.trappercent.text() StrapThreshold = str(QtrapThreshold) trapThreshold = int(StrapThreshold) trapThreshold = trapThreshold/100 Qsamplefrequency = self.frequencyinput.text() #get sampling frequency from ui samplefrequency = int(Qsamplefrequency) datalength = 10*samplefrequency #10 second storing interval self._displayloop = self.startdisplayloop(samplefrequency,datalength) #start the display loop function self._timerId = self.startTimer(0) # idle timer
# * Neither the name of the California Institute of Technology nor # the names of its contributors may be used to endorse or promote # products derived from this software without specific prior # written permission. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # Author: Andrew Straw import UniversalLibrary as UL BoardNum = 0 Gain = UL.BIP5VOLTS Chan = 0 while 1: DataValue = UL.cbAIn(BoardNum, Chan, Gain) EngUnits = UL.cbToEngUnits(BoardNum, Gain, DataValue) print DataValue, EngUnits
# <codecell> #example2.py import UniversalLibrary as UL import time BoardNum = 0 Gain = UL.BIP5VOLTS Chan = 0 tstart = time.time() data = [] times = [] while 1: DataValue = UL.cbAIn(BoardNum, Chan, Gain) EngUnits = UL.cbToEngUnits(BoardNum, Gain, DataValue) data.append(EngUnits) times.append(time.time() - tstart) if times[-1] > 1.0: break import pylab pylab.plot(times, data, 'o-') pylab.xlabel('time (sec)') pylab.ylabel('Volts') #pylab.savefig('example2.png',dpi=72) pylab.show() # <markdowncell>
#nextSample=input('enter next sample name:\n') nextSample=raw_input('enter next sample name:\n') if nextSample=='x': cont=0 else: cont=1 nextSample.replace(' ','_') #%% while cont==1: analogData=numpy.zeros((sampleNum,),dtype=numpy.int16) #actually collect the data print('starting data collection') actualRate = UL.cbAInScan(boardNum, lowChan, highChan, sampleNum, sampleRate, gain, analogData,Options) print('data collection complete') voltData = numpy.asarray([ UL.cbToEngUnits(boardNum, gain, int(y)) for y in analogData]) #take FFT #following matlab code to try and get scaling right--need to check complexFreqData=numpy.fft.fft(voltData)/sampleNum freqData=complexFreqData[:sampleNum/2+1] frequencies=actualRate*numpy.linspace(0,1,sampleNum/2+1) db=10*numpy.log10(2*numpy.abs(freqData)) plt.figure(figsize=(16,6),dpi=50) plt.subplot(2,1,1) fakeX=numpy.linspace(0,sampleNum,sampleNum)/actualRate downsample=100 plt.plot(fakeX[0::downsample],voltData[0::downsample],'-',linewidth=0.1) plt.xlabel('time (s)') plt.ylabel('voltage (V)')
#example2.py import UniversalLibrary as UL import time BoardNum = 0 Gain = UL.BIP5VOLTS Chan = 0 tstart = time.time() data = [] times = [] while 1: DataValue = UL.cbAIn(BoardNum, Chan, Gain) EngUnits = UL.cbToEngUnits(BoardNum, Gain, DataValue) data.append( EngUnits ) times.append( time.time()-tstart ) if times[-1] > 1.0: break import pylab pylab.plot(times,data,'o-') pylab.xlabel('time (sec)') pylab.ylabel('Volts') #pylab.savefig('example2.png',dpi=72) pylab.show() # <markdowncell> # Now the output values are in volts:
def lick_detection(SIDE): EngUnits = 0 DataValue = 0 if SIDE == 0: while ((EngUnits) * (EngUnits))**0.5 < 1.55: DataValue = UL.cbAIn(0, SIDE, UL.BIP5VOLTS) EngUnits = UL.cbToEngUnits(0, UL.BIP5VOLTS, DataValue) print((EngUnits) * (EngUnits))**0.5 else: get_reward(UL.FIRSTPORTA) a = np.zeros((1000, 1)) i = 0 for i in range(1000): DataValue = UL.cbAIn(0, 0, UL.BIP5VOLTS) EngUnits = UL.cbToEngUnits(0, UL.BIP5VOLTS, DataValue) a[i] = EngUnits i = i + 1 b = np.linspace(0, 999, 1000) print 'mean ', np.mean(a) print 'max ', np.max(a) print 'min ', np.min(a) plt.plot(b, a) plt.show()