Configuring U3 to count pulses | LabJack
 

Configuring U3 to count pulses

7 posts / 0 new
Last post
Bee Research Pty Ltd
steven1's picture
Configuring U3 to count pulses

Hi Guys, 

I am new to this and hope I can get a pointer in the right direction. I am doing an experiment where I need to stream 4 analogue inputs to measure voltages and one digital input to count pulses from a radiation detector. 

The pulses from the radiation detector are 3.3V square waves of around 50 microseconds, and I need to get the counts per second.

I have the 4 analogue inputs working, but not sure how to configure the last input for the detector.

Any assistance would be appreciated.

Steven

https://www.gammaspectacular.com

 


import sys
import traceback
from datetime import datetime
import time
import u3


def labjack(num_samples):
    MAX_REQUESTS = 100    # Number of requests per second
    SCAN_FREQUENCY = 20000   # Hz

    d = None
    d = u3.U3()
    d.configU3() #  Check if U3 is HV
    d.getCalibrationData()
    d.configIO(FIOAnalog=4) # Set the FIO0 to FIO3 to Analog (d3 = b00000011)
    d.streamConfig(
        NumChannels=4, # Number of channels to stream
        PChannels=[0, 1, 2, 3], # Numbers 0-7 for positive channel inputs
        NChannels=[31, 31, 31, 31], # Numbers 0-7 for negative voltage inputs or 32 for single ended
        Resolution=3, 
        ScanFrequency=SCAN_FREQUENCY)

    try:
        d.streamStart()
        missed = 0
        dataCount = 0
        packetCount = 0
        readings = {"AIN0":[],"AIN1":[],"AIN2":[],"AIN3":[]}

        for r in d.streamData():
            if len(readings["AIN0"]) >= num_samples:
                d.streamStop()
                d.close()
                break

            if r is not None:
                if r["errors"] != 0:
                    print("Errors counted: %s ; %s" % (r["errors"], datetime.now()))

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

                if r["missed"] != 0:
                    missed += r['missed']
                    print("+++ Missed %s" % r["missed"])
                
                for k in readings.keys():
                    readings[k] += r[k]
                
                dataCount += 1
                packetCount += r['numPackets']

            else:
                print("No data ; %s" % datetime.now())
    except:
        d.streamStop()
        print("Stream stopped.\n")
        d.close()

    now = int(datetime.now().strftime('%s%f'))
    avgs = {'time':now}

    for k in readings.keys():
        avgs[k] = sum(readings[k])/len(readings[k])    
        d.close()

    return avgs


while True:
    avgs = labjack(100)
    print(avgs)    

LabJack Support
labjack support's picture
A counter would be your best

A counter would be your best option to capture the edges while streaming:

https://labjack.com/support/datasheets/u3/hardware-description/timers-co...

You would use our ConfigIO function to setup the counter:

https://labjack.com/support/datasheets/u3/low-level-function-reference/c...

https://github.com/labjack/LabJackPython/blob/master/src/u3.py#L309

You will need to use special channels to capture the counter readings while streaming. Since stream mode is limited to 16-bit channel reads and the counter returns a 32-bit value, you will need to stream a special counter channel (lower two bytes of the counter return) and the TC_Capture channel (upper two bytes of the counter return) then recombine the two into the full 32-bit counter value:

https://labjack.com/support/datasheets/u3/operation/stream-mode/digital-...

 

Bee Research Pty Ltd
steven1's picture
Thanks for quick reply, I

Thanks for quick reply, I will try to follow the instructions. 

Bee Research Pty Ltd
steven1's picture
Support, 

Support, 

I followed your links, but most of the directions were above my level, do you have any sample programs in Python showing how to assign a pin to count pulses and output a result?

Even a few basic steps to get me started would be helpful.

 

Thanks..

 

Steven

 

Bee Research Pty Ltd
steven1's picture
Hi Guys, 

Hi Guys, 

I tried my best to follow instructions in the above post, but despite dedicating most of my weekend to this nothing has worked. I'm obviously missing something. 

Does anyone have a short Python sample script showing how to configure a counter?

"You will need to use special channels to capture the counter readings while streaming."

This also confused me, not sure which channels should I use, I tried 210 and 224 but consistently got errors.

d.configIO( ? ) 
d.streamConfig( ? )

I assume the problem must be in my settings.

Any help would be much appreciated. 

Steven

LabJack Support
labjack support's picture
There is an example for

There is an example for configuring a counter in our Python source:

https://github.com/labjack/LabJackPython/blob/master/src/u3.py#L2780

d.configIO(EnableCounter0 = True, FIOAnalog = 15)

If you do not change configIO to set TimerCounterPinOffset it will use the default value of 4, thus Counter0 would appear on FIO4 if no other timers or counters are set up. The pin offset must be 4 or higher when using the U3.

If you have Counter0 enabled, you would want to read channel 210 and 224 under stream mode.

    d.streamConfig(
        NumChannels=6, # Number of channels to stream
        PChannels=[0, 1, 2, 3, 210, 224], # Numbers 0-7 for positive channel inputs
        NChannels=[31, 31, 31, 31,31,31], # Numbers 0-7 for negative voltage inputs or 32 for single ended
        Resolution=3, 
        ScanFrequency=SCAN_FREQUENCY)

You may need to reduce the scan frequency. The maximum scan rate is 50000Hz, and that is split across all channels (so 10000 if sampling 5 channels).

 

Bee Research Pty Ltd
steven1's picture
Just a quick reply to say

Just a quick reply to say thanks, with your support and a bit of help from my son (programmer) I  got all functions including the counter to work on my Dashboard. This was coded in Python3 using Dash Plotly on a Linux platform.

Cheers.. 

Steven