1 | function [LimitFlag, LimitList] = checklimits(varargin) |
---|
2 | %CHECKLIMITS - Checks if the setpoint will exceed a limit |
---|
3 | % [LimitFlag, LimitList] = checklimits(Family, Field, Setpoints, DeviceList) |
---|
4 | % [LimitFlag, LimitList] = checklimits(Family, Setpoints, DeviceList) |
---|
5 | % [LimitFlag, LimitList] = checklimits(DataStructure) |
---|
6 | % |
---|
7 | % INPUTS |
---|
8 | % 1. Family or Data Structure |
---|
9 | % 2. Field - MML field {Default: 'Setpoint'} |
---|
10 | % 3. Setpoints - desired setpoint |
---|
11 | % 4. DeviceList {Default: []} |
---|
12 | % 5. Optional override of the units: |
---|
13 | % 'Physics' - Setpoints are in physics units |
---|
14 | % 'Hardware' - Setpoints are in hardware units |
---|
15 | % |
---|
16 | % OUTPUTS |
---|
17 | % 1. LimitFlag - 0 if a limit will not be exceeded |
---|
18 | % 1 if a limit will be exceeded |
---|
19 | % 2. LimitList - Index of which devices would exceed a limit |
---|
20 | % |
---|
21 | % NOTES |
---|
22 | % 1. For changes in setpoints use: |
---|
23 | % [LimitFlag, LimitList] = checklimits(Family, DeviceList, getsp(Family, DeviceList)+DeltaSetpoints) |
---|
24 | % 2. By default, limits are stored in MML setup in hardware units. |
---|
25 | % |
---|
26 | % See Also minsp, maxsp, minpv, maxpv |
---|
27 | |
---|
28 | % |
---|
29 | % Written by Gregory J. Portmann |
---|
30 | |
---|
31 | |
---|
32 | %%%%%%%%%%%%%%%%% |
---|
33 | % Input parsing % |
---|
34 | %%%%%%%%%%%%%%%%% |
---|
35 | UnitsFlagCell = {}; |
---|
36 | for i = length(varargin):-1:1 |
---|
37 | if isstruct(varargin{i}) |
---|
38 | % Ignor structures |
---|
39 | elseif iscell(varargin{i}) |
---|
40 | % Ignor cells |
---|
41 | elseif strcmpi(varargin{i},'struct') |
---|
42 | % Remove and ignor |
---|
43 | varargin(i) = []; |
---|
44 | elseif strcmpi(varargin{i},'numeric') |
---|
45 | % Remove and ignor |
---|
46 | varargin(i) = []; |
---|
47 | elseif strcmpi(varargin{i},'simulator') || strcmpi(varargin{i},'model') || strcmpi(varargin{i},'Online') || strcmpi(varargin{i},'Manual') |
---|
48 | % Remove and ignor |
---|
49 | varargin(i) = []; |
---|
50 | elseif strcmpi(varargin{i},'physics') |
---|
51 | UnitsFlagCell = {'Physics'}; |
---|
52 | varargin(i) = []; |
---|
53 | elseif strcmpi(varargin{i},'hardware') |
---|
54 | UnitsFlagCell = {'Hardware'}; |
---|
55 | varargin(i) = []; |
---|
56 | end |
---|
57 | end |
---|
58 | |
---|
59 | |
---|
60 | if isempty(varargin) |
---|
61 | error('Must have at least a family name input'); |
---|
62 | elseif iscell(varargin{1}) |
---|
63 | % Cell input |
---|
64 | for i = 1:length(varargin{1}) |
---|
65 | if length(varargin) == 1 |
---|
66 | [LimitFlag{i}, LimitList{i}] = checklimits(varargin{1}{i}, UnitsFlagCell{:}); |
---|
67 | elseif length(varargin) == 2 |
---|
68 | [LimitFlag{i}, LimitList{i}] = checklimits(varargin{1}{i}, varargin{2}{i}, UnitsFlagCell{:}); |
---|
69 | elseif length(varargin) == 3 |
---|
70 | [LimitFlag{i}, LimitList{i}] = checklimits(varargin{1}{i}, varargin{2}{i}, varargin{3}{i}, UnitsFlagCell{:}); |
---|
71 | else |
---|
72 | [LimitFlag{i}, LimitList{i}] = checklimits(varargin{1}{i}, varargin{2}{i}, varargin{3}{i}, varargin{4}{i}, UnitsFlagCell{:}); |
---|
73 | end |
---|
74 | end |
---|
75 | return |
---|
76 | end |
---|
77 | |
---|
78 | |
---|
79 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
80 | % Family or data structure inputs beyond this point % |
---|
81 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
82 | Family = varargin{1}; |
---|
83 | if isstruct(Family) |
---|
84 | % Data structure inputs |
---|
85 | if length(varargin) < 2 |
---|
86 | if isfield(Family,'Field') |
---|
87 | Field = Family.Field; |
---|
88 | else |
---|
89 | Field = ''; |
---|
90 | end |
---|
91 | end |
---|
92 | if length(varargin) < 3 |
---|
93 | if isfield(Family,'Data') |
---|
94 | Setpoints = Family.Data; |
---|
95 | else |
---|
96 | error('A .Data field must exist for data structure inputs.'); |
---|
97 | end |
---|
98 | end |
---|
99 | if length(varargin) < 4 |
---|
100 | if isfield(Family,'DeviceList') |
---|
101 | DeviceList = Family.DeviceList; |
---|
102 | else |
---|
103 | DeviceList = []; |
---|
104 | end |
---|
105 | end |
---|
106 | if isempty(UnitsFlagCell) |
---|
107 | if isfield(Family,'Units') |
---|
108 | UnitsFlagCell{1} = Family.Units; |
---|
109 | end |
---|
110 | end |
---|
111 | if isfield(Family,'FamilyName') |
---|
112 | Family = Family.FamilyName; |
---|
113 | else |
---|
114 | error('For data structure inputs FamilyName field must exist') |
---|
115 | end |
---|
116 | else |
---|
117 | % Family string input |
---|
118 | if length(varargin) >= 2 && ischar(varargin{2}) |
---|
119 | Field = varargin{2}; |
---|
120 | varargin(2) = []; |
---|
121 | else |
---|
122 | Field = ''; |
---|
123 | end |
---|
124 | if length(varargin) >= 2 |
---|
125 | Setpoints = varargin{2}; |
---|
126 | else |
---|
127 | Setpoints = ''; |
---|
128 | end |
---|
129 | if length(varargin) >= 3 |
---|
130 | DeviceList = varargin{3}; |
---|
131 | else |
---|
132 | DeviceList = []; |
---|
133 | end |
---|
134 | end |
---|
135 | |
---|
136 | if isempty(Field) |
---|
137 | Field = 'Setpoint'; |
---|
138 | end |
---|
139 | |
---|
140 | if isempty(DeviceList) |
---|
141 | DeviceList = family2dev(Family); |
---|
142 | end |
---|
143 | if (size(DeviceList,2) == 1) |
---|
144 | DeviceList = elem2dev(Family, DeviceList); |
---|
145 | end |
---|
146 | |
---|
147 | if isempty(Setpoints) || ~isnumeric(Setpoints) |
---|
148 | error('Setpoint input required'); |
---|
149 | end |
---|
150 | |
---|
151 | if size(Setpoints,1) == 1 |
---|
152 | Setpoints = ones(size(DeviceList,1),1) * Setpoints; |
---|
153 | end |
---|
154 | |
---|
155 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
156 | % CommonName Input: Convert common names to a DeviceList % |
---|
157 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
158 | if ischar(DeviceList) |
---|
159 | DeviceList = common2dev(DeviceList, Family); |
---|
160 | if isempty(DeviceList) |
---|
161 | error('DeviceList was a string but common names could not be found.'); |
---|
162 | end |
---|
163 | end |
---|
164 | |
---|
165 | |
---|
166 | %%%%%%%%%%%%%%%% |
---|
167 | % Check Limits % |
---|
168 | %%%%%%%%%%%%%%%% |
---|
169 | |
---|
170 | % Sometimes the sign gets reversed in physics units (like for defocusing quadrupoles or reverse |
---|
171 | % wired power supplies), so just check if it is between min and max. |
---|
172 | |
---|
173 | MinPV = minpv(Family, DeviceList, Field, UnitsFlagCell{:}); |
---|
174 | MaxPV = maxpv(Family, DeviceList, Field, UnitsFlagCell{:}); |
---|
175 | if isempty(MinPV) || isempty(MaxPV) |
---|
176 | warning('%s limits unknown for %s family, hence no limits check made.', Field, Family); |
---|
177 | return |
---|
178 | end |
---|
179 | |
---|
180 | [tmp, SortIndex] = sort([MinPV Setpoints MaxPV], 2); |
---|
181 | |
---|
182 | |
---|
183 | % 2 means the setpoint stayed in the middle column |
---|
184 | LimitList = find(SortIndex(:,2)~=2); |
---|
185 | |
---|
186 | if isempty(LimitList) |
---|
187 | LimitFlag = 0; |
---|
188 | else |
---|
189 | % Limit problem |
---|
190 | LimitFlag = 1; |
---|
191 | end |
---|
192 | |
---|
193 | |
---|
194 | % MinPV = minpv(Family, Field, 'Hardware'); |
---|
195 | % MaxPV = maxpv(Family, Field, 'Hardware'); |
---|
196 | % |
---|
197 | % % Setpoint must be in hardware units |
---|
198 | % if isempty(UnitsFlagCell) |
---|
199 | % UnitsFlagCell{1} = getunits(Family, Field); |
---|
200 | % end |
---|
201 | % if strcmpi(UnitsFlagCell{1}, 'Physics') |
---|
202 | % Setpoints = physics2hw(Family, Field, Setpoints, DeviceList); |
---|
203 | % end |
---|
204 | |
---|
205 | % iMax = (Setpoints > MaxPV); |
---|
206 | % iMin = (Setpoints < MinPV); |
---|
207 | % if any(iMin) || any(iMax) |
---|
208 | % LimitFlag = 1; |
---|
209 | % LimitList = find(max([iMax iMin]')'==1); |
---|
210 | % else |
---|
211 | % LimitFlag = 0; |
---|
212 | % LimitList = []; |
---|
213 | % end |
---|