MATLAB Answers

NI acquistion within App Designer

10 views (last 30 days)
Hi,
I have some trouble to start a data acquistion within my app made with app designer. Basically the overall behaviour of the app is:
  • playing a noise
  • Display a Question
  • The user will repply by 'yes' or 'no' via two button in a box. This box is plug to my NI daq card.
  • When the user repplied, the app start another noise
a bit of a code: Play Button:
[y,Fs] = audioread([app.dir '\audio\track' num2str(app.index) '.wav']);
tone = audioplayer(y,Fs);
app.PlayButton.Enable = 'off';
play(tone);
app.ThenoiseisplayingLabel.Visible = 'on';
while isplaying(tone)
app.Lamp.Visible = 'on';
pause(.5)
app.Lamp.Visible = 'off';
pause(.5)
end
% Configure data acquisition session and add analog input channels
testboitier(app)
And 'testboitier' is my function used to collect the data and save them Code:
methods (Access = public)
function stopWhenExceedOneV(src, event)
if any(event.Data(1,1) > 4.0 || event.Data(1,2) > 4.0)
if event.Data(1,1)>4.0
disp('Event listener: No')
save('no.mat', event.Data(1,1))
else
disp('Event listener: Yes')
end
% Continuous acquisitions need to be stopped explicitly.
src.stop()
else
disp('Event listener: Continuing to acquire')
end
end
function testboitier(app)
% Configure data acquisition session and add analog input channels
s = daq.createSession('ni');
ch1 = addAnalogInputChannel(s, 'cDAQ1Mod1', 0, 'Voltage');
ch2 = addAnalogInputChannel(s, 'cDAQ1Mod1', 1, 'Voltage');
s.Rate = 51200;
s.IsNotifyWhenDataAvailableExceedsAuto = true;
lh = addlistener(s,'DataAvailable', @stopWhenExceedOneV);
s.IsContinuous = true;
startBackground(s);
end
end
StopWhenexceedoneV will stop the acquisition and save the data.
I tested those two function and they are working when they aren't used within an App. EDIT: saving the data is not working either finally..
Does someone have an idea about how to fix this ? Furthermore if someone can explain to me why it doesn't work I would be very grateful as I think I will have some other app to make like that
Thank you !

Accepted Answer

Jouni Paaaho
Jouni Paaaho on 3 Mar 2018
I found out in similar script that if you put breakpoints for startBackground(s) and at stop calls, this will actually work. (you just press F5 to continue from breakpoint). In similar way, if you add a short pause BEFORE both start and stop calls, the data acquistion seems to work for data logging to app property.

More Answers (1)

Andrei Ursache
Andrei Ursache on 8 Aug 2018
Edited: Andrei Ursache on 8 Aug 2018
The issue with the code in question appears to be that the session object is automatically getting cleared when it's running out of scope in function testboitier.
A different approach would be to store the session object handle in an app property. As an example of this approach, can you refer to "Data Acquisition Live" app, which can be downloaded from File Exchange.
  1 Comment
Paolo Piseri
Paolo Piseri on 25 Sep 2018
I have a similar issue like in Bastien Lechat's post.
I tried using app properties to store my daq session handles class, or I also tried a different approach by putting it in a global handle class. None of them worked in app, although the same code works perfectly in m-files and command window. Inserting short pauses doesn't help either.
The issue arises when trying to change session .DurationInSeconds property (I get following error: "The property cannot be changed while the Session is running.", although session was stopped before setting the property and indeed session property .IsRunning returns false!). Also startBackground yelds an error: "This command can not be used while the Session is running."
Application is very simple: I want to set a voltage using AO then start a gated counter to count incoming pulses for a given and well defined amount of time. Class definition is:
classdef XPS_daq < handle
% class to setup and read daq board used for XPS measurements
properties (Access = public)
ao_session
counter_session
counter_channel
dt0
vscale
end
methods
function obj=XPS_daq(varargin)
% manage default options
if nargin>0
dt=varargin{1};
if nargin>1
v=varargin{2};
if nargin>2
dt0=varargin{3};
if nargin>3
vscale=varargin{4};
else
vscale=1;
end
else
dt0=0.05;
vscale=1;
end
else
v=0;
dt0=0.05;
vscale=1;
end
else
dt=1;
v=0;
dt0=0.05;
vscale=1;
end
% set numeric properties
obj.dt0=dt0;
obj.vscale=vscale;
% construct session for AO
obj.ao_session = daq.createSession('ni');
obj.ao_session.addAnalogOutputChannel('Dev1', 'ao0', 'Voltage');
warning off
%pause(obj.dt0)
obj.ao_session.outputSingleScan(v);
%pause(obj.dt0)
obj.ao_session.stop;
obj.ao_session.release;
% construct counter session
obj.counter_session = daq.createSession('ni');
obj.counter_session.DurationInSeconds = dt + 2*dt0;
%obj.counter_session.NumberOfScans = 1;
obj.counter_session.NotifyWhenDataAvailableExceeds = 2;
% define clock pulse
obj.counter_channel=addCounterOutputChannel(obj.counter_session,'Dev1','ctr1','PulseGeneration');
%obj.counter_channel.Terminal
obj.counter_channel.InitialDelay = dt0;
obj.counter_channel.Frequency = 1/dt(1);
obj.counter_channel.DutyCycle = 0.99;
obj.counter_channel.IdleState = 'Low';
% add counter input channel
obj.counter_session.addCounterInputChannel('Dev1', 'ctr0', 'EdgeCount');
% add counter cconnection
obj.counter_session.addClockConnection('External','Dev1/PFI9','ScanClock');
% define listener function
addlistener(obj.counter_session,'DataAvailable',@printData);
warning on
end
function counts=count(obj,dt,v)
% set voltage and start counting!
global numero conteggi
numero=0;conteggi=[];
if numel(dt)==1
dt=ones(size(v))*dt;
end
for ii=1:numel(v)
obj.ao_session.outputSingleScan(obj.vscale*v(ii));
obj.ao_session.stop;
obj.ao_session.release;
%pause(obj.dt0)
obj.counter_session.DurationInSeconds = dt(ii) + 2*obj.dt0;
obj.counter_channel.Frequency = 1/(dt(ii));
warning off
%pause(obj.dt0)
startBackground(obj.counter_session);
pause(dt(ii) + 2*obj.dt0)
obj.counter_session.stop;
warning on
obj.counter_session.release;
counts=conteggi; % get counts from global conteggi changed in the listener function printData
if isempty(counts)
disp('no data')
counts=0;
end
end
end
end
end
I'm new to mlapp so there's maybe something trivial I'm missing. I would appreciate help from the Experts here!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!