Time interval data from Vint hub

Supporting 2.7 and 3.2+
MaxS
Fresh meat
Posts: 3
Joined: Mon Dec 11, 2017 11:46 pm
Contact:

Time interval data from Vint hub

Postby MaxS » Mon Dec 11, 2017 11:58 pm

Hello all,

I have two sensors connected to a Vint hub. One on port 1 and the other on port 0. I would like to read the data from them simultaneously each regular interval of time. The code that I have reads the data from one and then closes the channel for that before reading the next one. This introduces two sleep.time() of 0.4 seconds into the mix meaning that my data readings will be about 1 second apart. I can not reduce the sleep.time() below 0.35 or I will lose data points. I think may be the solution is to change and reduce the data interval, probably using getMinDataInterval(). However, since I am new to both Python and Phidgets, I am not sure what is the format and syntax I should do it. Any advice will be much appreciated. Below is the code I have:

Code: Select all

# -*- coding: utf-8 -*-
#####################################
#There is about 0.8 seconds difference between the readings
import sys
import time
import datetime
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *

try:
    ch = VoltageRatioInput()
except RuntimeError as e:
    print("Runtime Exception %s" % e.details)
    print("Press Enter to Exit...\n")
    readin = sys.stdin.read(1)
    exit(1)
a=[]
b=[]
try:
    start = time.time()
    while True:
        def VoltageRatioChangeHandler(e, voltageRatio):
            n=voltageRatio
            a.append(n)
   
        ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandler)
        ch.setHubPort(1)
        ch.setIsHubPortDevice(1)
        ch.openWaitForAttachment(5000)
   
        if(ch.getChannelSubclass() == ChannelSubclass.PHIDCHSUBCLASS_VOLTAGERATIOINPUT_BRIDGE):
            ch.setBridgeEnabled(1)
   
        time.sleep(0.3)
        ch.close()
        end1 = time.time()
        Elt1 = end1-start
        print (Elt1)
        print a
###   
        def VoltageRatioChangeHandler(e, voltageRatio2):
            m=voltageRatio2
            b.append(m)
               
        ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandler)
        ch.setHubPort(0)
        ch.setIsHubPortDevice(0)
        ch.openWaitForAttachment(5000)
     
        if(ch.getChannelSubclass() == ChannelSubclass.PHIDCHSUBCLASS_VOLTAGERATIOINPUT_BRIDGE):
            ch.setBridgeEnabled(1)
     
        time.sleep(0.4)
        ch.close()

       
except KeyboardInterrupt:
    print ("gracefully aborted")
    sys.exit()

User avatar
mparadis
Site Admin
Posts: 531
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Time interval data from Vint hub

Postby mparadis » Tue Dec 12, 2017 11:18 am

Hi Max,

Some notes about your program:

- It doesn't make sense to put the definition for your VoltageRatioChangeHandler inside your while loop, you only need to define it once, like you would a variable.

- It doesn't make sense to access the value for the variable "a" outside of your change handler. You can think of change handlers as separate programs that run alongside your main program. By the time your main program attempts to access "a", you have no way of knowing whether it's been set by your event handler. You're better off printing "a" from the handler. Rather than continuously printing in an infinite loop, you'd normally do your prints in the event handler and wait for input on your main program. If you want both numbers output simultaneously, the infinite loop may be the easiest way. I needed to add the "global" keyword to the variables to make it work this way.


- The second time you call setIsHubPortDevice, the parameter should still be "1", as 1 means "true" in this case.

- You should use different handles for your different channels. That way, you can keep them both open at the same time.


- If you're just using ordinary sensors plugged into your VINT hub, it doesn't make sense to call setBridgeEnabled. That function only belongs to boards like our 1046 or DAQ1500. If this is the case, you would not use setIsHubPortDevice(1), since you're getting data from the attached device, not the hub port itself.

Here's a modified version that should work:

Code: Select all


#There is about 0.8 seconds difference between the readings
import sys
import time
import datetime
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *

try:
    ch = VoltageRatioInput()
    ch2 = VoltageRatioInput()
except RuntimeError as e:
    print("Runtime Exception %s" % e.details)
    print("Press Enter to Exit...\n")
    readin = sys.stdin.read(1)
    exit(1)

a=0
b=0

def VoltageRatioChangeHandlerA(e, voltageRatio):
        global a
        a = voltageRatio

def VoltageRatioChangeHandlerB(e, voltageRatio):
        global b
        b = voltageRatio


start = time.time()

ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandlerA)
ch.setHubPort(1)
ch.setIsHubPortDevice(1)
ch.openWaitForAttachment(5000)
ch.setDataInterval(1)

ch2.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandlerB)
ch2.setHubPort(0)
ch2.setIsHubPortDevice(1)
ch2.openWaitForAttachment(5000)
ch2.setDataInterval(1)

