-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceCalibrationCurveConverter_20112019.java
More file actions
232 lines (152 loc) · 7.78 KB
/
Copy pathDeviceCalibrationCurveConverter_20112019.java
File metadata and controls
232 lines (152 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package de.gsi.cs.co.ap.app.magstat.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import cern.accsoft.commons.value.BoundedPolynomial;
import cern.accsoft.commons.value.BoundedPolynomialSequence;
//import cern.accsoft.commons.value.Interval;
//import cern.japc.group.support.SysoutFailSafeParameterValueListener;
// import cern.accsoft.commons.value.Polynomial;
// import cern.accsoft.commons.value.operation.BoundedPolynomialOperations;
// import cern.accsoft.commons.value.operation.BoundedPolynomialSequenceOperations;
// import cern.accsoft.commons.value.operation.PolynomialOperations;
import cern.lsa.client.OpticService;
import cern.lsa.client.Services;
// import cern.lsa.domain.commons.BoundedFunction;
// import cern.lsa.domain.commons.CalibrationFunction;
import cern.lsa.domain.commons.spi.CalibrationFunctionWrapper;
import cern.lsa.domain.devices.factory.DevicesRequestBuilder;
// import cern.lsa.domain.devices.Device;
import cern.lsa.domain.optics.Calibration;
import cern.lsa.domain.optics.CalibrationFunctionTypes;
import cern.lsa.domain.optics.LogicalHardware;
/**
* @author fschirru
*/
public class DeviceCalibrationCurveConverter {
public DeviceCalibrationCurveConverter() {
}
private List<Object[]> deviceSign_list = new ArrayList<>();
private int logicalDeviceSign;
private String logicalDeviceName;
private DevicesRequestBuilder builder = new DevicesRequestBuilder();
private Set<LogicalHardware> logicalHardware;
final OpticService opticService = Services.getOpticService();
private static final RegulaFalsiMethod rfm = RegulaFalsiMethod.getInstance();
private static final DeviceCalibrationCurveConverter INSTANCE = new DeviceCalibrationCurveConverter();
public static DeviceCalibrationCurveConverter getInstance() {
return INSTANCE;
}
public void clearDeviceCalibrationSign() {
deviceSign_list.clear();
}
//separate method to retrieve the calibration sign
public void setDeviceCalibrationSign(final String device_name) {
logicalDeviceName = "LOGICAL." + device_name;
builder.setDeviceName(logicalDeviceName);
logicalHardware = Services.getDeviceService().findLogicalHardware(builder.build());
logicalDeviceSign = logicalHardware.iterator().next().getCalibrationSign();
deviceSign_list.add(new Object[] { device_name, logicalDeviceSign});
}
public void printDeviceCalibrationSign() {
for(int i = 0; i < deviceSign_list.size(); i++) {
System.out.println("List of DEVICES with CALBRATION SIGN");
System.out.println(deviceSign_list.get(i)[0] + " " + deviceSign_list.get(i)[1]);
}
}
public double getValueFromCalibrationCurve(final String name, final double y, final String calType) {
logicalDeviceName = "LOGICAL." + name;
double invertedValue = 0.0;
try {
final Calibration calibration = opticService.findCalibrationByLogicalHardware(logicalDeviceName);
// Case of conversion current to bfield
if (calType.equals("current")) {
// System.out.println("Current processing");
final CalibrationFunctionWrapper currentAsFunctionOfIntegralField = (CalibrationFunctionWrapper) calibration
.getCalibrationFunction(CalibrationFunctionTypes.MAG_INTFIELD2CURRENT);
// System.out.println(currentAsFunctionOfIntegralField);
final BoundedPolynomialSequence boundedPolynomialSequence = (BoundedPolynomialSequence) currentAsFunctionOfIntegralField
.getInterpolable();
// System.out.println(boundedPolynomialSequence);
final BoundedPolynomial polynomial = findBoundedPolynomial(boundedPolynomialSequence, y);
if (polynomial != null) {
invertedValue = rfm.findRoot(polynomial.getInterval().getLowerBound(),
polynomial.getInterval().getUpperBound(), 5E-8, 1000, polynomial, y);
//retrieve the proper calibration sign
// the sign applies only when converting I -> B
for(int i = 0; i < deviceSign_list.size(); i++) {
if(name.equals(deviceSign_list.get(i)[0].toString())) {
//System.out.println("device found " + deviceSign_list.get(i)[0].toString() + " " + deviceSign_list.get(i)[1].toString());
invertedValue = invertedValue * Integer.parseInt(deviceSign_list.get(i)[1].toString());
break;
}
}
} else {
invertedValue = 0.0;
}
}
// Case of conversion voltage to bfield
if (calType.equals("voltage")) {
if (logicalDeviceName.substring(8).equals("GTS3MU1") || logicalDeviceName.substring(8).equals("GTS3MU2")
|| logicalDeviceName.substring(8).equals("GTS4MU1")
|| logicalDeviceName.substring(8).equals("GHFSMU1")
|| logicalDeviceName.substring(8).equals("GTS5MU1")
|| logicalDeviceName.substring(8).equals("GTS6MU1")
|| logicalDeviceName.substring(8).equals("GTS7MU1")) {
// System.out.println("Voltage processing");
final CalibrationFunctionWrapper voltageAsFunctionOfIntegralField = (CalibrationFunctionWrapper) calibration
.getCalibrationFunction(CalibrationFunctionTypes.CURRENT2HALL_VOLTAGE);
// System.out.println(voltageAsFunctionOfIntegralField);
final BoundedPolynomialSequence boundedPolynomialSequence = (BoundedPolynomialSequence) voltageAsFunctionOfIntegralField
.getInterpolable();
// System.out.println(boundedPolynomialSequence);
final BoundedPolynomial polynomial = findBoundedPolynomial(boundedPolynomialSequence, y);
if (polynomial != null) {
invertedValue = rfm.findRoot(polynomial.getInterval().getLowerBound(),
polynomial.getInterval().getUpperBound(), 5E-8, 1000, polynomial, y);
//for hall probe the sign will not be retrieved otherwise calibration out of range
} else {
invertedValue = 0.0;
}
}
}
} catch (final Exception e) {
System.out.println("Exception found for calibration process!!! " + logicalDeviceName);
System.out.println(e);
// invertedValue = -0.5;
// System.exit(0);
}
return invertedValue;
}
private static BoundedPolynomial findBoundedPolynomial(final BoundedPolynomialSequence boundedPolynomialSequence,
final double y) {
final BoundedPolynomial[] boundedPolynomials = boundedPolynomialSequence.getBoundedPolynomials();
for (final BoundedPolynomial boundedPolynomial : boundedPolynomials) {
final double lowerBound = boundedPolynomial.getInterval().getLowerBound();
final double upperBound = boundedPolynomial.getInterval().getUpperBound();
// System.out.println("Lower bound: " + lowerBound);
// System.out.println("Upper bound: " + upperBound);
final double x1_interpolated = boundedPolynomial.interpolate(lowerBound);
final double x2_interpolated = boundedPolynomial.interpolate(upperBound);
// System.out.println("Interpolated x1: " + x1_interpolated);
// System.out.println("Interpolated x2: " + x2_interpolated);
// interval = Interval.of(boundedPolynomial.interpolate(lowerBound),
// boundedPolynomial.interpolate(upperBound));
if (x1_interpolated < x2_interpolated) {
if (y >= x1_interpolated && y <= x2_interpolated) {
return boundedPolynomial;
}
}
if (x1_interpolated > x2_interpolated) {
if (y >= x2_interpolated && y <= x1_interpolated) {
return boundedPolynomial;
}
}
// if (interval.contains(y)) {
// System.out.println("Retrieved polynom: " + boundedPolynomial);
// return boundedPolynomial;
// }
}
return null;
}
}