Herowaree

Text

Mar 7th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. function PF = generate_initial_PF(fov_hor, fov_ver, min_depth, max_depth, number_particles)
  2. particles_top = floor(number_particles * 0.75);
  3. particles_bottom = number_particles - particles_top;
  4. depth_slice = (max_depth - min_depth) / 2 + min_depth;
  5.  
  6. depth_bottom = min_depth + (depth_slice - min_depth) * rand(particles_bottom, 1);
  7. depth_top = depth_slice + (max_depth - depth_slice) * rand(particles_top, 1);
  8.  
  9. depth = [depth_bottom; depth_top];
  10.  
  11. PF = zeros(number_particles, 3);
  12.  
  13. for i = 1:number_particles
  14. PF(i, 1) = - tand(fov_hor / 2) * depth(i, 1) + ( 2 * tand(fov_hor / 2) * depth(i, 1)) * rand(1,1);
  15. PF(i, 2) = - tand(fov_ver / 2) * depth(i, 1) + ( 2 * tand(fov_ver / 2) * depth(i, 1)) * rand(1,1);
  16. PF(i, 3) = depth(i, 1);
  17. end
  18.  
  19. scatter3(PF(:,3), PF(:,2), PF(:,1),'.');
  20.  
  21. %{
  22. scatter(PF(:,1), PF(:,3), '.');
  23. hold on;
  24. x = min_depth:max_depth;
  25. y = tand(fov_hor / 2)*x;
  26. plot(y, x, 'k');
  27. y2 = -tand(fov_hor / 2)*x;
  28. plot(y2, x, 'k');
  29.  
  30. figure;
  31.  
  32. scatter(PF(:,2), PF(:,3), '.');
  33. hold on;
  34. x = min_depth:max_depth;
  35. y3 = tand(fov_ver / 2)*x;
  36. plot(y3, x, 'k');
  37. y4 = -tand(fov_ver / 2)*x;
  38. plot(y4, x, 'k');
  39.  
  40. figure;
  41.  
  42. x = 1:number_particles;
  43. scatter(x, PF(:,3), '.');
  44. %}
  45. end
  46.  
  47. camera_x = (ball_u - camera_info.K[2]) * camera_z / camera_info.K[0]
  48. camera_y = (ball_v - camera_info.K[5]) * camera_z / camera_info.K[4]
  49.  
  50. camera_x = -camera_x
  51. camera_y = -camera_y
  52.  
  53. # From camera to robot
  54. camera_trans_x = -0.125
  55. camera_trans_y = 0.0
  56. camera_trans_z = 0.25
  57.  
  58. robot_x = camera_z + camera_trans_x
  59. robot_y = camera_x + camera_trans_y
  60. robot_z = camera_y + camera_trans_z
  61.  
  62. # From robot to world
  63. theta = 2*math.atan2(odom.pose.pose.orientation.z, odom.pose.pose.orientation.w)
  64. world_x = math.cos(-theta)*robot_x - math.sin(-theta)*robot_y
  65. world_y = math.sin(-theta)*robot_x + math.cos(-theta)*robot_y
  66. world_z = robot_z
  67.  
  68. world_x = world_x + odom.pose.pose.position.x
  69. world_y = world_y + odom.pose.pose.position.y
  70.  
  71.  
  72. def create_uniform_particles(num_particles, fov_vertical=62, fov_horizontal=48.6, max_depth=3.0, min_depth=0.9):
  73. particles = np.empty((num_particles, 3))
  74. particles[:, 2] = uniform(min_depth, max_depth, size=num_particles)
  75. particles[:, 0] = -tan(fov_vertical / 2) * particles[:, 2] + (2 * tan(fov_vertical / 2) * particles[:, 2]) * uniform(0, 1)
  76. particles[:, 1] = -tan(fov_horizontal / 2) * particles[:, 2] + (2 * tan (fov_vertical / 2) * particles[:, 2]) * uniform(0, 1)
  77.  
  78. return particles
  79.  
  80. # TODO: Como prever o próximo estado da bola?
  81. def predict(particles, u, std, dt=1.0):
  82. return particles
  83.  
  84. def update(particles, weights, z, R, landmarks):
  85. for i, landmark in enumerate(landmarks):
  86. distance = np.linalg.norm(particles - landmark, axis=1)
  87. weights *= scipy.stats.norm(distance, R).pdf(z[i])
  88.  
  89. weights += 1.e-300
  90. weights /= sum(weights)
  91.  
  92. def estimate(particles, weights):
  93. pos = particles
  94. mean = np.average(pos, weights=weights, axis=0)
  95. var = np.average((pos-mean)**2, weights=weights, axis=0)
  96.  
  97. return mean, var
  98.  
  99.  
  100. num_particles = 500
  101. landmarks = np.array([[1.4, 2.2, 2.8], [1.5, 1.8, 2], [0.4, -1, 1.5]])
  102. nl = len(landmarks)
  103. sensor_std_err = .1
  104.  
  105. # Cálculo do Aamir
  106. robot_number = 0
  107. zs = []
  108. for r in range(0, num_robots):
  109. if r != robot_number:
  110. zs.append(confidence)
  111.  
  112. particles = create_uniform_particles(num_particles)
  113. weights = np.ones(num_particles)
  114.  
  115. predict(particles, u=(0.0, 0.0), std=(0.1, 0.1))
  116.  
  117. update(particles, weights, z=zs, R=sensor_std_err, landmarks=landmarks)
  118.  
  119. mean, var = estimate(particles, weights)
  120. print(mean)
  121. print(var)
Advertisement
Add Comment
Please, Sign In to add comment