Code covered by the BSD License  

Highlights from
Data Acquisition Thing

Data Acquisition Thing

by

 

DataQ device device interface for when the provided package doesn't work

anaLogger
classdef anaLogger < handle
% anaLogger
% Made to solve problem of dataQ instruments being really slow.
%
% Example Use: 
% 
% ai=anaLogger;
% ai.nChannels=4;
% ai.SampleRate=1000;
% ai.start;
% pause(3);
% ai.stop;
% [v t]=ai.getdata;
% 
% figure;
% plot(t,v);
%
% Author
% Peter O'Connor
% oconnorp ...at... ethz ___dot___ ch

    
    
    properties
        
        control;
        
        raw={};        % Raw Data collected
        
        raw2volts=@(x)double(x)*(10/32767);
        
        hF; % Myserious figure window
        
        SampleRate;
        
        LogRate=10;
        
        nChannels;
    end
    
    properties (SetAccess=private)
        
        lastblock=0;        % index of last block checked
        currentblock=0;     % index of current block in raw
        
        killme=false;   % Boolean variable.. Big workaround
    end
    
    
    methods % More advanced
        
        function [itdid channels]=didithappen(A,event,channels,transition)
            % Test if an event occurred since the last check. 
            % Event is a function handle, which evaluates to true if the
            % voltage on the given channels satisfies the condition in
            % "event"
            %
            % Transition is a boolean.  If true, the event must not just
            % happen, but have started to happen during the last interval
            
            if ~exist('transition','var'), transition=false; end
            
            if ~exist('channels','var'), channels=1:A.nChannels; end
            
            testdat=event(A.raw2volts(cell2mat(A.raw(A.lastblock+1:A.currentblock))));
            
            if isempty(testdat)
               channels=[];
            else 
               if transition
                   testdat=testdat(:,2:end) & ~testdat(:,1:end-1);
               end
               ix=any(testdat,2);
               
               channels=channels(ix(channels));
            end
                        
            if ~isempty(channels), 
                itdid=true; 
            else itdid=false;
            end
            
            A.lastblock=A.currentblock;
            
        end
        
    end
    
    methods % Basic AI methods
                
        function A=anaLogger
            
            hF_=figure('IntegerHandle','off');
            control_ = actxcontrol('DATAQSDK.DataqSdkCtrl.1',[450,300,100,50]);
            set(hF_,'visible','off');
            
%             set(control_, 'DeviceID', sprintf('COM0%c 148 460800',port));
%             set(control_, 'DeviceDriver', sprintf('DI10%cNT.DLL',port)); 

            % Set the Vdd Pins on
            set(control_, 'DigitalOutput',-32768)
            set(control_, 'DigitalOutput',32768)
            
            % Load into Object
            A.control=control_;
            A.hF=hF_;
        end
        
        function v=getsample(A)
            if isempty(A.raw), v=[]; 
            else
                v=A.raw2volts(A.raw{A.currentblock}(:,end))';
            end
            
        end
        
        function stop(A)
            A.control.Stop;
        end
        
        function start(A)
                        
            set(A.control,'EventPoint',A.SampleRate*A.nChannels/A.LogRate);
            
            A.control.registerevent({'NewData',@A.grabchunk;'ControlError',@A.ControlError;'OverRun',@anaLogger.OverrunError})

            A.control.Start;
            A.flush;
            
            if A.killme
                error('Control Error on the DAQ!  Something''s up with the drivers');
            end
            
        end
               
        function flush(A)
            A.currentblock=0;
            A.lastblock=0;
            A.raw={};
            A.getdata;
            
        end
        
        function [v t]=getdata(A)
            
            raww=A.raw;
            
            % For some reason the first sample is totally bogus
            if ~isempty(raww)
                raww{1}(:,1)=[];
            end
            
            v=A.raw2volts(cell2mat(raww(1:A.currentblock))');
            
            if nargout>1
               t=(0:size(v,1)-1)'/A.SampleRate;
            end
            
        end
         
    end 
    
    methods % Hidden from user
        
        function grabchunk(A,varargin)
%             
%             if A.killme
%                 error('Control Error on the DAQ!  Something''s up with the drivers');
%             end
%             
            
            data=A.control.GetData;
%             disp 'Grabbed that Chunk!'            
            % Grow raw in large chunks (more efficient)
            A.currentblock=A.currentblock+1;
            if A.currentblock>length(A.raw)
                A.raw=[A.raw cell(1,1024)];
            end
            
            A.raw{A.currentblock}=data;

            
            

        end
        
        function fS=get.SampleRate(A)
            fS=get(A.control,'SampleRate');
        end
        
        function set.SampleRate(A,val)
            set(A.control,'SampleRate',val);
        end
        
        function fS=get.nChannels(A)
            fS=get(A.control,'ADChannelCount');
        end
        
        function set.nChannels(A,val)
            set(A.control,'ADChannelCount',val);
        end
        
        function delete(ai)
            if ishandle(ai.hF)
                delete(ai.hF);
            end
        end
        
        
        function ControlError(A,varargin)
            
            disp(varargin');
            A.killme=true;
        end
        
        
    end
    
    
    
    methods (Static)
        
        
        function OverrunError(varargin)
            error('OverRun error! Data has been overwritten!');
        end
        
    end
    
end

Contact us