-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathH317_Common_Template.m
345 lines (296 loc) · 10.1 KB
/
H317_Common_Template.m
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
% File name: H317_Common_Template.m
%% Part 1 of TEMPLATE FOR ALL LIFU experiments (single and broad focus)
% matfilename,focustraversalfile, DutyCycle, PRF,Frequency parameters
% SelectedVoltage will be filled by the
% LIFU-APP
if bTestInTank
res=questdlg("SCRIPT ONLY TO BE USED IN A TANK TEST. Continue?");
if ~strcmp('Yes',res)
msgbox("stopping protocol generation");
error("Stopping protocol generation");
end
end
MaxVoltage = 40;
MaxAvgPower= 410;
filename = ['MatFiles/', matfilename];
assert(SelectedVoltage<=MaxVoltage && SelectedVoltage >1.0 );
assert(TotalTime<=40.0 && TotalTime >0.0 );
assert(PRF==500.0 || PRF == 750.0 || PRF == 1000.0 || PRF ==1250.0);
assert(DutyCycle <=0.035);
assert(contains(matfilename,SubjectID));
assert(Frequency==7e5); % we only support 7e5 in this study
%% Procedure Parameters
LIFU.port = 'COM3'; % external power supply port
LIFU.PRF = PRF; % Hz
LIFU.Duty = DutyCycle; % DutyCycle
LIFU.TransducerName = 'custom';
LIFU.Frequency = Frequency; % Hz
LIFU.MaxVoltage = MaxVoltage; % max voltage for operation. Change it once calibration is complete.
LIFU.Voltage = SelectedVoltage; % Operating voltage on startup
LIFU.TotalTime = TotalTime;
loadedFocusFile = load(focustraversalfile);
LIFU.FocalPoints = loadedFocusFile.ListPoints*1000; % in mm
LIFU.Phases = loadedFocusFile.PhasesReprogramPerPoint; % in radians
clear loadedFocusFile;
%% Define system parameters.
Resource.Parameters.Connector = 2; % connector no 2 for LIFU
Resource.Parameters.numTransmit = 128; % number of transmit channels.
Resource.Parameters.numRcvChannels = 128; % number of receive channels.
Resource.Parameters.speedOfSound = 1540;
% Resource.Parameters.speedCorrectionFactor = 1.0;
Resource.Parameters.verbose = 3;
Resource.Parameters.initializeOnly = 0;
Resource.Parameters.simulateMode = 0;
% Resource.Parameters.simulateMode = 1 forces simulate mode, even if hardware is present.
% Resource.Parameters.simulateMode = 2 stops sequence and processes RcvData continuously.
%% Extenal power supply and TPC
Resource.HIFU.externalHifuPwr = 1;
Resource.HIFU.extPwrComPortID = LIFU.port;
Resource.HIFU.psType = 'QPX600DP'; % set to 'QPX600DP' or 'XG40-38' to match supply being used
Resource.HIFU.voltageTrackP5=5;
% TPC(5).maxHighVoltage = LIFU.MaxVoltage;
TPC(5).hv = LIFU.Voltage;
%% LIFU Parameters.
Trans.name = LIFU.TransducerName;
Trans.units = 'wavelengths';
Trans.frequency = LIFU.Frequency*1e-6; % frequency is in MHz
Trans.maxAvgPower=MaxAvgPower;
Trans.maxHighVoltage=MaxVoltage;
Trans = generateH317Trans(Trans);
scaleToWvl = Trans.frequency/(Resource.Parameters.speedOfSound/1000);
%% TW structure
TW(1).type = 'parametric';
LIFU.PD = 1e3*LIFU.Duty/LIFU.PRF;% ms
C = LIFU.PD*1e3*Trans.frequency*2; % number of half cycles
TW(1).Parameters = [Trans.frequency,0.67,C,1];
if contains(matfilename,'SingleFocus')
%% Delay Calculation
% No Delays
Delay = zeros(1,Trans.numelements);
%% Tx Structure
TX(1).waveform = 1; % use 1st TW structure.
TX(1).Origin = [0.0,0.0,0.0]; % flash transmit origin at (0,0,0).
TX(1).focus = 0; % focus distance
TX(1).Steer = [0.0,0.0]; % theta, alpha = 0.
TX(1).Apod = ones(1,Trans.numelements); % elements on
TX(1).Delay = Delay;
%% Specify SeqControl structure arrays.
else
Delay = [];
for focalPointsCounter = 1:length(LIFU.FocalPoints)
FocalPt = [LIFU.FocalPoints(focalPointsCounter,:) 135]*scaleToWvl;
X = Trans.ElementPos(:,1)'- FocalPt(1);
Y = Trans.ElementPos(:,2)' - FocalPt(2);
Z = Trans.ElementPos(:,3)' - FocalPt(3);
DelayTemp = sqrt(X.*X + Y.*Y + Z.*Z);
Delay = [Delay; max(DelayTemp) - DelayTemp];
end
for focalPointsCounter = 1:length(LIFU.FocalPoints)
TX(focalPointsCounter).waveform = 1; % use 1st TW structure.
TX(focalPointsCounter).Origin = [0.0,0.0,0.0]; % flash transmit origin at (0,0,0).
TX(focalPointsCounter).focus = 0; % focus distance
TX(focalPointsCounter).Steer = [0.0,0.0]; % theta, alpha = 0.
TX(focalPointsCounter).Apod = ones(1,Trans.numelements); % elements on
TX(focalPointsCounter).Delay = Delay(focalPointsCounter,:);
end
end
if contains(matfilename,'SingleFocus')
nSteerFactor=1;
else % We try to do complete trajectories
assert(contains(matfilename,'BroadFocus'));
nSteerFactor=length(TX);
end
LIFU.Bursts = floor(LIFU.TotalTime/ (nSteerFactor/LIFU.PRF));
nsc=1;
TRIGGEROUT = nsc;
SeqControl(TRIGGEROUT).command = 'triggerOut';
nsc = nsc + 1;
TTNEB = nsc;
TTNALIFU = 1e6/LIFU.PRF;
SeqControl(TTNEB).command = 'timeToNextEB';
SeqControl(TTNEB).argument = TTNALIFU;
nsc = nsc + 1;
SYNC = nsc;
SeqControl(SYNC).command = 'sync';
nsc = nsc + 1;
RTNMatlab = nsc;
SeqControl(RTNMatlab).command = 'returnToMatlab';
nsc = nsc + 1;
%%%%%
SELTPC=nsc;
SeqControl(SELTPC).command = 'setTPCProfile';
SeqControl(SELTPC).argument = 5;
SeqControl(SELTPC).condition = 'immediate';
nsc = nsc + 1;
NOOPPRF=nsc;
SeqControl(NOOPPRF).command = 'noop';
SeqControl(NOOPPRF).argument = (1e6/PRF-LIFU.PD*1e3)*5; %1ms in steps of 0.2 us seconds
nsc = nsc + 1;
%%% Process
np=1;
TRACKPROCESS=np;
Process(TRACKPROCESS).classname = 'External';
Process(TRACKPROCESS).method = 'trackExposure';
Process(TRACKPROCESS).Parameters = {'srcbuffer','none',... %
'dstbuffer','none'}; % no output buffer
np = np + 1;
PROCEND=np;
Process(PROCEND).classname = 'External';
Process(PROCEND).method = 'EndExposure';
Process(PROCEND).Parameters = {'srcbuffer','none',... %
'dstbuffer','none'}; % no output buffer
np=np+1;
%% Event Sequence
n = 1;
Event(n).info = 'select TPC profile 5';
Event(n).tx = 0;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = SELTPC; % set TPC profile command.
n=n+1;
Event(n).info = 'NOOP';
Event(n).tx = 0;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = NOOPPRF; %
n=n+1;
% EventPulse=n;
if contains(matfilename,'SingleFocus')
Event(n).info = 'LIFU Pulse';
Event(n).tx = 1;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = [RTNMatlab, TRIGGEROUT] ;
n = n+1;
EventPulse=n;
Event(n).info = 'select TPC profile 5';
Event(n).tx = 0;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = SELTPC; % set TPC profile command.
n=n+1;
Event(n).info = 'NOOP';
Event(n).tx = 0;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = NOOPPRF; %
n=n+1;
Event(n).info = 'LIFU Pulse';
Event(n).tx = 1;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = [RTNMatlab] ;
n = n+1;
else
for j=1:length(TX)
Event(n).info = 'LIFU Pulse';
Event(n).tx = j;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = [RTNMatlab, TRIGGEROUT];
n = n+1;
if j<length(TX)-1
Event(n).info = 'select TPC profile 5';
Event(n).tx = 0;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = SELTPC; % set TPC profile command.
n=n+1;
Event(n).info = 'NOOP';
Event(n).tx = 0;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = 0;
Event(n).seqControl = NOOPPRF; %
n=n+1;
end
end
end
% EventPulse=n;
JUMP = nsc;
SeqControl(JUMP).command = 'jump';
SeqControl(JUMP).argument = EventPulse;
nsc = nsc + 1;
Event(n).info = 'Jump back to sonication';
Event(n).tx = 0;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = TRACKPROCESS;
Event(n).seqControl = JUMP;
n=n+1;
Event(n).info = 'End of LIFU';
Event(n).tx = 0;
Event(n).rcv = 0;
Event(n).recon = 0;
Event(n).process = PROCEND;
Event(n).seqControl = RTNMatlab;
%we start VSX with LIFU disabled
Resource.Parameters.startEvent=n;
% %UI
UI(1).Control = {'UserB1','Style','VsPushButton','Label',...
'Get ready!'};
UI(1).Callback=text2cell('%StartCallback');
% %UI
UI(2).Control = {'UserB2','Style','VsPushButton','Label',...
'Sham'};
UI(2).Callback=text2cell('%ShamCallback');
%% Save Files
% Save all the structures to a .mat file.
global mainLIFUTimeLimit
global mainRunInTank
global OnlineSelVoltage
mainLIFUTimeLimit=TotalTime;
mainRunInTank=bTestInTank;
OnlineSelVoltage=SelectedVoltage;
save(filename);
return
%StartCallback
global mainLIFUStartTime
global OnlineSelVoltage
mainLIFUStartTime=-1;
pgn = get(hObject,'Value');
curStartEvent=evalin('base','Resource.Parameters.startEvent');
simulateMode=evalin('base','Resource.Parameters.simulateMode');
Control = evalin('base', 'Control');
Control.Command = 'set&Run';
Control.Parameters = {'Parameters',1,'startEvent',1};
evalin('base','Resource.Parameters.startEvent = 1;');
assignin('base','Control',Control);
if simulateMode==0
fprintf('Hardware operation: We are setting the voltage monitor function\n');
P5HVmin = OnlineSelVoltage*0.9;
P5HVmax = OnlineSelVoltage*1.05;
assignin('base', 'P5HVmax', P5HVmax);
assignin('base', 'P5HVmin', P5HVmin);
% call the function to set new limit values
rc = setProfile5VoltageLimits(P5HVmin, P5HVmax);
if ~strcmp(rc, 'Success')
fprintf(2, ['Error from setProfile5VoltageLimits: ', rc, ' \n'])
end
else
fprintf('Unable to set voltage monitor function as we are running in SW mode\n');
end
%StartCallback
%ShamCallback
global mainLIFUTimeLimit
shamLIFUStartTime=tic;
mainNextTick=shamLIFUStartTime;
fLIFUbar=waitbar(0,sprintf('Sham progress - time to complete: %2.1f',mainLIFUTimeLimit));
while toc(shamLIFUStartTime)<mainLIFUTimeLimit
curtTime=toc(shamLIFUStartTime);
s=sprintf('Sham progress - time to complete: %2.1f',mainLIFUTimeLimit-curtTime);
waitbar(curtTime/mainLIFUTimeLimit,fLIFUbar,s);
pause(1.0)
end
close(fLIFUbar);
tEnd = toc(shamLIFUStartTime) ;
fprintf("Time to run %f\n",tEnd);
%ShamCallback