% MATLAB supports M-Series, E-Series, and USB hardware from % National Instruments with the Data Acquisition Toolbox. This basic code % example shows you how to use MATLAB to sample from two analog input % channels and put the data out on two analog output channels % Use this command to determine Board IDs in system, if needed %hw = daqhwinfo('nidaq') %hw.InstalledBoardIds %hw.BoardNames % Create an analog input object using Board ID "Dev1". ai = analoginput('nidaq','Dev1'); % Create an analog output object using Board ID "Dev1". ao = analogoutput('nidaq','Dev1'); % Data will be acquired from hardware AD channels 0 and 1 addchannel(ai,[0 1]); % Data will be sent to hardware DA channels 0 and 1 addchannel(ao,[0 1]); % Review the basic configuration of the acquisition by typing % the name of the variable. % ai % Use this command to see properties that can be configured % set(ai) % Use this comment to get a listing of all object properties and % their current settings % get(ai) % Configure the analog input for single-ended or differential mode set(ai,'InputType','SingleEnded'); %set(ao,'InputType','Differential'); numsamps = 500; % take 500 samples % allocate a data array to save data from AD channel 0 alldata = zeros(numsamps,1); tic; % start the timer so we can calculate sampling rate when finished % acquire the samples for i = 1:numsamps % get data from the analog input channels data = getsample(ai); % -------- % you would put your control law here % -------- % put data on the analog output channels putsample(ao,data); % save AI channel 0 into a data array alldata(i) = data(1); end % put zero volts on the AO channels when you are finished putsample(ao,[0 0]); % calculate your average sampling rate samprate = numsamps/toc % Graphically plot the results clf; plot(alldata,'.'); shg; % Clean up delete(ai); delete(ao);