class PowerControl(object): ''' PowerControl class wraps language around the 1014_2 - PhidgetInterfaceKit 0/0/4 4 relay device. ''' def __init__(self): #log.info("Start of power control object") pass def open_phidget(self): ''' Based on the InterfaceKit-simple.py example from Phidgets, create an relay object, attach the handlers, open it and wait for the attachment. This function's primarily purpose is to replace the prints with log statements. ''' try: self.interface = InterfaceKit() except RuntimeError as e: log.critical("Phidget runtime exception: %s" % e.details) return 0 try: self.interface.setOnAttachHandler( self.interfaceAttached ) self.interface.setOnDetachHandler( self.interfaceDetached ) self.interface.setOnErrorhandler( self.interfaceError ) except PhidgetException as e: log.critical("Phidget Exception %i: %s" % (e.code, e.details)) return 0 try: #print "Force open relay serial: 290968" self.interface.openPhidget() except PhidgetException as e: log.critical("Phidget Exception %i: %s" % (e.code, e.details)) return 0 #log.info("Waiting for attach....") try: self.interface.waitForAttach(100) except PhidgetException as e: log.critical("Phidget Exception %i: %s" % (e.code, e.details)) try: self.interface.closePhidget() except PhidgetException as e: log.critical("Close Exc. %i: %s" % (e.code, e.details)) return 0 return 1 #Event Handler Callback Functions def interfaceAttached(self, e): attached = e.device #log.info("interface %i Attached!" % (attached.getSerialNum())) def interfaceDetached(self, e): detached = e.device log.info("interface %i Detached!" % (detached.getSerialNum())) def interfaceError(self, e): try: source = e.device log.critical("Interface %i: Phidget Error %i: %s" % \ (source.getSerialNum(), e.eCode, e.description)) except PhidgetException as e: log.critical("Phidget Exception %i: %s" % (e.code, e.details)) def close_phidget(self): try: self.interface.closePhidget() except PhidgetException as e: log.critical("Phidget Exception %i: %s" % (e.code, e.details)) return 0 return 1 def change_relay(self, relay=0, status=0): ''' Toggle the status of the phidget relay line to low(0) or high(1)''' try: self.interface.setOutputState(relay, status) #self.emit_line_change(relay, status) except Exception as e: log.critical("Problem setting relay on %s" % e) return 0 return 1 ''' Convenience functions ''' def zero_on(self): #log.info("Zero relay on") return self.change_relay(relay=ZERO_RELAY, status=1) def zero_off(self): return self.change_relay(relay=ZERO_RELAY, status=0) def one_on(self): #log.info("one relay on") return self.change_relay(relay=ONE_RELAY, status=1) def one_off(self): return self.change_relay(relay=ONE_RELAY, status=0) def two_on(self): #log.info("two relay on") return self.change_relay(relay=TWO_RELAY, status=1) def two_off(self): return self.change_relay(relay=TWO_RELAY, status=0) def three_on(self): #log.info("two relay on") return self.change_relay(relay=THREE_RELAY, status=1) def three_off(self): return self.change_relay(relay=THREE_RELAY, status=0) def toggle_line(self, line=0): ''' Read the internal state of the specified line, then set the opposite state for a toggle function''' if not self.open_phidget(): log.critical("Problem opening phidget") return 0 try: curr_state = self.interface.getOutputState(line) except Exception as e: log.critical("Problem getting relay on %s" % e) self.close_phidget() return 0 if not self.change_relay(line, not curr_state): log.critical("Problem changing relay") return 0 if not self.close_phidget(): log.criticla("Problem closing phidget") return 0 return 1
class DewarFill(object): def __init__(self, master): self.master = master self.fill_log_name='/sandbox/lsst/lsst/GUI/particles/fill_log.dat' self.valve = InterfaceKit() self.comm_status = False self.ov_temp = 999.0 self.valve_state = "Closed" return def Check_Communications(self): # Checks on communications status with the Dewar Fill relay self.comm_status = False try: self.valve.openPhidget(431944) # Serial number 431944 is the Dewar valve Phidget self.valve.waitForAttach(10000) if self.valve.isAttached() and self.valve.getSerialNum() == 431944: self.comm_status = True self.valve.closePhidget() print "Successfully initialized DewarFill Relay\n" sys.stdout.flush() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print "Failed to initialize DewarFill Relay\n" sys.stdout.flush() self.valve.closePhidget() return def StartFill(self): # Opens the Dewar fill valve try: self.valve.openPhidget(431944) # Serial number 431944 is the Dewar valve Phidget self.valve.waitForAttach(1000) if self.valve.isAttached() and self.valve.getSerialNum() == 431944: time.sleep(0.1) self.valve.setOutputState(0,True) # This opens the valve self.valve.setOutputState(1,True) time.sleep(2.0) self.valve.closePhidget() self.valve.openPhidget(431944) # Serial number 431944 is the Dewar valve Phidget self.valve.waitForAttach(1000) if self.valve.isAttached() and self.valve.getSerialNum() == 431944: time.sleep(0.1) state0 = self.valve.getOutputState(0) state1 = self.valve.getOutputState(1) if state0 and state1: time.sleep(0.1) self.valve.closePhidget() print "Successfully initiated Dewar fill at ", datetime.datetime.now() sys.stdout.flush() time.sleep(0.1) self.valve.closePhidget() time.sleep(0.1) return True else: print "Error 1 in initiating Dewar fill at ", datetime.datetime.now() sys.stdout.flush() self.valve.closePhidget() return False except PhidgetException as e: print "Error 2 in initiating Dewar fill at ", datetime.datetime.now() print("Phidget Exception %i: %s" % (e.code, e.details)) sys.stdout.flush() self.valve.closePhidget() return False def StopFill(self): # Closes the Dewar fill valve try: self.valve.openPhidget(431944) # Serial number 431944 is the Dewar valve Phidget self.valve.waitForAttach(1000) if self.valve.isAttached() and self.valve.getSerialNum() == 431944: time.sleep(0.1) self.valve.setOutputState(0,False) # This closes the valve self.valve.setOutputState(1,False) time.sleep(1.0) self.valve.closePhidget() self.valve.openPhidget(431944) # Serial number 431944 is the Dewar valve Phidget self.valve.waitForAttach(1000) if self.valve.isAttached() and self.valve.getSerialNum() == 431944: time.sleep(0.1) state0 = self.valve.getOutputState(0) state1 = self.valve.getOutputState(1) if not state0 and not state1: time.sleep(0.1) self.valve.closePhidget() print "Successfully terminated Dewar fill at ", datetime.datetime.now() sys.stdout.flush() time.sleep(0.1) self.valve.closePhidget() time.sleep(0.1) return True else: print "Error 1 in terminating Dewar fill at ", datetime.datetime.now() sys.stdout.flush() self.valve.closePhidget() return False except PhidgetException as e: print "Error 2 in terminating Dewar fill at ", datetime.datetime.now() print("Phidget Exception %i: %s" % (e.code, e.details)) sys.stdout.flush() self.valve.closePhidget() return False def MeasureOverFlowTemp(self): # Measures the temperature in the overflow cup # returns both the valve state and the temperature try: self.valve.openPhidget(431944) # Serial number 431944 is the Dewar valve Phidget self.valve.waitForAttach(1000) if self.valve.isAttached() and self.valve.getSerialNum() == 431944: sensor = self.valve.getSensorValue(6) NumTries = 0 while sensor < 0.01 and NumTries < 5: time.sleep(0.1) sensor = self.valve.getSensorValue(6) NumTries += 1 self.ov_temp = sensor * 0.2222 - 61.111 state0 = self.valve.getOutputState(0) state1 = self.valve.getOutputState(1) state = state0 and state1 if state: self.valve_state = "Open" if not state: self.valve_state = "Closed" self.valve.closePhidget() return [state, self.ov_temp] else: self.valve.closePhidget() return [False, 999.0] except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) sys.stdout.flush() self.valve.closePhidget() return [False, 999.0] def MeasureOverFlowTempGUI(self): # Measures the temperature in the overflow cup [state, temp] = self.MeasureOverFlowTemp() if state: self.valve_state = "Open" if not state: self.valve_state = "Closed" self.ovtemp_text.set("Overflow Temp="+str(self.ov_temp)+" Valve State="+self.valve_state) return def LogFill(self): # Logs the Dewar Fill jd = Time(datetime.datetime.now(), format='datetime').jd out = "%.4f \n"%(jd) file = open(self.fill_log_name, 'a') file.write(out) file.close() time.sleep(0.1) return def Define_Frame(self): """ Dewar Fill frame in the Tk GUI. Defines buttons and their location""" self.frame=Frame(self.master, relief=GROOVE, bd=4) self.frame.grid(row=3,column=4,rowspan=1,columnspan=2) frame_title = Label(self.frame,text="Manual Dewar Fill",relief=RAISED,bd=2,width=36, bg="light yellow",font=("Times", 16)) frame_title.grid(row=0, column=0) fill_but = Button(self.frame, text="Start Dewar Fill", width=36, command=self.StartFill) fill_but.grid(row=1,column=0) stop_but = Button(self.frame, text="Stop Dewar Fill", width=20, command=self.StopFill) stop_but.grid(row=2,column=0) log_but = Button(self.frame, text="Log Dewar Fill", width=20, command=self.LogFill) log_but.grid(row=3,column=0) ovtemp_but = Button(self.frame, text="Check Overflow Temp", width=16,command=self.MeasureOverFlowTempGUI) ovtemp_but.grid(row=4,column=0) self.ovtemp_text=StringVar() ovtemp_out = Label(self.frame,textvariable=self.ovtemp_text) self.ovtemp_text.set("Overflow Temp="+str(self.ov_temp)+" Valve State="+self.valve_state) ovtemp_out.grid(row=5,column=0) return
class Relay(object): """ Relay class wraps language around the 1014_2 - PhidgetInterfaceKit 0/0/4 4 relay device. Also works for SSR relays.""" def __init__(self, in_serial=None): # http://victorlin.me/posts/2012/08/26/\ # good-logging-practice-in-python self.log = logging.getLogger(__name__) if in_serial != None: # On odroid C1, int conversion raises null byte in argument # strip out the null byte first in_serial = in_serial.strip('\0') self._serial = int(in_serial) else: self._serial = None self.log.debug("Start of phidgeter with serial: %s" % in_serial) def change_relay(self, relay=0, status=0): """ Toggle the status of the phidget relay line to low(0) or high(1) status """ self.interface.setOutputState(relay, status) return 1 def open_phidget(self): self.log.debug("Attempting to open phidget") self.interface = InterfaceKit() if self._serial != None: self.log.debug("Attempt to open serial: %s" % self._serial) self.interface.openPhidget(self._serial) else: self.log.debug("Attempt to open first found") self.interface.openPhidget() wait_interval = 10300 self.log.debug("Wait for attach %sms" % wait_interval) self.interface.waitForAttach(wait_interval) self.log.info("Opened phidget") return 1 def close_phidget(self): self.log.debug("Attempting to close phidget") self.interface.closePhidget() self.log.info("Closed phidget") return 1 def open_operate_close(self, relay, status): """ Open the phidget, change the relay to status, close phidget. """ self.open_phidget() result = self.change_relay(relay, status) self.close_phidget() return result def open_toggle_close(self, relay): """ Find the current status of the specified relay, and set the status to the opposite. """ self.open_phidget() curr_state = self.interface.getOutputState(relay) result = self.change_relay(relay, not curr_state) self.close_phidget() return result def zero_on(self): return self.open_operate_close(relay=0, status=1) def zero_off(self): return self.open_operate_close(relay=0, status=0) def zero_toggle(self): return self.open_toggle_close(relay=0) def one_on(self): return self.open_operate_close(relay=1, status=1) def one_off(self): return self.open_operate_close(relay=1, status=0) def one_toggle(self): return self.open_toggle_close(relay=1) def two_on(self): return self.open_operate_close(relay=2, status=1) def two_off(self): return self.open_operate_close(relay=2, status=0) def two_toggle(self): return self.open_toggle_close(relay=2) def three_on(self): return self.open_operate_close(relay=3, status=1) def three_off(self): return self.open_operate_close(relay=3, status=0) def three_toggle(self): return self.open_toggle_close(relay=3)