Analog input sampling rate

Supporting 2.7 and 3.2+
Post Reply
zdm11
Fresh meat
Posts: 4
Joined: Mon Jul 24, 2017 5:05 pm
Contact:

Analog input sampling rate

Post by zdm11 »

Dear all,
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.
Thanks in advance.
I use the sampling code downloaded from the phidget website:

Code: Select all

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)
                     
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Analog input sampling rate

Post by mparadis »

I think the easiest way to work around the 1000ms limit on sampling rate would be to accumulate 10 samples at a rate of 1 sample per second and then average them together before using them in your output.
User avatar
Patrick
Lead Developer
Posts: 634
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Analog input sampling rate

Post by Patrick »

You may be better off just using sleep and printing the voltage at whatever interval you want directly in your main. You definitely don't want to block in an event handler.

-Patrick
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests