import u3 import traceback from datetime import datetime import csv import sys import time import math # MAX_REQUESTS is the number of packets to be read. MAX_REQUESTS = 100 # ## 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() # ##Assigns FIO4 as Digital input timer d.configIO(TimerCounterPinOffset=4, NumberOfTimersEnabled=1) # cld= u3.Timer0Config(TimerMode=12, Value=0) d.getFeedback(cld) # ##6=48MHz clock base, 48= clock divisor. 48MHz/48=1MHz samples taken. crd=d.configTimerClock(6,48) d.getFeedback(crd) #timer.sleep() cmd=u3.Timer(timer=0) ticks = d.getFeedback(cmd)[0] d.streamConfig(NumChannels=4, PChannels=[0, 1, 2, 200], NChannels=[31, 31, 31, 31], Resolution=1, ScanFrequency=2000) fname=input("Output Data File Name? ") type(fname) while True: try: pmax1 = float(input("Input Sensor 1 pressure range: ")) except ValueError: print('Input invalid...please try again') else: break while True: try: s1offset = float(input("Input Sensor 1 Calibration offset: ")) except ValueError: print('Input invalid...please try again') else: break while True: try: pmax2 = float(input("Input Sensor 2 pressure range: ")) except ValueError: print('Input invalid...please try again') else: break while True: try: s2offset = float(input("Input Sensor 2 Calibration offset: ")) except ValueError: print('Input invalid...please try again') else: break with open('%s.csv' %fname, 'w', newline='') as csvfile: csvwriter = csv.writer(csvfile) 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']) if ticks > 0: print("good") period_sec = ticks/1000000 #ticks=microseconds between each rising edge. Divided by 1 million gives the period between rising edge in seconds. freq_hz = (1/period_sec) GPM = freq_hz/87*60 else: print("bad") freq_hz = 0 GPM = 0 print ("Average of", len(r['AIN1']), "AIN1,", len(r['AIN2']), "AIN2 reading(s):") print (sum(r['AIN1']) / len(r['AIN1']), ",", sum(r['AIN2']) / len(r['AIN2'])) print("Frequency = ", freq_hz) print("GPM =", GPM) outa1comp = pmax1/4*(((sum(r['AIN1'])/len(r['AIN1']))-(.5-(5-(sum(r['AIN0'])/len(r['AIN0']))))))+s1offset outa2comp = pmax2/4*(((sum(r['AIN2'])/len(r['AIN2']))-(.5-(5-(sum(r['AIN0'])/len(r['AIN0']))))))+s2offset csvwriter.writerow([outa1comp, outa2comp]) csvfile.flush() 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.") csvfile.close() 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 ))