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.geometry.Point;
19  import com.irurueta.navigation.LockedException;
20  import com.irurueta.navigation.NotReadyException;
21  import com.irurueta.navigation.indoor.Fingerprint;
22  import com.irurueta.navigation.indoor.RadioSource;
23  import com.irurueta.navigation.indoor.RadioSourceLocated;
24  import com.irurueta.navigation.indoor.RssiReading;
25  import com.irurueta.navigation.lateration.HomogeneousLinearLeastSquaresLaterationSolver;
26  import com.irurueta.navigation.lateration.InhomogeneousLinearLeastSquaresLaterationSolver;
27  import com.irurueta.navigation.lateration.LaterationException;
28  import com.irurueta.navigation.lateration.LaterationSolver;
29  import com.irurueta.navigation.lateration.LaterationSolverListener;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * Linearly estimates position using located radio sources and their RSSI readings at
36   * unknown locations.
37   * These kind of estimators can be used to determine the position of a given device by
38   * getting RSSI readings at an unknown location of different radio sources whose locations
39   * are known.
40   *
41   * @param <P> a {@link Point} type.
42   */
43  public abstract class LinearRssiPositionEstimator<P extends Point<P>> extends RssiPositionEstimator<P> {
44  
45      /**
46       * Indicates that by default an homogeneous linear solver is used to estimate
47       * position.
48       */
49      public static final boolean DEFAULT_USE_HOMOGENEOUS_LINEAR_SOLVER = true;
50  
51      /**
52       * An homogeneous linear lateration solver to solve position.
53       */
54      protected HomogeneousLinearLeastSquaresLaterationSolver<P> homogeneousTrilaterationSolver;
55  
56      /**
57       * An inhomogeneous linear lateration solver to solve position.
58       */
59      protected InhomogeneousLinearLeastSquaresLaterationSolver<P> inhomogeneousTrilaterationSolver;
60  
61      /**
62       * Listener for the lateration solver.
63       */
64      protected LaterationSolverListener<P> laterationSolverListener;
65  
66      /**
67       * Indicates whether an homogeneous linear solver is used to estimate position.
68       */
69      protected boolean useHomogeneousLinearSolver = DEFAULT_USE_HOMOGENEOUS_LINEAR_SOLVER;
70  
71      /**
72       * Constructor.
73       */
74      protected LinearRssiPositionEstimator() {
75          super();
76          init();
77      }
78  
79      /**
80       * Constructor.
81       *
82       * @param listener listener in charge of handling events.
83       */
84      protected LinearRssiPositionEstimator(final RssiPositionEstimatorListener<P> listener) {
85          super(listener);
86          init();
87      }
88  
89      /**
90       * Indicates whether an homogeneous linear solver is used to estimate position.
91       *
92       * @return true if homogeneous linear solver is used, false if an inhomogeneous
93       * linear one is used instead.
94       */
95      public boolean isHomogeneousLinearSolverUsed() {
96          return useHomogeneousLinearSolver;
97      }
98  
99      /**
100      * Specifies whether an homogeneous linear solver is used to estimate position.
101      *
102      * @param useHomogeneousLinearSolver true if homogeneous linear solver is used, false
103      *                                   if an inhomogeneous linear one is used instead.
104      * @throws LockedException if estimator is locked.
105      */
106     public void setHomogeneousLinearSolverUsed(final boolean useHomogeneousLinearSolver) throws LockedException {
107         if (isLocked()) {
108             throw new LockedException();
109         }
110 
111         this.useHomogeneousLinearSolver = useHomogeneousLinearSolver;
112     }
113 
114     /**
115      * Gets minimum required number of located radio sources to perform lateration.
116      *
117      * @return minimum required number of located radio sources to perform lateration.
118      */
119     @Override
120     public int getMinRequiredSources() {
121         return useHomogeneousLinearSolver ? homogeneousTrilaterationSolver.getMinRequiredPositionsAndDistances()
122                 : inhomogeneousTrilaterationSolver.getMinRequiredPositionsAndDistances();
123     }
124 
125     /**
126      * Indicates whether estimator is ready to find a solution.
127      *
128      * @return true if estimator is ready, false otherwise.
129      */
130     @Override
131     public boolean isReady() {
132         return (!useHomogeneousLinearSolver && inhomogeneousTrilaterationSolver.isReady())
133                 || (useHomogeneousLinearSolver && homogeneousTrilaterationSolver.isReady());
134     }
135 
136     /**
137      * Returns boolean indicating whether this estimator is locked because an estimation is already in progress.
138      *
139      * @return true if estimator is locked, false otherwise.
140      */
141     @Override
142     public boolean isLocked() {
143         return inhomogeneousTrilaterationSolver.isLocked() || homogeneousTrilaterationSolver.isLocked();
144     }
145 
146     /**
147      * Estimates position based on provided located radio sources and RSSI readings of
148      * such radio sources at an unknown location.
149      *
150      * @throws LockedException             if estimator is locked.
151      * @throws NotReadyException           if estimator is not ready.
152      * @throws PositionEstimationException if estimation fails for some other reason.
153      */
154     @SuppressWarnings("Duplicates")
155     @Override
156     public void estimate() throws LockedException, NotReadyException, PositionEstimationException {
157         try {
158             if (useHomogeneousLinearSolver) {
159                 homogeneousTrilaterationSolver.solve();
160                 estimatedPositionCoordinates = homogeneousTrilaterationSolver.getEstimatedPositionCoordinates();
161             } else {
162                 inhomogeneousTrilaterationSolver.solve();
163                 estimatedPositionCoordinates = inhomogeneousTrilaterationSolver.getEstimatedPositionCoordinates();
164             }
165         } catch (final LaterationException e) {
166             throw new PositionEstimationException(e);
167         }
168     }
169 
170     /**
171      * Gets known positions of radio sources used internally to solve lateration.
172      *
173      * @return known positions used internally.
174      */
175     @Override
176     public P[] getPositions() {
177         return useHomogeneousLinearSolver ? homogeneousTrilaterationSolver.getPositions()
178                 : inhomogeneousTrilaterationSolver.getPositions();
179     }
180 
181     /**
182      * Gets Euclidean distances from known located radio sources to
183      * the location of provided readings in a fingerprint.
184      * Distance values are used internally to solve lateration.
185      *
186      * @return Euclidean distances used internally.
187      */
188     @Override
189     public double[] getDistances() {
190         return useHomogeneousLinearSolver ? homogeneousTrilaterationSolver.getDistances()
191                 : inhomogeneousTrilaterationSolver.getDistances();
192     }
193 
194     /**
195      * Internally sets located radio sources used for lateration.
196      *
197      * @param sources located radio sources used for lateration.
198      * @throws IllegalArgumentException if provided value is null or the number of
199      *                                  provided sources is less than the required
200      *                                  minimum.
201      */
202     @Override
203     protected void internalSetSources(final List<? extends RadioSourceLocated<P>> sources) {
204         super.internalSetSources(sources);
205         buildPositionsAndDistances();
206     }
207 
208     /**
209      * Internally sets fingerprint containing readings at an unknown location for provided
210      * located radio sources.
211      *
212      * @param fingerprint fingerprint containing readings at an unknown location for
213      *                    provided located radio sources.
214      * @throws IllegalArgumentException if provided value is null.
215      */
216     @Override
217     protected void internalSetFingerprint(
218             final Fingerprint<? extends RadioSource, ? extends RssiReading<? extends RadioSource>> fingerprint) {
219         super.internalSetFingerprint(fingerprint);
220         buildPositionsAndDistances();
221     }
222 
223     /**
224      * Sets positions and distances on internal lateration solver.
225      *
226      * @param positions positions to be set.
227      * @param distances distances to be set.
228      */
229     protected abstract void setPositionsAndDistances(final List<P> positions, final List<Double> distances);
230 
231     /**
232      * Initializes lateration solver listener.
233      */
234     @SuppressWarnings("Duplicates")
235     private void init() {
236         laterationSolverListener = new LaterationSolverListener<>() {
237             @Override
238             public void onSolveStart(final LaterationSolver<P> solver) {
239                 if (listener != null) {
240                     listener.onEstimateStart(LinearRssiPositionEstimator.this);
241                 }
242             }
243 
244             @Override
245             public void onSolveEnd(final LaterationSolver<P> solver) {
246                 if (listener != null) {
247                     listener.onEstimateEnd(LinearRssiPositionEstimator.this);
248                 }
249             }
250         };
251     }
252 
253     /**
254      * Builds positions and distances for the internal lateration solver.
255      */
256     @SuppressWarnings("Duplicates")
257     private void buildPositionsAndDistances() {
258         if ((useHomogeneousLinearSolver && homogeneousTrilaterationSolver == null)
259                 || (!useHomogeneousLinearSolver && inhomogeneousTrilaterationSolver == null)) {
260             return;
261         }
262 
263         final var min = getMinRequiredSources();
264         if (sources == null || fingerprint == null || sources.size() < min || fingerprint.getReadings() == null
265                 || fingerprint.getReadings().size() < min) {
266             return;
267         }
268 
269         final var positions = new ArrayList<P>();
270         final var distances = new ArrayList<Double>();
271         PositionEstimatorHelper.buildPositionsAndDistances(sources, fingerprint, positions, distances);
272 
273         setPositionsAndDistances(positions, distances);
274     }
275 }