Jump to content

Zeyu

Members
  • Posts

    6
  • Joined

  • Last visited

Posts posted by Zeyu

  1. Hi,

    I am developing a system including the wavegen, digital out triggers and oscilloscope based on the analog discover 2 and programmed with the SDK python.

    The codes are shown as attached, currently the codes works well but I have three questions:

    1. nSamples = int(8192), the datasheet shows the maximum record length is 16 k, however, when I increase the nSample to any value over 8192, the records went wrong. Since there are two analogIn channels, is that means the maximum record length for each channel is 8 k?

    2. The time.sleep =3s issue, the python examples say the oscilloscope need 2 s to stabilize, I am wondering what is the minimum time interval between each time AnalogIn oscilloscope sampling? Since my application requires 500 times oscilloscope capture, decreasing the time interval helps a lot.

    3. Currently I used one AnalogIn channel, and I am trying to use two AnalogIn channels and each channel triggered with rising edge of different digital out signal. However, I found that it seems like the two channels are triggered with only the first detected rising edge of the two trigger signals. I am wondering is there any solution to apply the two seperate triggers for the two oscillisope channels?

     

    Thanks!

    Zeyu


    hdwf = c_int()
    sts = c_byte()
    hzAcq = c_double(5e6)
    nSamples = int(8192)
    cAvailable = c_int()
    cLost = c_int()
    cCorrupted = c_int()
    fLost = 0
    fCorrupted = 0

    #print(DWF version
    version = create_string_buffer(16)
    dwf.FDwfGetVersion(version)
    print("DWF Version: "+str(version.value))

    #open device
    for j in range(1,50, 1):

      print("Opening first device")
      dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
      rgSamples1 = (c_int16*nSamples)()
      if hdwf.value == hdwfNone.value:
          szerr = create_string_buffer(512)
          dwf.FDwfGetLastErrorMsg(szerr)

          print(str(szerr.value))
          print("failed to open device")
          quit()


      print("Generating sine wave...")

      #  dwf.FDwfDigitalOutTriggerSourceSet(hdwf,c_int(0), c_int(8))
    #  dwf.FDwfDigitalOutTriggerSourceSet(hdwf,c_int(1), c_int(8))
      dwf.FDwfDigitalOutDividerSet(hdwf, c_int(0), c_int(20))
      dwf.FDwfDigitalOutDividerSet(hdwf, c_int(1), c_int(20))
      dwf.FDwfDigitalOutDividerSet(hdwf, c_int(2), c_int(20))   
      dwf.FDwfDigitalOutDividerSet(hdwf, c_int(3), c_int(20)) 
    #  dwf.FDwfDigitalOutDividerSet(hdwf, c_int(4), c_int(20))
      dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(0), c_uint(0), c_uint(1))
      dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(1), c_uint(1), c_uint(1))
      dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(2), c_uint(0), c_uint(1))
      dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(3), c_uint(1), c_uint(1))
    #  dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(4), c_uint(1), c_uint(1))

      dwf.FDwfDigitalOutEnableSet(hdwf, c_int(0), c_int(1))
    #  dwf.FDwfDigitalOutRunSet(hdwf, c_double(0.00005)) # 1ms run
    #  dwf.FDwfDigitalOutWaitSet(hdwf, c_double(0)) # 1ms run
    #  dwf.FDwfDigitalOutRepeatSet(hdwf, c_int(3))
      dwf.FDwfDigitalOutCounterSet(hdwf, c_int(0), c_uint(4150), c_uint(27150)) # 100MHz base freq /(30+30) = 1.67 MHz
      dwf.FDwfDigitalOutEnableSet(hdwf, c_int(1), c_int(1))
      dwf.FDwfDigitalOutCounterSet(hdwf, c_int(1), c_uint(27050), c_uint(4250)) # 100MHz base freq /(30+30) = 1.67 MHz
    #  dwf.FDwfDigitalOutRunSet(hdwf, c_double(0.00005)) # 1ms run
    #  dwf.FDwfDigitalOutWaitSet(hdwf, c_double(0)) # 1ms run
    #  dwf.FDwfDigitalOutRepeatSet(hdwf, c_int(3)) # once
      dwf.FDwfDigitalOutEnableSet(hdwf, c_int(2), c_int(1))
      dwf.FDwfDigitalOutCounterSet(hdwf, c_int(2), c_uint(28350), c_uint(2950)) 
      dwf.FDwfDigitalOutEnableSet(hdwf, c_int(3), c_int(1))
      dwf.FDwfDigitalOutCounterSet(hdwf, c_int(3), c_uint(27300), c_uint(4000))
    #  dwf.FDwfDigitalOutEnableSet(hdwf, c_int(4), c_int(1))
    #  dwf.FDwfDigitalOutCounterSet(hdwf, c_int(4), c_uint(27300), c_uint(4000))

      dwf.FDwfAnalogOutTriggerSourceSet(hdwf,c_int(0),c_int(12))
    #  dwf.FDwfAnalogOutTriggerSlopeSet(hdwf,c_int(0), DwfTriggerSlopeRise)
    # dwf.FDwfAnalogInTriggerChannelSet(hdwf, 1) # 1st channel
    #  dwf.FDwfAnalogOutTriggerLevelSet(hdwf, c_double(0.5)) # 0.5V

      dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_bool(True))
      dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), AnalogOutNodeCarrier, funcSine)
      dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double((16.8+j/50.000)*hzAcq.value/500.000))
      dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(5)) 
      dwf.FDwfAnalogOutRunSet(hdwf,c_int(0), c_double(270000/(hzAcq.value*10))) # run for 2 periods
    #  dwf.FDwfAnalogOutWaitSet(hdwf, c_int(0), c_double(10000/hzAcq.value)) # wait one pulse time
      dwf.FDwfAnalogOutRepeatSet(hdwf, c_int(0), c_int(1)) # repeat 5 times  

      dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_bool(True))
      dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
      dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord)
      dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq) 
      dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(nSamples/hzAcq.value)) # -1 infinite record lengt 

      dwf.FDwfAnalogInTriggerAutoTimeoutSet(hdwf, c_double(0)) #disable auto trigger
      dwf.FDwfAnalogInTriggerSourceSet(hdwf, 11)
    # dwf.FDwfAnalogInTriggerSourceSet(hdwf, 2) #one of the analog in channels
      dwf.FDwfAnalogInTriggerTypeSet(hdwf, trigtypeEdge)
    # dwf.FDwfAnalogInTriggerChannelSet(hdwf, 1) # 1st channel
      dwf.FDwfAnalogInTriggerLevelSet(hdwf, c_double(0.5)) # 0.5V
    # dwf.FDwfAnalogInTriggerHysteresisSet(hdwf, c_double(0.5))
      dwf.FDwfAnalogInTriggerConditionSet(hdwf, trigcondRisingPositive) 
    # dwf.FDwfAnalogInTriggerConditionSet(hdwf, trigcondFallingNegative) 
    # dwf.FDwfAnalogInTriggerAutoTimeoutSet(hdwf, c_double(0)) #disable auto trigger
      dwf.FDwfAnalogInTriggerHoldOffSet(hdwf, c_double(0))   
    # dwf.FDwfAnalogInTriggerChannelInfo(hdwf, c_int(1), c_int(3))
      print("Starting oscilloscope") 
      dwf.FDwfAnalogInConfigure(hdwf, c_bool(False), c_bool(True)) 
      dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(True))
      dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))

      time.sleep(3) 

      iSample = 0
     
      while True:
          dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))
          dwf.FDwfAnalogInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted))
          iSample += cLost.value
          iSample %= nSamples
          if cLost.value :
              fLost = 1
          if cCorrupted.value :
              fCorrupted = 1
          iBuffer = 0
    #wait at least 2 sec
          while cAvailable.value>0:
              cSamples = cAvailable.value
            # we are using circular sample buffer, make sure to not overflow
              if iSample+cAvailable.value > nSamples:
                  cSamples = nSamples-iSample
              dwf.FDwfAnalogInStatusData16(hdwf, c_int(0), byref(rgSamples1, sizeof(c_int16)*iSample), c_int(iBuffer), c_int(cSamples)) # get channel 1 dat
             # dwf.FDwfAnalogInStatusData16(hdwf, c_int(1), byref(rgSamples2, sizeof(c_int16)*iSample), c_int(iBuffer), c_int(cSamples)) # get channel 2 dat
              iBuffer += cSamples
              cAvailable.value -= cSamples
              iSample += cSamples
              iSample %= nSamples

          if sts.value == DwfStateDone.value : # done
              break

      dwf.FDwfAnalogOutReset(hdwf, c_int(0))
      dwf.FDwfDigitalOutReset(hdwf) 

      dwf.FDwfDeviceCloseAll()

    # align recorded data
      if iSample != 0:
          rgSamples1 = rgSamples1[iSample:]+rgSamples1[:iSample]

      print("Recording done "+str(iSample))
      if fLost:
          print("Samples were lost! Reduce frequency")
      if fCorrupted:
          print("Samples could be corrupted! Reduce frequency")

      List = [(16.8+j/50.000)*hzAcq.value/500.000]
      for v in rgSamples1:
          List.append(v)
      with open("liqtest2075m.csv", "a") as f_object:
          writer_object = writer(f_object)
          writer_object.writerow(List)
      f_object.close()

    AnalogIn_Record_Trigger_int16_si5m.py

  2. 20 hours ago, attila said:

    Hi @Zeyu

    The RPI GPIOs are 0/3.3V signals so these won't reach the specified 3.5V trigger level, reduce this to 1.5V.
    You could also generate the square signal with the other Wavegen channel or with any DIO 0/3.3V clock/pulse signal.

    Hi Attila,

    I have tried with 1.5 V trigger but the system still can not recognize the trigger signal. I am wondering if it is possible to set up a meeting with you to see my setup? 
    If we do the trigger with AD2 DIO, my project requires 2 square signals which have 20 us high time, 30 us low time and 15 us offset between each other. I am wondering if it is possible to generate the two signals like that.

    Best regards,

    zeyu

  3. On 11/15/2021 at 2:35 AM, attila said:

    Hi @Zeyu

    I've set to trigger and capture the same channel 2 to verify the trigger correctness.
    You can get the data from the channel 1 as well:
    dwf.FDwfAnalogInStatusData16(hdwf, c_int(0), byref(rgSamples1, sizeof(c_int16)*iSample), c_int(iBuffer), c_int(cSamples)) # get channel 1 data
    dwf.FDwfAnalogInStatusData16(hdwf, c_int(1), byref(rgSamples2, sizeof(c_int16)*iSample), c_int(iBuffer), c_int(cSamples)) # get channel 2 data

     

    Thanks for your reply, this method works well. In my project, I am using a square wave generated by raspberry pi as trigger signal and it is connected to the AnalogIn channel 2. The trigger signal still can not work with the updated codes. I am not sure what is wrong. A possible solution would be generate the square wave with the Analog Discovery wavegenerator. I am wondering whether we can generate 10 kHz suare wave with the analog discover2 and use this signal as trigger?

    Best regards,

    Zeyu

  4. 16 hours ago, attila said:

    Hi @Zeyu

    The default settings for channel 2 should be fine, 50Vpk2pk 0Voff. 

    Here I have connected W1 to C2 and it triggers with your slightly modified script: t2.py

    image.png.a4f2befed4fc677cd8374997628a61cd.png

    image.png.2671628266353b3de7b3be7ee03ff86b.png

    Thanks for your reply, just wondering how did u set the trigger signal for this example? In the modified script, the trigger signal is set to be C2 which is also the sampled signal. May I know how to use the signal connected to another analog in channel to trigger the scope for the other channel?

  5. Hi Attila,

     

    Thanks for your reply. I have set the dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts)) to 1, however the system still cannot readout the trigger signal in AnalogIn (Channel 2). Do u have any suggestion? I just wondering whether any further setting for Analogin (Channel 2) is required (currently all the analogin settings are for channel 1).

     

    Best regards,

    Zeyu

  6. Hi,

    I am using Raspberry Pi 4 and Analog discovery 2 to building a sampling system. The trigger signal is generated by the raspberry pi and routed to the second analog input channel. The trigger signal has a voltage of 4 V and the trigger mode is set as Positive edge. From the lab oscilloscope, the trigger signal is observed while it is not read out by the analogIn channel of the Analog discovery 2 oscilloscope (it shows that the system stopped at Reading analog in status) . I am wondering whether there is anything wrong with my codes (shown as attached codes, especially in the trigger setting part), hope to have your advice.

    Best regards,

    Zeyu

     

    from time import sleep
    import RPi.GPIO as GPIO
    import signal
    from ctypes import *
    from dwfconstants import *
    import math
    import time
    import matplotlib.pyplot as plt
    import sys
    import numpy
    from csv import writer
    import pandas as pd

    if sys.platform.startswith("win"):
        dwf = cdll.dwf
    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()
    hzAcq = c_double(1e8)
    nSamples = int(1e4)
    cAvailable = c_int()
    cLost = c_int()
    cCorrupted = c_int()
    fLost = 0
    fCorrupted = 0

    #print(DWF version
    version = create_string_buffer(16)
    dwf.FDwfGetVersion(version)
    print("DWF Version: "+str(version.value))

    #open device
    for j in range(1, 3, 1):

      print("Opening first device")
      dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
      rgSamples1 = (c_int16*nSamples)()
      if hdwf.value == hdwfNone.value:
          szerr = create_string_buffer(512)
          dwf.FDwfGetLastErrorMsg(szerr)
          print(str(szerr.value))
          print("failed to open device")
          quit()
     print("Generating sine wave...")
      dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_bool(True))
      dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), AnalogOutNodeCarrier, funcSine)
      dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double((90.0+j)*hzAcq.value/nSamples))
      dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(5))
      dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(True))
     
    #set up acquisition
      dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_bool(True))
      dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
      dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord)
      dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq)
      dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(nSamples/hzAcq.value)) # -1 infinite record length

    def lopper():
            for i in range(1,300):
              GPIO.output(7,0)
              sleep(0.001)
              GPIO.output(8,1)
              sleep(0.001)
              GPIO.output(8,0)
              GPIO.output(7,1)
              sleep(0.001)
              GPIO.output(7,0)
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(7,GPIO.OUT)
      GPIO.setup(8,GPIO.OUT)
      lopper()

      dwf.FDwfAnalogInTriggerAutoTimeoutSet(hdwf, c_double(0)) #disable auto trigger
      dwf.FDwfAnalogInTriggerSourceSet(hdwf, trigsrcDetectorAnalogIn) #one of the analog in channels
      dwf.FDwfAnalogInTriggerTypeSet(hdwf, trigtypeEdge)
      dwf.FDwfAnalogInTriggerChannelSet(hdwf, 1) # first channel
      dwf.FDwfAnalogInTriggerLevelSet(hdwf, c_double(3.5)) # 0.5V
      dwf.FDwfAnalogInTriggerConditionSet(hdwf, trigcondRisingPositive) 
    #  dwf.FDwfAnalogInTriggerHoldOffSet(hdwf, c_double(0.01))

      GPIO.cleanup() 

     print("Starting dwf.FDwfAnalogInConfigure(hdwf, c_bool(False), c_bool(True)) 
      dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(True))
      dwf.FDwfAnalogOutReset(hdwf, c_int(0))

      iSample = 0
     
      while True:
          dwf.FDwfAnalogInStatus(hdwf, c_int(0), byref(sts))
          dwf.FDwfAnalogInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted))
          iSample += cLost.value
          iSample %= nSamples
          if cLost.value :
              fLost = 1
          if cCorrupted.value :
              fCorrupted = 1
          iBuffer = 0
    #wait at least 2 sec
          while cAvailable.value>0:
              cSamples = cAvailable.value
            # we are using circular sample buffer, make sure to not overflow
              if iSample+cAvailable.value > nSamples:
                  cSamples = nSamples-iSample
              dwf.FDwfAnalogInStatusData16(hdwf, c_int(0), byref(rgSamples1, sizeof(c_int16)*iSample), c_int(iBuffer), c_int(cSamples)) # get channel 1 dat
              iBuffer += cSamples
              cAvailable.value -= cSamples
              iSample += cSamples
              iSample %= nSamples

          if sts.value == DwfStateDone.value : # done
              break

      dwf.FDwfDeviceCloseAll()
      if iSample != 0 :
          rgSamples1 = rgSamples1[iSample:]+rgSamples1[:iSample]

      print("Recording done "+str(iSample))
      if fLost:
          print("Samples were lost! Reduce frequency")
      if fCorrupted:
          print("Samples could be corrupted! Reduce frequency")

      List = []
      for v in rgSamples1:
          List.append(v)
      with open("record5.csv", "a") as f_object:
          writer_object = writer(f_object)
          writer_object.writerow(List)
      f_object.close()

      plt.plot(numpy.fromiter(rgSamples1, dtype = numpy.int16))
      plt.show()

    file=open("record6.csv","w")
    df=pd.read_csv("record5.csv",header=None, low_memory=False)
    data=df.values
    data=list(map(list,zip(*data)))
    data=pd.DataFrame(data)
    data.to_csv(file,header=0,index=0)
    text = open("record6.csv", "r")
    text = ' '.join([i for i in text])
    text = text.replace(",", " ")
    file = open('read.txt', 'w')
    file.write(text)
    file.close()

×
×
  • Create New...