1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.navigation.inertial.calibration.intervals.thresholdfactor;
17
18 import com.irurueta.navigation.LockedException;
19 import com.irurueta.navigation.NavigationException;
20 import com.irurueta.navigation.NotReadyException;
21 import com.irurueta.navigation.inertial.calibration.BodyKinematicsSequence;
22 import com.irurueta.navigation.inertial.calibration.StandardDeviationBodyKinematics;
23 import com.irurueta.navigation.inertial.calibration.StandardDeviationTimedBodyKinematics;
24 import com.irurueta.navigation.inertial.calibration.accelerometer.AccelerometerCalibratorMeasurementType;
25 import com.irurueta.navigation.inertial.calibration.accelerometer.AccelerometerNonLinearCalibrator;
26 import com.irurueta.navigation.inertial.calibration.gyroscope.GyroscopeCalibratorMeasurementOrSequenceType;
27 import com.irurueta.navigation.inertial.calibration.gyroscope.GyroscopeNonLinearCalibrator;
28 import com.irurueta.numerical.EvaluationException;
29 import com.irurueta.numerical.InvalidBracketRangeException;
30 import com.irurueta.numerical.NumericalException;
31 import com.irurueta.numerical.SingleDimensionFunctionEvaluatorListener;
32 import com.irurueta.numerical.optimization.BracketedSingleOptimizer;
33 import com.irurueta.numerical.optimization.BrentSingleOptimizer;
34 import com.irurueta.numerical.optimization.OnIterationCompletedListener;
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer extends
49 AccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer {
50
51
52
53
54
55 private BracketedSingleOptimizer mseOptimizer;
56
57
58
59
60 private SingleDimensionFunctionEvaluatorListener optimizerListener;
61
62
63
64
65 private OnIterationCompletedListener iterationCompletedListener;
66
67
68
69
70 public BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer() {
71 super();
72 initializeOptimizerListeners();
73 try {
74 setMseOptimizer(new BrentSingleOptimizer());
75 } catch (final LockedException ignore) {
76
77 }
78 }
79
80
81
82
83
84
85 public BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer(
86 final AccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizerDataSource dataSource) {
87 super(dataSource);
88 initializeOptimizerListeners();
89 try {
90 setMseOptimizer(new BrentSingleOptimizer());
91 } catch (final LockedException ignore) {
92
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer(
110 final AccelerometerNonLinearCalibrator accelerometerCalibrator,
111 final GyroscopeNonLinearCalibrator gyroscopeCalibrator) {
112 super(accelerometerCalibrator, gyroscopeCalibrator);
113 initializeOptimizerListeners();
114 try {
115 setMseOptimizer(new BrentSingleOptimizer());
116 } catch (final LockedException ignore) {
117
118 }
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer(
137 final AccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizerDataSource dataSource,
138 final AccelerometerNonLinearCalibrator accelerometerCalibrator,
139 final GyroscopeNonLinearCalibrator gyroscopeCalibrator) {
140 super(dataSource, accelerometerCalibrator, gyroscopeCalibrator);
141 initializeOptimizerListeners();
142 try {
143 setMseOptimizer(new BrentSingleOptimizer());
144 } catch (final LockedException ignore) {
145
146 }
147 }
148
149
150
151
152
153
154
155 public BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer(
156 final BracketedSingleOptimizer mseOptimizer) {
157 super();
158 initializeOptimizerListeners();
159 try {
160 setMseOptimizer(mseOptimizer);
161 } catch (final LockedException ignore) {
162
163 }
164 }
165
166
167
168
169
170
171
172
173 public BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer(
174 final AccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizerDataSource dataSource,
175 final BracketedSingleOptimizer mseOptimizer) {
176 super(dataSource);
177 initializeOptimizerListeners();
178 try {
179 setMseOptimizer(mseOptimizer);
180 } catch (final LockedException ignore) {
181
182 }
183 }
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 public BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer(
201 final AccelerometerNonLinearCalibrator accelerometerCalibrator,
202 final GyroscopeNonLinearCalibrator gyroscopeCalibrator,
203 final BracketedSingleOptimizer mseOptimizer) {
204 super(accelerometerCalibrator, gyroscopeCalibrator);
205 initializeOptimizerListeners();
206 try {
207 setMseOptimizer(mseOptimizer);
208 } catch (final LockedException ignore) {
209
210 }
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 public BracketedAccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizer(
231 final AccelerometerAndGyroscopeIntervalDetectorThresholdFactorOptimizerDataSource dataSource,
232 final AccelerometerNonLinearCalibrator accelerometerCalibrator,
233 final GyroscopeNonLinearCalibrator gyroscopeCalibrator,
234 final BracketedSingleOptimizer mseOptimizer) {
235 super(dataSource, accelerometerCalibrator, gyroscopeCalibrator);
236 initializeOptimizerListeners();
237 try {
238 setMseOptimizer(mseOptimizer);
239 } catch (final LockedException ignore) {
240
241 }
242 }
243
244
245
246
247
248
249
250
251
252 public BracketedSingleOptimizer getMseOptimizer() {
253 return mseOptimizer;
254 }
255
256
257
258
259
260
261
262
263
264 public void setMseOptimizer(final BracketedSingleOptimizer optimizer)
265 throws LockedException {
266 if (running) {
267 throw new LockedException();
268 }
269
270 try {
271 if (optimizer != null) {
272 optimizer.setBracket(minThresholdFactor, minThresholdFactor, maxThresholdFactor);
273 optimizer.setListener(optimizerListener);
274 optimizer.setOnIterationCompletedListener(iterationCompletedListener);
275 }
276 mseOptimizer = optimizer;
277 } catch (final com.irurueta.numerical.LockedException e) {
278 throw new LockedException(e);
279 } catch (final InvalidBracketRangeException ignore) {
280
281 }
282 }
283
284
285
286
287
288
289 @Override
290 public boolean isReady() {
291 return super.isReady() && mseOptimizer != null;
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305 @Override
306 public double optimize() throws NotReadyException, LockedException,
307 IntervalDetectorThresholdFactorOptimizerException {
308 if (running) {
309 throw new LockedException();
310 }
311
312 if (!isReady()) {
313 throw new NotReadyException();
314 }
315
316 try {
317 running = true;
318
319 initProgress();
320
321 if (listener != null) {
322 listener.onOptimizeStart(this);
323 }
324
325 minMse = Double.MAX_VALUE;
326 mseOptimizer.minimize();
327
328 if (listener != null) {
329 listener.onOptimizeEnd(this);
330 }
331
332 return optimalThresholdFactor;
333 } catch (final NumericalException e) {
334 throw new IntervalDetectorThresholdFactorOptimizerException(e);
335 } finally {
336 running = false;
337 }
338 }
339
340
341
342
343 private void initializeOptimizerListeners() {
344 optimizerListener = point -> {
345 try {
346 return evaluateForThresholdFactor(point);
347 } catch (final NavigationException e) {
348 throw new EvaluationException(e);
349 }
350 };
351
352 iterationCompletedListener = (optimizer, iteration, maxIterations) -> {
353 if (maxIterations == null) {
354 return;
355 }
356
357 progress = (float) iteration / (float) maxIterations;
358 checkAndNotifyProgress();
359 };
360 }
361 }