02-07-2024 09:04 AM
Hello Everyone,
I am trying to acquire data from the code below
import nidaqmx
import csv
import time
from datetime import datetime
def acquire_data_and_write_to_csv(task, file_path, duration_seconds):
sample_rate = 1000.0 # Replace with your desired sample rate
channels = ["Dev1/ai0", "Dev1/ai1"] # Replace with your actual channels
samples_per_channel = int(sample_rate * duration_seconds)
# Configure channels
for i, channel in enumerate(channels):
task.ai_channels.add_ai_voltage_chan(channel, name_to_assign_to_channel=f"Channel_{i}")
# Configure timing
task.timing.cfg_samp_clk_timing(sample_rate, samps_per_chan=samples_per_channel)
# Start the task
task.start()
try:
with open(file_path, 'w', newline='') as csvfile:
csv_writer = csv.writer(csvfile)
# Write header
header = ["Timestamp"] + [f"Channel_{i}" for i in range(len(channels))]
csv_writer.writerow(header)
# Continuous acquisition and writingz
start_time = time.time()
while time.time() - start_time < duration_seconds:
data = task.read()
# Process data (e.g., calculate RMS for simplicity)
processed_data = [datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")]+data
#+ [sum(channel_data) / len(channel_data) for channel_data in data]
# Write to CSV
csv_writer.writerow(processed_data)
# Pause for a short time before the next iteration
time.sleep(0.1)
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Stop and clear the task
task.stop()
task.close()
# Usage example
task = nidaqmx.Task()
# Specify the file path for the CSV file
csv_file_path = 'acoustic_emission_data.csv'
# Specify the duration for data acquisition in seconds (adjust as needed)
acquisition_duration_seconds = 60
acquire_data_and_write_to_csv(task, csv_file_path, acquisition_duration_seconds)
This code is in Jupyter notebook. The sample rate here is 1000 where as I am only getting a a limited amount of data that is 9 reading per second
I am not sure as this is my first time with NI the Product I am using this code is USB 6001.
Can someone help me with this and explain if there is any changes need to be made on the software end or coding end it would be really helpful
the data looks like this can some one also explain me why is it Dev1/ai0 gives me a positive value and Dev1/ai1 negative
Thank you
02-07-2024 07:25 PM
I would recommend using the built-in TDMS logging feature for the hardware timestamp. See my examples in python-ni-examples/nidaqmx_examples at main · ZhiYang-Ong/python-ni-examples