Live Plotting from 2 sources simultaneously (GPIB and NI DAQ)

5 Ansichten (letzte 30 Tage)
Mark Nugent
Mark Nugent am 27 Jan. 2020
Hi,
I am having some trouble with live plotting in Matlab. Im trying to get a two GPIB devices and two NI DAQ devices to plot simulataneously (or as close as is possible). The GPIB devices are updated and plotted in a for loop and the NI DAQ is suppose to be running in the background, using startBackground. A tutorial video on Youtube created by MathWorks suggested that when using:
startBackground (s)
pause(5)
stop(s)
that "pause is a placeholder for functions you would like to run in the foreground".
If I replace the pause, however, no data is collected in the background. If I include a short pause ('pause(0.5)') within the for loop, the data collection seems random and sporadic, a small bit of backgrounf data is collected, then several iterations of the for loop can run before more is collected. I've tried using a startForeground method in the for loop but this runs slowly and wipes previous data with each iteration. I've also trie adjusting the plotting to occur within the callback function or the foor loop with varied results. My biggest confusion is the lack of consistency of the length of time the background data runs for. (Some of the code I've used is adjusted from generated code from Analog Input Recorder).
Essentially I am trying to get the callback function (with live plotting) to run simulatneously with the FOR loop, so that a real time graph of all variables is dispalyed.
Any help with this would be appreciated!
Thanks in advance.
%% First clear any connections with instruments
newobjs = instrfind;
if isempty(newobjs) == 0
fclose(newobjs);
delete(newobjs);
clear newobjs
end
%% Connect to the power supply and multimeter
%enter user's instrument connection string
DutAddr1 = 'GPIB0::11'; %String for GPIB - Power Supply
DutAddr2 = 'GPIB0::22'; %String for GPIB - Multimeter
%% Create Data Acquisition Session - connect to the DAQd
%Create a session for the specified vendor.
s = daq.createSession('ni');
s.Rate = 50;
%'test2'
%% connects with Power Supply and configures
myPS = visa('agilent',DutAddr1);
set(myPS,'EOSMode','read&write');
set(myPS,'EOSCharCode','LF') ;
fopen(myPS);
%'test3'
%% connects with Multimeter and configures
myDmm = visa('agilent',DutAddr2);
set(myDmm,'EOSMode','read&write');
set(myDmm,'EOSCharCode','LF') ;
fopen(myDmm);
%'test4'
%% Verify connections to PS and DMM
fprintf(myPS, '*IDN?');
idn = fscanf(myPS) %Remove comma for output of device ID
fprintf(myDmm, '*IDN?');
idn = fscanf(myDmm) %Remove comma for output of device ID
%'test5'
%% 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.
s.addAnalogInputChannel('cDAQ1Mod1','ai0','Voltage'); %Pressure channel
s.addAnalogInputChannel('cDAQ1Mod2','ai0','Voltage'); %Thermocouple 1
s.addAnalogInputChannel('cDAQ1Mod2','ai1','Voltage'); %Thermocouple 2
s.addAnalogInputChannel('cDAQ1Mod2','ai2','Voltage'); %Thermocouple 3
%'test7'
%% Initialize Session UserData Property
% Initialize the custom fields for managing the acquired data across callbacks.
s.UserData.Data = [];
s.UserData.TimeStamps = [];
s.UserData.StartTime = [];
%'test8'
%% 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)));
%'test9'
%% Acquire Data from DAQ
% Start the session in the background.
%timevalue = zeros(:,1);
res2Result = zeros;
volt2Result = zeros;
timevalue(1,1) = 0;
startBackground(s)
tic
pause(0.1);
%% Collects data from PS and DMM
for i = 1:20
%% Multimeter
timevalue(i+1,1) = toc;
fprintf(myDmm,'MEAS:RES? AUTO, MAX');
resRead = fscanf(myDmm);
res2Result(i,1) = toc;
res2Result(i,2) = str2double(regexp(resRead, '[\dEe+-\.]+','match'));
%timevalue(i+1,1) = toc;
%% Plot graphs
figure(1)
subplot(2,2,3)
plot(res2Result(:,1), res2Result(:,2));
axis auto;
% hold on
xlabel('Time (s)');
ylabel('Resistance (Ohm)');
%% Power Supply
fprintf(myPS,['V1 ',num2str(i)]);
fprintf(myPS,('V1?'));
voltRead = fscanf(myPS);
volt2Result(i,1) = toc;
voltRead(strfind(voltRead, ' ')) = [];
Key = 'V1';
Index = strfind(voltRead, Key);
volt2Result(i,2) = sscanf(voltRead(Index(1) + length(Key):end), '%g', 1)
%timevalue(i+1,1) = toc;
subplot(2,2,4)
plot(volt2Result(:,1), volt2Result(:,2));
axis auto;
% hold on
xlabel('Time (s)');
ylabel('Current Power Supply Voltage (V)');
%% Pause
pause(0.1)
end
stop(s)
'finished loop'
%% Log Data from DAQ
% Convert the acquired data and timestamps to a timetable in a workspace variable.
pai0 = s.UserData.Data(:,1);
tcai0 = s.UserData.Data(:,2);
tcai1 = s.UserData.Data(:,3);
tcai2 = s.UserData.Data(:,4);
DAQ_1 = timetable(seconds(s.UserData.TimeStamps),pai0);
DAQ_2 = timetable(seconds(s.UserData.TimeStamps),tcai0, tcai1, tcai2);
%% Clean Up
% Remove event listeners and clear the session and channels, if any.
delete(lh1)
delete(lh2)
clear s lh1 lh2
%'test21'
%% Close connections
fprintf(myPS, 'IFUNlOCK');
delete_all_objs()
%'test22'
%% 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.
figure(1)
subplot(2,2,1)
plot(eventData.TimeStamps, eventData.Data);
xlabel('Time (s)')
ylabel('Amplitude (V)')
legend('ai0','ai1','ai2')
subplot(2,2,2)
plot(eventData.TimeStamps, eventData.Data(:,1));
xlabel('Time (s)')
ylabel('Pressure Voltage (V)')
% legend('ai0','ai1','ai2')
end

Antworten (0)

Kategorien

Mehr zu Digital Input and Output finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by