def __init__(self):
        self.voltage_publisher = rospy.Publisher(
            'CurrentSensor/battery_voltage_draw', Float64, queue_size=10)
        self._hz = 10
        ch1 = VoltageInput()

        channelInfo = ChannelInfo()
        channelInfo.deviceSerialNumber = 539331
        channelInfo.hubPort = 1
        channelInfo.isHubPortDevice = 0
        channelInfo.channel = 0

        ch1.setDeviceSerialNumber(channelInfo.deviceSerialNumber)
        ch1.setHubPort(channelInfo.hubPort)
        ch1.setIsHubPortDevice(channelInfo.isHubPortDevice)
        ch1.setChannel(channelInfo.channel)

        ch1.setOnAttachHandler(self.onAttachHandler)
        ch1.setOnDetachHandler(self.onDetachHandler)
        ch1.setOnErrorHandler(self.onErrorHandler)
        ch1.setOnVoltageChangeHandler(self.onVoltageChangeHandler)
        ch1.setOnSensorChangeHandler(self.onSensorChangeHandler)

        print("\nOpening and Waiting for Attachment...")

        try:
            ch1.openWaitForAttachment(5000)
        except PhidgetException as e:
            PrintOpenErrorMessage(e, ch1)
            raise EndProgramSignal("Program Terminated: Open Failed")
 def __init__(self):
     self.voltage_publisher = rospy.Publisher('CurrentSensor/battery_voltage_draw', Float64, queue_size=10)
 	self._hz = 10
     ch1 = VoltageInput()
        
     channelInfo = ChannelInfo()
     channelInfo.deviceSerialNumber = 539331
     channelInfo.hubPort = 1
     channelInfo.isHubPortDevice = 0
     channelInfo.channel = 0
        
     ch1.setDeviceSerialNumber(channelInfo.deviceSerialNumber)
     ch1.setHubPort(channelInfo.hubPort)
     ch1.setIsHubPortDevice(channelInfo.isHubPortDevice)
     ch1.setChannel(channelInfo.channel)   
         
     ch1.setOnAttachHandler(self.onAttachHandler)
     ch1.setOnDetachHandler(self.onDetachHandler)
     ch1.setOnErrorHandler(self.onErrorHandler)
     ch1.setOnVoltageChangeHandler(self.onVoltageChangeHandler)
     ch1.setOnSensorChangeHandler(self.onSensorChangeHandler)
         
     print("\nOpening and Waiting for Attachment...")
     
     try:
         ch1.openWaitForAttachment(5000)
     except PhidgetException as e:
         PrintOpenErrorMessage(e, ch1)
         raise EndProgramSignal("Program Terminated: Open Failed")
class PowerSource():
    def __init__(self, channel):
        self.volts = None
        self.device = VoltageInput()
        self.channel = channel
        this = self

    def start(self):
        """
		* Add event handlers before calling open so that no events are missed.
		"""
        self.device.setOnAttachHandler(onAttachHandler)
        self.device.setOnDetachHandler(onDetachHandler)
        self.device.setOnErrorHandler(onErrorHandler)
        self.device.setOnVoltageChangeHandler(onVoltageChangeHandler)
        self.device.setOnSensorChangeHandler(onSensorChangeHandler)

        try:
            self.device.setDeviceSerialNumber(271638)
            self.device.setChannel(self.channel)
            self.device.openWaitForAttachment(5000)
        except PhidgetException as e:
            print("Program Terminated: Open PowerSource Failed")
            print(e)

    def getVolts(self):
        try:
            self.device.setDeviceSerialNumber(271638)
            self.device.setChannel(self.channel)
            self.device.openWaitForAttachment(5000)
        except PhidgetException as e:
            print("Program Terminated: Open PowerSource Failed")
            print(e)

        time.sleep(2)
        return self.device.getSensorValue()

    def closeDevice(self):
        self.device.close()
Exemple #4
0
class VoltageInputSensor(Sensor):
    '''Class used to handle any sensor which uses a standard voltage input '''

    def __init__(self, deviceSN,channelNo,dataInterval,refreshPeriod,sensorType,
                sensorName=None):
        '''
        Constructor for creating Voltage Input Sensor objects. Takes standard sensor
        arguments.
        '''
        self.channelNo = channelNo
        self.sensorType = sensorType
        self.sensorUnits = None
        Sensor.__init__(self,deviceSN,dataInterval,refreshPeriod,sensorName)

    def attachSensor(self):
        '''
        Connects the sensor to the application
        '''
        self.channel = VoltageInput()
        self.channel.setDeviceSerialNumber(self.deviceSN)
        self.channel.setChannel(self.channelNo)
        self.channel.openWaitForAttachment(100)
        print("\n***** {} Sensor Attached *****".format(self.sensorName))
        self.attached = True
        self.channel.setSensorType(self.sensorType)
        self.channel.setDataInterval(self.dataInterval)
        self.sensorUnits=self.channel.getSensorUnit().symbol

    def activateDataListener(self):
        '''
        Sets up the event which triggers when the sensor updates its outputs
        '''
        self.startTime = time.time()
        def onSensorValueChange(channelObject,sensorVlue,sensorUnit):
            rawTime = time.time()
            deltaTime = rawTime- self.startTime
            self.dataQ.put([channelObject.getSensorValue(),deltaTime,rawTime])
        self.channel.setOnSensorChangeHandler(onSensorValueChange)
