image thumbnail

DaqControl - Data Acquisition Session with MCC DAQ card

by

 

User-friendly interface helps you perform basic operations with a data acquisition card.

DaqControl(varargin)
function varargout = DaqControl(varargin)
% Author: Shmuel Ben-Ezra
% Date: 04-May-2009
% This GUI provides few basic operations with the Measurement Computing (MCC)
% Data acquisition (DAQ) card. 
% It was tested with the PCI-DAS4020/12 analog input (AI) device.
% Usage:    1. Create the AI object, 
%           2. Setup acquisition and trigger, 
%           3. Setup channels,
%           4. Start
%           5. Save
% 
% DAQCONTROL M-file for DaqControl.fig
%      DAQCONTROL, by itself, creates a new DAQCONTROL or raises the existing
%      singleton*.
%
%      H = DAQCONTROL returns the handle to a new DAQCONTROL or the handle to
%      the existing singleton*.
%
%      DAQCONTROL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DAQCONTROL.M with the given input arguments.
%
%      DAQCONTROL('Property','Value',...) creates a new DAQCONTROL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before DaqControl_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to DaqControl_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help DaqControl

% Last Modified by GUIDE v2.5 04-May-2009 17:56:30

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DaqControl_OpeningFcn, ...
                   'gui_OutputFcn',  @DaqControl_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before DaqControl is made visible.
function DaqControl_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to DaqControl (see VARARGIN)

% Choose default command line output for DaqControl
handles.output = hObject;
handles.ai = [];
handles.info = [];
handles.data = [];
handles.time = [];
daqreset;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes DaqControl wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = DaqControl_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pbStart.
function pbStart_Callback(hObject, eventdata, handles)
set(handles.txMassages, 'String', '')
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isempty(handles.ai.Channel),
    set(handles.txMassages, 'String', 'Please create at least 1 channel before calling START !')
    return
end
% if isrunning(handles.ai),
%    set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
%    set(hObject, 'Value', 0)
%    return
% end 
handles.data = [];
handles.time = [];
duration = handles.ai.SamplesperTrigger/handles.ai.SampleRate;
%wait(handles.ai, 1.1*duration);
timeout=200;
timer=0;
start(handles.ai)
while isrunning(handles.ai),
    pause(0.1)
    timer=timer+1;
    if timer>timeout,
       set(handles.txMassages, 'String', 'Run/ Log Timeout Reached !')
       stop(handles.ai);
       break
    end
    s1 = 'AI object is running...';
    if islogging(handles.ai),
        s2 = 'AI object is logging...';
    else
        s2 = 'waiting for trigger...';
    end
    s3 = sprintf('%s\n%s', s1,s2);
    set(handles.txMassages, 'String', s3)
    guidata(hObject, handles)
end
if timer <= timeout,
    set(handles.txMassages, 'String', '')
end
% s2 ='Immediate trigger';
% sTrigger = handles.ai.TriggerType;
% if strcmp(sTrigger, 'HwDigital'),
%     s2 = 'Waiting for HW trigger...';
% end
% if strcmp(sTrigger, 'Manual'),
%     s2 = 'Waiting for Manual trigger...';
% end
%wait(handles.ai, 1.1*duration);
if handles.ai.samplesAvailable > 0,
    [handles.data, handles.time] = getdata(handles.ai);
end
fig2=findobj('type', 'figure', 'name', 'MCC Data Acquisition');
if isempty(fig2),
    fig2 = figure('Name', 'MCC Data Acquisition');
end
figure(fig2)
t1=[];
for k=1:size(handles.data,2),
    t1=[t1 handles.time];
    leg{k} = get(handles.ai.Channel(k), 'ChannelName');
end
plot(t1, handles.data)
legend(leg)
guidata(hObject, handles)

% --- Executes on button press in pbSave.
function pbSave_Callback(hObject, eventdata, handles)
fn1 = inputdlg({'Enter file-name'}, 'Save data in a file', 1, {'Output01.csv'});
csvwrite(fn1{1}, [handles.time handles.data])

% --- Executes on button press in pbCreate.
function pbCreate_Callback(hObject, eventdata, handles)
% hObject    handle to pbCreate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
s1 = get(hObject, 'string');
if strcmp(s1, 'Create'),
    set(handles.txMassages, 'String', 'Trying to find MCC HW...');
    if (~isempty(daqfind)) % avoid interference between objects
        stop(daqfind)
    end
    try
        handles.info = daqhwinfo('mcc');
        handles.ai = eval(handles.info.ObjectConstructorName{1});    % create analog input
        set(handles.ai, 'Tag', 'DaqControl');
        set(hObject, 'string', 'Delete');
        s2 = ['Found: ', handles.info.BoardNames];
        set(handles.txMassages, 'String', s2);
    catch
        set(handles.txMassages, 'String', 'Could not find MCC HW !');
    end
else
    set(hObject, 'string', 'Create');
    try
        set(handles.txMassages, 'String', 'Deleting...');
        stop(handles.ai);
        delete(handles.ai);
        clear handles.ai
        set(handles.txMassages, 'String', '<...Welcome!...>');
    catch
        set(handles.txMassages, 'String', 'Problems in deleting MCC object !');
    end   
