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
Question
fishywisky
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 fishywiskyLink to comment
Share on other sites
1 answer 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