import os import sys import traceback import time from datetime import datetime import u3 import cv2 import cv2.aruco as aruco from decimal import Decimal # Labjack variables SCAN_FREQUENCY = 1000 # It is the scan frequency of stream mode [Hz], 10kHz => 0.1ms SAMPLES_FOR_EACH_PACKET = 1 # It is the number of samples NUMBER_OF_CHANNELS_TO_SCAN = 1 # It specifies how many actual DAC/ADC channels we will scan HIGH_V_THRESHOLD = 3 # High will be when > threshold [V] # U3 Device configuration d = None d = u3.U3() d.configU3() # To learn the if the U3 is an HV d.getCalibrationData() # For applying the proper calibration to readings. d.streamConfig(NumChannels=NUMBER_OF_CHANNELS_TO_SCAN, PChannels=[0], NChannels=[31], SamplesPerPacket=SAMPLES_FOR_EACH_PACKET, Resolution=3, ScanFrequency=SCAN_FREQUENCY) # with microsec resolution def fun_get_current_time_sec(): time_temp = format(time.time(),'.6f') return Decimal(time_temp) # Custom functions def fnc_callback(counter): return counter + 1 # ----- Main ----- if d is None: print("""Configure a device first. Please open streamTest.py in a text editor and uncomment the lines for your device. Exiting...""") sys.exit(0) try: counter = 0 sample_rate_array = [] print("Start stream") d.streamStart() # Start streaming the reading(s) start = datetime.now() print("Start time is %s" % start) missed = 0 dataCount = 0 packetCount = 0 for r in d.streamData(): if r is not None: # Reports of errors, missed or underflow conditions 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"]) # Comment out these prints and do something with r # round(random.uniform(0, 5), 3) ain0_avg = round(sum(r["AIN0"])/len(r["AIN0"]),2) #if DEBUG_FLAG: print("# of trigger events read: %s @ %s" % (counter,str(fun_get_current_time_sec()))) # if AIN>V_THRESHOLD we call the function which increases a counter if r["AIN0"][-1] > HIGH_V_THRESHOLD: counter = fnc_callback(counter) dataCount += 1 packetCount += r['numPackets'] else: # Got no data back from our read. This only happens if your stream isn't faster than the USB read timeout, ~1 sec. print("No data ; %s" % datetime.now()) except: print("".join(i for i in traceback.format_exc())) finally: stop = datetime.now() d.streamStop() print("Stream stopped.\n") 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("Set Scan Rate = %s Hz" % SCAN_FREQUENCY) print("Timed Sample Rate = %s samples / %s seconds = %s Hz" % (sampleTotal, runTime, float(sampleTotal)/runTime))