end
guidata(hObject, handles);
return

function edSampleRate_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'String', 1.0e+03)
   return
end
val_min = 1000;
val_max = 20e+6;
val = str2num(get(hObject, 'String'));
val = min(val_max, max(val_min, val));
set(handles.ai, 'sampleRate', val);
pause(1e-3);
set(hObject, 'String', handles.ai.sampleRate);
return

% --- Executes during object creation, after setting all properties.
function edSampleRate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edSampleRate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edNumberOfSaples_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'String', 2000)
   return
end
val = str2double(get(hObject, 'String'));
set(handles.ai, 'samplesPerTrigger', val);
pause(1e-3);
set(hObject, 'String', handles.ai.samplesPerTrigger);
return
% --- Executes during object creation, after setting all properties.
function edNumberOfSaples_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edNumberOfSaples (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in pmInputRange.
function pmInputRange_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 1)
   return
end
val = get(hObject, 'Value');
Range = [-5 5];
if val==2, Range = [-1 1]; end
set(handles.ai.Channel, 'InputRange', Range);
set(handles.ai.Channel, 'SensorRange', Range);
set(handles.ai.Channel, 'UnitsRange', Range);
pause(1e-3)
n_channels = size(handles.ai.Channel,1);
s1 = sprintf('Input Range Updated for %g channels', n_channels);
%s1=['Input Range Updated: ', get(handles.ai.Channel, 'InputRange')];
set(handles.txMassages, 'String', s1);
guidata(hObject, handles);
return

        

% --- Executes during object creation, after setting all properties.
function pmInputRange_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pmInputRange (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in pmTriggerType.
function pmTriggerType_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 1.0);
   return
end
val = get(hObject, 'Value');
switch val
    case 2,
        set(handles.pmTriggerCondition, 'Enable', 'On')
        set(handles.pbManualTrigger, 'Enable', 'Off')
        set(handles.ai, 'TriggerType', 'HWdigital')
    case 3,
        set(handles.pmTriggerCondition, 'Enable', 'Off')
        set(handles.pbManualTrigger, 'Enable', 'On')
        set(handles.ai, 'TriggerType', 'manual')
    otherwise,
        set(handles.pmTriggerCondition, 'Enable', 'Off')
        set(handles.pbManualTrigger, 'Enable', 'Off')
        set(handles.ai, 'TriggerType', 'immediate')
end
s1=['Trigger type: ', get(handles.ai, 'Triggertype')];
set(handles.txMassages, 'String', s1)
guidata(hObject, handles)
return

% --- Executes during object creation, after setting all properties.
function pmTriggerType_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pmTriggerType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in pmTriggerCondition.
function pmTriggerCondition_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   return
end
val = get(hObject, 'Value');
switch val
    case 2,
        set(handles.ai, 'TriggerCondition', 'trigNegEdge')
        otherwise,
        set(handles.ai, 'TriggerCondition', 'trigPosEdge')
end
s1=['Trigger condition: ', get(handles.ai, 'TriggerCondition')];
set(handles.txMassages, 'String', s1)
guidata(hObject, handles)
return