while 1:
    try:
        print("Channel 0: {0}   Channel 1: {1}".format(b,a))
        time.sleep(0.4)
    except (KeyboardInterrupt):
        print ("gracefully aborted")
        ch.close()       
        ch2.close()
        sys.exit()


I set the dataInterval for both channels to the minimum for the VINT Hub (1ms) so that 'a' and 'b' will always be as up-to-date as possible regardless of how long the loop sleeps for.

MaxS
Fresh meat
Posts: 3
Joined: Mon Dec 11, 2017 11:46 pm
Contact:

Re: Time interval data from Vint hub

Postby MaxS » Tue Dec 12, 2017 6:11 pm

Dear Mparadis,

Thanks for the very helpful and clear explanations. The first sensor is connected to port 1 of the hub. The second connection is to port 0 of the hub. Only problem is that, the second sensor is attached to hub via a Wheatstone bridge (DAQ) and not directly. In the form that you have suggested, it only prints the value from the first sensor that is directly connected to the VINT-hub and not the bridge one, for both of the channels. Should I add the following line back below the ch2 section?

Many thanks for your kind help in advance.

Code: Select all

        if(ch.getChannelSubclass() == ChannelSubclass.PHIDCHSUBCLASS_VOLTAGERATIOINPUT_BRIDGE):
            ch.setBridgeEnabled(1)

MaxS
Fresh meat
Posts: 3
Joined: Mon Dec 11, 2017 11:46 pm
Contact:

Re: Time interval data from Vint hub

Postby MaxS » Tue Dec 12, 2017 9:51 pm

p.s: after playing with the codes for some time I see that the original code that I posted "see"s both of the channels even without setBridgeEnabled.

The code that you kindly posted sees only the sensor directly connected to the Vint-hub (port1). However I tried to change your code to address this (by adding the setBridgeEnabled(1) to channel 2 for instance and setting from 1 to 0 in "ch2.setIsHubPortDevice(0)").
), it either didn't recognize the sensor connected to port 0 of the hub (as mentioned, in fact, a bridge is attached to port 0 and sensor is attached to bridge) or it returns an error like this:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "D:/Amir/Python/Newalg1.py", line 47, in <module>
ch2.setDataInterval(1)
File "C:\Python27\lib\Phidget22\Devices\VoltageRatioInput.py", line 174, in setDataInterval
raise PhidgetException(result)
Phidget22.PhidgetException.PhidgetException


I am really grateful for your advice.

User avatar
mparadis
Site Admin
Posts: 531
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Time interval data from Vint hub

Postby mparadis » Wed Dec 13, 2017 9:26 am

I had assumed that you were trying to access normal sensors on VINT ports 0 and 1. Now that I know you have a DAQ1500 connected to port 0, we don't need to setIsHubPortDevice(1) since we're accessing a Phidget that's connected to the hub.

Code: Select all


#There is about 0.8 seconds difference between the readings
import sys
import time
import datetime
from Phidget22.Devices.VoltageRatioInput import *
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
from Phidget22.Net import *

try:
    ch = VoltageRatioInput()
    ch2 = VoltageRatioInput()
except RuntimeError as e:
    print("Runtime Exception %s" % e.details)
    print("Press Enter to Exit...\n")
    readin = sys.stdin.read(1)
    exit(1)

a=0
b=0

def VoltageRatioChangeHandlerA(e, voltageRatio):
        global a
        a = voltageRatio

def VoltageRatioChangeHandlerB(e, voltageRatio):
        global b
        b = voltageRatio


start = time.time()

ch.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandlerA)
ch.setHubPort(1)
ch.setIsHubPortDevice(1)
ch.openWaitForAttachment(5000)
ch.setDataInterval(1)

ch2.setOnVoltageRatioChangeHandler(VoltageRatioChangeHandlerB)
ch2.setHubPort(0)
ch2.setChannel(0)
ch2.openWaitForAttachment(5000)
ch2.setDataInterval(20)

while 1:
    try:
        print("Bridge: {0}   Sensor: {1}".format(b,a))
        time.sleep(0.4)
    except (KeyboardInterrupt):
        print ("gracefully aborted")
        ch.close()       
        ch2.close()
        sys.exit()


- I changed the DataInterval for port 0, since the minimum data interval is 20ms for the DAQ1500. You can find this information on the API page and selecting DAQ1500 from the second drop-down box.

- I also added setChannel(0) because I have the load cell plugged into port 0 of the DAQ1500.

- You don't actually have to call setBridgeEnabled(1) because it is enabled by default (you only need this function if you choose to disable the bridge input for some reason).

- The reason that python is giving you such cryptic error messages is because you're not using try/except to catch PhidgetException errors. If you catch PhidgetExceptions, they'll be able to tell you what's wrong (e.g. invalid argument, Phidget not attached, etc). You can see how catching PhidgetException works if you look at the python sample code for VoltageRatio.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest