Hi,
How would I make sure that the data collected from both the digitalIn and the analogIn is from the same stretch of time? I have data going into the both scopes on the Analog Discovery 2 and data going into 5 of the digitalIn inputs. I am trying to use the external trigger T1 to trigger both analog and digital data collection. I am doing this through python.
from ctypes import *
from dwfconstants import *
import time
import matplotlib.pyplot as plt
import sys
import numpy
def analogSetup(dwf, hdwf, analogFreq, nSamples, trigSeconds):
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(1), c_int(1))
dwf.FDwfAnalogInTriggerSourceSet(hdwf, c_ubyte(11)) # 11 = external trigger T1
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(20))
dwf.FDwfAnalogOutNodeOffsetSet(hdwf, c_int(1), c_double(-10))
dwf.FDwfAnalogInFrequencySet(hdwf, analogFreq)
dwf.FDwfAnalogInTriggerPositionSet(hdwf, c_double(trigSeconds))
def digitalSetup(dwf, hdwf, digitalFreq, nSamples, hzDI, trigSamples):
dwf.FDwfDigitalInInternalClockInfo(hdwf, byref(hzDI))
print("DigitanIn base freq: "+str(hzDI.value))
divider = int(hzDI.value/digitalFreq)
dwf.FDwfDigitalInDividerSet(hdwf, c_int(divider))
dwf.FDwfDigitalInSampleFormatSet(hdwf, c_int(16)) # 16bit per sample format
dwf.FDwfDigitalInTriggerSourceSet(hdwf, c_ubyte(11)) # digital trigger with T1
dwf.FDwfDigitalInBufferSizeSet(hdwf, c_int(nSamples)) # set number of sample to acquire
dwf.FDwfDigitalInTriggerPositionSet(hdwf, c_int(trigSamples)) # set number of sample to acquire after trigger
def triggerSetup(dwf, hdwf):
dwf.FDwfAnalogInTriggerAutoTimeoutSet(hdwf, c_double(10)) # 10 second auto trigger timeout
dwf.FDwfAnalogInTriggerTypeSet(hdwf, c_int(0)) # trigtypeEdge
dwf.FDwfAnalogInTriggerChannelSet(hdwf, c_int(0)) # channel 1
dwf.FDwfAnalogInTriggerLevelSet(hdwf, c_double(5)) # 5V max
dwf.FDwfAnalogInTriggerHysteresisSet(hdwf, c_double(0.01)) # 0.01V
dwf.FDwfAnalogInTriggerConditionSet(hdwf, c_int(0)) # trigcondRisingPositive
def frameRequest(dwf, hdwf):
dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), c_int(0), c_bool(True))
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), c_int(0), funcPulse)
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), c_int(0), c_double(50000))
dwf.FDwfAnalogOutNodeOffsetSet(hdwf, c_int(0), c_int(0), c_double(0))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), c_int(0), c_double(float(3.3)))
dwf.FDwfAnalogOutWaitSet(hdwf, c_int(0), c_double(5)) # wait length
dwf.FDwfAnalogOutNodeSymmetrySet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(2))
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(True))
if sys.platform.startswith("win"):
dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
dwf = cdll.LoadLibrary("libdwf.so")
#declare ctype variables
hdwf = c_int()
sts = c_byte()
sts_d = c_byte()
hzAcq = c_double(10e6)
DIG_ACQ_FREQ = 10e6
AN_ACQ_FREQ = 10e6
nSamples = 1000
nSamples_d = 1000
rgdSamples1 = (c_double*nSamples)()
rgdSamples2 = (c_double*nSamples)()
rgwSamples = (c_uint16*int(nSamples))()
cAvailable = c_int()
cAvailable_d = c_int()
cLost = c_int()
cCorrupted = c_int()
fLost = 0
fCorrupted = 0
hzDI = c_double()
trigSamples = int(nSamples/2) # Digital Samples are number of samples after we see the trigger
trigSeconds = (1.0/int(hzAcq.value))*(nSamples-trigSamples) # Analog samples are seeing the trigger at this time
#print DWF version
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
#open device
print("Opening first device")
if dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf)) != 1 or hdwf.value == hdwfNone.value:
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
print("Digital Setup")
digitalSetup(dwf, hdwf, DIG_ACQ_FREQ, nSamples, hzDI,trigSamples)
print("Analog Setup")
analogSetup(dwf, hdwf, hzAcq, nSamples,trigSeconds)
print("Starting oscilloscope")
dwf.FDwfDigitalInConfigure(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1))
print("Frame Request")
frameRequest(dwf, hdwf)
sts_a = c_byte()
while True:
dwf.FDwfDigitalInStatus(hdwf, c_int(1), byref(sts))
dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts_a))
print("Digital Status:", str(sts.value))
print("Analog Status:", str(sts_a.value))
if sts.value == 2 & sts_a.value == 2: # done
break
time.sleep(1)
print("Digital and Analog done")
dwf.FDwfDigitalInStatusData(hdwf, rgwSamples, 2*nSamples) # Need to do 2*samples because this function is in bytes
dwf.FDwfAnalogInStatusData(hdwf, c_int(0), rgdSamples1, nSamples)
dwf.FDwfAnalogInStatusData(hdwf, c_int(1), rgdSamples2, nSamples)
dwf.FDwfAnalogOutReset(hdwf, c_int(0))
dwf.FDwfAnalogInReset(hdwf)
dwf.FDwfDigitalInReset(hdwf)
dwf.FDwfDeviceCloseAll()
print("Recording done")
dsamples_mask =[]
for i in range(len(rgwSamples)):
dsamples_mask.append(int(rgwSamples[i]) & 0b10000)
plt.plot(numpy.fromiter(dsamples_mask, dtype = numpy.uint16))
plt.plot(numpy.fromiter(rgdSamples1, dtype = numpy.float64), color='orange')
plt.plot(numpy.fromiter(rgdSamples2, dtype = numpy.float64), color = 'green')
plt.show()
This is what I have so far. I tried to use some math to figure out what the trigger positions is supposed to be given a digital trigger position, but that hasn't really worked out. The best I can get is shifting around the analog and digital plot by changing the triggerPositionSet value, but I need it so that both digital and analog is captured at the exact same time.
For the thing I'm measuring, I KNOW that the analog and digital lines up, I just can't figure it out in code.
Please let me know if any clarification is needed.