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