Counter variable won't increment/decrement within a continuous DAQ function?
Mostra commenti meno recenti
I'm attempting to create a script which creates a different event for even- and odd-numbered trigger instances (essentially just outputting either 1 or 0), but have run into a brick wall where the counter variable never changes. This script has been written for a NI DAQ device, and is using the Data Acquisition Toolbox Analog Input Recorder setup with a continuous session in the background. What can I do to fix this issue, and have the counter variable actually store its new variables throughout the DAQ session?
Here is some sample code:
% Create Data Acquisition Session
% Create a session for the specified vendor.
s = daq.createSession('ni');
% Set Session Properties
% Set properties that are not using default values.
s.IsContinuous = true;
% Add Channels to Session
% Add channels and set channel properties, if any.
addAnalogInputChannel(s,'Dev1','ai0','Voltage');
% Initialize Session UserData Property
% Initialize the custom fields for managing the acquired data across callbacks.
var1 = 240; % Constant scaling value
i = 0; % Simple counter variable
s.UserData.Data = [];
s.UserData.Data(1) = 0;
s.UserData.TimeStamps = [];
s.UserData.StartTime = [];
% Add Listeners
% Add listeners to session for available data and error events.
lh1 = addlistener(s, 'DataAvailable', @(src, eventData) recordData(src, eventData, var1, i));
% Acquire Data
% Start the session in the background.
startBackground(s)
% Callback Function
% Define the callback function for the 'DataAvailable' event.
function recordData(src, eventData, var1, i)
% RECORDDATA(SRC, EVENTDATA) records the acquired data, timestamps and
% trigger time. You can also use this function for plotting the
% acquired data live.
% SRC - Source object i.e. Session object
% EVENTDATA - Event data object i.e. 'DataAvailable' event data object
% Record the data and timestamps to the UserData property of the session.
src.UserData.Data = [src.UserData.Data; eventData.Data];
src.UserData.TimeStamps = [src.UserData.TimeStamps; eventData.TimeStamps];
constant = mean(src.UserData.Data(1:100)); %Washes out drift from initial baseline sensor measurements
data1 = (((eventData.Data-constant)*112.405)*2); % Convert data into relevant units
data = smoothdata(data1,'movmedian'); % Display data with live smoothing
% Record the starttime from the first execution of this callback function.
if isempty(src.UserData.StartTime)
src.UserData.StartTime = eventData.TriggerTime;
end
% Process Data
if data(end) > (PartLB*.025) && data(end) < (PartLB*0.15) && data(end-50) < data(end) % Setting simple sensor triggering threshold
if i == 0
disp('even')
i = i + 1;
end
if i == 1
disp('odd')
i = 0;
end
end
% Uncomment the following lines to enable live plotting.
plot(eventData.TimeStamps, data)
xlabel('Time (s)')
ylabel('Amplitude (V)')
% xlim([0 500])
ylim([-30 var1])
legend('ai0')
end
Risposte (1)
Voss
il 24 Dic 2021
Maybe the problem is this:
if i == 0
disp('even')
i = i + 1;
% i is now 1
end
% at this point:
% if i was 0 before the previous if, it is now 1
% if i was 1 before the previous if, it is now 1
%
% therefore, the 2nd if condition below is true whether i was 0 or 1
% before the first if above
if i == 1
disp('odd')
i = 0;
% i is now 0
end
% at this point i is 0 regardless of whether it was 0 or 1 before the first if
You can try this instead:
if i == 0
disp('even')
i = i + 1;
elseif i == 1
disp('odd')
i = 0;
end
Categorie
Scopri di più su Analog Data Acquisition in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!