diff --git a/ChimeraControls.py b/ChimeraControls.py index 6f23424..e20759b 100644 --- a/ChimeraControls.py +++ b/ChimeraControls.py @@ -1,216 +1,216 @@ #import ChimeraSettings import os import sys import numpy as np import scipy from scipy import signal as si import math import time import datetime def upsample(s, n, phase=0): """Increase sampling rate by integer factor n with included offset phase. """ return np.roll(np.kron(s, np.r_[1, np.zeros(n-1)]), phase) def InitializeChimera(ChimeraSettings, xem): ndevices = xem.GetDeviceCount() if ndevices == 0: print('No Device Attached!!') return -1 serials = xem.GetDeviceListSerial(0) models = xem.GetDeviceListModel(0) print('OK Devices detected (Model: {}) . Serial number of device 1 is {Serial} '.format(ndevices, models, Serial=serials)) bitfilename = 'bitfile_fpga_spartan6_usb3.bit' openanswer = xem.OpenBySerial(serials) print('Device opened with code {}'.format(openanswer)) confanswer = xem.ConfigureFPGA(bitfilename) print('Device configured with code {}'.format(confanswer)) ans1 = xem.SetWireInValue(ChimeraSettings.EP_WIREIN_TEST1, ChimeraSettings.EPBIT_GLOBALRESET, ChimeraSettings.EPBIT_GLOBALRESET) xem.UpdateWireIns() time.sleep(0.1) ans2 = xem.SetWireInValue(ChimeraSettings.EP_WIREIN_TEST1, ChimeraSettings.EPBIT_ENABLEADCFIFO, ChimeraSettings.EPBIT_ENABLEADCFIFO) xem.UpdateWireIns() time.sleep(0.1) ans3 = xem.SetWireInValue(ChimeraSettings.EP_WIREIN_TEST1, 0, ChimeraSettings.EPBIT_GLOBALRESET) xem.UpdateWireIns() time.sleep(0.1) ans4 = xem.SetWireInValue(ChimeraSettings.EP_WIREIN_TEST1, ChimeraSettings.EPBIT_HEADSTAGE_PWR_LED, ChimeraSettings.EPBIT_HEADSTAGE_PWR_LED) xem.UpdateWireIns() time.sleep(0.1) - CHIMERA_updateDACvalues1(ChimeraSettings,xem) + CHIMERA_updateDACvalues1(ChimeraSettings,xem,0) CHIMERA_updateDPOTvalues1(ChimeraSettings, xem) # Set Input SetADCInput(ChimeraSettings, xem, 'Filtered') outtext = CHIMERA_bandwidthtimer(ChimeraSettings, xem) def CHIMERA_process_triggers(ChimeraSettings,xem): xem.UpdateTriggerOuts() ba = bytearray(ChimeraSettings.ADC_pkt_len) out = xem.ReadFromBlockPipeOut(ChimeraSettings.EP_PIPEOUT_ADC, ChimeraSettings.ADC_xfer_blocksize, ba) #print(out) #int=np.uint8(ba) #bytesread = len(readbytes) #typecasted = int.view(np.uint32) blockvalues = np.uint16((np.uint8(ba)).view(np.uint32)) return blockvalues #globals.read_monitor += 2*len(globals.blockvalues) ?? def ConvertBlockvalues(ChimeraSettings,readvalues): #readvalues=g.blockvalues mygain = ChimeraSettings.SETUP_TIAgain * ChimeraSettings.SETUP_preADCgain bitmask = (2**16 - 1) - (2**(16-ChimeraSettings.ADCBITS) - 1) readvalues = -ChimeraSettings.ADCVREF + (2*ChimeraSettings.ADCVREF) * (readvalues & bitmask) / 2**16 readvalues = 10**9*(readvalues/mygain + ChimeraSettings.SETUP_pAoffset) return readvalues def lowpass(ChimeraSettings, readvalues, cutoff = None): #currentRMSRaw = np.std(readvalues) if cutoff==0: displaybuffer = si.resample(readvalues, int(1/ChimeraSettings.displaysubsample*len(readvalues))) else: downsampled = si.resample(readvalues, int(1/ChimeraSettings.displaysubsample*len(readvalues))) effsamplerate=(ChimeraSettings.ADCSAMPLERATE/ChimeraSettings.displaysubsample) Wn = round(2*cutoff/effsamplerate, 4) b, a = si.bessel(4, Wn, btype='low', analog=False) displaybuffer = si.filtfilt(b, a, downsampled) displaybuffer =g.displaybuffer[100:len(displaybuffer)-100] return displaybuffer def CHIMERA_updateDACvalues1(ChimeraSettings,xem, newbiasvalue): Cfastvalue = ChimeraSettings.CFAST_CAP0 #Cfastvalue = globals.CFAST_CAP0+globals.CFAST_CAP1 #for extended range newCfastDACvoltage = - newbiasvalue * ChimeraSettings.myCfast / (ChimeraSettings.CFAST_gain*Cfastvalue) biasDACcode = np.uint16((-newbiasvalue - ChimeraSettings.voltageOffset + 0.5 * ChimeraSettings.DACFULLSCALE)/ChimeraSettings.DACFULLSCALE*(math.pow(2, ChimeraSettings.DACBITS))) fastcDACcode = np.uint16((-newCfastDACvoltage + 0.5*ChimeraSettings.DACFULLSCALE)/ChimeraSettings.DACFULLSCALE * (math.pow(2, ChimeraSettings.DACBITS))) mydacvector = np.uint32(VECTOR_biasDAC_AD5541A(ChimeraSettings,biasDACcode,fastcDACcode)) CHIMERA_sendscanvector1(ChimeraSettings, xem, np.transpose(mydacvector)) def CHIMERA_sendscanvector1(ChimeraSettings,xem, scanvectorfile): bitinversions = 0 pack32 = np.uint32(scanvectorfile) packbixor = pack32 | bitinversions packet1 = packbixor.view(np.uint8) packet = bytearray(packet1) modu = np.mod(len(packet), 16) if modu: packet.reverse() for x in range(0, modu): packet.append(0) packet.reverse() bytes_sent = xem.WriteToPipeIn(ChimeraSettings.EP_PIPEIN_SCANCHAINS, packet) def CHIMERA_updateDPOTvalues1(ChimeraSettings, xem): myDPOTvalue = ChimeraSettings.freqComp DPOTbits = 8 DPOTcode = np.uint8(np.round(myDPOTvalue/100*(math.pow(2,DPOTbits)))) mydacvector = np.concatenate([VECTOR_DPOT_AD5262(ChimeraSettings, DPOTcode, 0), VECTOR_DPOT_AD5262(ChimeraSettings, DPOTcode, 1)]) CHIMERA_sendscanvector1(ChimeraSettings, xem, mydacvector) def VECTOR_DPOT_AD5262(ChimeraSettings, DPOTvalue, DPOTaddress): DPOTdata = np.zeros(ChimeraSettings.DPOT_SCANCHAIN_LENGTH, dtype=np.int) DPOTdata[ChimeraSettings.DPOT_A-1] = DPOTaddress DPOTbits = 8 getBin = lambda x, n: x >= 0 and str(bin(x))[2:].zfill(n) or "-" + str(bin(x))[3:].zfill(n) binaryarray = getBin(DPOTvalue,DPOTbits) binaryarray = np.array(list(binaryarray),dtype=int) DPOTdata[ChimeraSettings.DPOT_LSB-1:ChimeraSettings.DPOT_MSB] = np.flipud(binaryarray) returnvector = ChimeraSettings.EPBIT_DPOT_SDI * DPOTdata returnvector1 = np.array(upsample(returnvector,2,0), dtype=np.int) returnvector2 = np.array(upsample(returnvector,2,1), dtype=np.int) returnvector = returnvector1 + returnvector2 vectorlength = len(returnvector) v = np.arange(2, vectorlength + 2, dtype=np.int) myclk = 0.5 + 0.5 * scipy.signal.square(1/1*3.1416*v) returnvector = returnvector + np.array(ChimeraSettings.EPBIT_DPOT_CLK * myclk,dtype=np.int) returnvector = np.flipud(returnvector) returnvector = np.concatenate((np.array([ChimeraSettings.EPBIT_DPOT_NCS,0],dtype=int), returnvector, np.array([0, ChimeraSettings.EPBIT_DPOT_NCS],dtype=int))) returnvector = returnvector + np.array(ChimeraSettings.EP_ACTIVELOWBITS-ChimeraSettings.EPBIT_DPOT_NCS,dtype=int) return returnvector def VECTOR_biasDAC_AD5541A(ChimeraSettings,biasdacvalue, fastcdacvalue): biasDACdata = np.zeros(ChimeraSettings.DAC_SCANCHAIN_LENGTH, dtype=np.int) fastcDACdata = np.zeros(ChimeraSettings.DAC_SCANCHAIN_LENGTH, dtype=np.int) DACbits = 16 biasDACbinary = np.zeros(DACbits, dtype=np.int) binarystring = "{0:b}".format(biasdacvalue) binaryarray = np.array(list(binarystring), dtype=int) biasDACbinary[DACbits-len(binaryarray):DACbits] = binaryarray fastcDACbinary = np.zeros(DACbits, dtype=np.int) binarystring = "{0:b}".format(fastcdacvalue) binaryarray = np.array(list(binarystring), dtype=int) fastcDACbinary[DACbits-len(binaryarray):DACbits] = binaryarray biasDACdata = np.flipud(biasDACbinary) fastcDACdata = np.flipud(fastcDACbinary) returnvector = ChimeraSettings.EPBIT_DACSDI * biasDACdata + ChimeraSettings.EPBIT_DACSDI_FASTC * fastcDACdata upreturn = upsample(returnvector,2,0) + upsample(returnvector,2,1) vectorlength = len(upreturn) v = np.arange(2, vectorlength + 2, dtype=np.int) myclk = 0.5 + 0.5*scipy.signal.square(1/1*3.1416*v) returnvector = upreturn + ChimeraSettings.EPBIT_DACCLK*myclk returnvector = np.flipud(returnvector) returnvector = np.concatenate([[ChimeraSettings.EPBIT_DACNCS + ChimeraSettings.EPBIT_DAC_NLOAD, ChimeraSettings.EPBIT_DACNCS + ChimeraSettings.EPBIT_DAC_NLOAD, ChimeraSettings.EPBIT_DAC_NLOAD], returnvector + ChimeraSettings.EPBIT_DAC_NLOAD, [ChimeraSettings.EPBIT_DACNCS + ChimeraSettings.EPBIT_DAC_NLOAD, ChimeraSettings.EPBIT_DACNCS + ChimeraSettings.EPBIT_DAQTRIGGER, ChimeraSettings.EPBIT_DACNCS + ChimeraSettings.EPBIT_DAC_NLOAD]]) returnvector = returnvector + (ChimeraSettings.EP_ACTIVELOWBITS - ChimeraSettings.EPBIT_DACNCS - ChimeraSettings.EPBIT_DAC_NLOAD) lastelement = returnvector[len(returnvector)-1] returnvector = np.concatenate([returnvector, lastelement*np.ones(32)]) return returnvector def SetADCInput(ChimeraSettings, xem, mux): wiremask = ChimeraSettings.EPBIT_ADC1MUX0 + ChimeraSettings.EPBIT_ADC1MUX1 wirevalue=0 if mux == 'VIN1': wirevalue = wirevalue + ChimeraSettings.EPBIT_ADC1MUX1 if mux == 'Filtered': wirevalue = wirevalue + ChimeraSettings.EPBIT_ADC1MUX0 if mux == 'AUXJ14': wirevalue = 3 if mux == 'Unfiltered': wirevalue = 0 out = xem.SetWireInValue(ChimeraSettings.EP_WIREIN_ADCMUX, wirevalue, wiremask) xem.UpdateWireIns() # Bandwith Timer def CHIMERA_bandwidthtimer(ChimeraSettings, xem): np.set_printoptions(precision=3) #globals.init() mytime=datetime.datetime.today()-ChimeraSettings.timezero mytimestring=str(mytime) text_time=mytimestring text_readrate=ChimeraSettings.read_monitor/2000 text_writerate=ChimeraSettings.write_monitor/2000 xem.UpdateWireOuts() fifolevel = 4*(xem.GetWireOutValue(ChimeraSettings.EP_WIREOUT_ADCFIFOCOUNT) & int('0111111111111111', 2)) #globals.write_monitor=0 #globals.read_monitor=0 textOutputs = {'text_time': text_time, 'USB_readrate': text_readrate,'USB_writerate':text_writerate,'buffer':fifolevel, 'bufferPC': math.floor(100*fifolevel/ChimeraSettings.HWBUFFERSIZE_KB), 'bufferseconds': fifolevel*1024/(100e6 / 24 * 2)} return textOutputs \ No newline at end of file