Skip to content

Commit a6dcf96

Browse files
committed
check over some more examples
1 parent ce3fab2 commit a6dcf96

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

code/+vidrio/+AI/hardwareContinuousVoltage.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
% Task configuration
4747
sampleClockSource = 'OnboardClock'; % The source terminal used for the sample Clock.
4848
% For valid values see: zone.ni.com/reference/en-XX/help/370471AE-01/daqmxcfunc/daqmxcfgsampclktiming/
49-
sampleRate = 5000; % Sample Rate in Hz
49+
sampleRate = 10E3; % Sample Rate in Hz
5050
numSamplesPerChannel = sampleRate*2 ; % The number of samples to be stored in the buffer per channel
5151

5252

code/+vidrio/+AI/hardwareContinuousVoltageWithCallBack.m

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,9 @@
3737

3838

3939
% Task configuration
40-
sampleClockSource = 'OnboardClock'; % The source terminal used for the sample Clock.
41-
% For valid values see: zone.ni.com/reference/en-XX/help/370471AE-01/daqmxcfunc/daqmxcfgsampclktiming/
42-
sampleRate = 5000; % Sample Rate in Hz
43-
numSamplesPerChannel = sampleRate*2 ; % The number of samples to be stored in the buffer per channel
40+
sampleRate = 10E3; % Sample Rate in Hz
41+
numSamplesToPlot = 1000 ; % Read off this many samples each time to plot
42+
bufferSize_numSamplesPerChannel = 40*numSamplesToPlot; % The number of samples to be stored in the buffer per channel.
4443

4544

4645
try
@@ -58,22 +57,23 @@
5857
hTask.createAIVoltageChan(devName, physicalChannels, [], minVoltage, maxVoltage);
5958

6059

61-
% * Configure the sampling rate and the size of the buffer in samples
60+
% * Configure the sampling rate and the size of the buffer in samples using the on-board sanple clock
6261
% More details at: "help dabs.ni.daqmx.Task.cfgSampClkTiming"
6362
% C equivalent - DAQmxCfgSampClkTiming
6463
% http://zone.ni.com/reference/en-XX/help/370471AE-01/daqmxcfunc/daqmxcfgsampclktiming/
65-
hTask.cfgSampClkTiming(sampleRate, 'DAQmx_Val_ContSamps', numSamplesPerChannel, sampleClockSource);
64+
hTask.cfgSampClkTiming(sampleRate, 'DAQmx_Val_ContSamps', bufferSize_numSamplesPerChannel, 'OnboardClock');
6665

6766

6867
% * Set up a callback function to regularly read the buffer and plot the data.
6968
% More details at: "help dabs.ni.daqmx.Task.registerEveryNSamplesEvent"
70-
hTask.registerEveryNSamplesEvent(@readAndPlotData, 500, 1, 'Scaled');
69+
hTask.registerEveryNSamplesEvent(@readAndPlotData, numSamplesToPlot, 1, 'Scaled');
7170

7271

7372
% Open a figure window and have it shut off the acquisition when closed
7473
% See: basicConcepts/windowCloseFunction.m
7574
fig=clf;
76-
fig.CloseRequestFcn=@windowCloseFcn;
75+
set(fig,'CloseRequestFcn', @windowCloseFcn, ...
76+
'Name', 'Close figure window to stop acquisition')
7777

7878

7979
% Start the task and wait until it is complete. Task starts right away since we
@@ -113,12 +113,14 @@ function windowCloseFcn(~,~)
113113

114114

115115
function readAndPlotData(src,evt)
116+
% This callback function runs each time a pre-defined number of points have been collected
117+
% This is defined at the hTask.registerEveryNSamplesEvent method call.
116118
hTask = src;
117119
data = evt.data;
118120

119121
errorMessage = evt.errorMessage;
120-
121-
% check for errors
122+
123+
% check for errors and close the task if any occur.
122124
if ~isempty(errorMessage)
123125
delete(hTask);
124126
error(errorMessage);

code/+vidrio/+mixed/AOandAI.m

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,26 @@
4949

5050
AIDevice = 'Dev1';
5151
AIChans = 0;
52-
AIterminalConfig = 'DAQmx_Val_RSE'; %Valid values: 'DAQmx_Val_Cfg_Default', 'DAQmx_Val_RSE', 'DAQmx_Val_NRSE', 'DAQmx_Val_Diff', 'DAQmx_Val_PseudoDiff'
52+
AIterminalConfig = 'DAQmx_Val_Cfg_Default'; %Valid values: 'DAQmx_Val_Cfg_Default', 'DAQmx_Val_RSE', 'DAQmx_Val_NRSE', 'DAQmx_Val_Diff', 'DAQmx_Val_PseudoDiff'
5353
AODevice = 'Dev1';
5454
AOChan = 0;
5555

