def getStatus(self): """ Read the voltages and temperature of the LabJack. Returns a dictionary of monitor points and values """ stats = {} channels = channelmap.keys() channels.sort() ains = [u3.AIN(x,31,True) for x in channels] tic = time.time() meas = self.lj.getFeedback(ains) corelog.debug("got FORX measurements in: %.2f ms" % ((time.time()-tic)*1000)) for n,ch in enumerate(channels): if ch < 4: volts = self.lj.binaryToCalibratedAnalogVoltage(meas[n],isLowVoltage = False, isSingleEnded = True) else: volts = self.lj.binaryToCalibratedAnalogVoltage(meas[n],isLowVoltage = True, isSingleEnded = True) stats[ch] = volts*2 fields = {} for k,v in stats.items(): fields[channelmap[k]] = v fields['RackTemp'] = self.lj.getTemperature() - 273.15 # convert K to C return fields
def findTCT(devs): """ Identify the TCT by looking for the time stamp emitted at 1 second intervals """ for dev in devs: try: ser = serial.Serial(dev) except Exception,e: logger.warning("findTCT: Couldn't open serial port: %s... moving to next port" % dev) continue ser.setBaudrate(9600) ser.setParity('O') ser.setByteSize(7) ser.setTimeout(2) ser.flushInput() tries = 0 while tries < 2: r = ser.readline(eol='\r') logger.debug('findTCT: Trying %s Trial # %d got %s' % (dev,tries,r)) if len(r) == 18: break tries += 1 if tries == 2: logger.info('findTCT: failed to find TCT on port %s' % dev) ser.setParity('N') ser.setByteSize(8) ser.close() continue else: logger.info('findTCT: Found TCT on port %s' % dev) ser.close() return dev
def findPowerSupply(devs): """ Identify the Lambda Power supply which provdes +5 V to the SPSS equipment """ for dev in devs: try: ser = serial.Serial(dev) except Exception,e: logger.warning("findPowerSupply: Couldn't open serial port: %s... moving to next port" % dev) continue ser.setBaudrate(9600) ser.setParity('N') ser.setByteSize(8) ser.setTimeout(1) ser.flushInput() tries = 0 while tries < 3: ser.flushInput() ser.write('adr 6\r') r = ser.readline(eol='\r') logger.debug('findPowerSupply: Trying %s Trial # %d got %s' % (dev,tries,r)) if r.find('OK') >= 0: break tries += 1 if tries == 3: logger.info('findPowerSupply: failed to find on port %s' % dev) ser.close() continue else: logger.info('findPowerSupply: Found on port %s' % dev) ser.close() return dev
def findValon(devs): """ Identify the Valon synthesizer """ for dev in devs: try: ser = serial.Serial(dev) except Exception,e: logger.warning("findValon: Couldn't open serial port: %s... moving to next port" % dev) continue ser.setBaudrate(9600) ser.setParity('N') ser.setByteSize(8) ser.setTimeout(1) ser.flushInput() tries = 0 while tries < 3: ser.flushInput() ser.write(struct.pack('>B',0x82)) r = ser.read(16) logger.debug('findValon: Trying %s Trial # %d got %s' % (dev,tries,r)) if r.find('Synth') >= 0: break tries += 1 if tries == 3: logger.info('findValon: failed to find on port %s' % dev) ser.close() continue else: logger.info('findValon: Found on port %s' % dev) ser.close() return dev
def clear_personality(self): corelog.debug("%s Clearing personality" % self.name) self.personality = None with self.realtime_lock: if self.realtime_h5: self.realtime_h5.close() self.realtime_h5 = None corelog.debug("%s Closing realtime h5" % self.name)
def setFreq(self, f): """ Set output frequency in MHz """ corelog.debug("Setting frequency to %f" % f) settings = self.calcPLL(f) self.setPLL(settings) self.freq = f return settings
def loop(self,interval=3600): while True: corelog.info("Getting UPS data and inserting in database...") try: self.getData() self.insertData() except: corelog.exception("Failed to get UPS data and insert to database") corelog.debug("UPSLogger sleeping for %d seconds" % interval) time.sleep(interval)
def _init_rtbuf(self): self.realtime_filename = "/tmp/rt%d.h5" % self.id corelog.info("Starting realtime data capture. Creating %s" %self.realtime_filename) # try to open the realtime h5 file for writing with self.realtime_lock: try: self.realtime_h5 = tables.openFile(self.realtime_filename, "w") except Exception, e: corelog.exception("could not open new realtime h5 file for writing: %s" % self.realtime_filename) self.realtime_h5 = None self.realtime_filename = None return # if no iBOBs are registered, error and quit personality = self.personality iBOB_group = self.realtime_h5.root self.realtime_h5.createTable(iBOB_group, "file_info", dict(personality=tables.StringCol(128,dflt=' '))) iBOB_group.file_info.row['personality'] = self.personality.__class__.__name__ iBOB_group.file_info.row.append() iBOB_group.file_info.flush() corelog.debug("set personality: %s" % str(iBOB_group.file_info[:])) self.realtime_iBOB_group = iBOB_group self.realtime_measurements = {} iBOB_meas = self.realtime_measurements measurement_types = personality._measTypesDict for measurement_type in measurement_types.keys(): corelog.debug("adding measurement type: %s" % measurement_type) iBOB_meas[measurement_type] = {} meas_grp = self.realtime_h5.createGroup(iBOB_group,measurement_type) iBOB_meas[measurement_type]['group'] = meas_grp thistable = \ self.realtime_h5.createTable(meas_grp, 'table', measurement_types[measurement_type]['table'], expectedrows=2000) iBOB_meas[measurement_type]['table'] = thistable iBOB_meas[measurement_type]['arrays'] = {} iBOB_meas[measurement_type]['index'] = self.realtime_h5.createArray(meas_grp, 'index', np.zeros((1,),dtype='uint32')) for name,shape in measurement_types[measurement_type]['arrays'].items(): if (name.lower().find('adc') >= 0): fullshape = tuple([16]+list(shape)) # kludge to reduce wasted space on lots of adc snapshots elif shape[0] > 1024: fullshape = tuple([2**20/shape[0]]+list(shape)) #keep size = 1Mpoint else: fullshape = tuple([MAX_REALTIME_ROWS]+list(shape)) thisarr = self.realtime_h5.createArray(meas_grp, name, np.zeros(fullshape,dtype='float32')) iBOB_meas[measurement_type]['arrays'][name] = thisarr self.realtime_infotable = \ self.realtime_h5.createTable(iBOB_group, "InfoTable", personality._infoTable, expectedrows = 2000)
def checkConnected(self): try: self.sock.getpeername() except: self._connected = False self.addr = None corelog.warning("Xant not connected") return False corelog.debug("Xant is connected") self._connected = True return True
def selectIBOB(self,ibob): """ Select a given ibob with multiplexer circuits """ if ibob < 4: self.syn0.seliBOB(ibob) corelog.debug("bank 0, selected ibob %d" %ibob) else: ibobsel = ibob - 4 self.syn1.seliBOB(ibobsel) corelog.debug("bank 1, selected ibob %d (sel: %d)" %(ibob,ibobsel))
def set_personality(self, personality,adcClock=1024.0): """ sets the personality of a specific iBOB """ if self.writing or self.personality: corelog.warning("%s cannot set personality: it's already set" % self.name) return corelog.debug("%s Setting personality %s %s" % (self.name,str(personality), personalities.__file__)) self.personality = personality(adcClock=adcClock) # removed self as parent try: self._init_rtbuf() except Exception,e: corelog.exception("Could not init realtime buf for ibob %d %s"%(self.id,str(self.personality)))
def checkConnected(self): """ Check that connection is still available to RCT. Returns bool """ print "checking connection" try: self.sock.getpeername() except: self._connected = False self.addr = None corelog.warning("RSS not connected") return False corelog.debug("RSS is connected") self._connected = True return True
def parseData(self,fname): from matplotlib import mlab corelog.debug("Parsing UPS data file: %s",fname) def myfloat(x): try: return float(x) except: return 0.0 names = ['date','time','vmin','vmax','vout','wout','freq','cap','vbat','tupsc'] convd = {} for k in names: convd[k] = myfloat dp = dateutil.parser.parse convd['date'] = dp convd['time'] = dp try: res = mlab.csv2rec(fname,skiprows=6,names=names,converterd = convd,delimiter='\t') except Exception,e: corelog.exception("Could not parse UPS data file")
def programIBOBs(self,ibobs,ipfid=None,ipffile=None,force=False): """ Program a list of iBOBs with a given configuration file. The configuration file can be explicitly provided or an index to the database of designs can be given Arguments: *ibobs* : list of ints Which ibobs to program *ipfid* : int Record ID of row in dss28_spec.ibob_designs that indicates the desired configuration *ipffile* : str (advanced use only) Instead of a design in the database, use this IPF explicitly *force* : bool Program the ibob even if we think it may already be programmed with same design. """ if self.sql: if ipfid: try: resd = self.gdb.getIPF(ipfid) ipf = resd['IPF'] ipfdir = resd['Directory'] ipffile = os.path.join(ipfdir,ipf) except Exception, e: corelog.exception("Could not resolve programming file from database") raise e corelog.debug("Found ipfID: %d correspoing to %s" % (ipfid, ipffile)) else: ipfid = 1 #we are connected to database, but programming an arbitrary file, so # tell the database that the design is default if force == False: droplist = [] stat = self.gdb.getSPSSStatus() for ii in ibobs: if stat["iBOBDesignID%d" % ii] == ipfid: droplist.append(ii) corelog.info("iBOB%d is already programmed with this ipf, skipping" % ii) for k in droplist: ibobs.remove(k)
def connect(self): """ Connect to server running Xant, usually dss28-eac """ self.sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind(('',9013)) for addr in XANT_ADDRS: tries = 0 maxtries = 2 corelog.debug("Trying to connect to xant at %s" % (str(addr))) while tries < maxtries: tries += 1 try: self.sock.connect(addr) break except Exception, e: corelog.debug("Failed on %d try\n%s" % (tries,str(e))) if tries < maxtries: corelog.info("Succeeded in connecting to Xant at %s after %d tries" % (str(addr),tries)) self.sock.setblocking(False) self._connected = True self.addr = addr return addr
def _startIbobServers(self,ibs): ns = Pyro.naming.NameServerLocator().getNS() for ib in ibs: if self.iBOBProcesses.has_key(ib): self._stopIbobServer(ib) thisServer = IbobServer(ib) self.iBOBServers[ib] = thisServer thisProcess = Process(target = thisServer.run, args = ()) self.iBOBProcesses[ib] = thisProcess thisProcess.start() corelog.info("Started server for iBOB %d" % ib) corelog.info("Sleeping for 5 seconds while iBOB processes initialize...") time.sleep(5) corelog.debug("Done sleeping, starting to resolve ibobs") for ib in ibs: thisiBOB = ns.resolve(':IBOB.%d'%ib).getProxy() tupd,pers,clk = self.gdb.getPersonality(ib) corelog.info("Database entry at %s indicates iBOB %d, personality: %s clock rate %f MHz" % (time.ctime(tupd),ib,pers,clk)) thisiBOB.set_personality(getattr(personalities,pers)) self.iBOBProxies[ib] = thisiBOB corelog.info("Set personality for ibob %d" % ib)
def send_cmd(self,cmd,timeout=5): """ Main function for sending a commaand string to the RCT and receiving a response. """ try: self.sock.getpeername() except: corelog.warn("Trying to send command but found not connected, so trying to connect") self.connect() while True: try: self.sock.recv(10000) except: break tic = time.time() try: self.send(cmd) corelog.debug("Sent command %s" % cmd) except Exception,e: corelog.exception("Could not send command %s" % cmd) return
def sendget_robust(self,message): """ Send and receive control commands to iBOB using robust UDP protocol """ header_fmt = '>IHBB' self.control_flush() tstart = time.time() while time.time() - tstart < 1.0: msgid = (0xFF<<24)+self.msgid self.msgid += 1 if self.msgid > 0xFFFF00: self.msgid = 0 hdr = struct.pack(header_fmt,msgid,0,0,0) msg = hdr + message self.control_send_msg(msg) tsend = time.time() resp = '' nextseq = 0 while time.time() - tsend < 0.2: r = self.readone() if len(r) >= 8: #print "waiting for seq:",nextseq,("msgid:%08X" %msgid) rxid,seq,type,blah = struct.unpack(header_fmt,r[:8]) #print ("\nrxid:%08X"%rxid),"seq",seq,"type",type if rxid == msgid: if seq == nextseq: resp += r[8:] if type == 2: corelog.debug("%s cmd '%s' response in %.2f ms: '%s'" %(self.name,msg,(time.time()-tstart)*1000,resp)) return resp else: nextseq += 1 continue else: corelog.debug("%s ibob sequence error: expected seq: %d got %d" % (self.name,nextseq,seq,r)) else: corelog.debug("%s expected msgid %08X but got %08X" % (self.name,msgid,rxid)) elif len(r): corelog.debug("%s received: %s" % (self.name,r))
def __init__(self,sql = True): """ *sql* argument is largely a relic, no longer used without database """ Pyro.core.ObjBase.__init__(self) Pyro.config.PYRO_MULTITHREADED = 0 # do not use multiple threads, because we want to ensure programming finishes if sql: self.sql = True corelog.debug("Connecting to GavrtDB") self.gdb = gavrtdb.GavrtDB(rw = True) else: self.sql = False self.gdb = None self.iBOBProcesses = {} self.iBOBServers = {} self.serialPorts = [None,None] self.lj0 = None self.lj1 = None self.ljs = [None,None] self.syns = [None,None] self.syn0 = None self.syn1 = None self.jtagcables = ['usb21','usb22'] corelog.debug("Identifying JTAG programmers from list: %s" % str(self.jtagcables)) try: self.identifyCables() except: corelog.exception("Could not identify JTAG programmers!") corelog.info("JTAG cables identified") corelog.debug("Opening LabJacks") try: self.openLabJacks() except: corelog.exception("Could not open LabJacks!") # Check if synthesizers are already initialized before clobbering them. if not self.synsInitialized(): corelog.info("found synthesizers uninitialized, so initializing to 1024 MHz") self.initialize() self.setSynths(1024.0) corelog.info("Started")
iBOB_meas[measurement_type]['index'] = self.realtime_h5.createArray(meas_grp, 'index', np.zeros((1,),dtype='uint32')) for name,shape in measurement_types[measurement_type]['arrays'].items(): if (name.lower().find('adc') >= 0): fullshape = tuple([16]+list(shape)) # kludge to reduce wasted space on lots of adc snapshots elif shape[0] > 1024: fullshape = tuple([2**20/shape[0]]+list(shape)) #keep size = 1Mpoint else: fullshape = tuple([MAX_REALTIME_ROWS]+list(shape)) thisarr = self.realtime_h5.createArray(meas_grp, name, np.zeros(fullshape,dtype='float32')) iBOB_meas[measurement_type]['arrays'][name] = thisarr self.realtime_infotable = \ self.realtime_h5.createTable(iBOB_group, "InfoTable", personality._infoTable, expectedrows = 2000) corelog.debug("Finished realtime data capture init %s" % self.name) def start_writing(self,filename): self.prepare_for_writing(filename) self.writing = True def prepare_for_writing(self, filename): """ prepares the real h5 (history) file to be written """ self.filename = filename if os.path.isfile(filename): corelog.warning("%s h5 filename %s already exists, overwriting..." % (self.name,filename)) with self.h5_lock:
except: break tic = time.time() try: self._send(cmd) except Exception,e: corelog.exception("Couldn't send command to XANT") return tstart = time.time() resp = '' while (time.time()-tstart < timeout): try: resp = resp + self.sock.recv(1000) if resp.find('completed') >= 0: corelog.debug("Command %s accepted" % cmd) break elif resp.find('rejected') >= 0: corelog.warning("Command %s rejected with response: %s" % (cmd,resp)) break except: corelog.exception("Problem waiting for response from XANT") pass corelog.debug("Xant response time: %.2f ms" %((time.time()-tic)*1000)) time.sleep(0.1) #sleep a moment return resp def runXantInterface(): import Pyro.naming
try: r = prog_ipf(ipffile,cable=self.jtagcables[1],display=True) except Exception, e: corelog.exception("Failure programming ibob %d with ipffile %s" % (ibob, ipffile)) raise e results.append(r) if r[0] == 0: corelog.info("Programming ibob %d was successful. Impact returned: %d Status info:\n\n %s \n\n" % (ibob,r[0],r[1])) else: corelog.error("Programming ibob %d Failed! Impact returned: %d Status info:\n\n %s \n\n" % (ibob,r[0],r[1])) if results[-1][0] ==0: vald["iBOBDesignID%d" % ibob] = ipfid else: vald["iBOBDesignID%d" % ibob] = 1 # design 1 means unknown state corelog.debug("Updating status in db") vald['UnixTime'] = time.time() if self.sql: self.gdb.updateValues(vald, 'spss_config') return results if __name__=="__main__": import Pyro.naming Pyro.core.initServer() ns = Pyro.naming.NameServerLocator().getNS() try: ns.unregister('UtilServer') except: pass
def run(self): self.writing = False self.running = True self.personality = None self.realtime_lock = Lock() self.h5_lock = Lock() name = ':IBOB.'+str(self.id) self.name=name self.iBOB_addr = IBOB_NETWORK + str(self.id+16) self.control_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.control_sock.setblocking(False) self.control_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 7 is the port on which the iBOB listens for commands self.control_sock.connect((self.iBOB_addr, 7)) self.measurements_dict = {} self.measurements_list = [] self.spec_info_table = None self.realtime_infotable = None self.packets_received = 0 self.data_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.data_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.data_sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, RCV_BUFFER_SIZE) self.data_sock.bind(("", IBOB_BASE_PORT+self.id+16)) self.acc = 0 self.number_of_measurements = 0 self.h5 = None self.realtime_h5 = None self.ns = Pyro.naming.NameServerLocator().getNS() try: self.ns.createGroup(':IBOB') #Ensure IBOB group exists except NamingError: pass try: uri = self.ns.resolve(name) try: uri.getProxy().quit() corelog.debug("successfully quit existing daemon for ibob %d" % self.id) time.sleep(2) except: pass try: self.ns.unregister(name) corelog.debug("found stale name in ns, and unregistered for ibob %d" % self.id) except NamingError: pass except NamingError: corelog.debug("No existing daemon registered, good ibob %d" % self.id) self.pd = Pyro.core.Daemon() self.pd.useNameServer(self.ns) self.pd.connect(self, name) corelog.info("Starting %s server" % self.name) self.data_sock.setblocking(False) # crucial to avoid deadlock in loop while self.running: self.pd.handleRequests(1, [self.data_sock], self.processData) self.data_sock.close() corelog.info("%s server done running" % self.name)
break tic = time.time() try: self.send(cmd) corelog.debug("Sent command %s" % cmd) except Exception,e: corelog.exception("Could not send command %s" % cmd) return tstart = time.time() resp = '' while (time.time()-tstart < timeout): try: resp = resp + self.sock.recv(1000) if resp.find('COMPLETED:') >= 0: corelog.debug("RSS successfully executed command %s in %.2f ms" % (cmd,(time.time()-tic)*1000)) time.sleep(0.1) #sleep a moment to ensure receiver is setup return resp except: pass return resp if __name__=="__main__": import Pyro.naming Pyro.core.initServer() ns = Pyro.naming.NameServerLocator().getNS() try: ns.unregister('RSSInterface') except:
def setPLL(self, d): if d is None: return corelog.debug("Setting prescaler = 8/9") self.WriteReg(0x16, 0x04) corelog.debug("Setting r = 100 (100MHz ref)") self.WriteReg(0x11, 100) corelog.debug("Setting a: %d" % d["a"]) self.WriteReg(0x13, d["a"]) (h, l) = divmod(d["b"], 256) corelog.debug("Setting b: %d=%d*256+%d" % (d["b"], h, l)) self.WriteReg(0x14, l) self.WriteReg(0x15, h) VCOdivVal = d["VCOdiv"] - 2 if VCOdivVal < 0 or VCOdivVal > 4: corelog.error("Got bad VCOdivider: %d Original valud %d. Proceeding using 2" % (VCOdivVal, d["VCOdiv"])) VCOdivVal = 0 corelog.debug("Setting VCOdiv: %d, reg: %d" % (d["VCOdiv"], VCOdivVal)) self.WriteReg(0x1E0, VCOdivVal) finaldiv = d["finaldiv"] if finaldiv == 1: corelog.debug("Bypassing final divider") self.WriteReg(0x197, 0x80) self.WriteReg(0x191, 0x80) else: cycles = finaldiv / 2 - 1 corelog.debug("cycles= %d cyclereg: 0x%02x" % (cycles, cycles * 16 + cycles)) self.WriteReg(0x197, 0x00) self.WriteReg(0x191, 0x00) self.WriteReg(0x196, cycles * 16 + cycles) self.WriteReg(0x190, cycles * 16 + cycles) self.WriteReg(0x232, 1) corelog.debug("Calibrating VCO") self.WriteReg(0x18, 0x06) self.WriteReg(0x232, 1) self.WriteReg(0x18, 0x07) self.WriteReg(0x232, 1)