Amund

Members
  • Content Count

    2
  • Joined

  • Last visited

Posts posted by Amund

  1. I am using the python API for Waveforms to interface an Analog Discovery 2 and are trying monitor a voltage. The problem is that the measurements on channel 1 are capped to about 2.7 Volt wile channel 2 can measure up to 12.5 (the voltage I am supposed to measure) just fine. When I open Waveforms and use the scope there everything seems to work just fine.

    I am using the example code from AnalogIn_Record.py whit some small cleanup/modifications.

    Does anyone have an idea what I might be doing wrong?

    """
       DWF Python Example
       Author:  Digilent, Inc.
       Revision:  2018-07-19
    
       Requires:                       
           Python 2.7, 3
    """
    
    from ctypes import *
    from dwfconstants import *
    import math
    import time
    import matplotlib.pyplot as plt
    import sys
    import numpy as np
    import signal
    
    
    dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
    
    class AnalogDiscovery2():
        def __init__(self):
            self.hdwf = c_int()
            dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
    
        def record_analog_in(self, pin, duartion):
            return
    
        def __del__(self):
            dwf.FDwfDeviceCloseAll()
    
    def rec(fs, duration_seconds=1, infinite=False):
        #declare ctype variables
        hdwf = c_int()
        sts = c_byte()
        hzAcq = c_double(fs)
        nSamples = duration_seconds * fs
        rgdSamples_1 = (c_double*nSamples)()
        rgdSamples_2 = (c_double*nSamples)()
        cAvailable = c_int()
        cLost = c_int()
        cCorrupted = c_int()
        fLost = 0
        fCorrupted = 0
    
        #open device
        print("Opening first device")
        dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
    
        if hdwf.value == hdwfNone.value:
            szerr = create_string_buffer(512)
            dwf.FDwfGetLastErrorMsg(szerr)
            print(str(szerr.value))
            print("failed to open device")
            quit()
    
        #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)
        if infinite:
            dwf.FDwfAnalogInRecordLengthSet(hdwf, -1)#) # -1 infinite record length
        else:
            dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(nSamples/hzAcq.value))
            
        #wait at least 2 seconds for the offset to stabilize
        time.sleep(2)
    
        print("Starting oscilloscope")
        dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1))
    
        cSamples = 0
        time_start = time.time()
    
        while (cSamples < nSamples):
            dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))
            if cSamples == 0 and (sts == DwfStateConfig or sts == DwfStatePrefill or sts == DwfStateArmed) :
                # Acquisition not yet started.
                continue
    
            dwf.FDwfAnalogInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted))
            
            cSamples += cLost.value
    
            if cLost.value :
                fLost = 1
            if cCorrupted.value :
                fCorrupted = 1
    
            if cAvailable.value==0 :
                continue
    
            if cSamples+cAvailable.value > nSamples :
                cAvailable = c_int(nSamples-cSamples)
            
            dwf.FDwfAnalogInStatusData(hdwf, c_int(0), byref(rgdSamples_1, sizeof(c_double)*cSamples), cAvailable) # get channel 1 data
            dwf.FDwfAnalogInStatusData(hdwf, c_int(1), byref(rgdSamples_2, sizeof(c_double)*cSamples), cAvailable) # get channel 2 data
            cSamples += cAvailable.value
    
        time_used = time.time() - time_start
        dwf.FDwfAnalogOutReset(hdwf, c_int(0))
        dwf.FDwfDeviceCloseAll()
    
        print("Recording done")
        if fLost:
            print("Samples were lost! Reduce frequency")
        if fCorrupted:
            print("Samples could be corrupted! Reduce frequency")
    
        data_1 = np.fromiter(rgdSamples_1, dtype = np.float)
        data_2 = np.fromiter(rgdSamples_2, dtype = np.float)
        t = np.linspace(0, time_used, len(data_1))
        np.save("data_1", data_1)
        np.save("data_2", data_2)
        np.save("time", t)
        
    if __name__ == "__main__":
        rec(100, 2)