Skip to content

Instantly share code, notes, and snippets.

@rysk-t
Created September 4, 2019 03:33
Show Gist options
  • Save rysk-t/ad970e3ccf2709548ae18c9c1ec90b8c to your computer and use it in GitHub Desktop.
Save rysk-t/ad970e3ccf2709548ae18c9c1ec90b8c to your computer and use it in GitHub Desktop.
% Auto-generated by Data Acquisition Toolbox Analog Input Recorder on 2018/11/05 16:12:30
close all
%% 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.Rate = 5000;
s.IsContinuous = true;
%% Add Channels to Session
% Add channels and set channel properties, if any.
dev = 'Dev3';
channel1 = addAnalogInputChannel(s, dev, 'ai0', 'Voltage');
channel1.TerminalConfig = 'SingleEnded';
channel2 = addAnalogInputChannel(s, dev, 'ai1', 'Voltage');
channel2.TerminalConfig = 'Differential';
channel3 = addAnalogInputChannel(s, dev, 'ai2', 'Voltage');
channel3.TerminalConfig = 'Differential';
channel4 = addAnalogInputChannel(s, dev, 'ai4', 'Voltage');
channel4.TerminalConfig = 'SingleEnded';
%% Initialize Session UserData Property
% Initialize the custom fields for managing the acquired data across callbacks.
s.UserData.Data = [];
s.UserData.TimeStamps = [];
s.UserData.StartTime = [];
%% Add Listeners
% Add listeners to session for available data and error events.
lh1 = addlistener(s, 'DataAvailable', @recordData);
lh2 = addlistener(s, 'ErrorOccurred', @(~,eventData) disp(getReport(eventData.Error)));
%% Acquire Data
% Start the session in the background.
startBackground(s)
disp('DAQ-START')
pause(20) % Increase or decrease the pause duration to fit your needs.
stop(s)
%% Log Data
% Convert the acquired data and timestamps to a timetable in a workspace variable.
ai0 = s.UserData.Data(:,1);
ai1 = s.UserData.Data(:,2);
ai2 = s.UserData.Data(:,3);
ai3 = s.UserData.Data(:,4);
DAQ_0_2 = timetable(seconds(s.UserData.TimeStamps),ai0,ai1,ai2,ai3);
%% Plot Data
% Plot the acquired data on labeled axes.
figure;
plot(DAQ_0_2.Time, DAQ_0_2.Variables)
xlabel('Time')
ylabel('Amplitude (V)')
%% Clean Up
% Remove event listeners and clear the session and channels, if any.
delete(lh1)
delete(lh2)
clear s channel1 channel2 lh1 lh2
%%
close gcf
figure;
timev = (seconds(DAQ_0_2.Time));
sig1 = (DAQ_0_2.ai0);
sig2 = DAQ_0_2.ai1;
sig3 = DAQ_0_2.ai2;
sig4 = DAQ_0_2.ai3;
plot(timev(:), sig1); hold on;
plot(timev(:), (sig2)+5);
plot(timev(:), sig3+10);
plot(timev(:), sig4+15);
legend
ylim([-5, 20])
%% Callback Function
% Define the callback function for the 'DataAvailable' event.
function recordData(src, eventData)
% 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];
% Record the starttime from the first execution of this callback function.
if isempty(src.UserData.StartTime)
src.UserData.StartTime = eventData.TriggerTime;
end
% Uncomment the following lines to enable live plotting.
plot(eventData.TimeStamps, eventData.Data)
xlabel('Time (s)')
ylabel('Amplitude (V)')
legend
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment