IStyle
IStyle

Reputation: 1

Problem with 3 accelerometer using python

I have struggle with 3 accelerometer(Model 352c33) using with DAQ(NI-9230BNC + cDAQ-917) with this code:

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()

Before this code i can use 1 accelerometer to detect vibration signal, but when i adjust the code to use 3. It only detect 1, i can’t use 3 together. I want the solution to collect 3 accelerometer at the same time. Thanks btw

Upvotes: 0

Views: 99

Answers (1)

ZY-Ong
ZY-Ong

Reputation: 16

Since you are going to acquire multiple channels at the same time, you can simply add all of them using the NI-DAQmx Syntax.

task.ai_channels.add_ai_voltage_chan("cDAQ1Mod1/ai0:2")

Upvotes: 0

Related Questions