2

I'm using Phidget 8/8/8 board to acquire temperature and RH signal from a sensor. I would like to have a 10-second sampling interval, but I only can specify the interval as 1ms up to 1000ms by the statement setDataInterval(). What I want is 10 second sampling rate. I had try to use time.sleep(10) after the print function, but it doesn't work. For example, the second value it printed after 10 second is the value it sampled after default sampling interval (~250 ms) from the beginning. It seems I just delay the print function rather than changing the sampling interval.
Below is the sample code downloaded from Phidget website.

import sys
import time 
from Phidget22.Devices.VoltageInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *

try:
    ch = VoltageInput()
    ch.setDeviceSerialNumber(437701)
    ch.setChannel(1)
    #set channel 1 as the input voltage port
    #ch.setHubPort(1)
except RuntimeError as e:
    print("Runtime Exception %s" % e.details)
    print("Press Enter to Exit...\n")
    readin = sys.stdin.read(1)
    exit(1)

def VoltageInputAttached(e):
    try:
        attached = e
        print("\nAttach Event Detected (Information Below)")
        print("===========================================")
        print("Library Version: %s" % attached.getLibraryVersion())
        print("Serial Number: %d" % attached.getDeviceSerialNumber())
        print("Channel: %d" % attached.getChannel())
        print("Channel Class: %s" % attached.getChannelClass())
        print("Channel Name: %s" % attached.getChannelName())
        print("Device ID: %d" % attached.getDeviceID())
        print("Device Version: %d" % attached.getDeviceVersion())
        print("Device Name: %s" % attached.getDeviceName())
        print("Device Class: %d" % attached.getDeviceClass())
        print("\n")

    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)   

def VoltageInputDetached(e):
    detached = e
    try:
        print("\nDetach event on Port %d Channel %d" % (detached.getHubPort(), detached.getChannel()))
    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)   

def ErrorEvent(e, eCode, description):
    print("Error %i : %s" % (eCode, description))

def VoltageChangeHandler(e, voltage):
    print("Voltage: %f" % voltage)
    time.sleep(10)

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

try:
    ch.setOnAttachHandler(VoltageInputAttached)
    ch.setOnDetachHandler(VoltageInputDetached)
    ch.setOnErrorHandler(ErrorEvent)

    ch.setOnVoltageChangeHandler(VoltageChangeHandler)
    ch.setOnSensorChangeHandler(SensorChangeHandler)

    print("Waiting for the Phidget VoltageInput 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 20 seconds...")
time.sleep(20)

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 VoltageInput device")
exit(0)
Georgy
  • 12,464
  • 7
  • 65
  • 73
Damao
  • 31
  • 6

1 Answers1

0

I battled with this for some time in my own project... The only way I found to actually refresh the voltage read was to re-attach the voltage sensor. The sampling rate will not be accurate (as the 'wait for attachment' might take a few hundreds of milliseconds), but if you can allow a 5% deviation in measurement times, it is doable.

Another issue is the dependency on events - you are not actively reading the voltage, but wait upon changes in the voltage to trigger an event.

    import sys
    import time
    from Phidget22.Devices.VoltageInput import *
    from Phidget22.PhidgetException import *
    from Phidget22.Phidget import *
    from Phidget22.Net import *

    volt_array = []
    v_read = 0
    sample_array = []
    volt_time_output = []

    def VoltageInputAttached(e):
        try:
            attached = e
            print("\nAttach Event Detected (Information Below)")
            print("===========================================")
            print("Library Version: %s" % attached.getLibraryVersion())
            print("Serial Number: %d" % attached.getDeviceSerialNumber())
            print("Channel: %d" % attached.getChannel())
            print("Channel Class: %s" % attached.getChannelClass())
            print("Channel Name: %s" % attached.getChannelName())
            print("Device ID: %d" % attached.getDeviceID())
            print("Device Version: %d" % attached.getDeviceVersion())
            print("Device Name: %s" % attached.getDeviceName())
            print("Device Class: %d" % attached.getDeviceClass())
            print("\n")

        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)

    def VoltageInputDetached(e):
        detached = e
        try:
            print("\nDetach event on Port %d Channel %d" % (detached.getHubPort(),         detached.getChannel()))
        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)

    def ErrorEvent(e, eCode, description):
        print("Error %i : %s" % (eCode, description))

    def VoltageChangeHandler(e, voltage):
        volt_array.append(voltage)

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

    while True: #Set a more reasonable exit condition here
        try:
            ch = VoltageInput()
            ch.setDeviceSerialNumber(437701)
            ch.setChannel(1)
            #set channel 1 as the input voltage port
            #ch.setHubPort(1)
        except RuntimeError as e:
            print("Runtime Exception %s" % e.details)
            print("Press Enter to Exit...\n")
            readin = sys.stdin.read(1)
            exit(1)
        try:
            ch.setOnAttachHandler(VoltageInputAttached)
            ch.setOnDetachHandler(VoltageInputDetached)
            ch.setOnErrorHandler(ErrorEvent)

            ch.setOnVoltageChangeHandler(VoltageChangeHandler)
            ch.setOnSensorChangeHandler(SensorChangeHandler)

            print("Waiting for the Phidget VoltageInput 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...")
        time.sleep(1)
        ts = time.gmtime()
        sample_array.append(float(sum(volt_array)) / max(len(volt_array), 1))
        sample_array.append(time.strftime("%Y-%m-%d %H:%M:%S", ts))
        volt_time_output.append(sample_array)
        volt_array=[]
        sample_array=[]
        time.sleep(9)


    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 VoltageInput device")
    print (volt_time_output)
    exit(0)

the 'volt_time_output' array will include a time stamp and a voltage reading which is the average of all changes within a 1000ms of the sample. afterwards, a 9000ms pause is issued.

DarkLight
  • 79
  • 3
  • 16