Nalyd1002

matplotlib

Apr 28th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import pybullet as p
  2. import numpy as np
  3. import pybullet_data
  4. import time
  5.  
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8.  
  9. plt.style.use('seaborn-poster')
  10.  
  11.  
  12. # Initialize the physics simulation
  13. physicsClient = p.connect(p.GUI)
  14. p.setAdditionalSearchPath(pybullet_data.getDataPath())
  15.  
  16. # Load the objects into the simulation
  17. planeId = p.loadURDF("plane.urdf")
  18. sphereId = p.loadURDF("sphere_small.urdf", [0, 1, 0])
  19. cylinderId = p.loadURDF("sphere_small.urdf", [0.5, 1, 0])
  20. robotId = p.loadURDF("r2d2.urdf", [0.5,0.5,0.5],useFixedBase=True)
  21.  
  22. # Get the positions of the objects
  23. spherePos, sphereOrn = p.getBasePositionAndOrientation(sphereId)
  24. cylinderPos, cylinderOrn = p.getBasePositionAndOrientation(cylinderId)
  25.  
  26. # Calculate the distance between the two objects
  27. distance = np.linalg.norm(np.array(spherePos) - np.array(cylinderPos))
  28.  
  29. print("The distance between the sphere and cylinder is: ", distance)
  30.  
  31.  
  32. while True:
  33. # Get the image from the camera
  34. camera_pos = [0, 1, 2]
  35. viewMatrix = p.computeViewMatrix(
  36. cameraEyePosition=camera_pos,
  37. cameraTargetPosition=[0, 1, 0],
  38. cameraUpVector=[0, 1, 0])
  39.  
  40. # Set the intrinsic parameters of the camera
  41. fov = 60
  42. aspect = 320.0 / 240.0
  43. near = 0.01
  44. far = 100
  45.  
  46. # Get the image from the camera
  47. width, height, rgbImg, depthImg, segImg = p.getCameraImage(width=320, height=240, viewMatrix=viewMatrix,
  48. projectionMatrix=p.computeProjectionMatrixFOV(fov,
  49. aspect,
  50. near, far))
  51. print("1")
  52. print(depthImg)
  53. print(len(depthImg))
  54.  
  55. # Convert the depth image to a point cloud
  56. points = np.zeros((height * width, 3))
  57. for y in range(height):
  58. for x in range(width):
  59. depth = depthImg[y, x]
  60. if depth != 0:
  61. points[y * width + x, 0] = (x - 160) * depth / 160.0
  62. points[y * width + x, 1] = (y - 120) * depth / 120.0
  63. points[y * width + x, 2] = depth
  64. print("2")
  65. points.sort()
  66. print(points)
  67. print(len(points))
  68. x = np.zeros(len(points))
  69. y = np.zeros(len(points))
  70. z = np.zeros(len(points))
  71. for i in range(len(points)):
  72. x[i] = points[i,0]
  73. print(x)
  74. y[i] = points[i,1]
  75. z[i] = points[i,2]
  76. if z[i] > 0.00001:
  77. print([x[i],y[i],z[i]])
  78. print('ok')
  79. if i > 1 and z[i] - z[i - 1] > 0.2:
  80. z[i]
  81.  
  82. fig = plt.figure(figsize=(10, 10))
  83. ax = plt.axes(projection='3d')
  84. ax.grid()
  85.  
  86. ax.scatter(x, y, z, c='r', s=0.01)
  87. ax.set_title('3D Scatter Plot')
  88.  
  89. # Set axes label
  90. ax.set_xlabel('x', labelpad=20)
  91. ax.set_ylabel('y', labelpad=20)
  92. ax.set_zlabel('z', labelpad=20)
  93.  
  94. plt.show()
  95. # Find the points closest to the sphere and cylinder
  96. spherePoint = points[np.argmin(np.linalg.norm(points - np.array(spherePos), axis=1))]
  97. cylinderPoint = points[np.argmin(np.linalg.norm(points - np.array(cylinderPos), axis=1))]
  98.  
  99. # Calculate the distance between the two objects based on the point cloud
  100. distanceFromCamera = np.linalg.norm(np.array(spherePoint)-np.array(cylinderPoint))
  101. print("The distance between the sphere and cylinder based on the camera is: ", distanceFromCamera)
  102.  
  103. p.stepSimulation()
  104. time.sleep(1. / 240.)
Advertisement
Add Comment
Please, Sign In to add comment