NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

Read Wheatstone Bridge NI 9218 and Python NI DAQmx

Please correct me if this is the wrong place to post. 

 

We have a NI compactDAQ (cDAQ 9174) with an NI 9218 module for measuring differential signal inputs. 

Our team uses Python and the NI DAQmx library to get data from the DAQ as it has to interface with some niche software. 

 

We need to be able to measure across one resistor of a wheatstone bridge but have not had luck turning on the excitation voltage. The bridge we are using works well at 5V, has a max excitation voltage of 10V. 


Can someone help me identify how to configure the module so we can properly read data from it? The current code is posted below:


import matplotlib
import matplotlib.pyplot as plt
import numpy as np

import nidaqmx
import nidaqmx.stream_readers
from nidaqmx.stream_readers import AnalogMultiChannelReader
from nidaqmx import constants

import threading
import pickle
import scipy.io
import time


# Parameters
sampling_freq_in = 1000 # in Hz
time_buffer = 5 #s

buffer_in_size = 100
bufsize_callback = buffer_in_size
buffer_in_size_cfg = round(buffer_in_size * 1) # clock configuration
refresh_rate_plot = 60 # in Hz

n_data_points = int(sampling_freq_in * time_buffer)

# Initialize data placeholders
buffer_in = np.zeros((1, buffer_in_size))
data = np.zeros((1, 1)) # will contain a first column with zeros but that's fine

start_time = time.time()*1000
global ct
ct = 0

def millis():
now = time.time()*1000
return now-start_time


def cfg_read_task(acquisition): # uses above parameters
acquisition.ai_channels.add_ai_voltage_chan("cDAQ1Mod4/ai0")
acquisition.timing.cfg_samp_clk_timing(rate=sampling_freq_in, sample_mode=constants.AcquisitionType.CONTINUOUS,samps_per_chan=buffer_in_size_cfg)


def reading_task_callback(task_idx, event_type, num_samples, callback_data): # bufsize_callback is passed to num_samples
global data
global buffer_in, ct

if running:
# It may be wiser to read slightly more than num_samples here, to make sure one does not miss any sample,
# see: https://documentation.help/NI-DAQmx-Key-Concepts/contCAcqGen.html
buffer_in = np.zeros((1, num_samples)) # double definition ???
stream_in.read_many_sample(buffer_in, num_samples, timeout=constants.WAIT_INFINITELY)

data = np.append(data, buffer_in, axis=1) # appends buffered data to total variable callback_data

for dp in buffer_in[0,:]:
print(dp)
ct += 1

return 0 # Absolutely needed for this callback to be well defined (see nidaqmx doc).


# Configure and setup the tasks
task_in = nidaqmx.Task()
cfg_read_task(task_in)
stream_in = AnalogMultiChannelReader(task_in.in_stream)
task_in.register_every_n_samples_acquired_into_buffer_event(bufsize_callback, reading_task_callback)


# Main loop
running = True
task_in.start()


# Plot a visual feedback for the user's mental health
fig, ax = plt.subplots()

while True: # make this adapt to number of channels automatically
ax.clear()

if data.size > n_data_points:
data = data[-n_data_points:]
v = data[0, -n_data_points:]
t = np.arange(start=0,stop=v.size) / sampling_freq_in
ax.plot(t,v)

ax.set_ylabel('Voltage [V]')
ax.set_xlabel('Time [s]')

plt.pause(1/refresh_rate_plot) # required for dynamic plot to work (if too low, nulling performance bad)

 

 

# Close task to clear connection once done
task_in.close()

0 Kudos
Message 1 of 1
(139 Views)