source: MML/trunk/machine/SOLEIL/StorageRing/insertions/idSetUndParamSync.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: 8.3 KB
Line 
1function res = idSetUndParamSync(idName, attrName, attrValue, attrAbsTol)
2    res=-1;
3    res= idSetUndParamSync_old(idName, attrName, attrValue, attrAbsTol);
4    if (res==-1)
5        return
6    end
7    if (strcmp(idName, 'HU64_HERMES')&&strcmpi(attrName, 'phase'))
8        res= idSetUndParamSync_old(idName, attrName, attrValue, attrAbsTol);
9    end
10end
11
12function res= idSetUndParamSync_old(idName, attrName, attrValue, attrAbsTol)
13
14    res = 0;
15    %DServName = '';
16    %StandByStr = ''; %String to search in the return of "Status" command of DServer
17    %ActStatusStr = '';
18    %attr_name_list = {''};
19
20    numLoopsWaitRes = 300; %% valeur originale 300
21    sleepTime = 3; %[s]
22    sleepTimeFin = 3; % 7; %[s]
23    useAppleIIDServerLevel1 = 0;
24    %useModeAnti = 0;
25    %attrNameIsRecognized = 0;
26
27    [DServName, StandByStr] = idGetUndDServer(idName);
28
29%     if strcmp(idName, 'HU80_TEMPO')
30%         %DServName = 'ANS-C08/EI/M-HU80.2'; %Name of Level 2 DServer
31%         %StandByStr = 'ANS-C08/EI/M-HU80.2_MotorsControl : STANDBY';
32%
33%         %if strcmp(attrName, 'gap')
34%         %    attrNameIsRecognized = 1;
35%         %elseif strcmp(attrName, 'phase')
36%         %    attrNameIsRecognized = 1;
37%         %end
38%         %if attrNameIsRecognized == 0
39%         %    fprintf('Undulator Parameter / Device Server Attribute name is not recognized\n');
40%         %    res = -1; return;
41%         %end
42%
43%         if strcmp(attrName, 'mode')
44%            attrValue = int16(attrValue);
45%         end
46%
47%         %Debug
48%         %    attr_val_list = tango_read_attributes(DServName, attr_name_list);
49%         %    if (tango_error == -1) %handle error
50%         %        tango_print_error_stack; return;
51%         %    end
52%         %End Debug
53%     end
54
55    if strcmp(DServName, '') ~= 0
56        fprintf('Device Server name is not specified\n');
57        res = -1;
58        return
59    end
60   
61    if strcmpi(attrName, 'Mode')
62    Mode=idGetUndParam(idName, attrName);
63        if ~strcmpi(Mode, attrValue)
64            if strcmp(attrValue, 'ii')
65                attrValue=0;
66            elseif strcmp(attrValue, 'x')
67                attrValue=1;
68            elseif strcmp(attrValue, 'i2')
69                attrValue=2;
70            elseif strcmp(attrValue, 'x2')
71                attrValue=3;
72            else
73                fprintf ('Wrong mode\n')
74                return
75            end
76            attrValue=int16(attrValue);
77            tango_write_attribute2(DServName, 'phaseCtrlMode', attrValue);
78            if tango_error==-1
79                fprintf('Problem while writing mode\n')
80                res = -1;
81                return
82            end
83            res=0;
84            return
85        end
86    end
87%     TempValue=TempStruct.value;
88%     TempValue=TempValue(1); 
89    attr_name_val_list(1).name = attrName;
90    attr_name_val_list(1).value = attrValue;
91    attr_name_list = {attr_name_val_list(1).name};
92    %attr_name_list_AppleIILevel1 = {'encoder1Position','encoder2Position','encoder3Position','encoder4Position'};
93
94%     if strcmp(idName, 'HU52_DEIMOS')
95%         useAppleIIDServerLevel1 = 0;
96%     end
97
98%     if useAppleIIDServerLevel1
99%         %special case: controlling via DServer of Level 1
100%
101%         if (strcmpi(attrName, 'gap'))
102%             attr_name_list = {'encoder1Position','encoder2Position','encoder3Position','encoder4Position'};
103%             tango_command_inout2(DServName, 'GotoGap', attrValue);
104%         elseif (strcmpi(attrName, 'phase'))
105%             attr_name_list = {'encoder5Position','encoder6Position'};
106%             multMode = 1;
107%             if(imag(attrValue) ~= 0)
108%                 multMode = -1;
109%             end
110%             tango_command_inout2(DServName, 'GotoPhase', [real(attrValue), multMode*real(attrValue)]);
111%         end
112%     else
113         tango_write_attributes(DServName, attr_name_val_list);
114%     end
115
116
117    if (tango_error == -1) %handle error
118        tango_print_error_stack;
119
120        pause(sleepTime);
121        fprintf('Trying to repeat writing attributes after pause...\n');
122
123        if useAppleIIDServerLevel1
124            %special case: controlling via DServer of Level 1
125            if strcmpi(attrName, 'gap')
126                tango_command_inout2(DServName, 'GotoGap', attrValue);
127            elseif strcmpi(attrName, 'phase')
128                multMode = 1;
129                if(imag(attrValue) ~= 0)
130                    multMode = -1;
131                end
132                tango_command_inout2(DServName, 'GotoPhase', [real(attrValue), multMode*real(attrValue)]);
133            end
134        else
135            tango_write_attributes(DServName, attr_name_val_list);
136        end
137
138        if (tango_error == -1) %handle error
139            tango_print_error_stack;
140            fprintf('Failed again. Returning from the function abnormally.\n');
141            res = -1; %hoping that it could still recover
142            return;
143        else
144            fprintf('Succeeded writing attributes from 2nd attempt. Continuing execution of the function.\n');
145        end
146    end
147
148    %Waiting until the parameter is actually set
149    for i = 1:numLoopsWaitRes
150
151        attr_val_list = tango_read_attributes(DServName, attr_name_list);
152
153        if (tango_error == -1) %handle error
154            tango_print_error_stack;
155
156            pause(sleepTime);
157            fprintf('Trying to repeat reading attributes after pause...\n');
158            attr_val_list = tango_read_attributes(DServName, attr_name_list);
159            if (tango_error == -1) %handle error
160                tango_print_error_stack;
161                fprintf('Failed again. Returning from the function abnormally.\n');
162                res = -1; %hoping that it could still recover
163                return;
164            else
165                fprintf('Succeeded reading attributes from 2nd attempt. Continuing execution of the function.\n');
166            end
167        end
168
169        %valRequested = attr_name_val_list(1).value;
170        valRequested = real(attrValue);
171%         if (strcmp(idName, 'HU64_HERMES')&&strcmp(attrName, 'phase'))
172%             valSet=tango_read_attribute('ANS-C10/EI/M-HU64.2', 'encoder7Position');
173%             valSet=valSet.value;
174%         else
175            valSet = attr_val_list(1).value(1); %check why (1)
176%         end
177        if useAppleIIDServerLevel1
178            if strcmpi(attrName, 'gap')
179                valSet = 0.5*(attr_val_list(1).value + attr_val_list(2).value + attr_val_list(3).value + attr_val_list(4).value); %check
180            elseif strcmpi(attrName, 'phase')
181                valSet = 0.5*(abs(attr_val_list(1).value) + abs(attr_val_list(2).value))*sign(attr_val_list(1).value); %check
182            end
183        end
184
185        if(abs(valRequested - valSet) <= attrAbsTol)
186            %fprintf('%s',StandByStr )
187            %fprintf('\n' )
188            if strcmp(StandByStr, '') == 0
189                % fprintf('%s', 'STANDBYSTR NON NUL')
190                attr_val_list_status = tango_read_attributes(DServName, {'Status'});
191                if (tango_error == -1) %handle error
192                    tango_print_error_stack;
193
194                    pause(sleepTime);
195                    fprintf('Trying to repeat reading attributes after pause...\n');
196                    attr_val_list_status = tango_read_attributes(DServName, {'Status'});
197                    if (tango_error == -1) %handle error
198                        tango_print_error_stack;
199                        fprintf('Failed again. Returning from the function abnormally.\n');
200                        res = -1; %hoping that it could still recover
201                        return;
202                    else
203                        fprintf('Succeeded reading attributes from 2nd attempt. Continuing execution of the function.\n');
204                    end
205                end
206                ActStatusStr = attr_val_list_status.value;
207                %fprintf('%s %s',ActStatusStr, 'Number 2')
208                cmpStatus = strfind(ActStatusStr, StandByStr);
209                if (~isempty(cmpStatus))
210                    pause(sleepTimeFin); %to ensure that corrector values are set
211                    return; %Normal exit
212                end
213            else
214                pause(sleepTimeFin); %to ensure that corrector values are set
215                % fprintf('%s', 'STANDBYSTR NUL')
216                return; %Normal exit
217            end
218        end
219        pause(sleepTime);
220    end
221
222    res = -1;
223    fprintf('Failed to set requested Undulator Parameter value\n');
224end
Note: See TracBrowser for help on using the repository browser.