Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. /*
  2. * This method returns the range reading from the sensor mounted on top of robot.
  3. * It uses x and y as the actual position of the robot/particle and then creates Vx and Vy as virtual x and y.
  4. * These virtual x and y loop from the current position till some obstruction is there and tell us distance till there.
  5. */
  6.  
  7. private int calculateRange(double x, double y, double Vx, double Vy, int counter, int loop_counter)
  8. {
  9. while(robotIsWithinBoundary(Vx, Vy))
  10. {
  11. int pace = 2;
  12. Vx += pace* Math.sin(Math.toRadians(robot_orientation));
  13. Vy += pace* Math.cos(Math.toRadians(robot_orientation));
  14. counter++;
  15. Line2D line1 = new Line2D.Double(x,y,Vx,Vy);
  16. if(line1.intersects(obst1))
  17. {
  18. //System.out.println("Distance to obst1:"+counter);
  19. loop_counter++;
  20. break;
  21. }
  22. if(line1.intersects(obst2))
  23. {
  24. //System.out.println("Distance to obst2:"+counter);
  25. loop_counter++;
  26. break;
  27. }
  28.  
  29. }
  30. return counter;
  31. }
  32.  
  33. /*
  34. * This method tells us whether the robot/particle is within boundary or not.
  35. */
  36. private boolean robotIsWithinBoundary(double x, double y)
  37. {
  38. boolean verdict = true;
  39. if(x>680||x<0)
  40. {
  41. verdict = false;
  42. }
  43. if(y<0||y>450)
  44. {
  45. verdict = false;
  46. }
  47. return verdict;
  48. } /*
  49. * This method returns the range reading from the sensor mounted on top of robot.
  50. * It uses x and y as the actual position of the robot/particle and then creates Vx and Vy as virtual x and y.
  51. * These virtual x and y loop from the current position till some obstruction is there and tell us distance till there.
  52. */
  53. private int calculateRange(double x, double y, double Vx, double Vy, int counter, int loop_counter)
  54. {
  55. while(robotIsWithinBoundary(Vx, Vy))
  56. {
  57. int pace = 2;
  58. Vx += pace* Math.sin(Math.toRadians(robot_orientation));
  59. Vy += pace* Math.cos(Math.toRadians(robot_orientation));
  60. counter++;
  61. Line2D line1 = new Line2D.Double(x,y,Vx,Vy);
  62. if(line1.intersects(obst1))
  63. {
  64. //System.out.println("Distance to obst1:"+counter);
  65. loop_counter++;
  66. break;
  67. }
  68. if(line1.intersects(obst2))
  69. {
  70. //System.out.println("Distance to obst2:"+counter);
  71. loop_counter++;
  72. break;
  73. }
  74.  
  75. }
  76. return counter;
  77. }
  78.  
  79. /*
  80. * This method tells us whether the robot/particle is within boundary or not.
  81. */
  82. private boolean robotIsWithinBoundary(double x, double y)
  83. {
  84. boolean verdict = true;
  85. if(x>680||x<0)
  86. {
  87. verdict = false;
  88. }
  89. if(y<0||y>450)
  90. {
  91. verdict = false;
  92. }
  93. return verdict;
  94. }
  95.  
  96. /*
  97. * This method calculates the importance weights for the particles based on the robot_range which is
  98. * the reading of the range sensor for the robot.
  99. */
  100. private double measurementProbability(int index)
  101. {
  102. double probability=1;
  103. double particle_x_position=particleListX.get(index);
  104. double particle_y_position=particleListY.get(index);
  105. double particle_Vx=particle_x_position;
  106. double particle_Vy=particle_y_position;
  107. int range_counter=0;
  108. int loop_counter=0;
  109.  
  110. int distance = calculateRange(particle_x_position, particle_x_position, particle_Vx, particle_Vy ,range_counter, loop_counter);
  111. probability *= calculateGaussianDistance(distance, senseNoise, robot_range);
  112. //System.out.println(probability);
  113. return probability;
  114. }
  115.  
  116. private double calculateGaussianDistance(double mu, double sigma, double x )
  117. {
  118. double gDistance=Math.exp(-(((Math.pow((mu - x),2))/(Math.pow(sigma,2)) / 2.0) / (Math.sqrt(2.0 * Math.PI * (Math.pow(sigma,2))))));
  119. return gDistance;
  120. }
  121.  
  122. sample += randomDouble(0, maxWeight);
  123. while(sample > particleListProbability.get(index))
  124. {
  125. sample -= particleListProbability.get(index);
  126. index = (index +1) % n;
  127. }
  128. return index;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement