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