1 /*
2 * Copyright (C) 2019 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.navigation.indoor.position;
17
18 import com.irurueta.algebra.Matrix;
19 import com.irurueta.geometry.Point;
20 import com.irurueta.navigation.LockedException;
21 import com.irurueta.navigation.NotReadyException;
22 import com.irurueta.navigation.indoor.Fingerprint;
23 import com.irurueta.navigation.indoor.RadioSource;
24 import com.irurueta.navigation.indoor.RadioSourceLocated;
25 import com.irurueta.navigation.indoor.RangingAndRssiReading;
26 import com.irurueta.navigation.lateration.LaterationException;
27 import com.irurueta.navigation.lateration.LaterationSolver;
28 import com.irurueta.navigation.lateration.LaterationSolverListener;
29 import com.irurueta.navigation.lateration.NonLinearLeastSquaresLaterationSolver;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35 * Estimates position non-linearly using located radio sources and their ranging+RSSI
36 * readings at unknown locations.
37 * These kind of estimators can be used to determine the position of a given device by
38 * getting ranging+RSSI readings at an unknown location of different radio sources whose
39 * locations are known.
40 *
41 * @param <P> a {@link Point} type.
42 */
43 public abstract class NonLinearRangingAndRssiPositionEstimator<P extends Point<?>> extends
44 RangingAndRssiPositionEstimator<P> {
45
46 /**
47 * Distance standard deviation assumed for provided distances as a fallback when
48 * none can be determined.
49 */
50 public static final double FALLBACK_DISTANCE_STANDARD_DEVIATION =
51 NonLinearLeastSquaresLaterationSolver.DEFAULT_DISTANCE_STANDARD_DEVIATION;
52
53 /**
54 * A non-linear lateration solver to solve position.
55 */
56 protected NonLinearLeastSquaresLaterationSolver<P> trilaterationSolver;
57
58 /**
59 * Listener for the lateration solver.
60 */
61 protected LaterationSolverListener<P> laterationSolverListener;
62
63 /**
64 * Initial position to start position estimation.
65 * If not defined, centroid of provided located sources will be used.
66 */
67 private P initialPosition;
68
69 /**
70 * Indicates whether located radio source position covariance must be taken
71 * into account (if available) to determine distance standard deviation.
72 */
73 private boolean useRadioSourcePositionCovariance;
74
75 /**
76 * Distance standard deviation fallback value to use when none can be
77 * determined from provided radio sources and fingerprint readings.
78 */
79 private double fallbackDistanceStandardDeviation = FALLBACK_DISTANCE_STANDARD_DEVIATION;
80
81 /**
82 * Constructor.
83 */
84 protected NonLinearRangingAndRssiPositionEstimator() {
85 super();
86 init();
87 }
88
89 /**
90 * Constructor.
91 *
92 * @param listener listener in charge of handling events.
93 */
94 protected NonLinearRangingAndRssiPositionEstimator(final RangingAndRssiPositionEstimatorListener<P> listener) {
95 super(listener);
96 init();
97 }
98
99 /**
100 * Constructor.
101 *
102 * @param initialPosition initial position to start position estimation.
103 */
104 protected NonLinearRangingAndRssiPositionEstimator(final P initialPosition) {
105 this();
106 this.initialPosition = initialPosition;
107 }
108
109 /**
110 * Constructor.
111 *
112 * @param initialPosition initial position to start position estimation.
113 * @param listener listener in charge of handling events.
114 */
115 protected NonLinearRangingAndRssiPositionEstimator(
116 final P initialPosition, final RangingAndRssiPositionEstimatorListener<P> listener) {
117 this(listener);
118 this.initialPosition = initialPosition;
119 }
120
121 /**
122 * Gets initial position to start position estimation.
123 * If not defined, centroid of located sources position will be used to start
124 * the estimation.
125 *
126 * @return initial position to start position estimation.
127 */
128 public P getInitialPosition() {
129 return initialPosition;
130 }
131
132 /**
133 * Sets initial position to start position estimation.
134 * If not defined, centroid of located sources position will be used to start
135 * the estimation.
136 *
137 * @param initialPosition initial position to start position estimation.
138 * @throws LockedException if estimator is locked.
139 */
140 public void setInitialPosition(final P initialPosition) throws LockedException {
141 if (isLocked()) {
142 throw new LockedException();
143 }
144 this.initialPosition = initialPosition;
145 }
146
147 /**
148 * Indicates whether located radio source position covariance must be taken into
149 * account (if available) to determine distance standard deviation.
150 *
151 * @return true to take radio source position covariance into account, false
152 * otherwise.
153 */
154 public boolean isRadioSourcePositionCovarianceUsed() {
155 return useRadioSourcePositionCovariance;
156 }
157
158 /**
159 * Specifies whether located radio source position covariance must be taken into
160 * account (if available) to determine distance standard deviation.
161 *
162 * @param useRadioSourcePositionCovariance true to take radio source position
163 * covariance into account, false otherwise.
164 * @throws LockedException if estimator is locked.
165 */
166 public void setRadioSourcePositionCovarianceUsed(final boolean useRadioSourcePositionCovariance)
167 throws LockedException {
168 if (isLocked()) {
169 throw new LockedException();
170 }
171 this.useRadioSourcePositionCovariance = useRadioSourcePositionCovariance;
172 }
173
174 /**
175 * Gets distance standard deviation fallback value to use when none can be
176 * determined from provided radio sources and fingerprint readings.
177 *
178 * @return distance standard deviation to use as fallback.
179 */
180 public double getFallbackDistanceStandardDeviation() {
181 return fallbackDistanceStandardDeviation;
182 }
183
184 /**
185 * Sets distance standard deviation fallback value to use when none can be
186 * determined from provided radio sources and fingerprint readings.
187 *
188 * @param fallbackDistanceStandardDeviation distance standard deviation to use
189 * as fallback.
190 * @throws LockedException if estimator is locked.
191 */
192 public void setFallbackDistanceStandardDeviation(final double fallbackDistanceStandardDeviation) throws LockedException {
193 if (isLocked()) {
194 throw new LockedException();
195 }
196 this.fallbackDistanceStandardDeviation = fallbackDistanceStandardDeviation;
197 }
198
199 /**
200 * Gets minimum required number of located radio sources to perform lateration.
201 *
202 * @return minimum required number of located radio sources to perform lateration.
203 */
204 @Override
205 public int getMinRequiredSources() {
206 return trilaterationSolver.getMinRequiredPositionsAndDistances();
207 }
208
209 /**
210 * Indicates whether estimator is ready to find a solution.
211 *
212 * @return true if estimator is ready, false otherwise.
213 */
214 @Override
215 public boolean isReady() {
216 return trilaterationSolver.isReady();
217 }
218
219 /**
220 * Returns boolean indicating whether this estimator is locked because an estimation
221 * is already in progress.
222 *
223 * @return true if estimator is locked, false otherwise.
224 */
225 @Override
226 public boolean isLocked() {
227 return trilaterationSolver.isLocked();
228 }
229
230 /**
231 * Gets standard deviations of distances from known located radio sources to the
232 * location of provided readings in a fingerprint.
233 * Distance standard deviations are used internally to solve lateration.
234 *
235 * @return standard deviations used internally.
236 */
237 public double[] getDistanceStandardDeviations() {
238 return trilaterationSolver.getDistanceStandardDeviations();
239 }
240
241 /**
242 * Estimates position based on provided located radio sources and readings of such
243 * radio sources at an unknown location.
244 *
245 * @throws LockedException if estimator is locked.
246 * @throws NotReadyException if estimator is not ready.
247 * @throws PositionEstimationException if estimation fails for some other reason.
248 */
249 @Override
250 public void estimate() throws LockedException, NotReadyException, PositionEstimationException {
251 try {
252 trilaterationSolver.setInitialPosition(initialPosition);
253
254 trilaterationSolver.solve();
255 estimatedPositionCoordinates = trilaterationSolver.getEstimatedPositionCoordinates();
256 } catch (final LaterationException e) {
257 throw new PositionEstimationException(e);
258 }
259 }
260
261 /**
262 * Gets known positions of radio sources used internally to solve lateration.
263 *
264 * @return known positions used internally.
265 */
266 @Override
267 public P[] getPositions() {
268 return trilaterationSolver.getPositions();
269 }
270
271 /**
272 * Gets Euclidean distances from known located radio sources to the location of
273 * provided readings in a fingerprint.
274 * Distance values are used internally to solve lateration.
275 *
276 * @return Euclidean distances used internally.
277 */
278 @Override
279 public double[] getDistances() {
280 return trilaterationSolver.getDistances();
281 }
282
283 /**
284 * Gets estimated covariance matrix for estimated position.
285 *
286 * @return estimated covariance matrix for estimated position.
287 */
288 public Matrix getCovariance() {
289 return trilaterationSolver.getCovariance();
290 }
291
292 /**
293 * Internally sets located radio sources used for lateration.
294 *
295 * @param sources located radio sources used for lateration.
296 * @throws IllegalArgumentException if provided value is null or the number of
297 * provided sources is less than the required minimum.
298 */
299 @Override
300 protected void internalSetSources(final List<? extends RadioSourceLocated<P>> sources) {
301 super.internalSetSources(sources);
302 buildPositionsDistancesAndDistanceStandardDeviations();
303 }
304
305 /**
306 * Internally sets fingerprint containing readings at an unknown location for provided
307 * located radio sources.
308 *
309 * @param fingerprint fingerprint containing readings at an unknown location for
310 * provided located radio sources.
311 * @throws IllegalArgumentException if provided value is null.
312 */
313 @Override
314 protected void internalSetFingerprint(
315 final Fingerprint<? extends RadioSource, ? extends RangingAndRssiReading<?
316 extends RadioSource>> fingerprint) {
317 super.internalSetFingerprint(fingerprint);
318 buildPositionsDistancesAndDistanceStandardDeviations();
319 }
320
321 /**
322 * Sets positions, distances and standard deviations of distances on internal
323 * lateration solver.
324 *
325 * @param positions positions to be set.
326 * @param distances distances to be set.
327 * @param distanceStandardDeviations standard deviations of distances to be set.
328 */
329 protected abstract void setPositionsDistancesAndDistanceStandardDeviations(
330 final List<P> positions, List<Double> distances, final List<Double> distanceStandardDeviations);
331
332 /**
333 * Initializes lateration solver listener.
334 */
335 @SuppressWarnings("Duplicates")
336 private void init() {
337 laterationSolverListener = new LaterationSolverListener<P>() {
338 @Override
339 public void onSolveStart(final LaterationSolver<P> solver) {
340 if (listener != null) {
341 listener.onEstimateStart(NonLinearRangingAndRssiPositionEstimator.this);
342 }
343 }
344
345 @Override
346 public void onSolveEnd(final LaterationSolver<P> solver) {
347 if (listener != null) {
348 listener.onEstimateEnd(NonLinearRangingAndRssiPositionEstimator.this);
349 }
350 }
351 };
352 }
353
354 /**
355 * Builds positions, distances and standard deviation of distances for the internal
356 * lateration solver.
357 */
358 @SuppressWarnings("Duplicates")
359 private void buildPositionsDistancesAndDistanceStandardDeviations() {
360 if (trilaterationSolver == null) {
361 return;
362 }
363
364 final int min = getMinRequiredSources();
365 if (sources == null || fingerprint == null || sources.size() < min || fingerprint.getReadings() == null
366 || fingerprint.getReadings().size() < min) {
367 return;
368 }
369
370 final var positions = new ArrayList<P>();
371 final var distances = new ArrayList<Double>();
372 final var distanceStandardDeviations = new ArrayList<Double>();
373 PositionEstimatorHelper.buildPositionsDistancesAndDistanceStandardDeviations(
374 sources, fingerprint, useRadioSourcePositionCovariance, fallbackDistanceStandardDeviation, positions,
375 distances, distanceStandardDeviations);
376
377 setPositionsDistancesAndDistanceStandardDeviations(positions, distances, distanceStandardDeviations);
378 }
379 }