Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. package Location;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import Utils.*;
  8.  
  9. /**
  10. * Simple Location finder that returns the first known APs location from the list of received MAC addresses
  11. * @author Bernd
  12. *
  13. */
  14. public class CustomLocationFinder implements LocationFinder{
  15.  
  16. private HashMap<String, Position> knownLocations; //Contains the known locations of APs. The long is a MAC address.
  17. private Position lastPosition = new Position(0, 0);
  18. private PositionBuffer positionHistory = new PositionBuffer(10);
  19.  
  20. public CustomLocationFinder(){
  21. knownLocations = Utils.getKnownLocations(); //Put the known locations in our hashMap
  22. }
  23.  
  24. @Override
  25. public Position locate(MacRssiPair[] data) {
  26. //printMacs(data); //print all the received data
  27. return getFirstKnownFromList(data); //return the first known APs location
  28. }
  29.  
  30. /**
  31. * Returns the position of the first known AP found in the list of MacRssi pairs
  32. * @param data
  33. * @return
  34. */
  35. private Position getFirstKnownFromList(MacRssiPair[] data){
  36. List<MacRssiPair> locations = selectKnownPairs(data);
  37. locations = selectStrongestPairs(locations, 3);
  38.  
  39. if(locations.size() == 3) {
  40. MacRssiPair pairA = locations.get(0);
  41. MacRssiPair pairB = locations.get(1);
  42. MacRssiPair pairC = locations.get(2);
  43. Position posA = knownLocations.get(pairA.getMacAsString());
  44. Position posB = knownLocations.get(pairB.getMacAsString());
  45. Position posC = knownLocations.get(pairC.getMacAsString());
  46. double factorAB = getLerpFactor(pairA, pairB);
  47. double factorAC = getLerpFactor(pairA, pairC);
  48. double factorBC = getLerpFactor(pairB, pairC);
  49. Position posAB = lerpPosition(posA, posB, factorAB);
  50. Position posAC = lerpPosition(posA, posC, factorAC);
  51. Position posBC = lerpPosition(posAB, posAC, factorBC);
  52.  
  53. System.out.println("[Locating with 3 signals]");
  54. System.out.printf("%s %s%n", pairA, posA);
  55. System.out.printf("%s %s%n", pairB, posB);
  56. System.out.printf("%s %s%n", pairC, posC);
  57. System.out.println("Factors");
  58. System.out.printf("AB: %f%n", factorAB);
  59. System.out.printf("AC: %f%n", factorAC);
  60. System.out.printf("BC: %f%n", factorBC);
  61.  
  62. //lastPosition = posBC;
  63. positionHistory.add(posBC);
  64. } else if(locations.size() > 0) {
  65. MacRssiPair pair = locations.get(0);
  66. String mac = pair.getMacAsString();
  67.  
  68. System.out.println("[Locating with 1 signal]");
  69. System.out.println(pair);
  70.  
  71. //lastPosition = knownLocations.get(mac);
  72. positionHistory.add(knownLocations.get(mac));
  73. } else {
  74. System.out.println("[No data, returning last known position]");
  75. }
  76.  
  77. //return lastPosition;
  78. return positionHistory.average();
  79. }
  80.  
  81. private List<MacRssiPair> selectKnownPairs(MacRssiPair[] data) {
  82. List<MacRssiPair> result = new ArrayList<MacRssiPair>();
  83.  
  84. for(MacRssiPair pair : data) {
  85. if(knownLocations.containsKey(pair.getMacAsString())) {
  86. result.add(pair);
  87. }
  88. }
  89.  
  90. return result;
  91. }
  92.  
  93. private List<MacRssiPair> selectStrongestPairs(List<MacRssiPair> data, int count) {
  94. List<MacRssiPair> result = new ArrayList<MacRssiPair>();
  95.  
  96. for(int i = 0; i < count && count < data.size(); i++) {
  97. MacRssiPair pair = getStrongestPair(data);
  98.  
  99. result.add(pair);
  100. data.remove(pair);
  101. }
  102.  
  103. return result;
  104. }
  105.  
  106. private MacRssiPair getStrongestPair(List<MacRssiPair> data) {
  107. int max = Integer.MIN_VALUE;
  108. MacRssiPair result = null;
  109.  
  110. for(MacRssiPair pair : data) {
  111. if(pair.getRssi() >= max) {
  112. result = pair;
  113. max = pair.getRssi();
  114. }
  115. }
  116.  
  117. return result;
  118. }
  119.  
  120. /**
  121. * Outputs all the received MAC RSSI pairs to the standard out
  122. * This method is provided so you can see the data you are getting
  123. * @param data
  124. */
  125. private void printMacs(MacRssiPair[] data) {
  126. for (MacRssiPair pair : data) {
  127. System.out.println(pair);
  128. }
  129. }
  130.  
  131. private static double getLerpFactor(MacRssiPair a, MacRssiPair b) {
  132. // diff 0 = factor 0.5
  133. // huge diff (eg 100) = factor 0
  134. MacRssiPair strongest = a.getRssi() > b.getRssi() ? a : b;
  135. MacRssiPair weakest = strongest == a ? b : a;
  136.  
  137. double exp = 0.25d;
  138. int difference = -weakest.getRssi() + strongest.getRssi();
  139. double factor = 0.5d - (Math.pow(difference / 100d, exp) * 0.5d);
  140.  
  141. // Linear blend
  142. //double factor = 0.5d - ((difference / 100d) * 0.5d);
  143.  
  144. return factor;
  145. }
  146.  
  147. private static Position lerpPosition(Position a, Position b, double f) {
  148. double x = (1.0d - f) * a.getX() + f * b.getX();
  149. double y = (1.0d - f) * a.getY() + f * b.getY();
  150.  
  151. return new Position(x, y);
  152. }
  153. }
  154.  
  155. class PositionBuffer {
  156. private int size;
  157. private Position[] positions;
  158.  
  159. public PositionBuffer(int size) {
  160. this.size = size;
  161. this.positions = new Position[size];
  162.  
  163. for(int i = 0; i < size; i++) {
  164. positions[i] = new Position(0, 0);
  165. }
  166. }
  167.  
  168. public void add(Position position) {
  169. System.arraycopy(positions, 0, positions, 1, size - 1);
  170. positions[0] = position;
  171. }
  172.  
  173. public Position average() {
  174. double x = 0.0d;
  175. double y = 0.0d;
  176.  
  177. for(Position pos : positions) {
  178. x += pos.getX();
  179. y += pos.getY();
  180. }
  181.  
  182. return new Position(x / size, y / size);
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement