Skip to content

Commit 473cbb7

Browse files
committed
Tidy
1 parent 5b701da commit 473cbb7

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

scanAndAcquire.m

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
% Rob Campbell - Basel 2015
1515

1616

17+
%Define a cleanup object that will release the DAQ gracefully
18+
tidyUp = onCleanup(@stopAcq);
19+
20+
1721
%----------------------------------
1822
%User settings
1923
amp=2; %Scanner amplitude
@@ -24,60 +28,62 @@
2428
%----------------------------------
2529

2630

27-
%Define a cleanup object in case the user does a ctrl-c and we end up with the
28-
%stages having peculiar speed settings, etc
29-
tidyUp = onCleanup(@stopAcq);
31+
%- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32+
% CONNECT TO THE HARDWARE
3033

3134
%Create a session using NI hardware
3235
s=daq.createSession('ni');
3336

3437

35-
%Add an analog input channel
38+
%Add an analog input channel for the PMT signal
3639
AI=s.addAnalogInputChannel('Dev1', 'ai1', 'Voltage'); %Hard-coded. This is a PCI 6115
3740
AI.Range = [-2,2];
3841

3942

4043
%Add a listener to get data back from this channel
4144
addlistener(s,'DataAvailable', @plotData);
4245

43-
%Add analog output channels for scanners 0 is x and 1 is y
46+
47+
%Add analog two output channels for scanners 0 is x and 1 is y
4448
s.addAnalogOutputChannel('Dev1',0:1,'Voltage'); %the 6115 is assigned to Dev1
4549

4650

47-
%Calculate the number of samples per line
48-
samplesPerLine = pointsPerLine*samplesPerPoint;
4951

50-
yWaveform = linspace(amp,-amp,samplesPerLine*linesPerFrame);
5152

5253

53-
%The output buffer is re-filled for the next line when it becomes half empty
54-
s.NotifyWhenScansQueuedBelow = round(length(yWaveform)*0.5);
54+
%- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
55+
% BUILD THE GALVO WAVEFORMS
5556

57+
%Calculate the number of samples per line
58+
samplesPerLine = pointsPerLine*samplesPerPoint;
5659

57-
%The sample rate is fixed, so we report the frame rate
58-
s.Rate = sampleRate;
59-
frameRate = length(yWaveform)/sampleRate;
60-
fprintf('Scanning at %0.2f frames per second\n',1/frameRate)
60+
%So the Y waveform is:
61+
yWaveform = linspace(amp,-amp,samplesPerLine*linesPerFrame);
6162

6263
%Produce the X waveform
6364
xWaveform = linspace(-amp, amp, samplesPerLine)*1;
6465
xWaveform = repmat(xWaveform,1,length(yWaveform)/length(xWaveform));
6566

66-
6767
%Assemble the two waveforms into an N-by-2 array
6868
dataToPlay = [xWaveform(:),yWaveform(:)];
6969
fprintf('Data waveforms have length %d\n',size(dataToPlay,1))
7070

7171

72-
%Plot the waveforms
73-
hFig=clf;
74-
plot(dataToPlay)
75-
drawnow
76-
7772

78-
%This listener tops up the output buffer
79-
addlistener(s,'DataRequired', @(src,event) src.queueOutputData(dataToPlay)); %add listener on the DataRequired event to top it up
8073

74+
%- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75+
% PREPARE TO ACQUIRE
76+
77+
%The sample rate is fixed, so we report the frame rate
78+
s.Rate = sampleRate;
79+
frameRate = length(yWaveform)/sampleRate;
80+
fprintf('Scanning at %0.2f frames per second\n',1/frameRate)
81+
82+
%The output buffer is re-filled for the next line when it becomes half empty
83+
s.NotifyWhenScansQueuedBelow = round(length(yWaveform)*0.5);
84+
85+
%This listener tops up the output buffer
86+
addlistener(s,'DataRequired', @(src,event) src.queueOutputData(dataToPlay));
8187

8288
s.IsContinuous = true; %needed to provide continuous behavior
8389
s.queueOutputData(dataToPlay); %queue the first frame
@@ -86,23 +92,31 @@
8692
s.NotifyWhenDataAvailableExceeds=size(dataToPlay,1); %when to read back
8793

8894

95+
96+
97+
%- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98+
% SET UP THE FIGURE WINDOW THAT WILL DISPLAY THE DATA
99+
89100
%We will plot the data on screen as they come in, so make a blank image
90-
clf(hFig);
101+
hFig=clf;
91102
histAx=subplot(1,2,1);
92103

93104
imAx=subplot(1,2,2);
94105
hAx=imagesc(zeros(linesPerFrame,pointsPerLine));
95106
colormap gray
96107

97-
s.startBackground %start the acquisition in the background
98108

99-
%Block. User presses ctrl-C to to quit, this calls stopAcq
100-
while 1,pause(0.1), end
101109

102110

111+
%- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112+
% START!
113+
s.startBackground %start the acquisition in the background
103114

115+
%Block. User presses ctrl-C to to quit, this calls stopAcq
116+
while 1,pause(0.1), end
104117

105118

119+
%-----------------------------------------------
106120
function stopAcq
107121
fprintf('Zeroing AO channels\n')
108122
s.stop;
@@ -112,7 +126,7 @@
112126

113127
fprintf('Releasing NI hardware\n')
114128
release(s);
115-
end
129+
end %stopAcq
116130

117131

118132
function plotData(src,event)
@@ -134,7 +148,6 @@ function plotData(src,event)
134148
set(hAx,'CData',im);
135149
set(imAx,'CLim',[0,2]);
136150

137-
138-
end
151+
end %plotData
139152

140-
end
153+
end %scanAndAcquire

0 commit comments

Comments
 (0)