source: MML/trunk/machine/SOLEIL/StorageRing/tune/tuneFBgui.m @ 17

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

To have a stable version on the server.

  • Property svn:executable set to *
File size: 35.5 KB
Line 
1function tuneFBgui(action, Input2, Input3)
2%tuneFBgui - GUI for tune feedback
3%
4%  INPUTS
5%  None to launch the programme
6%  1. action - Callback to execute
7%  2. Input2 - First argument for callback function
8%  3. Input3  First argument for callback function
9%
10%  OUPUTS
11%  None
12%
13
14% For the compiler
15%#function gettune
16%#function solamor2linb
17%#function solamor2linc
18%#function lat_2020_3170f
19%#function lat_2020_3170e
20%#function lat_2020_3170b
21%#function lat_2020_3170a
22%#function lat_pseudo_nanoscopium_juin2011_122BPM
23%#function lat_nano_176_234_122BPM
24
25%
26%  Written by Laurent S. Nadolski
27
28% Check if the AO exists
29checkforao;
30
31DEBUGFLAG = 0;
32
33% Dedicated dserver Device
34devSpeakerName = getfamilydata('TANGO', 'TEXTTALKERS');
35
36devLockName    = 'ANS/CA/SERVICE-LOCKER';
37
38Mode = 'Online';
39%Mode = 'Model';
40
41% Arguments
42if nargin < 1
43    action = 'Initialize';
44end
45
46if nargin < 2
47    Input2 = 0;
48end
49
50if nargin < 3
51    Input3 = 0;
52end
53
54%
55
56% Minimum stored current to allow correction
57% Default values
58DCCTMIN = read_fbnu_property('CurrentThreshold');
59%DCCTMIN = 50; % mA multibunch
60%DCCTMIN = 7; % mA single bunch
61
62%%%%%%%%%%%%%%%%
63%% Main Program
64%%%%%%%%%%%%%%%%
65
66switch(action)
67
68
69    %% Initialize
70    case 'Initialize'
71
72        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73        % GUI  CONSTRUCTION
74        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75        ButtonWidth  = 200;
76        ButtonHeight = 25;
77
78        Frame0height = 2*(ButtonHeight) + 10; % Close button. DO NOT MODIFY
79        Frame1height = 12*(ButtonHeight) + 10 ; % Tune Feedback frame
80        Frame2height = 4*(ButtonHeight) + 10 ; % Status frame
81        FrameWidth   = ButtonWidth+6;
82        FrameLeft    = 0.025*FrameWidth;
83        LeftMargin   = 3*FrameLeft;
84
85        Frametop  = Frame0height + Frame1height + Frame2height + 3*8;
86        Frame1top = Frametop - 8;
87        Frame2top = Frametop - Frame1height - 2*8;
88        Frame0top = Frametop - Frame1height - Frame2height - 3*8;
89
90        FigWidth    = 1.15*FrameWidth;                % Figure width
91        FigHeight   = Frametop+0.2*ButtonHeight+6;% Figure heigth
92
93        % Change figure position
94        set(0,'Units','pixels');
95        p = get(0,'screensize');
96
97        orbfig = findobj(allchild(0),'tag','TUNEFBGUIFig1');
98
99        if ~isempty(orbfig)
100            return; % IHM already exists
101        end
102
103        h0 = figure( ...
104            'Color',[0.1 0.1 1], ...
105            'HandleVisibility','Off', ...
106            'Interruptible', 'on', ...
107            'MenuBar','none', ...
108            'Name','TUNE FB CONTROL', ...
109            'NumberTitle','off', ...
110            'Units','pixels', ...
111            'Position',[30 p(4)-FigHeight-40 FigWidth FigHeight], ...
112            'Resize','off', ...
113            'HitTest','off', ...
114            'IntegerHandle', 'off', ...
115            'Tag','TUNEFBGUIFig1');
116
117        %% Frame Box I Tune Feedback frame
118        uicontrol('Parent',h0, ...
119            'BackgroundColor',[0.8 0.8 0.8], ...
120            'ListboxTop',0, ...
121            'Position',[FrameLeft Frame2top 1.1*FrameWidth Frame1height], ...
122            'Style','frame');
123
124        uicontrol('Parent',h0, ...
125            'BackgroundColor',[0.8 0.8 0.8], ...
126            'FontSize',10, ...
127            'FontWeight', 'Bold', ...
128            'ListboxTop',0, ...
129            'Position',[LeftMargin 3 + Frame1top - 1*(ButtonHeight+3) FrameWidth .6*ButtonHeight], ...
130            'String','TUNE Feedback', ...
131            'Style','text');
132
133        uicontrol('Parent',h0, ...
134            'BackgroundColor',[0.8 0.8 0.8], ...
135            'Enable','on', ...
136            'Interruptible', 'on', ...
137            'Position',[LeftMargin 3 + Frame1top - 2*(ButtonHeight+3) 0.5*ButtonWidth-32 .8*ButtonHeight], ...
138            'String','H-plane', ...
139            'Style','checkbox', ...
140            'Value',1,...
141            'Tag','TUNEFBGUICheckboxHcorrection');
142
143        uicontrol('Parent',h0, ...
144            'BackgroundColor',[0.8 0.8 0.8], ...
145            'Enable','on', ...
146            'Interruptible', 'on', ...
147            'Position',[LeftMargin + 0.55*ButtonWidth 3 + Frame1top - 2*(ButtonHeight+3) 0.5*ButtonWidth-32 .8*ButtonHeight], ...
148            'String','V-plane', ...
149            'Style','checkbox', ...
150            'Value',1,...
151            'Tag','TUNEFBGUICheckboxVcorrection');
152
153        uicontrol('Parent',h0, ...
154            'BackgroundColor',[0.8 0.8 0.8], ...
155            'Enable','on', ...
156            'FontSize',10, ...
157            'FontWeight', 'Bold', ...
158            'ListboxTop',0, ...
159            'Position',[LeftMargin  3 + Frame1top - 5*(ButtonHeight+3) FrameWidth ButtonHeight], ...
160            'String','Golden Tunes', ...
161            'Style','Text', ...
162            'Value',0,...
163            'Tag','TUNEFBGUIGoldenTune');
164
165
166        uicontrol('Parent',h0, ...
167            'Callback','tuneFBgui(''StartTuneFB'');', ...
168            'BackgroundColor',[0.8 0.8 0.8], ...
169            'Enable','on', ...
170            'FontSize',10, ...
171            'FontWeight', 'Bold', ...
172            'ListboxTop',0, ...
173            'Position',[LeftMargin 3 + Frame1top - 4*(ButtonHeight+3) 0.5*FrameWidth ButtonHeight], ...
174            'String','Start TUNE FB', ...
175            'Style','PushButton', ...
176            'Value',0,...
177            'Tag','TUNEFBGUIStartTUNEFB');
178
179        uicontrol('Parent',h0, ...
180            'Callback','tuneFBgui(''StopTuneFB'');', ...
181            'BackgroundColor',[0.8 0.8 0.8], ...
182            'Enable','on', ...
183            'FontSize',10, ...
184            'FontWeight', 'Bold', ...
185            'ListboxTop',0, ...
186            'Position',[LeftMargin + 0.55*ButtonWidth  3 + Frame1top - 4*(ButtonHeight+3) 0.5*FrameWidth ButtonHeight], ...
187            'String','Stop TUNE FB', ...
188            'Style','PushButton', ...
189            'Value',0,...
190            'Tag','TUNEFBGUIStopTUNEFB');
191
192        uicontrol('Parent',h0, ...
193            'BackgroundColor',[0.5 0.5 1], ...
194            'Enable','on', ...
195            'FontSize',10, ...
196            'FontWeight', 'Bold', ...
197            'ListboxTop',0, ...
198            'Position',[LeftMargin  3 + Frame1top - 6*(ButtonHeight+3) FrameWidth ButtonHeight], ...
199            'String','Configuration settings', ...
200            'Style','Text', ...
201            'Value',0,...
202            'Tag','TUNEFBGUISettings');
203
204        uicontrol('Parent',h0, ...
205            'BackgroundColor',[0.5 0.5 1], ...
206            'Enable','on', ...
207            'FontSize',10, ...
208            'ListboxTop',0, ...
209            'Position',[LeftMargin  3 + Frame1top - 7*(ButtonHeight+3) FrameWidth ButtonHeight], ...
210            'String','Min Dnux', ...
211            'Style','Text', ...
212            'Value',0,...
213            'Tag','TUNEFBGUIMinDnu');
214
215        uicontrol('Parent',h0, ...
216            'BackgroundColor',[0.5 0.5 1], ...
217            'Enable','on', ...
218            'FontSize',10, ...
219            'ListboxTop',0, ...
220            'Position',[LeftMargin  3 + Frame1top - 8*(ButtonHeight+3) FrameWidth ButtonHeight], ...
221            'String','Max Dnux', ...
222            'Style','Text', ...
223            'Value',0,...
224            'Tag','TUNEFBGUIMaxDnu');
225
226        uicontrol('Parent',h0, ...
227            'BackgroundColor',[0.5 0.5 1], ...
228            'Enable','on', ...
229            'FontSize',10, ...
230            'ListboxTop',0, ...
231            'Position',[LeftMargin  3 + Frame1top - 9*(ButtonHeight+3) FrameWidth ButtonHeight], ...
232            'String','tuneFB.LoopDelay', ...
233            'Style','Text', ...
234            'Value',0,...
235            'Tag','TUNEFBGUItuneFB.LoopDelay');
236
237        uicontrol('Parent',h0, ...
238            'BackgroundColor',[0.5 0.5 1], ...
239            'Enable','on', ...
240            'FontSize',10, ...
241            'ListboxTop',0, ...
242            'Position',[LeftMargin  3 + Frame1top - 10*(ButtonHeight+3) FrameWidth ButtonHeight], ...
243            'String','tuneFB.factor', ...
244            'Style','Text', ...
245            'Value',0,...
246            'Tag','TUNEFBGUItuneFB.factor');
247
248        uicontrol('Parent',h0, ...
249            'Callback','tuneFBgui(''CurrentThresholdSetting'');', ...
250            'BackgroundColor',[0.8 0.8 0.8], ...
251            'ForegroundColor',[1 0 0], ...
252            'Enable','on', ...
253            'FontSize',10, ...
254            'ListboxTop',0, ...
255            'Position',[LeftMargin  3 + Frame1top - 11*(ButtonHeight+3) FrameWidth ButtonHeight], ...
256            'String','Current threshold setting', ...
257            'Style','PushButton', ...
258            'Value',0,...
259            'Tag','TUNEFBGUICurrentThresholdSetting');
260
261        uicontrol('Parent',h0, ...
262            'CreateFcn','tuneFBgui(''OrbitSetupTuneFB'',1);', ...
263            'Callback','tuneFBgui(''OrbitSetupTuneFB'');', ...
264            'BackgroundColor',[0.8 0.8 0.8], ...
265            'FontSize',10, ...
266            'FontWeight', 'Bold', ...
267            'ListboxTop',0, ...
268            'Position',[LeftMargin 3 + Frame1top - 3*(ButtonHeight+3) FrameWidth ButtonHeight], ...
269            'String','Edit FB configuration', ...
270            'Style','PushButton', ...
271            'Value',0,...
272            'Tag','TUNEFBGUIButtonTuneFBSetup');
273
274        %% Frame Box Status
275        uicontrol('Parent',h0, ...
276            'BackgroundColor',[0.8 0.8 0.8], ...
277            'ListboxTop',0, ...
278            'Position',[FrameLeft Frame0top  1.1*FrameWidth Frame2height], ...
279            'Style','frame');
280
281        uicontrol('Parent',h0, ...
282            'BackgroundColor',[0.8 0.8 0.8], ...
283            'HorizontalAlignment','center', ...
284            'FontWeight', 'Bold', ...
285            'ListboxTop',0, ...
286            'Position',[LeftMargin Frame2top - 1*(ButtonHeight+3) ButtonWidth .7*ButtonHeight], ...
287            'String','TUNEFB status ', ...
288            'Style','text');
289
290        uicontrol('Parent',h0, ...
291            'BackgroundColor',[0.8 0.8 0.8], ...
292            'HorizontalAlignment','center', ...
293            'ListboxTop',0, ...
294            'Position',[LeftMargin Frame2top - 2*(ButtonHeight+3) 0.6*ButtonWidth .7*ButtonHeight], ...
295            'String','To get Status Push button: ', ...
296            'Style','text', ...
297            'Tag','TUNEFBGUIStaticTextHeader');
298
299        uicontrol('Parent',h0, ...
300            'callback','tuneFBgui(''UpdateStatusTuneFB'');pause(0);', ...
301            'BackgroundColor',[0.8 0.8 0.8], ...
302            'HorizontalAlignment','center', ...
303            'ListboxTop',0, ...
304            'Position',[6 + 0.7*ButtonWidth Frame2top - 2.1*ButtonHeight 0.3*ButtonWidth .7*ButtonHeight], ...
305            'String','Update', ...
306            'Style','PushButton', ...
307            'Tag','TUNEFBGUIUpdateStatusTuneFB');
308
309        h1= uicontrol('Parent',h0, ...
310            'BackgroundColor',[1 1 1], ...
311            'ForegroundColor','b', ...
312            'ListboxTop',0, ...
313            'Position',[LeftMargin+0.1*ButtonWidth Frame2top - 3*ButtonHeight 0.25*ButtonWidth .7*ButtonHeight], ...
314            'String','H-plane', ...
315            'Style','text', ...
316            'Tag','TUNEFBGUIisTunexFBRunning');
317
318        h2 = uicontrol('Parent',h0, ...
319            'BackgroundColor',[1 1 1], ...
320            'ForegroundColor','b', ...
321            'ListboxTop',0, ...
322            'Position',[LeftMargin+0.4*ButtonWidth Frame2top - 3*ButtonHeight 0.25*ButtonWidth .7*ButtonHeight], ...
323            'String','V-plane', ...
324            'Style','text', ...
325            'Tag','TUNEFBGUIisTunezFBRunning');
326
327        uicontrol('Parent',h0, ...
328            'BackgroundColor',[1 1 0], ...
329            'ForegroundColor','b', ...
330            'ListboxTop',0, ...
331            'Position',[LeftMargin Frame2top - 4*ButtonHeight ButtonWidth .7*ButtonHeight], ...
332            'String','current', ...
333            'Style','text', ...
334            'Tag','TUNEFBGUIStatus');
335
336        %% Frame Box "Close"
337        uicontrol('Parent',h0, ...
338            'BackgroundColor',[0.8 0.8 0.8], ...
339            'ListboxTop',0, ...
340            'Position',[FrameLeft 8 1.1*FrameWidth 0.7*Frame0height], ...
341            'Style','frame');
342
343        uicontrol('Parent',h0, ...
344            'Callback', 'close(gcbf);', ...
345            'Enable','On', ...
346            'Interruptible','Off', ...
347            'Position',[LeftMargin 13 ButtonWidth ButtonHeight], ...
348            'String','Close', ...
349            'Tag','TUNEFBGUIClose');
350
351        % Read planes for FB correction in device server
352
353        try
354            isTunexFBRunning = readattribute([devLockName, '/istunexfbrunning']);
355            isTunezFBRunning = readattribute([devLockName, '/istunezfbrunning']);
356        catch
357            warndlg(sprintf('Error with dserveur %s', devLockName));
358        end
359
360        set(h1, 'Value',isTunexFBRunning);
361        set(h2, 'value',isTunezFBRunning);
362
363
364        if isTunexFBRunning || isTunezFBRunning
365            % Disable buttons in GUI
366            set(0,'showhiddenhandles','on');
367            set(findobj(gcf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Enable','on');
368            set(findobj(gcf,'Tag','TUNEFBGUIClose'),'Enable','on');
369            set(findobj(gcf,'Tag','TUNEFBGUICheckboxHcorrection'),'Enable','off');
370            set(findobj(gcf,'Tag','TUNEFBGUICheckboxVcorrection'),'Enable','off');
371            pause(0);
372        end
373
374        % init FB structure
375        pause(0.5)
376        tuneFBgui('UpdateStatusTuneFB');               
377
378        %tuneFBgui('ShowFBConfig');
379
380    %% CurrentThresholdSetting
381    case 'CurrentThresholdSetting'
382     tuneFB = get(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata');
383     answer = inputdlg({'New Current Threshold'}, 'DIALOG BOX',1, {num2str(tuneFB.DCCTMIN)});
384     if isempty(answer)
385         return
386     end
387     tuneFB.DCCTMIN = str2num(answer{1});
388     set(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata', tuneFB);
389     % UPDATE TANGO free property   
390     write_fbnu_property('CurrentThreshold',num2str(tuneFB.DCCTMIN));
391     
392     strdata = sprintf('Corr. Percent.: %d %%  Min current: %d mA', tuneFB.fact*100, tuneFB.DCCTMIN);
393     set(findobj(gcbf,'Tag','TUNEFBGUItuneFB.factor'),'String', strdata);
394
395
396    %%  UpdateStatusTuneFB
397    case 'UpdateStatusTuneFB'
398        try
399            isTunexFBRunning = readattribute([devLockName, '/istunexfbrunning']);
400            isTunezFBRunning = readattribute([devLockName, '/istunezfbrunning']);
401
402            if isempty(gcbf)
403                mainFig = findall(0, 'Tag', 'TUNEFBGUIFig1');
404            else
405                mainFig = gcbf;
406            end
407
408            if isTunexFBRunning
409                set(findobj(mainFig,'Tag','TUNEFBGUIisTunexFBRunning'),'BackGroundColor', [0 1 0]);
410            else
411                set(findobj(mainFig,'Tag','TUNEFBGUIisTunexFBRunning'),'BackGroundColor', [1 1 1]);
412            end
413
414            if isTunezFBRunning
415                set(findobj(mainFig,'Tag','TUNEFBGUIisTunezFBRunning'),'BackGroundColor', [0 1 0]);
416            else
417                set(findobj(mainFig,'Tag','TUNEFBGUIisTunezFBRunning'),'BackGroundColor', [1 1 1]);
418            end
419
420            set(findobj(mainFig,'Tag','TUNEFBGUIStaticTextHeader'),'String', datestr(clock));
421
422            sdata = sprintf('Stored current is %3.1f mA', getdcct(Mode));
423            set(findobj(mainFig,'Tag','TUNEFBGUIStatus'),'String', sdata);
424        catch
425            dbstack
426            fprintf('\n  %s \n',lasterr);
427            fprintf('Error in UpdateStatusTuneFB\n') ;
428        end
429
430        %% OrbitSetupTuneFB
431    case 'OrbitSetupTuneFB'
432        % NOTES setting for FB
433
434        InitFlag = Input2;  % Input #2: if InitFlag, then initialize variables
435
436        if InitFlag % just at startup
437            % Setup orbit correction elements : DEFAULT configuration
438            tune0 = getgolden('TUNE',[1 1;1 2]);
439            %tune0 = [0.2020; 0.3165];
440            if any(tune0==0) % if bad golden
441                tuneFB.Golden.tune(1) = 0.2020;
442                tuneFB.Golden.tune(2) = 0.3100;
443            else % otherwise take by default golden value
444                tuneFB.Golden.tune = tune0;
445            end
446
447            % configuration of TUNEFB
448
449            % minimum tune variation for correction
450            tuneFB.minDeltaTuneX = 5e-4;
451            tuneFB.minDeltaTuneZ = 6e-4;
452
453            % maximum tune variation
454            tuneFB.maxDeltaTuneX = 1e-2;
455            tuneFB.maxDeltaTuneZ = 1e-2;
456
457            % Feedback loop setup
458            tuneFB.LoopDelay = 5.0;    % Period of Feedback loop [seconds], make sure the BPM averaging is correct
459
460            % Maximum allowed tune variation during 3 seconds
461            tuneFB.TuneErrorMax = 5e-3;
462
463            % tuneFB.factor to apply for the correction 1 means full correction
464            tuneFB.fact = 0.3;
465
466            % Current trheshold for starting the feedback
467            tuneFB.DCCTMIN = DCCTMIN;         
468
469        else % For orbit correction Configuration
470            tuneFB = get(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata');
471            answer = inputdlg({'Golden fractional tunex', 'Golden fractional tunez '},'DIALOG BOX',1,{num2str(tuneFB.Golden.tune(1)), num2str(tuneFB.Golden.tune(2))});
472            if isempty(answer)
473                return
474            end
475            tunex = str2num(answer{1});
476            tunez = str2num(answer{2});
477
478            if tunex > 0 && tunex < 0.5 && tunez > 0 && tunez < 0.5
479                tuneFB.Golden.tune(1) = tunex;
480                tuneFB.Golden.tune(2) = tunez;
481            else
482                warndlg('Wrong values: 0 < tune < 0.5')
483                return;
484            end
485        end
486
487       
488        set(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata', tuneFB);
489
490        strdata = sprintf('tunex=%5.4f          tunez=%5.4f',tuneFB.Golden.tune);
491        set(findobj(gcbf,'Tag','TUNEFBGUIGoldenTune'),'String', strdata);
492
493        tuneFB = get(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata');
494        strdata = sprintf('Min DnuX=%5.1e    DnuZ=%5.1e', tuneFB.minDeltaTuneX, tuneFB.minDeltaTuneZ);
495        set(findobj(gcbf,'Tag','TUNEFBGUIMinDnu'),'String', strdata);
496
497
498        strdata = sprintf('Max DnuX=%5.1e    DnuZ=%5.1e', tuneFB.maxDeltaTuneX, tuneFB.maxDeltaTuneZ);
499        set(findobj(gcbf,'Tag','TUNEFBGUIMaxDnu'),'String', strdata);
500
501
502        strdata = sprintf('LoopDelay=%d s     TuneError=%5.1e', tuneFB.LoopDelay, tuneFB.TuneErrorMax);
503        set(findobj(gcbf,'Tag','TUNEFBGUItuneFB.LoopDelay'),'String', strdata);
504
505
506        strdata = sprintf('Corr. Percent.: %d %%  Min current: %d mA', tuneFB.fact*100, tuneFB.DCCTMIN);
507        set(findobj(gcbf,'Tag','TUNEFBGUItuneFB.factor'),'String', strdata);
508
509
510        %% StartTuneFB
511    case 'StartTuneFB'
512
513        % Check if not already running
514        check4tunefb(devLockName);
515
516
517        % Check if plane(s) to correct is/are selected
518        if ~get(findobj(gcbf,'Tag','TUNEFBGUICheckboxHcorrection'),'Value')  && ...
519                ~get(findobj(gcbf,'Tag','TUNEFBGUICheckboxVcorrection'),'Value')
520            warndlg('TuneFBgui:StartTuneFB: No plane selected, action aborted')
521            fprintf('%s\n TuneFBgui:StartTuneFB: No plane selected, action aborted\n', datestr(clock))
522            return;
523        end
524
525
526        % Confirmation dialogbox
527        StartFlag = questdlg('Start orbit TUNE Feedback?', 'TUNE Feedback','Yes','No','No');
528
529        if strcmp(StartFlag,'No')
530            fprintf('   %s \n', datestr(clock));
531            fprintf('   ***************************\n');
532            fprintf('   **  TUNE  Feedback Exit  **\n');
533            fprintf('   ***************************\n\n');
534            pause(0);
535            return
536        end
537
538        % Disable buttons in GUI
539        set(0,'showhiddenhandles','on');
540        set(findobj(gcf,'Tag','TUNEFBGUIButtonOrbitCorrectionSetupTuneFB'),'Enable','on');
541        set(findobj(gcf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Enable','off');
542        set(findobj(gcf,'Tag','TUNEFBGUIStartTUNEFB'),'Enable','off');
543        set(findobj(gcf,'Tag','TUNEFBGUIStopTUNEFB'),'Enable','on');
544        set(findobj(gcf,'Tag','TUNEFBGUICheckboxHcorrection'),'Enable','off');
545        set(findobj(gcf,'Tag','TUNEFBGUICheckboxVcorrection'),'Enable','off');
546        set(findobj(gcf,'Tag','TUNEFBGUIClose'),'Enable','off');
547        pause(0);
548
549        set(findobj(gcbf,'Tag','TUNEFBGUIStatus'),'BackgroundColor', [0 1 0])
550        tuneFBgui('UpdateStatusTuneFB');
551
552        tuneFB = get(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata');
553        % Lock service
554        if get(findobj(gcbf,'Tag','TUNEFBGUICheckboxHcorrection'),'Value')
555            tuneFB.XLockTag  = tango_command_inout2(devLockName,'Lock', 'istunexfbrunning');
556        end
557        if get(findobj(gcbf,'Tag','TUNEFBGUICheckboxVcorrection'),'Value')
558            tuneFB.ZLockTag  = tango_command_inout2(devLockName,'Lock', 'istunezfbrunning');
559        end
560
561        set(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata', tuneFB);
562        tuneFBgui('UpdateStatusTuneFB');
563
564        TUNE_FEEDBACK_STOP_FLAG = 0;
565        setappdata(findobj(gcbf,'Tag','TUNEFBGUIFig1'),'TUNE_FEEDBACK_STOP_FLAG', TUNE_FEEDBACK_STOP_FLAG)
566
567        % Number of error before stopping
568        stallError = 0;
569        stallErrorMax = 50; % Maximum error permissible
570        % Init
571        tuneFB = get(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata');
572       
573        tune_old  = tuneFB.Golden.tune;
574        tune_old2 = tuneFB.Golden.tune;
575       
576        while TUNE_FEEDBACK_STOP_FLAG == 0 % infinite loop
577            try
578                t00 = gettime;
579                fprintf('Iteration time %s\n',datestr(clock));
580
581                % Check if GUI has been closed
582                if isempty(gcbf)
583                    TUNE_FEEDBACK_STOP_FLAG = 1;
584                    lasterr('TUNE GUI DISAPPEARED!');
585                    error('TUNE GUI DISAPPEARED!');
586                end
587
588                tuneFB = get(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata');
589               
590                %% main loop
591                % read tune from FBT
592
593                if getdcct(Mode) < tuneFB.DCCTMIN     % Don't Feedback if the current is too small
594                    TUNE_FEEDBACK_STOP_FLAG = 1;
595                    fprintf('%s         TUNE Feedback stopped due to low beam current (<%d mA)\n',datestr(now), tuneFB.DCCTMIN);
596                    strgMessage = 'Fideubaque des nombres d''ondes arraitai car le courant est trop bas';
597                    tango_giveInformationMessage(devSpeakerName,  strgMessage);
598                    warndlg('TuneFB : arrété car courant trop bas');
599                    tuneFBgui('StopTuneFB');
600                    break;
601                else
602                    tune  = gettuneFBT;
603                end
604
605                % double reading for getting of parasite peaks
606                pause(1);
607
608                tune2  = gettuneFBT;
609
610                ErrorTune = tune2-tune;
611                if max(abs(ErrorTune)) > tuneFB.TuneErrorMax % redo measurement
612                    fprintf('StallError #%d\n', stallError);
613                    stallError = stallError + 1;
614                    pause(1);
615                    tune3  = gettuneFBT;
616                    ErrorTune = tune3-tune2;
617                    if max(abs(ErrorTune)) > tuneFB.TuneErrorMax %stop FB
618                        TUNE_FEEDBACK_STOP_FLAG = 1;
619                        fprintf('%s         TUNE Feedback stopped due tune oscillation\n',datestr(now));
620                        strgMessage = 'Fideubaque des nombres d''ondes arraitai car oscillations des nombres d''ondes';
621                        tango_giveInformationMessage(devSpeakerName,  strgMessage);
622                        str1 = sprintf('TuneFB : arrété car oscillations des nombres d''ondes durant 3 s (max=%5.4f)\n', tuneFB.TuneErrorMax);
623                        str2 = sprintf('tunex(1) = %5.4f tunex(2) = %5.4f tunex(3) = %5.4f\n', ...
624                            tune(1), tune2(1), tune3(1));
625                        str3 = sprintf('tunez(1) = %5.4f tunez(2) = %5.4f tunez(3) = %5.4f\n', ...
626                            tune(2), tune2(2), tune3(2));
627                        warndlg([str1 str2 str3]);
628                        fprintf([str1 str2 str3]);
629                        tuneFBgui('StopTuneFB');
630                        pause(1)
631                        break;
632                    else
633                        tune = tune3;
634                    end
635                end                     
636               
637                if stallError > stallErrorMax     % Don't Feedback if the current is too small
638                    TUNE_FEEDBACK_STOP_FLAG = 1;
639                    fprintf('%s         TUNE Feedback stopped due stallError Max reached on tunes\n',datestr(now));
640                    strgMessage = 'Fideubaque des nombres d''ondes arraitai car trop d''erreurs';
641                    tango_giveInformationMessage(devSpeakerName,  strgMessage);
642                    warndlg(sprintf('TuneFB : trop d''erreurs, %d', stallErrorMax));
643                    tuneFBgui('StopTuneFB');
644                    pause(1)
645                    break;
646                end
647
648                % Further more checking on tune values
649
650                if any(tune==0) || any(isnan(tune))
651                    str1 = sprintf('TuneFB:StartTuneFB: something is wrong with the tunes reading\n');
652                    str2 = sprintf('TuneFB:StartTuneFB: FB stopped at %s\n', datestr(clock));
653                    fprintf([str1 str2]);
654                    warndlg([str1 str2]);
655                    strgMessage = 'Arret du fiideubaque des nombres d''ondes : problaime de mesure';
656                    tango_giveInformationMessage(devSpeakerName,  strgMessage);
657                    tuneFBgui('StopTuneFB');
658                    break               
659                else
660                    deltaTune = tune - tuneFB.Golden.tune;
661                end
662
663
664                % Zeroing tunex if not selected for correction
665                if ~get(findobj(gcbf,'Tag','TUNEFBGUICheckboxHcorrection'),'Value')
666                    deltaTune(1) =0;
667                end
668
669                % Zeroing tunez if not selected for correction
670                if ~get(findobj(gcbf,'Tag','TUNEFBGUICheckboxVcorrection'),'Value')
671                    deltaTune(2) =0;
672                end
673
674                if abs(deltaTune(1)) > tuneFB.maxDeltaTuneX && get(findobj(gcbf,'Tag','TUNEFBGUICheckboxHcorrection'),'Value')
675                    TUNE_FEEDBACK_STOP_FLAG = 1;
676                    fprintf('TuneFB:StartTuneFB:%s         TUNE Feedback stopped due to too large tune variations\n',datestr(now));
677                    str1 = sprintf('deltaNux = %5.4f (max: %5.4f) \ndeltaNuz = %5.4f (max: %5.4f) \n', ...
678                        deltaTune(1), tuneFB.maxDeltaTuneX, deltaTune(2), tuneFB.maxDeltaTuneZ);
679                    fprintf(str1);
680                    str2 = sprintf('TuneFB:StartTuneFB: arret Feedback \n car variations des nombres d''ondes trop importantes\n ');
681                    strgMessage = 'Arret du fiideubaque des nombres d''ondes : variations trop grandes';
682                    tango_giveInformationMessage(devSpeakerName,  strgMessage);
683                    warndlg([str2, str1]);
684                    tuneFBgui('StopTuneFB');
685                    break;
686                elseif abs(deltaTune(2)) > tuneFB.maxDeltaTuneZ && get(findobj(gcbf,'Tag','TUNEFBGUICheckboxVcorrection'),'Value')
687                    TUNE_FEEDBACK_STOP_FLAG = 1;
688                    fprintf('TuneFB:StartTuneFB:%s         TUNE Feedback stopped due to too large tune variations\n',datestr(now));
689                    str1 = sprintf('deltaNux = %5.4f (max: %5.4f) \ndeltaNuz = %5.4f (max: %5.4f) \n', ...
690                        deltaTune(1), tuneFB.maxDeltaTuneX, deltaTune(2), tuneFB.maxDeltaTuneZ);
691                    fprintf(str1);
692                    str2 = sprintf('TuneFB:StartTuneFB: arret Feedback \n car variations des nombres d''ondes trop importantes\n ');
693                    tango_command_inout2(devSpeakerName,'DevTalk','Arret du fiideubaque des nombres d''ondes : variations trop grandes');
694                    warndlg([str2, str1]);
695                    tuneFBgui('StopTuneFB');
696                    break;
697                elseif getdcct(Mode) < tuneFB.DCCTMIN     % Don't Feedback if the current is too small
698                    TUNE_FEEDBACK_STOP_FLAG = 1;
699                    fprintf('%s         TUNE Feedback stopped due to low beam current (<%d mA)\n',datestr(now), tuneFB.DCCTMIN);
700                    strgMessage = 'Arret du fiideubaque des nombres d''ondes : courant trop bas';
701                    tango_giveInformationMessage(devSpeakerName,  strgMessage);
702                    warndlg('TuneFB;StartTuneFB: arret Feedback car courant trop bas');
703                    tuneFBgui('StopTuneFB');
704                    break;
705                else
706                    % Correction tune using Q7 and Q9
707                    if abs(deltaTune(1)) < tuneFB.minDeltaTuneX && abs(deltaTune(2)) < tuneFB.minDeltaTuneZ
708                        fprintf('Skip correction Dnux = %5.4e < %5.4e, Dnuz = %5.4e < %5.4e\n', deltaTune(1), ...
709                            tuneFB.minDeltaTuneX, deltaTune(2), tuneFB.minDeltaTuneZ)
710                    else
711                        if DEBUGFLAG
712                            QuadVal = steptune(-deltaTune*tuneFB.fact,Mode,'NoSp');
713                            fprintf('Correction Dnux=%5.4f Dnuz=%5.4f\n', deltaTune*tuneFB.fact)
714                            fprintf('Quadrupole changes: Q7=%3.2e A, Q9=%3.2e A\n', QuadVal);
715                        end                       
716                        % Check if tune reading is frozen then stop FB
717                        if any(tune - tune_old2 == 0)
718                            if tune(1) == tune_old2(1)
719                                str1 = sprintf('TuneFB:StartTuneFB: nux does not change\n');
720                            end
721                            if tune(2) == tune_old2(2)
722                                str1 = sprintf('TuneFB:StartTuneFB: nuz does not change\n');
723                            end
724                            str2 = sprintf('TuneFB:StartTuneFB: FB stopped at %s\n', datestr(clock));
725                            fprintf([str1 str2]);
726                            fprintf('nux = %f nux_old = %f nuz = %f nuz_old = %f \n', tune(1), tune_old2(1), ...
727                                tune(2), tune_old2(2));
728                            warndlg([str1 str2]);
729                            strgMessage = 'Arret du fiideubaque des nombres d''ondes : problaime de mesure';
730                            tango_giveInformationMessage(devSpeakerName,  strgMessage);
731                            tuneFBgui('StopTuneFB');
732                            break
733                        else
734                            tune_old1 = tune_old;
735                            tune_old = tune;
736                            steptune(-deltaTune*tuneFB.fact, Mode);
737                        end
738                    end
739                end
740
741                % Pause until tuneFB.LoopDelay
742                if DEBUGFLAG
743                    fprintf('Time elapsed between 2 iterations (before pause) is %f s\n', gettime-t00);
744                end
745
746                while (gettime-t00) < tuneFB.LoopDelay
747                    pause(.1); % pause large enough for allowing GUI interaction
748                    % Check if GUI has been closed
749                    if isempty(gcbf)
750                        TUNE_FEEDBACK_STOP_FLAG = 1;
751                        lasterr('TUNEGUI GUI DISAPPEARED!');
752                        error('TUNEGUI GUI DISAPPEARED!');
753                    end
754                    %% fast loop to check whether FB loop was asked to be stopped
755                    if TUNE_FEEDBACK_STOP_FLAG == 0
756                        TUNE_FEEDBACK_STOP_FLAG = getappdata(findobj(gcbf,'Tag','TUNEFBGUIFig1'),'TUNE_FEEDBACK_STOP_FLAG');
757                    end
758                end
759                tuneFBgui('UpdateStatusTuneFB');
760
761                % Pause until tuneFB.LoopDelay
762                if DEBUGFLAG
763                    fprintf('Time elapsed between 2 iterations (after pause) is %f s\n', gettime-t00);
764                end
765               
766                % Maintain lock on FB service
767                if get(findobj(gcbf,'Tag','TUNEFBGUICheckboxHcorrection'),'Value')
768                    argin.svalue={'istunexfbrunning'};
769                    argin.lvalue=int32(tuneFB.XLockTag);
770                    tango_command_inout2(devLockName,'MaintainLock', argin);
771                end
772
773                if get(findobj(gcbf,'Tag','TUNEFBGUICheckboxVcorrection'),'Value')
774                    argin.svalue={'istunezfbrunning'};
775                    argin.lvalue=int32(tuneFB.ZLockTag);
776                    tango_command_inout2(devLockName,'MaintainLock', argin);
777                end
778
779                % Check if button stop was pressed
780                TUNE_FEEDBACK_STOP_FLAG = getappdata(findobj(gcbf,'Tag','TUNEFBGUIFig1'),'TUNE_FEEDBACK_STOP_FLAG');
781
782            catch
783                dbstack
784                fprintf('\n  %s \n',lasterr);
785                warndlg('Tune Feedback stopped on fatal error');
786                strgMessage = 'Erreur fatale, arret du fiideubaque des nombres d''ondes';
787                tango_giveInformationMessage(devSpeakerName,  strgMessage);
788                TUNE_FEEDBACK_STOP_FLAG = 1;
789                tuneFBgui('StopTuneFB');
790                set(findobj(gcbf,'Tag','TUNEFBGUIStatus'),'BackgroundColor', [1 0 0])
791            end
792        end
793
794
795
796        %% StopTuneFB
797    case 'StopTuneFB'
798
799        setappdata(findobj(gcbf,'Tag','TUNEFBGUIFig1'),'TUNE_FEEDBACK_STOP_FLAG', 1);
800
801        tuneFB = get(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata');
802
803        % Unlock TUNE service
804
805        if get(findobj(gcbf,'Tag','TUNEFBGUICheckboxHcorrection'),'Value')
806            argin.svalue={'istunexfbrunning'};
807            argin.lvalue=int32(tuneFB.XLockTag);
808            tango_command_inout2(devLockName,'Unlock', argin);
809        end
810
811        if get(findobj(gcbf,'Tag','TUNEFBGUICheckboxVcorrection'),'Value')
812            argin.svalue={'istunezfbrunning'};
813            argin.lvalue=int32(tuneFB.ZLockTag);
814            tango_command_inout2(devLockName,'Unlock', argin);
815        end
816
817        set(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Userdata', tuneFB);
818        tuneFBgui('UpdateStatusTuneFB');
819
820        fprintf('   %s \n', datestr(clock));
821        fprintf('   ***************************\n');
822        fprintf('   **  TUNE  Feedback Stopped**\n');
823        fprintf('   ***************************\n\n');
824        pause(1);
825
826        % enable buttons in GUI
827        set(0,'showhiddenhandles','on');
828
829        set(findobj(gcbf,'Tag','TUNEFBGUIButtonTuneFBSetup'),'Enable','on');
830        set(findobj(gcbf,'Tag','TUNEFBGUIStartTUNEFB'),'Enable','on');
831        set(findobj(gcbf,'Tag','TUNEFBGUIStopTUNEFB'),'Enable','off');
832        set(findobj(gcbf,'Tag','TUNEFBGUICheckboxHcorrection'),'Enable','on');
833        set(findobj(gcbf,'Tag','TUNEFBGUICheckboxVcorrection'),'Enable','on');
834        set(findobj(gcbf,'Tag','TUNEFBGUIClose'),'Enable','on');
835        pause(0);
836
837    otherwise
838        fprintf('   Unknown action name: %s.\n', action);
839
840end
841
842%% Check status of tune feedback
843function check4tunefb(devLockName)
844
845if strcmp(getmode('Q7'),'Online') && strcmp(getmode('Q9'),'Online')
846    %look for already running feedback loops
847
848    isTunexFBRunning = readattribute([devLockName, '/istunexfbrunning']);
849
850    isTunezFBRunning = readattribute([devLockName, '/istunezfbrunning']);
851
852    if isTunexFBRunning
853        warning('TUNEX FB is already running. Stop other application first!')
854        warndlg('TUNEX FB is already running. Stop other application first!')
855    end
856
857    if isTunezFBRunning
858        warning('TUNEZ FB is already running. Stop other application first!')
859        warndlg('TUNEZ FB is already running. Stop other application first!')
860    end
861end
862
863
864
865%% Write database property
866function write_fbnu_property (prop_name, prop_val)
867    db = tango_get_dbname; % Get database
868    cmd_name = 'DbPutProperty';
869    cmd_arg  = {'FBnu', '1', prop_name, '1', num2str(prop_val)};
870    tango_command_inout2(db, cmd_name, cmd_arg);
871
872   
873%% Read database property   
874function pv = read_fbnu_property(prop_name)
875    db = tango_get_dbname;  % Get database
876    cmd_name = 'DbGetProperty';
877    cmd_arg  = {'FBnu', prop_name};
878    cmd_res = tango_command_inout2(db, cmd_name, cmd_arg);
879    pv = str2double(cmd_res(5));
Note: See TracBrowser for help on using the repository browser.