Exemple #5
0
    def _publish(self):
        """
		* Allocate a new Phidget Channel object
		"""

        try:
            try:
                ch = VoltageInput()
            except PhidgetException as e:
                sys.stderr.write(
                    "Runtime Error -> Creating VoltageInput: \n\t")
                DisplayError(e)
                raise
            except RuntimeError as e:
                sys.stderr.write(
                    "Runtime Error -> Creating VoltageInput: \n\t" + e)
                raise

            def signal_handler(signal, frame):
                print("exiting...")
                raise EndProgramSignal("Program Terminated")

            signal.signal(signal.SIGINT, signal_handler)

            channelInfo = ChannelInfo()
            channelInfo.deviceSerialNumber = 539331
            channelInfo.hubPort = 0
            channelInfo.isHubPortDevice = 1
            channelInfo.channel = 0

            ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber)
            ch.setHubPort(channelInfo.hubPort)
            ch.setIsHubPortDevice(channelInfo.isHubPortDevice)
            ch.setChannel(channelInfo.channel)
            """
			* Add event handlers before calling open so that no events are missed.
			"""
            print(channelInfo.deviceSerialNumber)
            print(channelInfo.hubPort)
            ch.setOnAttachHandler(onAttachHandler)
            ch.setOnDetachHandler(onDetachHandler)
            ch.setOnErrorHandler(onErrorHandler)
            ch.setOnVoltageChangeHandler(onVoltageChangeHandler)
            ch.setOnSensorChangeHandler(onSensorChangeHandler)

            print("\nOpening and Waiting for Attachment...")
            try:
                ch.openWaitForAttachment(5000)
            except PhidgetException as e:
                PrintOpenErrorMessage(e, ch)
                raise EndProgramSign00al("Program Terminated: Open Failed")

            print("running...")
            rate = rospy.Rate(self._hz)
            while True:
                self.current_publisher.publish(str(current[0]))
                if current[0] > 14:
                    playsound('/home/mars/Downloads/torture.wav')
                rate.sleep()

        except PhidgetException as e:
            sys.stderr.write("\nExiting with error(s)...")
            DisplayError(e)
            traceback.print_exc()
            print("Cleaning up...")
            ch.setOnVoltageChangeHandler(None)
            ch.setOnSensorChangeHandler(None)
            ch.close()
            return 1
        except EndProgramSignal as e:
            print(e)
            print("Cleaning up...")
            ch.setOnVoltageChangeHandler(None)
            ch.setOnSensorChangeHandler(None)
            ch.close()
            return 1
        except KeyboardInterrupt:
            ch.setOnVoltageChangeHandler(None)
            ch.setOnSensorChangeHandler(None)
            ch.close()
            return 1
Exemple #6
0
def VoltageChangeHandler(self, voltage):
    #print("Voltage1: %f" % voltage)
    dist=519/((voltage*50)+(11/4))
    pub.publish(dist)
  

def SensorChangeHandler(self, sensorValue, sensorUnit):
    print("Sensor Value: %f" % sensorValue)

try:
    ch1.setOnAttachHandler(VoltageInputAttached)
    ch1.setOnDetachHandler(VoltageInputDetached)
    ch1.setOnErrorHandler(ErrorEvent)
    ch1.setOnVoltageChangeHandler(VoltageChangeHandler)
    ch1.setOnSensorChangeHandler(SensorChangeHandler)
    
    print("Waiting for the Phidget VoltageInput Object to be attached...")
    ch1.openWaitForAttachment(5000)
    
except PhidgetException as e:
    print("Phidget Exception %i: %s" % (e.code, e.details))
    print("Press Enter to Exit...\n")
    readin = sys.stdin.read(1)
    exit(1)

print("Gathering data for 100 seconds...")

