Stop Data Acquisition when Needed

3 Ansichten (letzte 30 Tage)
Nile3
Nile3 am 12 Nov. 2021
Bearbeitet: Walter Roberson am 10 Aug. 2024
Hello,
I am building a data aquisition program using NI devices. I developed a program using the data aquisition toolbox, however the code that I have needs a run time as input which is only when I know the data aquisition time.
However in cases when I need to stop the data aquisition before the set input time, I am unable to do so before the complete run is done.
Is there a way to stop the program and still have access to the aquired data because if I stop the program, the workspace clears out and there in no data there or is there a way I can continously add data into a buffer which the data is being read so that even if the program is stipped, the aquired data is accessible.
I have attached my code for review.
close all
clear all
clc
%% Create a data acquisition session
daqSession = daq.createSession('ni');
%% Add channels specified by subsystem type and device
daqSession.addAnalogInputChannel('Dev1','ai0','Bridge');
daqSession.addAnalogInputChannel('Dev1','ai1','Bridge');
daqSession.addAnalogInputChannel('Dev1','ai2','Bridge');
daqSession.addAnalogInputChannel('Dev1','ai3','Bridge');
%% Configure properties
daqSession.Rate = 1653;
daqSession.IsContinuous = 1;
hlistener = daqSession.addlistener('DataAvailable',@(src,event) plot(event.TimeStamps, event.Data));
daqSession.IsContinuous = 0;
daqSession.DurationInSeconds = 1800;
daqSession.Channels(1).BridgeMode = 'Full';
daqSession.Channels(1).ExcitationVoltage = 5;
daqSession.Channels(1).NominalBridgeResistance = 1;
daqSession.Channels(1).Name = 'Kulite 1';
daqSession.Channels(2).BridgeMode = 'Full';
daqSession.Channels(2).NominalBridgeResistance = 1;
daqSession.Channels(2).Name = 'Kulite2';
daqSession.Channels(3).BridgeMode = 'Full';
daqSession.Channels(3).NominalBridgeResistance = 1;
daqSession.Channels(3).Name = 'Kulite3';
daqSession.Channels(4).BridgeMode = 'Full';
daqSession.Channels(4).NominalBridgeResistance = 1;
daqSession.Channels(4).Name = 'Kulite4';
%Read supplied voltage
V=daqSession.Channels(1).ExcitationVoltage;
%% Run the data acquisition session
[data,time] = daqSession.startForeground();
figure
plot(time,data)
%Take values from DAQ Matrix
V1=data(:,1);
V2=data(:,2);
V3=data(:,3);
V4=data(:,4);
%Create Matrix for other Time and Pressure Values
Voltagedata=[time,V1,V2,V3,V4];
fileName = sprintf('RawVData_%s.csv', datestr(now, 'yyyymmdd_HHMMSS'))
writematrix(Voltagedata,fileName)
%% Disconnect from the device
daqSession.release();
delete(daqSession);
clear daqSession;
  1 Kommentar
