""" Demonstrates how to stream a range of sequential analog inputs using the eStream functions. Useful when streaming many analog inputs. AIN channel scan list is FIRST_AIN_CHANNEL to FIRST_AIN_CHANNEL + NUMBER_OF_AINS - 1. """ import json import matplotlib.pyplot as plt from labjack import ljm import time import sys from datetime import datetime MAX_REQUESTS = 2 # The number of eStreamRead calls that will be performed. FIRST_AIN_CHANNEL = 0 #AIN0 NUMBER_OF_AINS = 16 # Open first found LabJack handle = ljm.open(ljm.constants.dtANY, ljm.constants.ctANY, "ANY") #handle = ljm.openS("ANY", "ANY", "ANY") info = ljm.getHandleInfo(handle) print("Opened a LabJack with Device type: %i, Connection type: %i,\n" \ "Serial number: %i, IP address: %s, Port: %i,\nMax bytes per MB: %i" % \ (info[0], info[1], info[2], ljm.numberToIP(info[3]), info[4], info[5])) # Stream Configuration readA="DIO6_EF_READ_A" addrA = ljm.nameToAddress(readA)[0] aScanListNames = ["AIN0", "DIO6_EF_READ_A", "STREAM_DATA_CAPTURE_16" ] # ["AIN%i"%i for i in range(FIRST_AIN_CHANNEL, FIRST_AIN_CHANNEL+NUMBER_OF_AINS)] #Scan list names print("\nScan List = " + " ".join(aScanListNames)) numAddresses = len(aScanListNames) aScanList = ljm.namesToAddresses(numAddresses, aScanListNames)[0] scanRate = 1000 scansPerRead = int(scanRate/2) try: # Configure the analog inputs' negative channel, range, settling time and # resolution. # Note when streaming, negative channels and ranges can be configured for # individual analog inputs, but the stream has only one settling time and # resolution. aNames = ["AIN_ALL_RANGE", "STREAM_SETTLING_US", "STREAM_RESOLUTION_INDEX"] aValues = [10.0, 0, 1] #single-ended, +/-10V, 0 (default), #0 (default) ljm.eWriteNames(handle, len(aNames), aNames, aValues) #set the parameters for the quadrature signal fioA = "DIO"+str(6) # Digital input for signal A of encoder fioB = "DIO"+str(6+1) # readA = fioA + "_EF_READ_A" # Read from encoder signal A # addrA = ljm.nameToAddress(readA)[0] # Get the Address of signal # Configures the counter ljm.eWriteName(handle, fioA+"_EF_ENABLE", 0) # shut off input signal A ljm.eWriteName(handle, fioB+"_EF_ENABLE", 0) # shut off input signal B # activate quad mode & define input 6 as A signal ljm.eWriteName(handle, fioA+"_EF_INDEX", 10) # activate quad mode & define input 7 as B signal ljm.eWriteName(handle, fioB+"_EF_INDEX", 10) ljm.eWriteName(handle, fioA+"_EF_ENABLE", 1) # enable input 6 ljm.eWriteName(handle, fioB+"_EF_ENABLE", 1) # enable input 7 ljm.eWriteName(handle, fioA+"_EF_CONFIG_A", 1) # enable idx sig of encoder ljm.eWriteName(handle, fioA+"_EF_CONFIG_B", 5) # def input 5 as index sig ljm.eWriteName(handle, fioB+"_EF_CONFIG_A", 1) # enable idx sig of encoder ljm.eWriteName(handle, fioB+"_EF_CONFIG_B", 5) # def input 5 as index sig # Configure and start stream scanRate = ljm.eStreamStart(handle, scansPerRead, numAddresses, aScanList, scanRate) print("\nStream started with a scan rate of %0.0f Hz." % scanRate) print("\nPerforming %i stream reads." % MAX_REQUESTS) start = datetime.now() totScans = 0 totSkip = 0 # Total skipped samples dataResult = [] i = 1 quad0 = [] quad1 = [] ain0 = [] positionFB = [] while i <= MAX_REQUESTS: ret = ljm.eStreamRead(handle) data = ret[0] scans = len(data)/numAddresses totScans += scans quad0.append(data[1::3]) quad1.append(data[2::3]) ain0.append(data[::3]) # Count the skipped samples which are indicated by -9999 values. Missed # samples occur after a device's stream buffer overflows and are # reported after auto-recover mode ends. curSkip = data.count(-9999.0) totSkip += curSkip print("\neStreamRead %i" % i) ainStr = "" for j in range(0, numAddresses): ainStr += "%s = %0.5f " % (aScanListNames[j], data[j]) print(" 1st scan out of %i: %s" % (scans, ainStr)) print(" Scans Skipped = %0.0f, Scan Backlogs: Device = %i, LJM = " \ "%i" % (curSkip/numAddresses, ret[1], ret[2])) i += 1 end = datetime.now() print("\nTotal scans = %i" % (totScans)) tt = (end-start).seconds + float((end-start).microseconds)/1000000 print("Time taken = %f seconds" % (tt)) print("LJM Scan Rate = %f scans/second" % (scanRate)) print("Timed Scan Rate = %f scans/second" % (totScans/tt)) print("Timed Sample Rate = %f samples/second" % (totScans*numAddresses/tt)) print("Skipped scans = %0.0f" % (totSkip/numAddresses)) except ljm.LJMError: ljme = sys.exc_info()[1] print(ljme) except Exception: e = sys.exc_info()[1] print(e) print("\nStop Stream") ljm.eStreamStop(handle) for k in range(len(quad0)): pos0 = quad0[k] pos1 = quad1[k] for l in range(len(pos0)): positionFB.append((pos0[l]+pos1[l]*65536)*360/8192.0) plt.plot(positionFB) # Close handle ljm.close(handle) with open('data'+'.json', 'w') as datfile: json.dump(data, datfile)