% --- Executes during object creation, after setting all properties.
function pmTriggerCondition_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pmTriggerCondition (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pbManualTrigger.
function pbManualTrigger_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   return
end
if ~isrunning(handles.ai),
   set(handles.txMassages, 'String', 'DAQ Object must be running before triggering (please do START) !')
   return
end 
trigger(handles.ai)

function edCh4Name_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isrunning(handles.ai),
   set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
   set(hObject, 'Value', 0)
   return
end 
s1 = get(hObject, 'String');
set(handles.ai.Channel(4), 'ChannelName', s1)

% --- Executes during object creation, after setting all properties.
function edCh4Name_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edCh4Name (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in cbCh4Enable.
function cbCh4Enable_Callback(hObject, eventdata, handles)
set(handles.txMassages, 'String', '')
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isrunning(handles.ai),
   set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
   set(hObject, 'Value', 0)
   return
end 
val = get(hObject, 'Value');
if val == 1,
    set(handles.edCh4Name, 'Enable', 'On')
    %set(handles.cbCh2Enable, 'Enable', 'On')
    addchannel(handles.ai, 3, 4)
    Range = [-5 5];
    if get(handles.pmInputRange, 'Value') == 2,
        Range = [-1 1];
    end
    set(handles.ai.Channel(4), 'InputRange', Range);
    set(handles.ai.Channel(4), 'SensorRange', Range);
    set(handles.ai.Channel(4), 'UnitsRange', Range);
 else
%     if get(handles.cbCh2Enable, 'Value') ~= 0.0,
%         s1 = 'Delete higher indexed channels first !';
%         set(handles.txMassages, 'String', s1)
%         set(hObject, 'Value', 1.0)
%         return
 %   end
    delete(handles.ai.Channel(4));
    set(handles.edCh4Name, 'Enable', 'Off', 'String', '')
    %set(handles.cbCh2Enable, 'Enable', 'Off')
end
return


function edCh3Name_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isrunning(handles.ai),
   set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
   set(hObject, 'Value', 0)
   return
end 
s1 = get(hObject, 'String');
set(handles.ai.Channel(3), 'ChannelName', s1)

% --- Executes during object creation, after setting all properties.
function edCh3Name_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in cbCh3Enable.
function cbCh3Enable_Callback(hObject, eventdata, handles)
set(handles.txMassages, 'String', '')
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isrunning(handles.ai),
   set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
   set(hObject, 'Value', 0)
   return
end 
val = get(hObject, 'Value');
if val == 1,
    set(handles.edCh3Name, 'Enable', 'On')
    set(handles.cbCh4Enable, 'Enable', 'On')
    addchannel(handles.ai, 2, 3)
    Range = [-5 5];
    if get(handles.pmInputRange, 'Value') == 2,
        Range = [-1 1];
    end
    set(handles.ai.Channel(3), 'InputRange', Range);
    set(handles.ai.Channel(3), 'SensorRange', Range);
    set(handles.ai.Channel(3), 'UnitsRange', Range);
else
    if get(handles.cbCh4Enable, 'Value') ~= 0.0,
        s1 = 'Delete higher indexed channels first !';
        set(handles.txMassages, 'String', s1)
        set(hObject, 'Value', 1.0)
        return
    end
    delete(handles.ai.Channel(3));
    set(handles.edCh3Name, 'Enable', 'Off', 'String', '')
    set(handles.cbCh4Enable, 'Enable', 'Off')
end
return



function edCh2Name_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isrunning(handles.ai),
   set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
   set(hObject, 'Value', 0)
   return
end 
s1 = get(hObject, 'String');
set(handles.ai.Channel(2), 'ChannelName', s1)

% --- Executes during object creation, after setting all properties.
function edCh2Name_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in cbCh2Enable.
function cbCh2Enable_Callback(hObject, eventdata, handles)
set(handles.txMassages, 'String', '')
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isrunning(handles.ai),
   set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
   set(hObject, 'Value', 0)
   return
end 
val = get(hObject, 'Value');
if val == 1,
    set(handles.edCh2Name, 'Enable', 'On')
    set(handles.cbCh3Enable, 'Enable', 'On')
    addchannel(handles.ai, 1, 2)
    Range = [-5 5];
    if get(handles.pmInputRange, 'Value') == 2,
        Range = [-1 1];
    end
    set(handles.ai.Channel(2), 'InputRange', Range);
    set(handles.ai.Channel(2), 'SensorRange', Range);
    set(handles.ai.Channel(2), 'UnitsRange', Range);
else
    if get(handles.cbCh3Enable, 'Value') ~= 0.0,
        s1 = 'Delete higher indexed channels first !';
        set(handles.txMassages, 'String', s1)
        set(hObject, 'Value', 1.0)
        return
    end
    delete(handles.ai.Channel(2));
    set(handles.edCh2Name, 'Enable', 'Off', 'String', '')
    set(handles.cbCh3Enable, 'Enable', 'Off')
end
return


function edCh1Name_Callback(hObject, eventdata, handles)
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isrunning(handles.ai),
   set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
   set(hObject, 'Value', 0)
   return
end 
s1 = get(hObject, 'String');
set(handles.ai.Channel(1), 'ChannelName', s1)

% --- Executes during object creation, after setting all properties.
function edCh1Name_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in cbCh1Enable.
function cbCh1Enable_Callback(hObject, eventdata, handles)
set(handles.txMassages, 'String', '')
if isempty(daqfind('Tag', 'DaqControl')),
   set(handles.txMassages, 'String', 'Analog Input Object was not found (please create!)')
   set(hObject, 'Value', 0)
   return
end
if isrunning(handles.ai),
   set(handles.txMassages, 'String', 'Can not change channels setup while AI object is running !')
   set(hObject, 'Value', 0)
   return
end 
val = get(hObject, 'Value');
if val == 1,
    set(handles.edCh1Name, 'Enable', 'On')
    set(handles.cbCh2Enable, 'Enable', 'On')
    addchannel(handles.ai, 0, 1)
    Range = [-5 5];
    if get(handles.pmInputRange, 'Value') == 2,
        Range = [-1 1];
    end
    set(handles.ai.Channel(1), 'InputRange', Range);
    set(handles.ai.Channel(1), 'SensorRange', Range);
    set(handles.ai.Channel(1), 'UnitsRange', Range);
else
    if get(handles.cbCh2Enable, 'Value') ~= 0.0,
        s1 = 'Delete higher indexed channels first !';
        set(handles.txMassages, 'String', s1)
        set(hObject, 'Value', 1.0)
        return
    end
    delete(handles.ai.Channel(1));
    set(handles.edCh1Name, 'Enable', 'Off', 'String', '')
    set(handles.cbCh2Enable, 'Enable', 'Off')
end
return

Contact us