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
#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))
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()
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()
Question
Thomas Lee
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
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 accountSign in
Already have an account? Sign in here.
Sign In Now