DSPACE/TakeData1

From PrattWiki
Jump to navigation Jump to search

The code below contains the TakeData1.m script for use with the Instrumentation Basics lab for ME 344. If you need to, you can copy and paste the text out of the block below.

%% Close figures and clear - NOTE - globals will still work!
close all; clear

%% Access dSPACE card
mlib('SelectBoard','ds1104')

%% Define globals for ADC and DAC
%% Assumes ADC/DAC handles have been defined
global ADC1 ADC5 DAC1

%% Gather data using the dSpace card
% Set voltage to 0 and wait 5 seconds
fprintf('Setting 0 V on DACH1 - waiting 5 seconds\n')
mlib('Write', DAC1, 'Data', 0)
pause(5)
% Start data collection and start counter
fprintf('Starting data collection\n')
Count = 0;
% Set voltage to 5 V and start timer
fprintf('Setting 5 V on DACH1\n')
mlib('Write', DAC1, 'Data', 5);
tic;
% Run for 5 seconds - asynchronus data collection mode
while toc<5
    % Increment counter
    Count = Count+1;
    % Tell dSPACE which channels to acquire and start acquisition
    mlib('Set','TraceVars',[ADC1; ADC5],'NumSamples',1);
    mlib('StartCapture')
    % Do not go to next step until data is taken
    while mlib('CaptureState')~=0
    end
    % Store time and data in Count row of vector
    tout(Count, :) = toc;
    data(Count, :) = mlib('FetchData');
end
% Set voltage to 0 and take data for another ~5 seconds 
% (see above for comments)
fprintf('Setting 0 V on DACH1\n')
mlib('Write', DAC1, 'Data', 0)
while toc<10
    Count = Count+1;
    mlib('Set','TraceVars',[ADC1; ADC5],'NumSamples',1);
    mlib('StartCapture')
    while mlib('CaptureState')~=0
    end
    tout(Count) = toc;
    data(Count, :) = mlib('FetchData');
end

fprintf('Finished with data collection\n')
%% Make a plot
plot(tout, data)