setDataInterval does not work while running in while loop

Supporting 2.6 and up
Post Reply
YLM
Phidgetsian
Posts: 11
Joined: Mon Nov 18, 2019 9:38 am
Contact:

setDataInterval does not work while running in while loop

Post by YLM »

Hi,

with PhidgetBridge 4 input, I am using the setDataInterval() API to set the sample rate of the acquisition.

I am trying to run the code inside a while loop to acquire the data for long time. I have set the frequency of 1 Hz but when I print the result with the time it appears to not be 1 Hz but 4 Hz (256 ms between each point)

Here is my code :

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time

start_time = round(time.time()*1000)

def onVoltageRatioChange(self, voltageRatio):
    print(str(round(time.time()*1000) - start_time) + " : " + str(voltageRatio))


def main():

    while True:
        voltageRatioInput0 = VoltageRatioInput()
        voltageRatioInput0.setChannel(1)
        voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
        voltageRatioInput0.openWaitForAttachment(5000)
        voltageRatioInput0.setDataInterval(1000)
        voltageRatioInput0.close()

main()
and the console value :

Code: Select all

1888 : -0.000284235924
2144 : -0.000284283422
2400 : -0.000284277834
2656 : -0.000284262002
2912 : -0.000284207053
3168 : -0.000284248963
3424 : -0.000284295529
3680 : -0.000284306705
3936 : -0.000284236856
If I do not use a while loop but a time.sleep() instead it works correctly..

The code :

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time

start_time = round(time.time()*1000)

def onVoltageRatioChange(self, voltageRatio):
    print(str(round(time.time()*1000) - start_time) + " : " + str(voltageRatio))


def main():

    #while True:
        voltageRatioInput0 = VoltageRatioInput()
        voltageRatioInput0.setChannel(1)
        voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
        voltageRatioInput0.openWaitForAttachment(5000)
        voltageRatioInput0.setDataInterval(1000)

        time.sleep(10)

        voltageRatioInput0.close()

main()
and the console :

Code: Select all

597 : -0.000283599
1117 : -0.000283599
2157 : -0.000284255482
3117 : -0.000284262933
4117 : -0.000284257345
5117 : -0.000284262002
6117 : -0.000284257345
7117 : -0.000284252688
8117 : -0.000284258276
9117 : -0.000284264795
10117 : -0.000284259208
fraser
Engineering
Posts: 138
Joined: Thu Nov 19, 2009 4:41 pm
Contact:

Re: setDataInterval does not work while running in while loop

Post by fraser »

That isn't a great way to use the object, since the device will try to take as small a time as possible to send the first event. The time you're seeing is the time it takes to open the device and get that first event. Every time you close the device you're losing the 1000ms data interval you set.

You will want to set the data interval and leave the device open so that it can continue to send events at that interval.

doing something like...

Code: Select all

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time

start_time = round(time.time()*1000)

def onVoltageRatioChange(self, voltageRatio):
    print(str(round(time.time()*1000) - start_time) + " : " + str(voltageRatio))


def main():

        voltageRatioInput0 = VoltageRatioInput()
        voltageRatioInput0.setChannel(1)
        voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange)
        voltageRatioInput0.openWaitForAttachment(5000)
        voltageRatioInput0.setDataInterval(1000)
        
        while True:
    		time.sleep(5)
        
main()
here the device will remain open and continue to send events at 1000ms intervals.

Check out the code samples tab on the product page, you can generate examples showing some best practices

https://www.phidgets.com/?tier=3&catid= ... rodid=1027
YLM
Phidgetsian
Posts: 11
Joined: Mon Nov 18, 2019 9:38 am
Contact:

Re: setDataInterval does not work while running in while loop

Post by YLM »

Great thank you. For what goal is your time.sleep(5) because this code run more than 5 seconds...
Thanks
fraser
Engineering
Posts: 138
Joined: Thu Nov 19, 2009 4:41 pm
Contact:

Re: setDataInterval does not work while running in while loop

Post by fraser »

It was in a while loop just to run the program indefinitely.
YLM
Phidgetsian
Posts: 11
Joined: Mon Nov 18, 2019 9:38 am
Contact:

Re: setDataInterval does not work while running in while loop

Post by YLM »

ok thanks (sorry for easy questions)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests