class AnalogPhidget(VanDevice): def __init__(self, parent, name, phidget=-1): self.phidget = Analog() self.phidget.openPhidget(phidget) self.phidget.waitForAttach(0) super(AnalogPhidget, self).__init__(parent, name) @deviceCommand(Int(0, 3)) def enableOutput(self, output): if 'output%i' % output not in self.children: AnalogOutput(self, output)
def AttachAnalog(databasepath, serialNumber): def onAttachHandler(event): logString = "Analog Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Analog Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Analog Error " + str( event.device.getSerialNum()) + ", Error: " + event.description print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "Analog Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Analog Server Disconnect " + str( event.device.getSerialNum()) #print(logString) try: p = Analog() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
def AttachAnalog(databasepath, serialNumber): def onAttachHandler(event): logString = "Analog Attached " + str(event.device.getSerialNum()) #print(logString) DisplayAttachedDeviceInfo(event.device) def onDetachHandler(event): logString = "Analog Detached " + str(event.device.getSerialNum()) #print(logString) DisplayDetachedDeviceInfo(event.device) event.device.closePhidget() def onErrorHandler(event): logString = "Analog Error " + str(event.device.getSerialNum()) + ", Error: " + event.description print(logString) DisplayErrorDeviceInfo(event) def onServerConnectHandler(event): logString = "Analog Server Connect " + str(event.device.getSerialNum()) #print(logString) def onServerDisconnectHandler(event): logString = "Analog Server Disconnect " + str(event.device.getSerialNum()) #print(logString) try: p = Analog() p.setOnAttachHandler(onAttachHandler) p.setOnDetachHandler(onDetachHandler) p.setOnErrorhandler(onErrorHandler) p.setOnServerConnectHandler(onServerConnectHandler) p.setOnServerDisconnectHandler(onServerDisconnectHandler) p.openPhidget(serialNumber) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting...") exit(1)
#Main Program Code try: analog.setOnAttachHandler(AnalogAttached) analog.setOnDetachHandler(AnalogDetached) analog.setOnErrorhandler(AnalogError) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: analog.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: analog.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: analog.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details))
try: #logging example, uncomment to generate a log file #analog.enableLogging(PhidgetLogLevel.PHIDGET_LOG_VERBOSE, "phidgetlog.log") analog.setOnAttachHandler(AnalogAttached) analog.setOnDetachHandler(AnalogDetached) analog.setOnErrorhandler(AnalogError) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Opening phidget object....") try: analog.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) print("Waiting for attach....") try: analog.waitForAttach(10000) except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) try: analog.closePhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details))
class PhidgetsAnalogOut: def __init__(self, topic_1_subscriber=None, topic_2_subscriber=None, topic_publisher=None, channel_1=0, channel_2=0, pulselength=0, pulseinterval=0, pulse=False, serial_number=-1): ########################################################## #Create an analog out object try: self.analog = Analog() except RuntimeError as e: print("Runtime Exception: %s" % e.details) print("Exiting....") exit(1) try: if serial_number != -1: self.analog.openPhidget(serial_number) else: self.analog.openPhidget() except PhidgetException as e: print("Phidget Exception %i: %s" % (e.code, e.details)) print("Exiting....") exit(1) rospy.sleep(1) ########################################################### self.channel_1 = channel_1 self.channel_2 = channel_2 self.pulselength = pulselength self.pulseinterval = pulseinterval self.pulse = pulse self.voltage = 0 self.minsleeptime = 0.05 for channel in [self.channel_1, self.channel_2]: self.analog.setEnabled(channel, True) self.subscriber_1 = rospy.Subscriber(topic_1_subscriber, Float32, self.subscriber_1_callback) self.subscriber_2 = rospy.Subscriber(topic_2_subscriber, Float32, self.subscriber_2_callback) self.publisher = rospy.Publisher(topic_publisher, Float32) nodename = 'phidgets_chrimson_analog' rospy.init_node(nodename, anonymous=True) def subscriber_1_callback(self, data): self.voltage = data.data if not self.pulse: self.analog.setVoltage(self.channel_1, self.voltage) self.publisher.publish(Float32(self.voltage)) rospy.sleep(self.minsleeptime) def subscriber_2_callback(self, data): self.voltage = data.data if not self.pulse: self.analog.setVoltage(self.channel_2, self.voltage) self.publisher.publish(Float32(self.voltage)) rospy.sleep(self.minsleeptime) def pulse_train(self): if self.voltage != 0: self.analog.setVoltage(self.channel, self.voltage) self.publisher.publish(Float32(self.voltage)) rospy.sleep(self.pulselength) self.publisher.publish(Float32(self.voltage)) self.analog.setVoltage(self.channel, 0) self.publisher.publish(Float32(0)) rospy.sleep(self.pulseinterval - self.pulselength) self.publisher.publish(Float32(0)) else: self.analog.setVoltage(self.channel, 0) self.publisher.publish(Float32(0)) rospy.sleep(self.minsleeptime) return def main(self): if self.pulse: while not rospy.is_shutdown(): self.pulse_train() else: rospy.spin()
class AnalogOut(object): """ Class wraps language around the PhidgetAnalog 4-Output ID: 1002_0 analog output device. """ 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 open_phidget(self): self.log.debug("Attempting to open phidget") self.interface = Analog() 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 change_enable(self, output=0, status=0): """ Toggle the enable of the phidget analog output line to enable(1) or disable(0) status """ self.interface.setEnabled(output, status) return 1 def open_operate_close(self, output, status): """ Open the phidget, change the enable status of the analog output, close phidget. """ self.open_phidget() result = self.change_enable(output, status) self.close_phidget() return result def open_toggle_close(self, output): """ Find the current enable status of the specified output, and set the status to the opposite. """ self.open_phidget() curr_state = self.interface.getEnabled(output) res_volt = self.interface.setVoltage(output, 3.3) result = self.change_enable(output, not curr_state) self.close_phidget() return result def zero_enable(self): return self.open_operate_close(output=0, status=1) def zero_disable(self): return self.open_operate_close(output=0, status=0) def zero_toggle(self): return self.open_toggle_close(output=0) def two_enable(self): return self.open_operate_close(output=2, status=1) def two_disable(self): return self.open_operate_close(output=2, status=0) def two_toggle(self): return self.open_toggle_close(output=2)