Exemple #1
0
class VoltageRatioSensor(Sensor):
    '''Class used to handle any sensor which uses a ratiometric voltage input '''
    def __init__(self,
                 deviceSN,
                 channelNo,
                 dataInterval,
                 refreshPeriod,
                 sensorType,
                 sensorName=None):
        '''
        Connects the sensor to the application
        '''
        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 = VoltageRatioInput()
        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 #2
0
def main():
    ir_reflective_sensor = VoltageRatioInput()
    ir_reflective_sensor.setChannel(5)
    ir_reflective_sensor.setOnSensorChangeHandler(on_ir_change)
    ir_reflective_sensor.openWaitForAttachment(5000)
    ir_reflective_sensor.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1103)

    sharp_distance_sensor_left = VoltageRatioInput()
    sharp_distance_sensor_left.setChannel(6)
    sharp_distance_sensor_left.setOnSensorChangeHandler(on_sharp_change)
    sharp_distance_sensor_left.openWaitForAttachment(5000)
    sharp_distance_sensor_left.setSensorType(
        VoltageRatioSensorType.SENSOR_TYPE_1101_SHARP_2Y0A21)

    sharp_distance_sensor_right = VoltageRatioInput()
    sharp_distance_sensor_right.setChannel(7)
    sharp_distance_sensor_right.setOnSensorChangeHandler(on_sharp_change)
    sharp_distance_sensor_right.openWaitForAttachment(5000)
    sharp_distance_sensor_right.setSensorType(
        VoltageRatioSensorType.SENSOR_TYPE_1101_SHARP_2Y0A21)

    while True:
        pass

    ir_reflective_sensor.close()
    sharp_distance_sensor_left.close()
    sharp_distance_sensor_right.close()
Exemple #3
0
def main():
    #Create your Phidget channels
    voltageRatioInput0 = VoltageRatioInput()

    #Set addressing parameters to specify which channel to open (if any)

    #Assign any event handlers you need before calling open so that no events are missed.
    voltageRatioInput0.setOnSensorChangeHandler(onSensorChange)

    #Open your Phidgets and wait for attachment
    voltageRatioInput0.openWaitForAttachment(5000)

    #Do stuff with your Phidgets here or in your event handlers.
    #Set the sensor type to match the analog sensor you are using after opening the Phidget
    voltageRatioInput0.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1111)

    try:
        input("Press Enter to Stop\n")
    except (Exception, KeyboardInterrupt):
        pass

    #Close your Phidgets once the program is done.
    voltageRatioInput0.close()
Exemple #4
0
def main():
    try:
        """
        * Allocate a new Phidget Channel object
        """
        ch = VoltageRatioInput()
        """
        * 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 OnVoltageRatioChangeHandler...")
        ch.setOnVoltageRatioChangeHandler(onVoltageRatioChangeHandler)

        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
        """
        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 #5
0
def SensorChangeHandler(e, sensorValue, sensorUnit):
    print("Sensor Value: %f" % sensorValue)


#%% This is Phidget's code that does the collecting
# We only have 1 load cell hooked up, so this should be all good for now
# If we ever have more than one, use the Phidget control panel to specify hub ports
# and serial numbers
try:
    ch.setOnAttachHandler(VoltageRatioInputAttached)
    ch.setOnDetachHandler(VoltageRatioInputDetached)
    ch.setOnErrorHandler(ErrorEvent)

    ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandler)
    ch.setOnSensorChangeHandler(SensorChangeHandler)

    # Please review the Phidget22 channel matching documentation for details on the device
    # and class architecture of Phidget22, and how channels are matched to device features.

    # Specifies the serial number of the device to attach to.
    # For VINT devices, this is the hub serial number.
    #
    # The default is any device.
    #
    # ch.setDeviceSerialNumber(470070)

    # For VINT devices, this specifies the port the VINT device must be plugged into.
    #
    # The default is any port.
    #
