1 | function DataOut = real2raw(varargin) |
---|
2 | %REAL2RAW - Converts "real" data (calibrated values) to control system data |
---|
3 | % RawData = real2raw(Family, Field, RawData, DeviceList) |
---|
4 | % RawData = real2raw(DataStruct) |
---|
5 | % |
---|
6 | % INPUTS |
---|
7 | % 1. Family - Family Name |
---|
8 | % DataStruct - Data Structure (.FamilyName, .Field, and .DeviceList fields are used if not input) |
---|
9 | % 2. RawData - Raw control system data |
---|
10 | % 3. DeviceList - Device list for that family |
---|
11 | % 4. Flag |
---|
12 | % 'Absolute' - Include the offset {Default (unless it's a response matrix)} |
---|
13 | % 'Incremental' - Gain only {Response matrices are incremental only} |
---|
14 | % 'Numeric' - forces numeric output for structure inputs |
---|
15 | % |
---|
16 | % OUTPUTS |
---|
17 | % 1. RawData - Raw control system values |
---|
18 | % RawData = (RealData ./ Gain) + Offset |
---|
19 | % |
---|
20 | % ALGORITHM |
---|
21 | % RawData = (RealData ./ Gain) + Offset |
---|
22 | % RealData = Gain .* (RawData - Offset) |
---|
23 | % |
---|
24 | % NOTES |
---|
25 | % 1. If the input is a structure, then the output will be a structure |
---|
26 | % 2. If the inputs are cell arrays, then the output is a cell array |
---|
27 | % 3. If Family is a cell array, then all other inputs must be cell arrays |
---|
28 | % 4. The Offset is always in hardware units. |
---|
29 | % |
---|
30 | % See also raw2real, physics2hw, hw2physics, getoffset, getgain, getgolden |
---|
31 | % |
---|
32 | % Written by Greg Portmann |
---|
33 | |
---|
34 | GCRFlag = 1; |
---|
35 | |
---|
36 | %%%%%%%%%%%%%%%%% |
---|
37 | % Input Parsing % |
---|
38 | %%%%%%%%%%%%%%%%% |
---|
39 | UnitsFlag = ''; |
---|
40 | NumericFlag = []; |
---|
41 | IncrementalFlag = {}; |
---|
42 | InputFlags = {}; |
---|
43 | for i = length(varargin):-1:1 |
---|
44 | if isstruct(varargin{i}) |
---|
45 | % Ignor structures |
---|
46 | elseif iscell(varargin{i}) |
---|
47 | % Ignor cells |
---|
48 | elseif strcmpi(varargin{i},'struct') |
---|
49 | NumericFlag = 0; |
---|
50 | InputFlags = [InputFlags varargin(i)]; |
---|
51 | varargin(i) = []; |
---|
52 | elseif strcmpi(varargin{i},'numeric') |
---|
53 | NumericFlag = 1; |
---|
54 | InputFlags = [InputFlags varargin(i)]; |
---|
55 | varargin(i) = []; |
---|
56 | elseif strcmpi(varargin{i},'Incremental') |
---|
57 | IncrementalFlag = varargin(i); |
---|
58 | varargin(i) = []; |
---|
59 | elseif strcmpi(varargin{i},'Absolute') |
---|
60 | IncrementalFlag = varargin(i); |
---|
61 | InputFlags = [InputFlags varargin(i)]; |
---|
62 | varargin(i) = []; |
---|
63 | elseif strcmpi(varargin{i},'simulator') || strcmpi(varargin{i},'model') || strcmpi(varargin{i},'Online') || strcmpi(varargin{i},'Manual') |
---|
64 | % Remove and ignor |
---|
65 | varargin(i) = []; |
---|
66 | elseif strcmpi(varargin{i},'physics') |
---|
67 | fprintf(' WARNING: ''Physics'' flag ignored by real2raw\n'); |
---|
68 | varargin(i) = []; |
---|
69 | elseif strcmpi(varargin{i},'hardware') |
---|
70 | fprintf(' WARNING: ''Hardware'' flag ignored by real2raw\n'); |
---|
71 | varargin(i) = []; |
---|
72 | end |
---|
73 | end |
---|
74 | |
---|
75 | |
---|
76 | if isempty(varargin) |
---|
77 | error('Must have at least 1 input for structures and 3 for non-structures.'); |
---|
78 | else |
---|
79 | Family = varargin{1}; |
---|
80 | if length(varargin) >= 2 |
---|
81 | Field = varargin{2}; |
---|
82 | end |
---|
83 | if length(varargin) >= 3 |
---|
84 | DataIn = varargin{3}; |
---|
85 | end |
---|
86 | if length(varargin) >= 4 |
---|
87 | DeviceList = varargin{4}; |
---|
88 | end |
---|
89 | end |
---|
90 | |
---|
91 | |
---|
92 | %%%%%%%%%%%%%%%%%%%% |
---|
93 | % Cell Array Input % |
---|
94 | %%%%%%%%%%%%%%%%%%%% |
---|
95 | if iscell(Family) |
---|
96 | if length(varargin) >= 2 |
---|
97 | if ~iscell(DataIn) |
---|
98 | error('If Family is a cell array, then RawData must be a cell array.'); |
---|
99 | end |
---|
100 | if length(Family) ~= length(DataIn) |
---|
101 | error('RawData and Family must be the same size cell arrays'); |
---|
102 | end |
---|
103 | end |
---|
104 | if length(varargin) >= 3 |
---|
105 | if ~iscell(DeviceList) |
---|
106 | error('If Family is a cell array, then DeviceList must be a cell array.'); |
---|
107 | end |
---|
108 | if length(Family) ~= length(DeviceList) |
---|
109 | error('Family and DeviceList must be the same size cell arrays'); |
---|
110 | end |
---|
111 | end |
---|
112 | for i = 1:length(Family) |
---|
113 | if length(varargin) == 1 |
---|
114 | DataOut{i} = real2raw(Family{i}, InputFlags{:}); |
---|
115 | elseif length(varargin) == 2 |
---|
116 | DataOut{i} = real2raw(Family{i}, Field{i}, InputFlags{:}); |
---|
117 | elseif length(varargin) == 3 |
---|
118 | DataOut{i} = real2raw(Family{i}, Field{i}, DataIn{i}, InputFlags{:}); |
---|
119 | else |
---|
120 | DataOut{i} = real2raw(Family{i}, Field{i}, DataIn{i}, DeviceList{i}, InputFlags{:}); |
---|
121 | end |
---|
122 | end |
---|
123 | return |
---|
124 | end |
---|
125 | % End cell inputs |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | %%%%%%%%%%%%%%%%%%%% |
---|
130 | % Structure Inputs % |
---|
131 | %%%%%%%%%%%%%%%%%%%% |
---|
132 | if isstruct(Family) |
---|
133 | % Convert entire data structure |
---|
134 | DataOut = Family; |
---|
135 | |
---|
136 | for j = 1:size(DataOut,1) |
---|
137 | for k = 1:size(DataOut,2) |
---|
138 | |
---|
139 | if isfield(DataOut(j,k),'Monitor') && isfield(DataOut(j,k),'Actuator') |
---|
140 | % Response matrix structure |
---|
141 | |
---|
142 | if strcmpi(IncrementalFlag, 'Absolute') && j==1 && k==1 |
---|
143 | fprintf(' WARNING: ''Absolute'' flag passed to real2raw for a response matrix. Flag ignored!'); |
---|
144 | end |
---|
145 | |
---|
146 | % Incrementally scale rows by Monitor and Columns by Actuators |
---|
147 | DataOut(j,k).Data = real2raw(DataOut(j,k).Monitor.FamilyName, DataOut(j,k).Monitor.Field, DataOut(j,k).Data, DataOut(j,k).Monitor.DeviceList, 'Incremental'); |
---|
148 | DataOut(j,k).Data = real2raw(DataOut(j,k).Actuator.FamilyName, DataOut(j,k).Actuator.Field, DataOut(j,k).Data', DataOut(j,k).Actuator.DeviceList, 'Incremental')'; |
---|
149 | |
---|
150 | DataOut(j,k).Monitor = real2raw(DataOut(j,k).Monitor, 'Absolute'); |
---|
151 | DataOut(j,k).Actuator = real2raw(DataOut(j,k).Actuator, 'Absolute'); |
---|
152 | if isfield(DataOut(j,k), 'ActuatorDelta') |
---|
153 | DataOut(j,k).ActuatorDelta = real2raw(DataOut(j,k).Actuator.FamilyName, DataOut(j,k).Actuator.Field, DataOut(j,k).ActuatorDelta, [1 1], 'Incremental'); |
---|
154 | end |
---|
155 | |
---|
156 | elseif isfield(DataOut(j,k),'FamilyName') && isfield(DataOut(j,k),'Field') |
---|
157 | % Data structure |
---|
158 | DataOut(j,k).Data = real2raw(DataOut(j,k).FamilyName, DataOut(j,k).Field, DataOut(j,k).Data, DataOut(j,k).DeviceList, IncrementalFlag{:}); |
---|
159 | |
---|
160 | else |
---|
161 | error('Unknown data structure type'); |
---|
162 | end |
---|
163 | |
---|
164 | if isfield(DataOut(j,k), 'CreatedBy') |
---|
165 | DataOut(j,k).CreatedBy = ['real2raw,', DataOut(j,k).CreatedBy]; |
---|
166 | end |
---|
167 | end |
---|
168 | end |
---|
169 | |
---|
170 | % Make numeric if requested |
---|
171 | if ~isempty(NumericFlag) && NumericFlag == 1 |
---|
172 | DataOutNew = []; |
---|
173 | for j = 1:size(DataOut,1) |
---|
174 | OutCol = []; |
---|
175 | for k = 1:size(DataOut,2) |
---|
176 | OutCol = [OutCol DataOut(j,k).Data]; |
---|
177 | end |
---|
178 | DataOutNew = [DataOutNew; OutCol]; |
---|
179 | end |
---|
180 | DataOut = DataOutNew; |
---|
181 | end |
---|
182 | |
---|
183 | return; |
---|
184 | end |
---|
185 | |
---|
186 | |
---|
187 | %%%%%%%%%%%%%%%%% |
---|
188 | % Main Function % |
---|
189 | %%%%%%%%%%%%%%%%% |
---|
190 | |
---|
191 | % Family string input |
---|
192 | FamilyName = Family; |
---|
193 | if length(varargin) < 3 |
---|
194 | error('No RealData input'); |
---|
195 | end |
---|
196 | if length(varargin) < 4 |
---|
197 | DeviceList = []; |
---|
198 | end |
---|
199 | if isempty(IncrementalFlag) |
---|
200 | IncrementalFlag = 'Absolute'; |
---|
201 | end |
---|
202 | |
---|
203 | |
---|
204 | % Defaults |
---|
205 | if isempty(Field) |
---|
206 | if ismember(FamilyName,'BPM') |
---|
207 | Field = 'Monitor'; |
---|
208 | else |
---|
209 | Field = 'Setpoint'; |
---|
210 | end |
---|
211 | end |
---|
212 | if isempty(DeviceList) |
---|
213 | DeviceList = family2dev(FamilyName); |
---|
214 | end |
---|
215 | if (size(DeviceList,2) == 1) |
---|
216 | DeviceList = elem2dev(FamilyName, DeviceList); |
---|
217 | end |
---|
218 | |
---|
219 | |
---|
220 | %%%%%%%%%%%%%%%%%%%% |
---|
221 | % Convert the data % |
---|
222 | %%%%%%%%%%%%%%%%%%%% |
---|
223 | |
---|
224 | Gain = getgain(FamilyName, Field, DeviceList); |
---|
225 | if strcmpi(IncrementalFlag, 'Incremental') |
---|
226 | Offset = 0; |
---|
227 | else |
---|
228 | Offset = getoffset(FamilyName, Field, DeviceList, 'Hardware'); |
---|
229 | end |
---|
230 | |
---|
231 | |
---|
232 | for i = 1:size(DataIn,2) |
---|
233 | DataOut(:,i) = (DataIn(:,i) ./ Gain) + Offset; |
---|
234 | end |
---|
235 | |
---|
236 | |
---|
237 | %raw2real: DataOut(:,i) = Gain .* (DataIn(:,i) - Offset); |
---|
238 | %real2raw: DataOut(:,i) = (DataIn(:,i) ./ Gain) + Offset; |
---|
239 | |
---|
240 | |
---|
241 | |
---|