Jump to content
  • 0

External trigger signal not recognized


Zeyu

Question

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()

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Hi @Zeyu

1. Use argument 1 to fetch data dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))

2. On AD2 the Record mode, streaming over USB working at up to about 1-2MHz
You could use 2nd configuration to have 16ki sample device buffer which can be used at up to 100MHz. The default, first configuration has 8kiS, 8192S

3. The generated signal is set to 5V amplitude, 10Vpk2pk. The Scope channel 1 range is set to 5Vpk2pk.

Link to comment
Share on other sites

  • 0

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

Link to comment
Share on other sites

  • 0
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?

Link to comment
Share on other sites

  • 0

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

 

Link to comment
Share on other sites

  • 0
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

Link to comment
Share on other sites

  • 0
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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...