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.RangingReading;
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 readings
36   * at unknown locations.
37   * These kind of estimators can be used to determine the position of a given device by
38   * getting ranging 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 NonLinearRangingPositionEstimator<P extends Point<?>> extends RangingPositionEstimator<P> {
44  
45      /**
46       * Distance standard deviation assumed for provided distances as a fallback when
47       * none can be determined.
48       */
49      public static final double FALLBACK_DISTANCE_STANDARD_DEVIATION =
50              NonLinearLeastSquaresLaterationSolver.DEFAULT_DISTANCE_STANDARD_DEVIATION;
51  
52      /**
53       * A non-linear lateration solver to solve position.
54       */
55      protected NonLinearLeastSquaresLaterationSolver<P> trilaterationSolver;
56  
57      /**
58       * Listener for the lateration solver.
59       */
60      protected LaterationSolverListener<P> laterationSolverListener;
61  
62      /**
63       * Initial position to start position estimation.
64       * If not defined, centroid of provided located sources will be used.
65       */
66      private P initialPosition;
67  
68      /**
69       * Indicates whether located radio source position covariance must be taken
70       * into account (if available) to determine distance standard deviation.
71       */
72      private boolean useRadioSourcePositionCovariance;
73  
74      /**
75       * Distance standard deviation fallback value to use when none can be
76       * determined from provided radio sources and fingerprint readings.
77       */
78      private double fallbackDistanceStandardDeviation = FALLBACK_DISTANCE_STANDARD_DEVIATION;
79  
80      /**
81       * Constructor.
82       */
83      protected NonLinearRangingPositionEstimator() {
84          super();
85          init();
86      }
87  
88      /**
89       * Constructor.
90       *
91       * @param listener listener in charge of handling events.
92       */
93      protected NonLinearRangingPositionEstimator(final RangingPositionEstimatorListener<P> listener) {
94          super(listener);
95          init();
96      }
97  
98      /**
99       * Constructor.
100      *
101      * @param initialPosition initial position to start position estimation.
102      */
103     protected NonLinearRangingPositionEstimator(final P initialPosition) {
104         this();
105         this.initialPosition = initialPosition;
106     }
107 
108     /**
109      * Constructor.
110      *
111      * @param initialPosition initial position to start position estimation.
112      * @param listener        listener in charge of handling events.
113      */
114     protected NonLinearRangingPositionEstimator(
115             final P initialPosition, final RangingPositionEstimatorListener<P> listener) {
116         this(listener);
117         this.initialPosition = initialPosition;
118     }
119 
120     /**
121      * Gets initial position to start position estimation.
122      * If not defined, centroid of located sources position will be used to start
123      * the estimation.
124      *
125      * @return initial position to start position estimation.
126      */
127     public P getInitialPosition() {
128         return initialPosition;
129     }
130 
131     /**
132      * Sets initial position to start position estimation.
133      * If not defined, centroid of located sources position will be used to start
134      * the estimation.
135      *
136      * @param initialPosition initial position to start position estimation.
137      * @throws LockedException if estimator is locked.
138      */
139     public void setInitialPosition(final P initialPosition) throws LockedException {
140         if (isLocked()) {
141             throw new LockedException();
142         }
143         this.initialPosition = initialPosition;
144     }
145 
146     /**
147      * Indicates whether located radio source position covariance must be taken into
148      * account (if available) to determine distance standard deviation.
149      *
150      * @return true to take radio source position covariance into account, false
151      * otherwise.
152      */
153     public boolean isRadioSourcePositionCovarianceUsed() {
154         return useRadioSourcePositionCovariance;
155     }
156 
157     /**
158      * Specifies whether located radio source position covariance must be taken into
159      * account (if available) to determine distance standard deviation.
160      *
161      * @param useRadioSourcePositionCovariance true to take radio source position
162      *                                         covariance into account, false otherwise.
163      * @throws LockedException if estimator is locked.
164      */
165     public void setRadioSourcePositionCovarianceUsed(final boolean useRadioSourcePositionCovariance)
166             throws LockedException {
167         if (isLocked()) {
168             throw new LockedException();
169         }
170         this.useRadioSourcePositionCovariance = useRadioSourcePositionCovariance;
171     }
172 
173     /**
174      * Gets distance standard deviation fallback value to use when none can be
175      * determined from provided radio sources and fingerprint readings.
176      *
177      * @return distance standard deviation to use as fallback.
178      */
179     public double getFallbackDistanceStandardDeviation() {
180         return fallbackDistanceStandardDeviation;
181     }
182 
183     /**
184      * Sets distance standard deviation fallback value to use when none can be
185      * determined from provided radio sources and fingerprint readings.
186      *
187      * @param fallbackDistanceStandardDeviation distance standard deviation to use
188      *                                          as fallback.
189      * @throws LockedException if estimator is locked.
190      */
191     public void setFallbackDistanceStandardDeviation(final double fallbackDistanceStandardDeviation)
192             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 RangingReading<? extends RadioSource>> fingerprint) {
316         super.internalSetFingerprint(fingerprint);
317         buildPositionsDistancesAndDistanceStandardDeviations();
318     }
319 
320     /**
321      * Sets positions, distances and standard deviations of distances on internal
322      * lateration solver.
323      *
324      * @param positions                  positions to be set.
325      * @param distances                  distances to be set.
326      * @param distanceStandardDeviations standard deviations of distances to be set.
327      */
328     protected abstract void setPositionsDistancesAndDistanceStandardDeviations(
329             final List<P> positions, final List<Double> distances, final List<Double> distanceStandardDeviations);
330 
331     /**
332      * Initializes lateration solver listener.
333      */
334     @SuppressWarnings("Duplicates")
335     private void init() {
336         laterationSolverListener = new LaterationSolverListener<>() {
337             @Override
338             public void onSolveStart(final LaterationSolver<P> solver) {
339                 if (listener != null) {
340                     listener.onEstimateStart(NonLinearRangingPositionEstimator.this);
341                 }
342             }
343 
344             @Override
345             public void onSolveEnd(final LaterationSolver<P> solver) {
346                 if (listener != null) {
347                     listener.onEstimateEnd(NonLinearRangingPositionEstimator.this);
348                 }
349             }
350         };
351     }
352 
353     /**
354      * Builds positions, distances and standard deviation of distances for the internal
355      * lateration solver.
356      */
357     @SuppressWarnings("Duplicates")
358     private void buildPositionsDistancesAndDistanceStandardDeviations() {
359         if (trilaterationSolver == null) {
360             return;
361         }
362 
363         final var min = getMinRequiredSources();
364         if (sources == null || fingerprint == null || sources.size() < min || fingerprint.getReadings() == null
365                 || fingerprint.getReadings().size() < min) {
366             return;
367         }
368 
369         final var positions = new ArrayList<P>();
370         final var distances = new ArrayList<Double>();
371         final var distanceStandardDeviations = new ArrayList<Double>();
372         PositionEstimatorHelper.buildPositionsDistancesAndDistanceStandardDeviations(sources, fingerprint,
373                 useRadioSourcePositionCovariance, fallbackDistanceStandardDeviation, positions, distances,
374                 distanceStandardDeviations);
375 
376         setPositionsDistancesAndDistanceStandardDeviations(positions, distances, distanceStandardDeviations);
377     }
378 }