Exemple #6
0
def main2():
    global sleeptime
    try:
        """
        * Allocate a new Phidget Channel object
        """
        ch2 = VoltageRatioInput()
        """
        * 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)

        ch2.setDeviceSerialNumber(566690)
        ch2.setChannel(2)
        """
        * Add event handlers before calling open so that no events are missed.
        """
        #print("\n--------------------------------------")
        #print("\nSetting OnAttachHandler...")
        ch2.setOnAttachHandler(onAttachHandler)

        #print("Setting OnDetachHandler...")
        ch2.setOnDetachHandler(onDetachHandler)

        #print("Setting OnErrorHandler...")
        ch2.setOnErrorHandler(onErrorHandler)

        #This call may be harmlessly removed
        #PrintEventDescriptions()

        #print("\nSetting OnVoltageRatioChangeHandler...")
        ch2.setOnVoltageRatioChangeHandler(onVoltageRatioChangeHandler)

        #print("\nSetting OnSensorChangeHandler...")
        ch2.setOnSensorChangeHandler(onSensorChangeHandler)
        """
        * Open the channel with a timeout
        """

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

        try:
            ch2.openWaitForAttachment(5000)
        except PhidgetException as e:
            PrintOpenErrorMessage(e, ch2)
            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(sleeptime)
        """
        * Perform clean up and exit
        """
        #print("\nDone Sampling...")

        #print("Cleaning up...")
        ch2.close()
        #print("\nExiting...")
        global data
        weight2 = data
        datalist.clear()
        return weight2

    except PhidgetException as e:
        sys.stderr.write("\nExiting with error(s)...")
        DisplayError(e)
        traceback.print_exc()
        print("Cleaning up...")
        ch2.close()
        return 1
    except EndProgramSignal as e:
        print(e)
        print("Cleaning up...")
        ch2.close()
        return 1
    except RuntimeError as e:
        sys.stderr.write("Runtime Error: \n\t" + e)
        traceback.print_exc()
        return 1
Exemple #7
0
class WeightSensor:
    def __init__(self):
        self.weight_value = -1
        self.has_ended = False
        self.try_weight_sensor()

    def try_weight_sensor(self):
        print("Try Weight Sensor")
        try:
            # Allocate a new Phidget Channel object
            self.ch = VoltageRatioInput()
            self.do = DigitalOutput()

            self.ch.setChannel(0)
            self.do.setChannel(6)

            self.ch.setOnAttachHandler(onAttachHandler)
            self.ch.setOnDetachHandler(onDetachHandler)
            self.ch.setOnErrorHandler(onErrorHandler)
            self.ch.setOnVoltageRatioChangeHandler(onVoltageRatioChangeHandler)
            self.ch.setOnSensorChangeHandler(onSensorChangeHandler)
            # Open the channel with a timeout
            try:
                self.ch.openWaitForAttachment(5000)
                self.do.openWaitForAttachment(5000)
            except PhidgetException as e:
                # PrintOpenErrorMessage(e, self.ch)
                raise EndProgramSignal("Program Terminated: Open Failed")

            print("THREAD STARTING")

            weight_thread = threading.Thread(
                target=self.start_getting_weight_value)
            weight_thread.daemon = True
            weight_thread.start()

        except Exception as e:
            self.ch.close()
            self.try_conneting_again()

    def start_getting_weight_value(self):
        while not self.has_ended:
            try:
                self.weight_value = self.ch.getVoltageRatio()
            except:
                self.ch.close()
                break
            time.sleep(0.25)
        self.try_conneting_again()

    def try_conneting_again(self):
        self.has_ended = True
        print(
            "Weight sensor failed! Trying to connect to the weight sensor again in 3 2 1..."
        )
        time.sleep(3)
        self.ch = None
        self.do = None
        self.has_ended = False
        self.weight_value = -1
        self.try_weight_sensor()

    def get_weight_value(self):
        return self.weight_value