#time.sleep(100)
while True: pass
Exemple #7
0
def main():
    try:
        """
        * Allocate a new Phidget Channel object
        """
        try:
            ch = VoltageInput()
        except PhidgetException as e:
            sys.stderr.write("Runtime Error -> Creating VoltageInput: \n\t")
            DisplayError(e)
            raise
        except RuntimeError as e:
            sys.stderr.write("Runtime Error -> Creating VoltageInput: \n\t" + e)
            raise

        """
        * Set matching parameters to specify which channel to open
        """
        
        #You may remove this line and hard-code the addressing parameters to fit your application
        channelInfo = AskForDeviceParameters(ch)
        
        ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber)
        ch.setHubPort(channelInfo.hubPort)
        ch.setIsHubPortDevice(channelInfo.isHubPortDevice)
        ch.setChannel(channelInfo.channel)   
        
        if(channelInfo.netInfo.isRemote):
            ch.setIsRemote(channelInfo.netInfo.isRemote)
            if(channelInfo.netInfo.serverDiscovery):
                try:
                    Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)
                except PhidgetException as e:
                    PrintEnableServerDiscoveryErrorMessage(e)
                    raise EndProgramSignal("Program Terminated: EnableServerDiscovery Failed")
            else:
                Net.addServer("Server", channelInfo.netInfo.hostname,
                    channelInfo.netInfo.port, channelInfo.netInfo.password, 0)
        
        """
        * Add event handlers before calling open so that no events are missed.
        """
        print("\n--------------------------------------")
        print("\nSetting OnAttachHandler...")
        ch.setOnAttachHandler(onAttachHandler)
        
        print("Setting OnDetachHandler...")
        ch.setOnDetachHandler(onDetachHandler)
        
        print("Setting OnErrorHandler...")
        ch.setOnErrorHandler(onErrorHandler)
        
        #This call may be harmlessly removed
        PrintEventDescriptions()
        
        print("\nSetting OnVoltageChangeHandler...")
        ch.setOnVoltageChangeHandler(onVoltageChangeHandler)
        
        print("\nSetting OnSensorChangeHandler...")
        ch.setOnSensorChangeHandler(onSensorChangeHandler)
        
        """
        * Open the channel with a timeout
        """
        
        print("\nOpening and Waiting for Attachment...")
        
        try:
            ch.openWaitForAttachment(5000)
        except PhidgetException as e:
            PrintOpenErrorMessage(e, ch)
            raise EndProgramSignal("Program Terminated: Open Failed")
        
        print("Sampling data for 10 seconds...")
        
        print("You can do stuff with your Phidgets here and/or in the event handlers.")
        
        time.sleep(10)
        
        """
        * Perform clean up and exit
        """

        #clear the VoltageChange event handler 
        ch.setOnVoltageChangeHandler(None)  
        #clear the SensorChange event handler
        ch.setOnSensorChangeHandler(None)
        
        print("\nDone Sampling...")

        print("Cleaning up...")
        ch.close()
        print("\nExiting...")
        return 0

    except PhidgetException as e:
        sys.stderr.write("\nExiting with error(s)...")
        DisplayError(e)
        traceback.print_exc()
        print("Cleaning up...")
        ch.setOnVoltageChangeHandler(None)
        ch.setOnSensorChangeHandler(None)
        ch.close()
        return 1
    except EndProgramSignal as e:
        print(e)
        print("Cleaning up...")
        ch.setOnVoltageChangeHandler(None)
        ch.setOnSensorChangeHandler(None)
        ch.close()
        return 1
    finally:
        print("Press ENTER to end program.")
        readin = sys.stdin.readline()
	def _publish(self):
		"""
		* Allocate a new Phidget Channel object
		"""

		try:
			try:
				ch = VoltageInput()
			except PhidgetException as e:
				sys.stderr.write("Runtime Error -> Creating VoltageInput: \n\t")
				DisplayError(e)
				raise
			except RuntimeError as e:
				sys.stderr.write("Runtime Error -> Creating VoltageInput: \n\t" + e)
				raise
			
			def signal_handler(signal, frame):
				print("exiting...")
				raise EndProgramSignal("Program Terminated")
			signal.signal(signal.SIGINT, signal_handler)
			
			channelInfo = ChannelInfo()
			channelInfo.deviceSerialNumber = 539331
			channelInfo.hubPort = 0
			channelInfo.isHubPortDevice = 1
			channelInfo.channel = 0
	
			ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber)
			ch.setHubPort(channelInfo.hubPort)
			ch.setIsHubPortDevice(channelInfo.isHubPortDevice)
			ch.setChannel(channelInfo.channel)
				
			"""
			* Add event handlers before calling open so that no events are missed.
			"""	
			print(channelInfo.deviceSerialNumber)
			print(channelInfo.hubPort)
			ch.setOnAttachHandler(onAttachHandler)
			ch.setOnDetachHandler(onDetachHandler)
			ch.setOnErrorHandler(onErrorHandler)
			ch.setOnVoltageChangeHandler(onVoltageChangeHandler)
			ch.setOnSensorChangeHandler(onSensorChangeHandler)
			
			print("\nOpening and Waiting for Attachment...")
			try:
				ch.openWaitForAttachment(5000)
			except PhidgetException as e:
				PrintOpenErrorMessage(e, ch)
				raise EndProgramSign00al("Program Terminated: Open Failed")
			

			print("running...")
			rate = rospy.Rate(self._hz)
			while True:
				self.current_publisher.publish(str(current[0]))
				if current[0] > 14 : playsound('/home/mars/Downloads/torture.wav')
				rate.sleep()

		except PhidgetException as e:
			sys.stderr.write("\nExiting with error(s)...")
			DisplayError(e)
			traceback.print_exc()
			print("Cleaning up...")
			ch.setOnVoltageChangeHandler(None)
			ch.setOnSensorChangeHandler(None)
			ch.close()
			return 1
		except EndProgramSignal as e:
			print(e)
			print("Cleaning up...")
			ch.setOnVoltageChangeHandler(None)
			ch.setOnSensorChangeHandler(None)
			ch.close()
			return 1
		except KeyboardInterrupt:
			ch.setOnVoltageChangeHandler(None)
			ch.setOnSensorChangeHandler(None)
			ch.close()
			return 1