import nidaqmx
import csv
import time
import numpy as np
# Define your accelerometer channel names here
accelerometer_channels = ["cDAQ1Mod1/ai0", "cDAQ1Mod1/ai1", "cDAQ1Mod1/ai2"]
# Create a task for analog input for each accelerometer channel
tasks = []
for channel in accelerometer_channels:
task = nidaqmx.Task()
task.ai_channels.add_ai_voltage_chan(channel)
# Set the voltage range for each channel (adjust as needed)
task.ai_channels[-1].ai_max = 5.0 # Set the maximum voltage to +5V
task.ai_channels[-1].ai_min = -5.0 # Set the minimum voltage to -5V
tasks.append(task)
for i in range(30):
# Start all tasks
for task in tasks:
task.start()
# Read data from all channels
data = [task.read(number_of_samples_per_channel=10000) for task in tasks]
# Stop all tasks
for task in tasks:
task.stop()
# Create a time axis
time_axis = np.arange(len(data[0])) / 10000 # Assuming the same sampling rate for all channels
# Save data to a CSV file
csv_file = f"vibration_data{i}.csv"
with open(csv_file, "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Time (s)", "Channel 0", "Channel 1", "Channel 2"]) # Write header row
for t, ch0, ch1, ch2 in zip(time_axis, *data):
writer.writerow([t, ch0, ch1, ch2])
# Sleep to wait for the next trial (optional)
time.sleep(10.0) # Sleep for 10 seconds between trials
# Close all tasks
for task in tasks:
task.close()