Jaimin
Jaimin am 8 Aug. 2024
Hi @Nile3,
As I understand you are building a data acquisition program with NI devices using the Data Acquisition Toolbox. You need a way to stop the program before the set runtime and still access the acquired data, either by stopping the program without losing data or by continuously adding data to a buffer.
I suggest adding logic to store your variable in the "DataAvailable" event listener. You can use the "save" function to store any variable. Specifically, you need to modify the following line.
hlistener = daqSession.addlistener('DataAvailable',@(src,event) plot(event.TimeStamps, event.Data));
For more information about listeners, please refer to this documentation.
For more information about "save" function, please refer to this documentation.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Umar
Umar am 10 Aug. 2024
Bearbeitet: Walter Roberson am 10 Aug. 2024
Hi @Nile3 ,
I agree with @Jaimin’s second optional approach, “adding data to a buffer” and suggest implementing this solution about continuously buffering the data during acquisition. This way, even if the program is stopped prematurely, the acquired data remains accessible. Below is an updated version of your code with modifications to enable continuous data buffering:
%% Create a data acquisition session
daqSession = daq.createSession('ni');
%% Add channels specified by subsystem type and device
daqSession.addAnalogInputChannel('Dev1','ai0','Bridge'); % Add analog input channel for Bridge sensor 1
daqSession.addAnalogInputChannel('Dev1','ai1','Bridge'); % Add analog input channel for Bridge sensor 2
daqSession.addAnalogInputChannel('Dev1','ai2','Bridge'); % Add analog input channel for Bridge sensor 3
daqSession.addAnalogInputChannel('Dev1','ai3','Bridge'); % Add analog input channel for Bridge sensor 4
%% Configure properties
daqSession.Rate = 1653; % Set the sampling rate
daqSession.IsNotifyWhenDataAvailableExceedsAuto = false; % Disable automatic buffer handling
daqSession.IsContinuous = true; % Set continuous data acquisition
daqSession.DurationInSeconds = 1800; % Set the duration of data acquisition
daqSession.Channels(1).BridgeMode = 'Full'; % Set Bridge mode for sensor 1
daqSession.Channels(1).ExcitationVoltage = 5; % Set excitation voltage for sensor 1
daqSession.Channels(1).NominalBridgeResistance = 1; % Set nominal bridge resistance for sensor 1
daqSession.Channels(1).Name = 'Kulite 1'; % Assign a name to sensor 1
daqSession.Channels(2).BridgeMode = 'Full'; % Set Bridge mode for sensor 2
daqSession.Channels(2).NominalBridgeResistance = 1; % Set nominal bridge resistance for sensor 2
daqSession.Channels(2).Name = 'Kulite2'; % Assign a name to sensor 2
daqSession.Channels(3).BridgeMode = 'Full'; % Set Bridge mode for sensor 3
daqSession.Channels(3).NominalBridgeResistance = 1; % Set nominal bridge resistance for sensor 3
daqSession.Channels(3).Name = 'Kulite3'; % Assign a name to sensor 3
daqSession.Channels(4).BridgeMode = 'Full'; % Set Bridge mode for sensor 4
daqSession.Channels(4).NominalBridgeResistance = 1; % Set nominal bridge resistance for sensor 4
daqSession.Channels(4).Name = 'Kulite4'; % Assign a name to sensor 4
%% Set up a buffer to store acquired data
bufferSize = 10000; % Define buffer size based on your requirements
dataBuffer = zeros(bufferSize, 4); % Initialize buffer to store 4 channels
%% Create a listener to continuously update the buffer
hlistener = daqSession.addlistener('DataAvailable', @(src, event) updateBuffer(event));
%% Function to update the buffer with new data
function updateBuffer(event)
newData = event.Data;
dataBuffer = [dataBuffer; newData]; % Append new data to the buffer
% Trim buffer if it exceeds the predefined size
if size(dataBuffer, 1) > bufferSize
dataBuffer = dataBuffer(end - bufferSize + 1:end, :);
end
end
%% Run the data acquisition session
daqSession.startBackground(); % Start data acquisition in the background
% You can now stop the data acquisition at any point without losing the acquired data
% To stop the acquisition, use:
% daqSession.stop();
% Access the acquired data from the buffer even after stopping the acquisition
% Example: Access the last 100 samples of data from the buffer
last100Samples = dataBuffer(end-99:end, :);
% Perform further processing or save the acquired data as needed
So, after analyzing this modification, you will see a buffer of size 10000 is created to store acquired data, and a listener (hlistener) is added to continuously update the buffer with new data using the updateBuffer function defined in the code.The updateBuffer function appends new data to the buffer and trims it if it exceeds the predefined size, ensuring efficient data storage.
To start the data acquisition session in the background, daqSession.startBackground() is called. It also demonstrates how to stop the acquisition using daqSession.stop() and access the acquired data from the buffer even after stopping the acquisition. Finally, an example is provided to access the last 100 samples of data from the buffer (last100Samples = dataBuffer(end-99:end, :)), allowing for further processing or saving of the acquired data. So, now you should be able to buffer the acquired data, which will allow you to stop the data acquisition program at any point while still retaining access to the collected data. Please let me know if you have any further questions.

Kategorien

Mehr zu Simultaneous and Synchronized Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by