class PhidgetMonitor:
    
    def __init__(self,channel,sensor_name,topic_name,modo):
        self.channel = channel
        self.sensor_name = sensor_name
        self.topic_name = topic_name
        self.modo = modo
        self.pub = rospy.Publisher(self.topic_name, DeviceInfo, queue_size=10)
        self.msg = DeviceInfo()
        self.modo = modo
        if modo == 'ANALOG':
            self.ch = VoltageInput()
        elif modo == 'DIGITAL':
            self.ch = DigitalInput()
            
             
    
    def setup(self):
        
        self.msg.channel = self.channel
        self.msg.device_name = self.sensor_name
        self.ch.setChannel(self.channel)   
        self.ch.setOnAttachHandler(self.VoltageInputAttached)
        self.ch.setOnDetachHandler(self.VoltageInputDetached)
        self.ch.setOnErrorHandler(self.ErrorEvent)
        if self.modo == 'ANALOG':
            self.ch.setOnVoltageChangeHandler(self.VoltageChangeHandler)
        elif self.modo == 'DIGITAL':
            self.ch.setOnStateChangeHandler(self.VoltageChangeHandler)
        
        print("Waiting for the Phidget VoltageInput Object to be attached...")
        self.ch.openWaitForAttachment(5000)
        
    def close(self):
        self.ch.close()
        print("Closed VoltageInput device")   
        
    def VoltageInputAttached(self,attached):

        print("\nAttach Event Detected (Information Below)")
        print("===========================================")
        print("Channel: %d" % attached.getChannel())
        print("Channel Name: %s" % attached.getChannelName())
        print("Device ID: %d" % attached.getDeviceID())
        print("Device Name: %s" % attached.getDeviceName())
        print("Mode: %s" %self.modo)
        print("\n")   
            
    def VoltageInputDetached(self,detached):
        print("\nDetach event on Port %d Channel %d" % (detached.getHubPort(), detached.getChannel()))   
             
    def ErrorEvent(self, subject, eCode, description):
        print("Error %i : %s" % (eCode, description))

    def VoltageChangeHandler(self, subject, voltage):
        self.msg.voltage = voltage
        self.pub.publish(self.msg)
Exemple #2
0
def main():
    try:
        """
        * Allocate a new Phidget Channel object
        """
        ch = DigitalInput()
        """
        * 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("Setting OnStateChangeHandler...")
        ch.setOnStateChangeHandler(onStateChangeHandler)
        """
        * 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...")

        time.sleep(10)
        """
         * Perform clean up and exit
         """
        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.close()
        return 1
    except EndProgramSignal as e:
        print(e)
        print("Cleaning up...")
        ch.close()
        return 1
    except RuntimeError as e:
        sys.stderr.write("Runtime Error: \n\t" + e)
        traceback.print_exc()
        return 1
    finally:
        print("Press ENTER to end program.")
        readin = sys.stdin.readline()
Exemple #3
0
    # In order to attach to a network Phidget, the program must connect to a Phidget22 Network Server.
    # In a normal environment this can be done automatically by enabling server discovery, which
    # will cause the client to discovery and connect to available servers.
    #
    # To force the channel to only match a network Phidget, set remote to 1.
    #
    # Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICE);
    # ch.setIsRemote(1)

    print("Waiting for the Phidget DigitalInput Object to be attached...")
    ch.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)

try:
    ch.close()
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("Closed DigitalInput device")
exit(0)
Exemple #4
0
def main():
    digitalInput0 = DigitalInput()
    digitalInput1 = DigitalInput()
    digitalInput2 = DigitalInput()
    digitalInput3 = DigitalInput()
    digitalInput4 = DigitalInput()
    digitalInput5 = DigitalInput()
    digitalInput6 = DigitalInput()
    digitalInput7 = DigitalInput()

    digitalInput0.setChannel(0)
    digitalInput1.setChannel(1)
    digitalInput2 = DigitalInput()
    digitalInput3 = DigitalInput()
    digitalInput4 = DigitalInput()
    digitalInput5 = DigitalInput()
    digitalInput6 = DigitalInput()
    digitalInput7 = DigitalInput()

    digitalInput0.setChannel(0)
    digitalInput1.setChannel(1)
    digitalInput2.setChannel(2)
    digitalInput3.setChannel(3)
    digitalInput4.setChannel(4)
    digitalInput5.setChannel(5)
    digitalInput6.setChannel(6)
    digitalInput7.setChannel(7)

    digitalInput0.setOnStateChangeHandler(onStateChange)
    digitalInput1.setOnStateChangeHandler(onStateChange)
    digitalInput2.setOnStateChangeHandler(onStateChange)
    digitalInput3.setChannel(3)
    digitalInput4.setChannel(4)
    digitalInput5.setChannel(5)
    digitalInput6.setChannel(6)
    digitalInput7.setChannel(7)

    digitalInput0.setOnStateChangeHandler(onStateChange)
    digitalInput1.setOnStateChangeHandler(onStateChange)
    digitalInput2.setOnStateChangeHandler(onStateChange)
    digitalInput3.setOnStateChangeHandler(onStateChange)
    digitalInput4.setOnStateChangeHandler(onStateChange)
    digitalInput5.setOnStateChangeHandler(onStateChange)
    digitalInput6.setOnStateChangeHandler(onStateChange)
    digitalInput7.setOnStateChangeHandler(onStateChange)

    digitalInput0.openWaitForAttachment(5000)
    digitalInput1.openWaitForAttachment(5000)
    digitalInput2.openWaitForAttachment(5000)
    digitalInput3.openWaitForAttachment(5000)
    digitalInput4.setOnStateChangeHandler(onStateChange)
    digitalInput5.setOnStateChangeHandler(onStateChange)
    digitalInput6.setOnStateChangeHandler(onStateChange)
    digitalInput7.setOnStateChangeHandler(onStateChange)

    digitalInput0.openWaitForAttachment(5000)
    digitalInput1.openWaitForAttachment(5000)
    digitalInput2.openWaitForAttachment(5000)
    digitalInput3.openWaitForAttachment(5000)
    digitalInput4.openWaitForAttachment(5000)
    digitalInput5.openWaitForAttachment(5000)
    digitalInput6.openWaitForAttachment(5000)
    digitalInput7.openWaitForAttachment(5000)

    try:
        input("Press Enter to Stop\n")
    except (Exception, KeyboardInterrupt):
        pass
    digitalInput0.close()
    digitalInput1.close()
    digitalInput2.close()
    digitalInput3.close()
    digitalInput4.close()
    digitalInput5.close()
    digitalInput6.close()
    digitalInput7.close()