5656
minVoltage = -10;
5757
maxVoltage = 10;
5858

59-
sampleRate = 10e3; %Hz
60-
updatePeriod = 0.15; % How often to read
61-
59+
sampleRate = 30e3; % Samples per second
6260

6361
%Play a sinewave out of the AO
6462
waveform = sin(linspace(-pi,pi, sampleRate/55))' * 5; % Build a sine wave to play through the AO line. NOTE: column vector
65-
numSamplesPerChannel = length(waveform) ; % The number of samples to be stored in the buffer per channel
66-
63+
updatePeriod = 0.15; % How often to read
64+
readEveryNpoints=round(updatePeriod * sampleRate); % every this many points read data
65+
66+
% Open a figure window and have it shut off the acquisition when closed
67+
% See: basicConcepts/windowCloseFunction.m
68+
fprintf('Close figure to quit acquisition\n')
69+
fig = clf;
70+
set(fig, 'CloseRequestFcn', @windowCloseFcn, ...
71+
'Name', 'Close figure to stop acquisition')
6772

6873
try
6974
% * Create separate DAQmx tasks for the AI and AO
@@ -89,13 +94,12 @@
8994
% More details at: "help dabs.ni.daqmx.Task.cfgSampClkTiming"
9095
% C equivalent - DAQmxCfgSampClkTiming
9196
% http://zone.ni.com/reference/en-XX/help/370471AE-01/daqmxcfunc/daqmxcfgsampclktiming/
92-
hAITask.cfgSampClkTiming(sampleRate, 'DAQmx_Val_ContSamps', round(sampleRate*updatePeriod)*10);
97+
hAITask.cfgSampClkTiming(sampleRate, 'DAQmx_Val_ContSamps', readEveryNpoints*10);
9398

9499
% * Use a callback function to read from the buffer at the interval set by updatePeriod
95100
% have been played out. Also see: basicConcepts/anonymousFunctionExample.
96101
% More details at: "help dabs.ni.daqmx.Task.registerEveryNSamplesEvent"
97-
hAITask.registerEveryNSamplesEvent(@readAndPlotData, round(updatePeriod * sampleRate), false, 'Scaled');
98-
102+
hAITask.registerEveryNSamplesEvent(@readAndPlotData, readEveryNpoints, false, 'Scaled');
99103

100104

101105
%-------------------------------------------------------------------------------
@@ -126,16 +130,8 @@
126130
% More details at: "help dabs.ni.daqmx.Task.cfgDigEdgeStartTrig"
127131
% DAQmxCfgDigEdgeStartTrig
128132
% http://zone.ni.com/reference/en-XX/help/370471AE-01/daqmxcfunc/daqmxcfgdigedgestarttrig/
129-
hAOTask.cfgDigEdgeStartTrig(['/',AIDevice,'/ai/StartTrigger'], 'DAQmx_Val_Rising');
130-
131-
132-
% Open a figure window and have it shut off the acquisition when closed
133-
% See: basicConcepts/windowCloseFunction.m
134-
fprintf('Close figure to quit acquisition\n')
135-
fig = clf;
136-
set(fig, 'Name', 'Close figure to stop acquisition')
137-
fig.CloseRequestFcn = @windowCloseFcn;
138-
133+
hAOTask.cfgDigEdgeStartTrig(['/',AIDevice,'/ai/StartTrigger'], 'DAQmx_Val_Rising')
134+
139135
hAOTask.start();
140136
hAITask.start();
141137

@@ -146,6 +142,7 @@
146142
hAITask.stop; % Calls DAQmxStopTask
147143
hAOTask.stop;
148144
delete([hAITask, hAOTask]); % The destructor (dabs.ni.daqmx.Task.delete) calls DAQmxClearTask
145+
delete(fig)
149146
else
150147
fprintf('No task variable present for clean up\n')
151148
end
@@ -155,7 +152,7 @@
155152
end %try/catch
156153

157154

158-
function readAndPlotData(src,evnt)
155+
function readAndPlotData(src,~)
159156
% Scaled sets the input to be represented as a voltage value
160157
inData = readAnalogData(src, src.everyNSamples, 'Scaled');
161158
plot(inData)

0 commit comments

Comments
 (0)