Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Photo

U3-HV stream mode example; interpreting results ?

U3 Python stream

  • Please log in to reply
1 reply to this topic

#1 Paul.L.

Paul.L.
  • Members
  • 22 posts

Posted 06 April 2015 - 04:26 PM

I'm using the streamTest.py example to learn how to use the U3 from Python Unix (Mac). The setup is current transducers with +-10V output on 120V/60Hz standard current. The loads are fluorescent fixtures with electronic dimmers. 

 

I don't know how to interpret the results. Most numbers are negative. Are they raw values that need calibration? I would like to know the power of the circuits and then would post-process to calculate energy. 

 

Data run results:

start stream 2015-03-30 09:33:18.514646

Average of 600 AIN0, 600 AIN1 reading(s): -0.0125433347126 , -0.00273322432923
Average of 600 AIN0, 600 AIN1 reading(s): -0.0149552490686 , -0.0059797189602
Average of 600 AIN0, 600 AIN1 reading(s): -0.0150236721709 , -0.00773966078646
Average of 600 AIN0, 600 AIN1 reading(s): -0.014134171841 , -0.00703910141873
snip
Average of 600 AIN0, 600 AIN1 reading(s): -0.0151434126 , -0.00690240690795
Average of 600 AIN0, 600 AIN1 reading(s): -0.0163579226658 , -0.0096704707512
Average of 600 AIN0, 600 AIN1 reading(s): -0.0142881238212 , -0.00538168047555
Traceback (most recent call last):
  File "labjack_2_channel_0319.py", line 72, in <module>
    d.streamStop()
  File "/Users/PaulSan/.virtualenvs/labjack/lib/python2.7/site-packages/LabJackPython.py", line 820, in streamStop
    raise LowlevelErrorException(results[2], "StreamStop returned an error:\n    %s" % lowlevelErrorToString(results[2]) )
LabJackPython.LowlevelErrorException: StreamStop returned an error:
    SCRATCH_ERASE_FAIL (2)
(labjack)Pauls:PaulSan ~/.virtualenvs/labjack/labjack_scripts$ 
 
Script:
__author__ = 'chex'

# to read on analog channels AIN0 and AIN1 at one minute intervals with the data written to a csv file.
# Each row should have a date, time, channel 0 value, channel 1 value.
import u3
import traceback
from datetime import datetime

# MAX_REQUESTS is the number of packets to be read.
MAX_REQUESTS = 75

################################################################################
## U3
## Uncomment these lines to stream from a U3
################################################################################
## At high frequencies ( >5 kHz), the number of samples will be MAX_REQUESTS times 48 (packets per request) times 25 (samples per packet).
d = u3.U3()
#
## To learn if the U3 is an HV
d.configU3()
#
## For applying the proper calibration to readings.
d.getCalibrationData()
#
## Set the FIO0 to Analog
d.configIO(FIOAnalog = 3)
#   
#print "configuring U3 stream"
d.streamConfig(NumChannels=2, PChannels=[0, 1], NChannels=[31, 31], Resolution=3, ScanFrequency=5000)

try:
    print "start stream",
    d.streamStart()
    start = datetime.now()
    print start

    missed = 0
    dataCount = 0
    packetCount = 0

    for r in d.streamData():
        if r is not None:
            # Our stop condition
            if dataCount >= MAX_REQUESTS:
                break

            if r['errors'] != 0:
                print "Error: %s ; " % r['errors'], datetime.now()

            if r['numPackets'] != d.packetsPerRequest:
                print "----- UNDERFLOW : %s : " % r['numPackets'], datetime.now()

            if r['missed'] != 0:
                missed += r['missed']
                print "+++ Missed ", r['missed']

            # Comment out these prints and do something with r
            print "Average of", len(r['AIN0']), "AIN0,", len(r['AIN1']), "AIN1 reading(s):",
            print sum(r['AIN0']) / len(r['AIN0']), ",", sum(r['AIN1']) / len(r['AIN1'])

            dataCount += 1
            packetCount += r['numPackets']
        else:
            # Got no data back from our read.
            # This only happens if your stream isn't faster than the
            # the USB read timeout, ~1 sec.
            print "No data", datetime.now()
except:
    print "".join(i for i in traceback.format_exc())
finally:
    stop = datetime.now()
    d.streamStop()
    print "stream stopped."
    d.close()

    sampleTotal = packetCount * d.streamSamplesPerPacket

    scanTotal = sampleTotal / 2  #sampleTotal / NumChannels
    print "%s requests with %s packets per request with %s samples per packet = %s samples total." % (
    dataCount, (float(packetCount) / dataCount), d.streamSamplesPerPacket, sampleTotal )
    print "%s samples were lost due to errors." % missed
    sampleTotal -= missed
    print "Adjusted number of samples = %s" % sampleTotal

    runTime = (stop - start).seconds + float((stop - start).microseconds) / 1000000
    print "The experiment took %s seconds." % runTime
    print "Scan Rate : %s scans / %s seconds = %s Hz" % ( scanTotal, runTime, float(scanTotal) / runTime )
    print "Sample Rate : %s samples / %s seconds = %s Hz" % ( sampleTotal, runTime, float(sampleTotal) / runTime )

 

 

 



#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 06 April 2015 - 06:36 PM

Analog input values are in volts. The streamData method will return voltage values by default, and calling getCalibrationData() before starting your stream will make those values calibrated. Keep in mind that the example is displaying the average of 600 samples for AIN0 and AIN1 each streamData loop iteration. If you wanted to go through all samples returned by streamData, in the loop it would be something like this for AIN0 and AIN1:

 

#Iterate through AIN0's (r['AIN0']) stream samples that were returned by streamData.

for samp in r['AIN0']:

    #handle AIN0 sample (samp)

 

#Iterate through AIN1's (r['AIN1']) stream samples that were returned by streamData

for samp in r['AIN1']:

    #handle AIN1 sample (samp)

 

For general documentation on stream mode look here:

 

http://labjack.com/s...users-guide/3.2

 

For documentation on the Python interface's stream methods refer to the LabJackPython.py and u3.py source code, or use the Python help function on the class or method you want documentation on.





Also tagged with one or more of these keywords: U3, Python, stream

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users