Jump to content
  • 0

Analog discovery 2 with Python code


pawel1010

Question

I wanted to write a code that should generate trinagle wave with frequency 128 Hz and sawtooth wave with frequency 75 Hz. Voltage of both wave should be in range from 0 to 750 mV. During the generation I wanted to collect 16384 samples with sampling rate 128 samples per second.

I create something like this but it doesn't stop:
 

from ctypes import cdll, c_double, c_int

# Load the libdwf.so library
dwf = cdll.LoadLibrary("libdwf.so")

# Constants
BUFFER_SIZE = 16384
SAMPLING_RATE = 128
TRIANGLE_FREQ = 128
SAWTOOTH_FREQ = 75
VOLTAGE_RANGE = 0.75  # 750 mV

# Configure the Analog Discovery 2
dwf.FDwfDeviceOpen(c_int(-1), c_int(0))

# Set the waveform generator parameters
dwf.FDwfAnalogOutNodeEnableSet(c_int(0), c_int(0), c_int(1))  # Enable channel 1
dwf.FDwfAnalogOutNodeFunctionSet(c_int(0), c_int(0), c_int(0))  # Set channel 1 to triangle waveform
dwf.FDwfAnalogOutNodeFrequencySet(c_int(0), c_int(0), c_double(TRIANGLE_FREQ))  # Set triangle frequency
dwf.FDwfAnalogOutNodeAmplitudeSet(c_int(0), c_int(0), c_double(VOLTAGE_RANGE))  # Set voltage range

dwf.FDwfAnalogOutNodeEnableSet(c_int(0), c_int(1), c_int(1))  # Enable channel 2
dwf.FDwfAnalogOutNodeFunctionSet(c_int(0), c_int(1), c_int(1))  # Set channel 2 to sawtooth waveform
dwf.FDwfAnalogOutNodeFrequencySet(c_int(0), c_int(1), c_double(SAWTOOTH_FREQ))  # Set sawtooth frequency
dwf.FDwfAnalogOutNodeAmplitudeSet(c_int(0), c_int(1), c_double(VOLTAGE_RANGE))  # Set voltage range

# Configure the analog input for data collection
dwf.FDwfAnalogInChannelEnableSet(c_int(0), c_int(0), c_int(1))  # Enable channel 1 for data collection
dwf.FDwfAnalogInChannelRangeSet(c_int(0), c_int(0), c_double(VOLTAGE_RANGE))  # Set voltage range for channel 1
dwf.FDwfAnalogInBufferSizeSet(c_int(0), c_int(BUFFER_SIZE))  # Set buffer size
dwf.FDwfAnalogInFrequencySet(c_int(0), c_double(SAMPLING_RATE))  # Set sampling rate

# Start the waveform generation and data collection
dwf.FDwfAnalogOutConfigure(c_int(0), c_int(1))
dwf.FDwfAnalogInConfigure(c_int(0), c_int(1))

# Wait for data collection to complete
while True:
    status = dwf.FDwfAnalogInStatus(c_int(0), c_int(1))
    if status == 2:  # Acquisition done
        break

# Read the collected data
buffer = (c_double * BUFFER_SIZE)()
dwf.FDwfAnalogInStatusData(c_int(0), c_int(0), buffer, BUFFER_SIZE)

# Print the collected data
for i in range(BUFFER_SIZE):
    print(buffer[i])

# Stop the waveform generation and data collection
dwf.FDwfAnalogOutReset(c_int(0), c_int(1))
dwf.FDwfAnalogInReset(c_int(0), c_int(1))

# Close the Analog Discovery 2 device
dwf.FDwfDeviceCloseAll()

I don't know what causes such problem. I'll be very grateful for help.

Edited by pawel1010
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

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...