Short_course_DAQ_matlab_AAAR_chapter

advertisement
DAQ Matlab
Hardware Information
-
Three things you need for DAQ (hardware box, daq card, and software has the
operation program for acquiring data and controlling purposes)
-
The number of bits (e.g. 16-bit gives 2^16 increments between your range of the
DAQ card, e.g. -1 to 1 volt there will be 65,536 increments or places to store data
between -1 and 1 volt). Ranges can be mv to volts (depends on NI hardware)
-
Quantization error is due to the fact that the range of voltages you are receiving needs
to be broader. (e.g. if your range from the DAQ card is -1 to 1 volts, but your output
is no larger than 1 [mV], then you are going to get this error. The results will look
chopping.
-
You can get different DAQ cards that you can send out and receive voltages at the
same time. (This cards are more powerful, but are more expensive)
-
The way DAQ works; for an input you begin the data acquisition then you download
the data from the buffer, while an output you download the data to the buffer, then it
waits till you tell it to begin.
Matlab Toolbox
1. Need Matlab toolbox DAQ (type help daq in the command window), these are all
the parameters that you will need to learn to be able to program the analog input
and output.
Data acquisition object construction.
daq/analoginput - Construct analog input object.
daq/analogoutput - Construct analog output object.
daq/digitalio - Construct digital input/output object.
Getting and setting parameters.
daqdevice/get - Get value of data acquisition object property.
daqdevice/set - Set value of data acquisition object property.
daqdevice/inspect - Open property inspector and configure data acquisition object properties.
setverify
- Set and return value of data acquisition object property.
Execution.
daqdevice/start - Start object running.
stop
- Stop object running and logging/sending.
trigger
- Manually initiate logging/sending for running object.
daqdevice/wait - Wait for the object to stop running.
Analog input functions.
addchannel
- Add channels to analog input object.
addmuxchannel - Add mux'd channels to analog input object.
flushdata
- Remove data from engine.
getdata
- Return acquired data samples.
getsample
- Immediately acquire a single sample.
muxchanidx
- Return scan channel index associated with mux board.
peekdata
- Preview most recent acquired data.
islogging
- Determine if object is logging data.
Analog output functions.
addchannel
- Add channels to analog output object.
putdata
- Queue data samples for output.
putsample
- Immediately output single sample to object.
issending
- Determine if object is sending data.
Digital input/output functions.
addline
- Add lines to digital input/output object.
getvalue
- Read line values.
putvalue
- Write line values.
General.
binvec2dec
- Convert binary vector to decimal number.
daq/private/clear - Clear data acquisition object from the workspace.
daqcallback
- Display event information for specified event.
daqfind
- Find specified data acquisition objects.
daqmem
- Allocate or display memory for one or more device
objects.
daqread
- Read Data Acquisition Toolbox (.daq) data file.
daqregister
- Register or unregister adaptor DLLs.
daqreset
- Delete and unload all data acquisition objects and
DLLs.
daqdevice/delete - Remove data acquisition objects from the engine.
dec2binvec
- Convert decimal number to binary vector.
ischannel
- Determine if object is a channel.
isdioline
- Determine if object is a line.
isvalid
- Determine if object is associated with hardware.
isrunning
- Determine if object is running.
length
- Determine length of data acquisition object.
daq/private/load - Load data acquisition objects from disk into MATLAB
workspace.
makenames
- Generate cell array of names for naming channels/lines.
obj2mfile
- Convert data acquisition object to MATLAB code.
daq/private/save - Save data acquisition objects to disk.
showdaqevents - Display summary of event log.
size
- Determine size of data acquisition object.
softscope
- Data Acquisition oscilloscope GUI.
Information and help.
daqhelp
- Data acquisition property and function help.
daqhwinfo
- Information on available hardware.
daqsupport
- Data acquisition technical support tool.
propinfo
- Property information for data acquisition objects.
Data acquisition demos.
demodaq_intro - Introduction to Data Acquisition Toolbox.
demodaq_save - Methods for saving and loading data acquisition objects.
demodaq_callback - Introduction to data acquisition callback functions.
daqtimerplot
- Example callback function which plots the data acquired.
Analog input demos.
daqrecord
- Record data from the specified adaptor.
demoai_channel - Introduction to analog input channels.
demoai_fft
- FFT display of an incoming analog input signal.
demoai_intro - Introduction to analog input objects.
demoai_logging - Demonstrate data logging.
demoai_trig
- Demonstrate the use of immediate, manual and software triggers.
daqscope
- Example oscilloscope for the Data Acquisition Toolbox.
Analog output demos.
daqplay
- Output data to the specified adaptor.
daqsong
- Output data from HANDEL.MAT to a sound card.
demoao_channel - Introduction to analog output channels.
demoao_intro
- Introduction to analog output objects.
demoao_trig
- Demonstrate the use of immediate and manual triggers.
daqfcngen
- Example function generator for the Data Acquisition Toolbox.
Digital I/O demos.
demodio_intro - Introduction to digital I/O objects.
demodio_line - Introduction to digital I/O lines.
diopanel
- Display digital I/O panel.
See also analoginput, analogoutput, digitalio, daqhelp.
2. Let’s begin with Analog input….
clc
clear all
close all
%1. Create a device object - Create the analog input object AI for a
...
% National Instrument Instruments board. The installed adaptors and ...
% hardware IDs are found with daqhwinfo.
AI = analoginput('nidaq','Dev1');
%2. Add channels
chan1_input = addchannel(AI,0);
chan2_input = addchannel(AI,1); % - could add another channel if you
want
%3. Define DAQ parameters
duration = 0.1; % acquisition total time
ScanFreq = 1000; % how fast do you want to acquire the data
Scans = ScanFreq*duration; % number of scans
t = [0:1/ScanFreq:duration]'; % time window check... make sure t =
time...
%... time will be defined later, which Matlab gets from the input
data...
%... we want t = time, so check this.
%4. Set DAQ parameters
% set(AI) %- uncomment this to see how to program the input commands
set(AI,'SampleRate',ScanFreq) %set the sample rate of the DAQ
ScaleRange = [-10 10];
set(chan1_input,'InputRange',ScaleRange); % setting the limit
magnitudes
set(AI,'SamplesPerTrigger',Scans+1); % set samples per trigger, you...
%... have to add 1 to be consistant with the length(t)! (type t and
then
%...time in command line after you run the code, you will see what I
mean.
set(AI,'TriggerType','Manual'); % set a manual trigger
%5. You can also get some parameters
% get(AI) %- uncomment this to see what you can get
%6. Acquire data - Start AI, issue a manual trigger
start(AI)
trigger(AI)
%7. Get the data from the buffer
[data time] = getdata(AI);
[z,p,k] = butter(10,60/ScanFreq,'low'); % this is a filter...
%...don't entirely sure how it works.
Mean_value = mean(data);
size(data); % the channels are in columns and instances run down as
row...
% numbers, data has multiple columns, one for each channel
%8. Take care of data
figure
v = data(:,1); % data in col 1
v1 = data(:,2); % data in col 2
plot(time,data(:,1),'-r')
%9. delete, clear, and reset the DAQ hardware box and card
delete(AI)
clear AI
daqreset % resets the DAQ
3. AO and AI together…
clc
clear all
close all
global Total_time Len_t dt
%% 1. Define total scan time for analysis %%
Total_time = 1; % final time [s]
dt = 0.02; % time step [s]
t = [0:dt:Total_time]'; % [s]
Len_t = length(t); % length of time (number of Scans = Len_t)
%% 2. Define your output voltage for analysis %%
f = 5; % [Hz]
v = 2.*sin(2.*pi.*f.*t); % output voltage
v = t;
%% 3. Configuration - Define Outputs %%
ChannelNumbers = [0]; % if you want more channels then type [0 1 2]
[AO,Channels_AO] = ConfigureAOChannel_short_course(ChannelNumbers); %
Use this funciton to define anaolog output
%% 4. Write - useful for AO, takes variable data and puts it the AO
buffer
putdata(AO,v);
%% 5. Configuration - Define Inputs %%
ChannelNumbers = [0]; % again if you want more channels [0 1 2]
[AI,Channels_AI] = ConfigureAIChannel_short_course(ChannelNumbers); %
Use this funciton to define anaolog input
%% 6. Set parameters, some parameters are set in the configure
functions %%
% fprintf('SET AO parameters\n')
% set(AO) %- uncomment this to see how to program the set commands
% fprintf('SET AI parameters\n')
% set(AI) %- uncomment this to see how to program the input commands
% fprintf('GET AO parameters\n')
% get(AO) %- uncomment to get AO parameters
% fprintf('GET AI parameters \n')
% get(AI) %- uncomment to get AI parameters
set([AI],'TransferMode','Interrupts'); % you need this set parameter
inorder to start AO and AI at same time
%% 7. Start DAQ, AO and AI at same time %%
start([AO AI]); % Start both objects at same time
%% 8. Determine initial times for AI and AO %%
aitime = AI.InitialTriggerTime; % AI initial time
aotime = AO.InitialTriggerTime; % AO initial time
delta = abs(aotime - aitime); % time difference between starting AI and
AO, should be very small
ai_t = fix(aitime); % rounds to nearest integers
ao_t = fix(aotime); % rounds to nearest integers
fprintf('The initial time of AI is %d:%d:%d\n',
ai_t(4),ai_t(5),ai_t(6)) % display times
fprintf('The initial time of AO is %d:%d:%d\n',
ao_t(4),ao_t(5),ao_t(6)) % display times
fprintf('The difference in time is %f [s]\n',delta(6))
% 9. Wait - gives hardware time to complete AO or AI,
% If this is less than the time required, an error will occur
WaitTime=Total_time*20; % [s]
wait(AO,WaitTime); % tell AO to wait until all data has been sent out
%% 10. Get your data from the buffer %%
[data time abstime] = getdata(AI);
showdaqevents(AO) % shows daq events for AO
showdaqevents(AI) % shows daq events for AI
%% 11. Do something with the data %%
v = data;
v = v(:,1);
N = length(v);
ScanFrequency = get(AI,'SampleRate');
ScanPeriod = 1/ScanFrequency;
Duration = N*ScanPeriod;
t = [0:ScanPeriod:Duration]; % this time is used to check
t = t(1:N);
plot(t,v,'.r')
hold on
plot(time,v)
% 6. stop, delete, clear AO and AI
flushdata(AI);
stop(AO)
delete(AO)
clear AO
stop(AI)
delete(AI)
clear AI
daqreset
4. AO function
% This function determines your analog output %
function [DeviceConfiguration,ChannelConfiguration] =
ConfigureAOChannel_short_course(ChannelNumbers);
%
global Total_time Len_t dt
Type = 'AO'; % this is used to name the output, see below in the for
loop
% 1. Define Hardwave Device in Software, see or use daqhwinfo
% In the MAE Lab, a single PCI6024E board is installed and it is Dev1
DeviceNumber = 1;
DeviceConfiguration = analogoutput('nidaq',DeviceNumber);
% 2. Define channels to be used (add channels for each one you defined)
ChannelCount = 0;
for ChannelNumber = ChannelNumbers;
ChannelCount = ChannelCount + 1;
ChannelConfiguration(ChannelCount) =
addchannel(DeviceConfiguration,ChannelNumber);
end;
% ShowChannels = ChannelConfiguration; % show the channels created
% 3. Determine sample rate
HardwareMaximum = 200000;
% [Hz]=[scans/second]
NumberOfScans = Len_t-1; % number of scans
UpdateFrequency = (NumberOfScans/Total_time); % [Hz]=[scans/second]
Scan_Freq = UpdateFrequency; % how fast to scan the data
if UpdateFrequency*ChannelCount > HardwareMaximum;
UpdateFrequency = floor(HardwareMaximum/ChannelCount);
end; % if hardware max is reached
set(DeviceConfiguration,'SampleRate',UpdateFrequency);
% 4. Configure channels - each channel can have its own input limits
%% -------------------------------------------------------- %%
OutputLimitMagnitudes = [10]; % this has to be changed if you have more
than one output
%% -------------------------------------------------------- %%
%% Loops for each channel created %%
OutputLimitMagnitudes =
OutputLimitMagnitudes(1:length(ChannelNumbers));
ChannelCount = 0;
for OutputLimitMagnitude = OutputLimitMagnitudes;
ChannelCount = ChannelCount+1;
ScaleMaximum = OutputLimitMagnitude;
ScaleMinimum = -OutputLimitMagnitude;
FSR = ScaleMaximum - ScaleMinimum;
ScaleRange = [ScaleMinimum ScaleMaximum];
ChannelConfiguration(ChannelCount);
ChannelNumbers(ChannelCount);
set(ChannelConfiguration(ChannelCount),'ChannelName',[Type
int2str(ChannelNumbers(ChannelCount))]);
set(ChannelConfiguration(ChannelCount),'OutputRange',ScaleRange);
end;
5. AI Function
% This function determines your analog input %
function [DeviceConfiguration,Channels] =
ConfigureAIChannel_short_course(ChannelNumbers);
global Total_time Len_t dt
Type = 'AI';
% 1. Define Hardwave Device in Software, see or use daqhwinfo
% In the MAE Lab, a single PCI6024E board is installed and it is Dev1
DeviceNumber = 1;
DeviceConfiguration = analoginput('nidaq',DeviceNumber);
% 2. Define channels to be used
ChannelCount=0;
for ChannelNumber=ChannelNumbers, ChannelCount=ChannelCount+1;
Channels(ChannelCount) =
addchannel(DeviceConfiguration,ChannelNumber);
end;
% 3. Determine sample rate
HardwareMaximum = 200000;
% [Hz]=[scans/second]
NumberOfScans = Len_t-1; % number of scans
ScanFrequency = NumberOfScans/Total_time; % how fast you want to scan
if ScanFrequency*ChannelCount > HardwareMaximum
ScanFrequency = floor(HardwareMaximum/ChannelCount);
end;
set(DeviceConfiguration,'SampleRate',ScanFrequency);
% 3.1 set samples per trigger
Duration = Total_time;
% seconds
NumberOfScans = ScanFrequency*Duration;
% [scans/second]*[seconds]
set(DeviceConfiguration,'SamplesPerTrigger',NumberOfScans+1);
% 4. Configure channels - each channel can have its own input limits
%% --------------------------------------------------------------- %%
InputLimitMagnitudes = [5]; % this has to be changed based on number of
inputs you want
%% --------------------------------------------------------------- %%
%% Loops for each channel created %%
InputLimitMagnitudes = InputLimitMagnitudes(1:length(ChannelNumbers));
ChannelCount = 0;
for InputLimitMagnitude = InputLimitMagnitudes
ChannelCount = ChannelCount + 1;
ScaleMaximum = InputLimitMagnitude;
ScaleMinimum = -InputLimitMagnitude;
FSR = ScaleMaximum-ScaleMinimum;
ScaleRange = [ScaleMinimum ScaleMaximum];
set(Channels(ChannelCount),'ChannelName',[Type
int2str(ChannelNumbers(ChannelCount))]);
set(Channels(ChannelCount),'InputRange',ScaleRange);
end;
Download