-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchaseFunctionPool.m
More file actions
355 lines (336 loc) · 12.1 KB
/
Copy pathchaseFunctionPool.m
File metadata and controls
355 lines (336 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
% THINGS TO DO:
% 1. Read output from the AWG
% 2. Send Multiple commands
% 3. ...
function chaseFunctionPool(varargin)
% chaseFunctionPool('createWaveform',waveform,sampleRate,length)
% will create a wave and save it to sweep.txt in the waveform folder path
% chaseFunctionPool('createWaveform',waveform,sampleRate,length,name)
% will create a wave and save it to a given filename in the waveform
% folder path
% chaseFunctionPool('endChase)
% will stop running the Wavepond program in the background
% chaseFunctionPool('loadChase')
% will start running the Wavepond program in the background and startup
% the AWG
% chaseFunctionPool('loadWaveform')
% will load sweep.txt to the AWG
% chaseFunctionPool('loadWaveform',name)
% will load the waveform with given filename to the AWG
% chaseFunctionPool('PWR_DWN')
% will power down the AWG (many commands takes in a cardNumber which is 1
% by default). In future, if there is more than one waveform connected to
% the computer then there might be a need to change this.
% chaseFunctionPool('PWR_UP')
% will power up the AWG if it is connected and powered down. The wavefrom
% program has to be running in the background. This will not connect to
% the AWG
% chaseFunctionPool('runChase')
% will start running the AWG output
% chaseFunctionPool('senSingleCommand',command)
% will send one commnad to the AWG. The commnad is a string and has to
% have the correct syntax
% chaseFunctionPool('setClkRate',clkRate)
% will set the clock rate for the AWG
% chaseFunctionPool('stopChase')
% will stop the AWG output
global daxDir % directory in which the dax_CMDRunning.txt file is locted the
% tmpCmd.txt file should be copied to this directory
% Please make sure that any changes done to daxDir are made
% within the scope of this current function!
% if daxDir= [] then the AWG program is NOT running.
global waveformPath % directory where the waveforms will be saved to
waveformPath= 'C:\MATLAB_Code\mytoolboxes\AWG\Wave_Form';
%daxDir = "C:\MATLAB_Code\mytoolboxes\AWG\Win7_64_bit";
switch varargin{1}
% all case statements are in alphabetical order
case 'createWaveform'
if length(varargin)<4
disp('Input arguments invalid!');
return
end
if length(varargin)==4
filename= fullfile(waveformPath,'sweep.txt');
elseif length(varargin)==5
filename= fullfile(waveformPath,varargin{5});
end
wave= Wave_generator_Chase(varargin{2},varargin{3},varargin{4});
fid= fopen(filename,'w');
fprintf(fid,'%4g\r\n',wave);
fclose(fid);
% fprintf('Waveform saved to:\n %s \n',filename);
case 'createSegStruct'
if mod(length(varargin)-2,4)~=0
disp('Input arguments invalid!');
return
end
filename= fullfile(waveformPath,varargin{2});
fid= fopen(filename,'w');
for zz = 1:(length(varargin)-2)/4
if varargin{4*(zz-1)+5} > 65535
warning('Loop number exceed the limit 65536.');
end
fprintf(fid,'%s %i %i %i\r\n',fullfile(waveformPath,varargin{4*(zz-1)+3}), varargin{4*(zz-1)+4},varargin{4*(zz-1)+5},varargin{4*(zz-1)+6});
end
fclose(fid);
case 'endChase'
endChase();
delete('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt');
clearvars -global daxDir;
case 'loadChase'
daxDir= pwd;
loadChase();
case 'loadWaveform'
if length(varargin)<2
filename= fullfile(waveformPath,'sweep.txt');
else
filename= fullfile(waveformPath,varargin{2});
end
loadWaveform(filename);
case 'PWR_DWN'
PWR_DWN(varargin{2});
case 'PWR_UP'
PWR_UP(varargin{2});
case 'runChase'
if length(varargin)<3
trigger= 'true';
else
trigger= varargin{3};
end
runChase(varargin{2},trigger);
case 'sendSingleCommand'
sendSingleCommand(varargin{2});
case 'setClkRate'
if length(varargin)~=3
clkRate= 1e9;
else
clkRate= varargin{3};
end
setClkRate(varargin{2},clkRate);
case 'stopChase'
stopChase(varargin{2});
case 'SelExtTrig'
if length(varargin)~=3
disp('Input argument invalid');
else
trigger = varargin{3};
SelExtTrig(varargin{2}, trigger);
end
case 'CreateSingleSegment'
if length(varargin)~=9
disp('Input argument invalid');
else
filename= fullfile(waveformPath,varargin{8});
CreateSingleSegment(varargin{2}, varargin{3}, varargin{4}, varargin{5}, ...
varargin{6}, varargin{7},filename, varargin{9})
end
case 'CreateSegments'
if length(varargin)~=8
disp('Input argument invalid');
else
filename= fullfile(waveformPath,varargin{7});
CreateSegments(varargin{2}, varargin{3}, varargin{4}, varargin{5}, ...
varargin{6}, filename, varargin{8})
end
case 'Initialize'
if length(varargin)~=2
disp('Input argument invalid')
else
InitializeChase(varargin{2});
end
case 'ExtClk10MHzChase'
if length(varargin)~=3
disp('Input argument invalid')
else
ExtClk10MHzChase(varargin{2},varargin{3});
end
case 'SoftTrigger'
if length(varargin)~=2
disp('Input argument invalid')
else
SoftTrigger(varargin{2});
end
otherwise
disp('Request Unknown!');
end
end
function endChase() % have to remember to stop it first
% Stops the API program running in the back ground and effectively
% disconnects the device
global daxDir
% Kills the session with dax22000 by effectively calling end task
!Taskkill /im dax22000_GUI_64.exe /f /t
% Deletes the file dax_cmd.txt created during the session
delete(fullfile(daxDir,'DAx_CMD_Running.txt'));
end
function loadChase()
% NOTE: Only thing you need to edit in the program is where the
% dax22000_GUI_64.exe file is located!
% Starts the script API program
try
!start "" "C:\MATLAB_Code\mytoolboxes\AWG\Win7_64_bit\dax22000_GUI_64.exe" load_api
disp('SUCCESS: Connected to Wavepond DAx22000');
catch ME
disp('ERROR: Cannot connect to Wavepond');
rethrow(ME);
end
end
function loadWaveform(filename)
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %s','load',filename);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function PWR_DWN(cardNum)
% Powers down the device. A good way to test if you can communicate
% with the AWG
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i','PWR_DWN',cardNum);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function PWR_UP(cardNum)
% Note: this does not power up the device if it is not loaded. This is
% intentional to enforce a protocol when communicate with the device!
global daxDir
if daxDir
PWR_DWN(cardNum);
endChase();
loadChase();
else
disp('Device not loaded');
end
end
function runChase(cardNum,trigger)
% runs output
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i %s','Run',cardNum,trigger);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function sendSingleCommand(command)
% Writes a single command to the AWG
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s',command);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function setClkRate(cardNum, clkRate)
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i %g','SetClkRate',cardNum,clkRate);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function stopChase(cardNum)
% stops output
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i','Stop',cardNum);
fclose(fileID);
pause(0.1);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
pause(0.1);
catch ME
rethrow(ME);
end
end
function SelExtTrig(cardNum, Trigger)
% stops output
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i %s','SelExtTrig',cardNum, Trigger);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function CreateSingleSegment(cardNum, ChanNum, NumPoints, NumLoops, PAD_Val_Beg, PAD_Val_End, pUserArrayWORD, Triggered)
% stops output
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i %i %i %i %i %i %s %i','CreateSingleSegment', cardNum, ChanNum, NumPoints, NumLoops, ...
PAD_Val_Beg, PAD_Val_End, pUserArrayWORD, Triggered);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function CreateSegments(cardNum, ChanNum, SegNum, PAD_Val_Beg, PAD_Val_End, pUserArrayWORD, loop)
% stops output
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i %i %i %i %i %s %s','CreateSegments', cardNum, ChanNum, SegNum, ...
PAD_Val_Beg, PAD_Val_End, pUserArrayWORD, loop);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function InitializeChase(cardNum)
% Initialize AWG
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i','Initialize',cardNum);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function ExtClk10MHzChase(cardNum, Enable)
% enable/disable (1/0) external 10MHz clock
global daxDir
try
fileID= fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt','w');
fprintf(fileID,'%s %i %i','Ext10MHz',cardNum, Enable);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt',fullfile(daxDir,'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end
function SoftTrigger(cardNum)
% Apply a soft trigger.
global daxDir
try
fileID = fopen('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt', 'w');
fprintf(fileID,'%s %i', 'SoftTrigger', cardNum);
fclose(fileID);
copyfile('C:\MATLAB_Code\mytoolboxes\AWG\tmpCmd.txt', fullfile(daxDir, 'dax_cmd.txt'));
catch ME
rethrow(ME);
end
end