Jump to content
  • 0

Help with python script using mcculw for MCC DAQ USB-1808X


fishywisky

Question

Posted (edited)

Hello,

I started an internship and I must write a script in Python that scans multiple pins on my board (MCC DAQ USB 1808 - X) and saves their values into CSV files for testing. To optimize my time, I wanted to use the a_in_scan() function to scan multiple pins in one go. However, the values that I get are really bad and incorrect. I have the impression that the sampling rate is not what I intended (my signals are two PWM signals of 29.5 kHz and a chirp from 1-2500 Hz).

I manage to get accurate values when I perform solo scans.

Here is the code:

 

"""

from __future__ import absolute_import, division, print_function

 

import csv

from builtins import *  # @UnusedWildImport

from ctypes import POINTER, c_double, c_ulong, c_ushort, cast

 

from mcculw import enums, ul

from mcculw.device_info import DaqDeviceInfo


 

def run_example(

    duration_RT, sampling_rate_RT, csv_filenames, PIN_RT_IN_start, PIN_RT_IN_end

):

    board_num = 0

    points_per_scan = int(sampling_rate_RT * duration_RT)

    num_pins = PIN_RT_IN_end - PIN_RT_IN_start + 1

    total_count = points_per_scan * num_pins

 

    if len(csv_filenames) != num_pins:

        raise ValueError("Number of CSV filenames must match the number of pins.")

 

    try:

        daq_dev_info = DaqDeviceInfo(board_num)

        if not daq_dev_info.supports_analog_input:

            raise Exception("Error: The DAQ device does not support analog input")

 

        print(

            "\nActive DAQ device: ",

            daq_dev_info.product_name,

            " (",

            daq_dev_info.unique_id,

            ")\n",

            sep="",

        )

 

        ai_info = daq_dev_info.get_ai_info()

        ai_range = ai_info.supported_ranges[0]

        scan_options = enums.ScanOptions.FOREGROUND

 

        if enums.ScanOptions.SCALEDATA in ai_info.supported_scan_options:

            scan_options |= enums.ScanOptions.SCALEDATA

 

        ul.a_chan_input_mode(

            board_num, PIN_RT_IN_start, enums.AnalogInputMode.SINGLE_ENDED

        )

 

        if enums.ScanOptions.SCALEDATA in scan_options:

            memhandle = ul.scaled_win_buf_alloc(total_count)

            ctypes_array = cast(memhandle, POINTER(c_double))

        elif ai_info.resolution <= 16:

            memhandle = ul.win_buf_alloc(total_count)

            ctypes_array = cast(memhandle, POINTER(c_ushort))

        else:

            memhandle = ul.win_buf_alloc_32(total_count)

            ctypes_array = cast(memhandle, POINTER(c_ulong))

 

        if not memhandle:

            raise Exception("Error: Failed to allocate memory")

 

        ul.a_in_scan(

            board_num,

            PIN_RT_IN_start,

            PIN_RT_IN_end,

            total_count,

            sampling_rate_RT,

            ai_range,

            memhandle,

            scan_options,

        )

 

        print("Scan completed successfully on analog input ports")

 

        data_per_pin = [[] for _ in range(num_pins)]

        x = []

 

        for index in range(points_per_scan):

            t = index / sampling_rate_RT

            x.append(t)

            for pin in range(num_pins):

                if enums.ScanOptions.SCALEDATA in scan_options:

                    eng_value = ctypes_array[index * num_pins + pin]

                else:

                    eng_value = ul.to_eng_units(

                        board_num, ai_range, ctypes_array[index * num_pins + pin]

                    )

                data_per_pin[pin].append(eng_value)

 

        for pin in range(num_pins):

            csv_file_path = csv_filenames[pin]

            with open(csv_file_path, mode="w", newline="") as file:

                writer = csv.writer(file)

                for i in range(len(x)):

                    writer.writerow([x[i], data_per_pin[pin][i]])

 

        ul.win_buf_free(memhandle)

 

    except Exception as e:

        print("\n", e)

    finally:

        ul.release_daq_device(board_num)


 

if __name__ == "__main__":

    csv_filenames = ["TP901.csv", "R804.csv", "TP900.csv"]

    run_example(10, 200000, csv_filenames, 0, 2)

"""

Edited by fishywisky
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0
Posted (edited)

Hello @fishywisky.

On 5/15/2024 at 4:28 AM, fishywisky said:

scan_options = enums.ScanOptions.FOREGROUND

FOREGROUND is not a supported option for the MCC USB-1808 Series.  Also, add CONTINUOUS and BACKGROUND as a ScanOption.

        scan_options = enums.ScanOptions.CONTINUOUS
        scan_options |= enums.ScanOptions.BACKGROUND

https://files.digilent.com/manuals/Mcculw_WebHelp/ULStart.htm

image.png

 

Lastly, your code only sets analog input channel 0 in single-ended mode.  How are the analog input channels configured in InstaCal?  Is that how your signals are wired to the USB-1808X?

 

Regards,

Fausto

Edited by Fausto
additional info
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...