source: MML/trunk/machine/SOLEIL/StorageRing/orbit/mosteffectivecorrectorgui.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 17.7 KB
Line 
1function varargout = mosteffectivecorrectorgui(varargin)
2% MOSTEFFECTIVECORRECTORGUI M-file for mosteffectivecorrectorgui.fig
3%      MOSTEFFECTIVECORRECTORGUI, by itself, creates a new MOSTEFFECTIVECORRECTORGUI or raises the existing
4%      singleton*.
5%
6%      H = MOSTEFFECTIVECORRECTORGUI returns the handle to a new MOSTEFFECTIVECORRECTORGUI or the handle to
7%      the existing singleton*.
8%
9%      MOSTEFFECTIVECORRECTORGUI('CALLBACK',hObject,eventData,handles,...) calls the local
10%      function named CALLBACK in MOSTEFFECTIVECORRECTORGUI.M with the given input arguments.
11%
12%      MOSTEFFECTIVECORRECTORGUI('Property','Value',...) creates a new MOSTEFFECTIVECORRECTORGUI or raises the
13%      existing singleton*.  Starting from the left, property value pairs are
14%      applied to the GUI before mosteffectivecorrectorgui_OpeningFunction gets called.  An
15%      unrecognized property name or invalid value makes property application
16%      stop.  All inputs are passed to mosteffectivecorrectorgui_OpeningFcn via varargin.
17%
18%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
19%      instance to run (singleton)".
20%
21% See also: GUIDE, GUIDATA, GUIHANDLES
22
23% Edit the above text to modify the response to help mosteffectivecorrectorgui
24
25% Last Modified by GUIDE v2.5 24-Jan-2008 13:03:58
26
27% Begin initialization code - DO NOT EDIT
28gui_Singleton = 1;
29gui_State = struct('gui_Name',       mfilename, ...
30    'gui_Singleton',  gui_Singleton, ...
31    'gui_OpeningFcn', @mosteffectivecorrectorgui_OpeningFcn, ...
32    'gui_OutputFcn',  @mosteffectivecorrectorgui_OutputFcn, ...
33    'gui_LayoutFcn',  [] , ...
34    'gui_Callback',   []);
35if nargin && ischar(varargin{1})
36    gui_State.gui_Callback = str2func(varargin{1});
37end
38
39if nargout
40    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
41else
42    gui_mainfcn(gui_State, varargin{:});
43end
44% End initialization code - DO NOT EDIT
45
46
47% --- Executes just before mosteffectivecorrectorgui is made visible.
48function mosteffectivecorrectorgui_OpeningFcn(hObject, eventdata, handles, varargin)
49% This function has no output args, see OutputFcn.
50% hObject    handle to figure
51% eventdata  reserved - to be defined in a future version of MATLAB
52% handles    structure with handles and user data (see GUIDATA)
53% varargin   command line arguments to mosteffectivecorrectorgui (see VARARGIN)
54
55% Choose default command line output for mosteffectivecorrectorgui
56handles.output = hObject;
57
58%
59handles.Rmat = getbpmresp('Struct');
60nBPMx = length(family2dev('BPMx'));
61nHCOR = length(family2dev('HCOR'));
62nBPMy = length(family2dev('BPMz'));
63nVCOR = length(family2dev('VCOR'));
64
65handles.Hdelcm{1} = zeros(nHCOR,1);
66handles.Vdelcm{1} = zeros(nVCOR,1);
67handles.ResidualHOrbit{1} = zeros(nBPMx,nHCOR);
68handles.ResidualVOrbit{1} = zeros(nBPMy,nVCOR);
69handles.HRefOrbit = zeros(nBPMx,nHCOR);
70handles.VRefOrbit = zeros(nBPMy,nVCOR);
71handles.HOrbit = getx;
72handles.VOrbit = getz;
73handles.correctornumber = 1;
74handles.HGoalOrbit = getgolden('BPMx');
75handles.VGoalOrbit = getgolden('BPMz');
76handles.spos = getspos('BPMx');
77handles.Hiter = 1; % Iteration num in H-plane
78handles.Viter = 1; % Iteration num in H-plane
79handles.HCORselect = 1;
80handles.HCORRemove = [];
81handles.VCORselect = 1;
82handles.VCORRemove = [];
83
84% Update handles structure
85guidata(hObject, handles);
86
87% UIWAIT makes mosteffectivecorrectorgui wait for user response (see UIRESUME)
88% uiwait(handles.figure1);
89
90
91% --- Outputs from this function are returned to the command line.
92function varargout = mosteffectivecorrectorgui_OutputFcn(hObject, eventdata, handles)
93% varargout  cell array for returning output args (see VARARGOUT);
94% hObject    handle to figure
95% eventdata  reserved - to be defined in a future version of MATLAB
96% handles    structure with handles and user data (see GUIDATA)
97
98% Get default command line output from handles structure
99varargout{1} = handles.output;
100
101
102% --- Executes on selection change in listbox_Hcorrector.
103function listbox_Hcorrector_Callback(hObject, eventdata, handles)
104% hObject    handle to listbox_Hcorrector (see GCBO)
105% eventdata  reserved - to be defined in a future version of MATLAB
106% handles    structure with handles and user data (see GUIDATA)
107
108% Hints: contents = get(hObject,'String') returns listbox_Hcorrector contents as cell array
109%        contents{get(hObject,'Value')} returns selected item from listbox_Hcorrector
110
111plotHdata(hObject, handles);
112
113% --- Executes during object creation, after setting all properties.
114function listbox_Hcorrector_CreateFcn(hObject, eventdata, handles)
115% hObject    handle to listbox_Hcorrector (see GCBO)
116% eventdata  reserved - to be defined in a future version of MATLAB
117% handles    empty - handles not created until after all CreateFcns called
118
119% Hint: listbox controls usually have a white background on Windows.
120%       See ISPC and COMPUTER.
121if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
122    set(hObject,'BackgroundColor','white');
123end
124
125
126% --- Executes on button press in pushbutton_Hfind.
127function pushbutton_Hfind_Callback(hObject, eventdata, handles)
128% hObject    handle to pushbutton_Hfind (see GCBO)
129% eventdata  reserved - to be defined in a future version of MATLAB
130% handles    structure with handles and user data (see GUIDATA)
131
132iFam = 'BPMx';
133%set(handles.listbox_Hcorrector,'Value',1);
134
135iterNum = handles.Hiter
136
137if iterNum == 1
138  %handles.HGoalOrbit = getgolden(iFam);
139  handles.HGoalOrbit = handles.HRefOrbit;
140  handles.HCORRemove = [];
141    set(handles.text_HGoldenRMS, 'String', num2str(std(handles.HGoalOrbit), '%3.2e'));
142else
143    handles.HGoalOrbit =  handles.ResidualHOrbit{iterNum-1}(:, handles.HCORselect(iterNum-1));
144end
145
146%handles.HGoalOrbitNew = handles.HGoalOrbit;
147   
148% refreshthering;
149%handles.HOrbit = getx;
150
151OrbitDiff = handles.HOrbit - handles.HGoalOrbit;
152
153% Loop over all valid correctors
154for k = 1:size(handles.Rmat(1,1).Data,2),
155    Rmat  = handles.Rmat(1,1).Data(:,k);
156    handles.Hdelcm{iterNum}(k,:) = -pinv(Rmat)*OrbitDiff; %-Rmat\OrbitDiff;
157    handles.ResidualHOrbit{iterNum}(:,k) =  handles.Rmat(1,1).Data(:,k)*handles.Hdelcm{iterNum}(k,:) ...
158        + OrbitDiff;
159end
160
161% Look for most effective corrector
162Xrms = std(handles.ResidualHOrbit{iterNum});
163[tmp ind] = sort(Xrms,'Ascend');
164
165% Remove correctors already used in previous iterations
166[tmp1 tmp2] = intersect(handles.Rmat(1,1).Actuator.DeviceList(ind,:), handles.HCORRemove,'rows');
167if ~isempty(tmp1),
168    ind(tmp2) = [];
169end
170
171fprintf('H-corrector [%2d %2d] is set to % 6.2e A or % 6.2e µT.m\n', handles.Rmat(1,1).Actuator.DeviceList(ind(1),:), ...
172    handles.Hdelcm{iterNum}(ind(1)), hw2physics('HCOR', 'Setpoint', handles.Hdelcm{iterNum}(ind(1)), [1 1])*getbrho);
173
174set(handles.text_HRMS, 'String', num2str(std(handles.ResidualHOrbit{iterNum}(:,1)), '%3.2e'));
175
176handles.HCORRemove(iterNum,:) = handles.Rmat(1,1).Actuator.DeviceList(ind(1),:);
177handles.HCORselect(iterNum) = ind(1);
178
179% Update handles structure
180guidata(hObject, handles);
181
182plotHdata(hObject, handles);
183
184% --- Executes as a subfunction
185function plotHdata(hObject, handles)
186
187iterNum = handles.Hiter;
188
189% look whether most effective corrector function already was run for this iteration
190lastIter = size(handles.HCORRemove, 1);
191if isempty(handles.HCORRemove) || lastIter < iterNum
192    if iterNum - lastIter > 1
193        set(handles.popupmenu_Hiter,'Value', lastIter +1);
194        handles.Hiter = lastIter +1;
195    end
196    pushbutton_Hfind_Callback(handles.pushbutton_Hfind , [], handles)   
197end
198
199iterNum = handles.Hiter
200
201Bind = get(handles.listbox_Hcorrector,'Value');
202Hrms = std(handles.ResidualHOrbit{iterNum});
203[tmp ind] = sort(Hrms,'Ascend');
204
205handles.HCORselect(handles.Hiter) = ind(Bind(1));
206%handles.HCORRemove(iterNum,:) = handles.Rmat(1,1).Actuator.DeviceList(ind(1),:);
207%handles.HCORRemove(iterNum,:) = handles.Rmat(1,1).Actuator.DeviceList(ind(Bind(1)),:);
208
209set(handles.text_HRMS, 'String', num2str(std(handles.ResidualHOrbit{iterNum}(:,ind(Bind(1)))), '%3.2e'));
210
211spos = handles.spos;
212
213axes(handles.axes_Horbit)
214cla
215%plot(spos, handles.HGoalOrbit ,'k-*'); hold on
216%plot(spos, handles.ResidualHOrbit{handles.Hiter}(:,ind(Bind(1)))  + handles.HGoalOrbit, 'r')
217plot(spos, handles.HOrbit - handles.HGoalOrbit ,'k-*'); hold on
218plot(spos, handles.ResidualHOrbit{handles.Hiter}(:,ind(Bind(1)))  + (handles.HOrbit - handles.HGoalOrbit), 'r')
219
220grid on;
221xlabel('s-position (m)');
222ylabel('H-orbit (mm)');
223
224% Corrector strenghts
225axes(handles.axes_Hcorrector)
226cla
227plot(handles.Hdelcm{handles.Hiter});
228hold on; plot(ind(Bind(1)), handles.Hdelcm{handles.Hiter}(ind(Bind(1))), 'k*');
229grid on;
230xlabel('H-corrector number');
231ylabel('Current (A)');
232
233%handles.Hdelcm(ind(1));
234
235% Residual orbit for selected corrector
236axes(handles.axes_Hrmsorbit)
237cla
238plot(Hrms);
239grid on;
240hold on; plot(ind(Bind(1)), Hrms(ind(Bind(1))), 'k*')
241xlabel('H-corrector number');
242ylabel('Delta Orbit rms (mm)');
243
244ListBoxString = [];
245% Remove correctors already used in previous iterations
246% [tmp1 tmp2] = intersect(handles.Rmat(1,1).Actuator.DeviceList(ind,:), handles.HCORRemove,'rows');
247% if ~isempty(tmp1),
248%     ind(tmp2) = [];
249% end
250
251for k=1:size(ind,2),
252    ListBoxString = strvcat(ListBoxString, sprintf('[%2d %2d] = %+6.2e \n', ...
253        handles.Rmat(1,1).Actuator.DeviceList(ind(k),:), handles.Hdelcm{handles.Hiter}(ind(k))));
254end
255
256set(handles.listbox_Hcorrector, 'String', ListBoxString);
257
258% Update handles structure
259guidata(hObject, handles);
260
261% --- Executes as a subfunction
262function plotVdata(handles)
263
264iterNum = handles.Viter;
265
266% look whether most effective corrector function already was run for this iteration
267lastIter = size(handles.VCORRemove, 1);
268if isempty(handles.VCORRemove) || lastIter < iterNum
269    if iterNum - lastIter > 1
270        set(handles.popupmenu_Viter,'Value', lastIter +1);
271        handles.Viter = lastIter +1;
272    end
273    pushbutton_Vfind_Callback(handles.pushbutton_Vfind , [], handles)   
274end
275
276iterNum = handles.Viter
277
278Bind = get(handles.listbox_Vcorrector,'Value');
279Vrms = std(handles.ResidualVOrbit{iterNum});
280[tmp ind] = sort(Vrms,'Ascend');
281
282handles.VCORselect(handles.Viter) = ind(Bind(1));
283% handles.VCORRemove(iterNum,:) = handles.Rmat(2,2).Actuator.DeviceList(ind(1),:);
284
285set(handles.text_VRMS, 'String', num2str(std(handles.ResidualVOrbit{iterNum}(:,ind(Bind(1)))), '%3.2e'));
286spos = handles.spos;
287
288axes(handles.axes_Vorbit)
289cla
290plot(spos, handles.VGoalOrbit ,'k-*'); hold on
291
292plot(spos, handles.ResidualVOrbit{handles.Viter}(:,ind(Bind(1))) +  handles.VGoalOrbit, 'r')
293
294grid on;
295xlabel('s-position (m)');
296ylabel('V-orbit (mm)');
297
298% Corrector strenghts
299axes(handles.axes_Vcorrector)
300cla
301plot(handles.Vdelcm{handles.Viter});
302hold on; plot(ind(Bind(1)), handles.Vdelcm{handles.Viter}(ind(Bind(1))), 'k*');
303grid on;
304xlabel('V-corrector number');
305ylabel('Current (A)');
306
307% Residual orbit for selected corrector
308axes(handles.axes_Vrmsorbit)
309cla
310plot(Vrms);
311grid on;
312hold on; plot(ind(Bind(1)), Vrms(ind(Bind(1))), 'k*')
313xlabel('V-corrector number');
314ylabel('Delta Orbit rms (mm)');
315
316ListBoxString = [];
317% Remove correctors already used in previous iterations
318% [tmp1 tmp2] = intersect(handles.Rmat(2,2).Actuator.DeviceList(ind,:), handles.VCORRemove,'rows');
319% if ~isempty(tmp1),
320%     ind(tmp2) = [];
321% end
322
323for k=1:size(ind,2),
324    ListBoxString = strvcat(ListBoxString, sprintf('[%2d %2d] = %+6.2e \n', ...
325        handles.Rmat(2,2).Actuator.DeviceList(ind(k),:), handles.Vdelcm{handles.Viter}(ind(k))));
326end
327
328set(handles.listbox_Vcorrector, 'String', ListBoxString);
329
330
331
332% --- Executes on selection change in listbox_Vcorrector.
333function listbox_Vcorrector_Callback(hObject, eventdata, handles)
334% hObject    handle to listbox_Vcorrector (see GCBO)
335% eventdata  reserved - to be defined in a future version of MATLAB
336% handles    structure with handles and user data (see GUIDATA)
337
338% Hints: contents = get(hObject,'String') returns listbox_Vcorrector contents as cell array
339%        contents{get(hObject,'Value')} returns selected item from listbox_Vcorrector
340plotVdata(handles);
341
342% --- Executes during object creation, after setting all properties.
343function listbox_Vcorrector_CreateFcn(hObject, eventdata, handles)
344% hObject    handle to listbox_Vcorrector (see GCBO)
345% eventdata  reserved - to be defined in a future version of MATLAB
346% handles    empty - handles not created until after all CreateFcns called
347
348% Hint: listbox controls usually have a white background on Windows.
349%       See ISPC and COMPUTER.
350if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
351    set(hObject,'BackgroundColor','white');
352end
353
354
355% --- Executes on button press in pushbutton_Vfind.
356function pushbutton_Vfind_Callback(hObject, eventdata, handles)
357% hObject    handle to pushbutton_Vfind (see GCBO)
358% eventdata  reserved - to be defined in a future version of MATLAB
359% handles    structure with handles and user data (see GUIDATA)
360
361iFam = 'BPMz';
362%set(handles.listbox_Hcorrector,'Value',1);
363
364iterNum = handles.Viter
365
366if iterNum == 1
367    handles.VGoalOrbit = getgolden(iFam);
368    handles.VCORRemove = [];
369    set(handles.text_VGoldenRMS, 'String', num2str(std(handles.VGoalOrbit), '%3.2e'));
370else
371    handles.VGoalOrbit =  handles.ResidualVOrbit{iterNum-1}(:, handles.VCORselect(iterNum-1));
372end
373
374handles.VOrbit = getz;
375
376OrbitDiff = handles.VOrbit - handles.VGoalOrbit;
377
378% Loop over all valid correctors
379for k = 1:size(handles.Rmat(2,2).Data,2),
380    Rmat  = handles.Rmat(2,2).Data(:,k);
381    handles.Vdelcm{iterNum}(k,:) = -Rmat \ OrbitDiff;
382    handles.ResidualVOrbit{iterNum}(:,k) =  handles.Rmat(2,2).Data(:,k)*handles.Vdelcm{iterNum}(k) ...
383    + OrbitDiff;
384end
385
386% Look for most effective corrector
387Vrms = std(handles.ResidualVOrbit{iterNum});
388[tmp ind] = sort(Vrms,'Ascend');
389
390% Remove correctors already used in previous iterations
391[tmp1 tmp2] = intersect(handles.Rmat(1,1).Actuator.DeviceList(ind,:), handles.VCORRemove,'rows');
392if ~isempty(tmp1),
393    ind(tmp2) = [];
394end
395
396fprintf('V-corrector [%2d %2d] is set to % 6.2e A or % 6.2e µT.m\n\n', handles.Rmat(2,2).Actuator.DeviceList(ind(1),:), ...
397    handles.Vdelcm{iterNum}(ind(1)), hw2physics('VCOR', 'Setpoint', handles.Vdelcm{iterNum}(ind(1)),[1 1])*getbrho);
398
399set(handles.text_VRMS, 'String', num2str(std(handles.ResidualVOrbit{iterNum}(:,1)), '%3.2e'));
400
401handles.VCORRemove(iterNum,:) = handles.Rmat(2,2).Actuator.DeviceList(ind(1),:);
402handles.VCORselect(iterNum) = ind(1);
403
404% Update handles structure
405guidata(hObject, handles);
406
407plotVdata(handles);
408
409
410% --------------------------------------------------------------------
411function uimenu_loadHreffile_Callback(hObject, eventdata, handles)
412% hObject    handle to uimenu_loadHreffile (see GCBO)
413% eventdata  reserved - to be defined in a future version of MATLAB
414% handles    structure with handles and user data (see GUIDATA)
415
416[fileName directoryName] = uigetfile('*.mat','Select H-BPM file as H-reference');
417
418set(handles.text_Hreffilename,'String', fileName);
419
420tmp = load(fileName);
421
422handles.HRefOrbit = tmp.Data1.Data;
423
424
425% Update handles structure
426guidata(hObject, handles);
427
428% --------------------------------------------------------------------
429function uimenu_loadHfile_Callback(hObject, eventdata, handles)
430% hObject    handle to uimenu_loadHfile (see GCBO)
431% eventdata  reserved - to be defined in a future version of MATLAB
432% handles    structure with handles and user data (see GUIDATA)
433[fileName directoryName] = uigetfile('*.mat','Select H-BPM file as H-reference');
434
435%set(handles.text_Hreffilename,'String', fileName);
436
437tmp = load(fileName);
438
439handles.HOrbit = tmp.Data1.Data;
440
441% Update handles structure
442guidata(hObject, handles);
443
444
445% --- Executes on selection change in popupmenu_Hiter.
446function popupmenu_Hiter_Callback(hObject, eventdata, handles)
447% hObject    handle to popupmenu_Hiter (see GCBO)
448% eventdata  reserved - to be defined in a future version of MATLAB
449% handles    structure with handles and user data (see GUIDATA)
450
451% Hints: contents = get(hObject,'String') returns popupmenu_Hiter contents as cell array
452%        contents{get(hObject,'Value')} returns selected item from popupmenu_Hiter
453
454val = get(hObject,'Value');
455
456handles.Hiter = val;
457% Update handles structure
458guidata(hObject, handles);
459
460pushbutton_Hfind_Callback(handles.pushbutton_Hfind, eventdata, handles);
461% plotHdata(hObject, handles);
462
463% --- Executes during object creation, after setting all properties.
464function popupmenu_Hiter_CreateFcn(hObject, eventdata, handles)
465% hObject    handle to popupmenu_Hiter (see GCBO)
466% eventdata  reserved - to be defined in a future version of MATLAB
467% handles    empty - handles not created until after all CreateFcns called
468
469% Hint: popupmenu controls usually have a white background on Windows.
470%       See ISPC and COMPUTER.
471if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
472    set(hObject,'BackgroundColor','white');
473end
474
475
476% --- Executes on selection change in popupmenu_Viter.
477function popupmenu_Viter_Callback(hObject, eventdata, handles)
478% hObject    handle to popupmenu_Viter (see GCBO)
479% eventdata  reserved - to be defined in a future version of MATLAB
480% handles    structure with handles and user data (see GUIDATA)
481
482% Hints: contents = get(hObject,'String') returns popupmenu_Viter contents as cell array
483%        contents{get(hObject,'Value')} returns selected item from popupmenu_Viter
484
485val = get(hObject,'Value');
486
487handles.Viter = val;
488% Update handles structure
489guidata(hObject, handles);
490
491pushbutton_Vfind_Callback(handles.pushbutton_Vfind, eventdata, handles);
492% plotHdata(hObject, handles);
493
494% --- Executes during object creation, after setting all properties.
495function popupmenu_Viter_CreateFcn(hObject, eventdata, handles)
496% hObject    handle to popupmenu_Viter (see GCBO)
497% eventdata  reserved - to be defined in a future version of MATLAB
498% handles    empty - handles not created until after all CreateFcns called
499
500% Hint: popupmenu controls usually have a white background on Windows.
501%       See ISPC and COMPUTER.
502if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
503    set(hObject,'BackgroundColor','white');
504end
505
506
Note: See TracBrowser for help on using the repository browser.