collision-with-point-clouds.py
Go to the documentation of this file.
1 # This examples shows how to perform collision detection between the end-effector of a robot and a point cloud depicted as a Height Field
2 # Note: this feature requires Meshcat to be installed, this can be done using
3 # pip install --user meshcat
4 
5 import pinocchio as pin
6 import hppfcl as fcl
7 import numpy as np
8 import sys
9 from os.path import dirname, join, abspath
10 
11 from pinocchio.visualize import MeshcatVisualizer
12 
13 # Load the URDF model.
14 # Conversion with str seems to be necessary when executing this file with ipython
15 pinocchio_model_dir = join(dirname(dirname(str(abspath(__file__)))), "models")
16 
17 model_path = join(pinocchio_model_dir, "example-robot-data/robots")
18 mesh_dir = pinocchio_model_dir
19 urdf_filename = "panda.urdf"
20 urdf_model_path = join(join(model_path, "panda_description/urdf"), urdf_filename)
21 
22 model, collision_model, visual_model = pin.buildModelsFromUrdf(
23  urdf_model_path, mesh_dir
24 )
25 
26 # Add point clouds
27 num_points = 5000
28 points = np.random.rand(3, num_points)
29 point_cloud_placement = (
30  pin.SE3.Identity()
31 ) # Placement of the point cloud wrt the WORLD frame
32 point_cloud_placement.translation = np.array([0.2, 0.2, -0.5])
33 
34 X = points[0, :]
35 Y = points[1, :]
36 Z = points[2, :]
37 
38 nx = 20
39 x_grid = np.linspace(0.0, 1.0, nx)
40 x_half_pad = 0.5 * (x_grid[1] - x_grid[0])
41 x_bins = np.digitize(X, x_grid + x_half_pad)
42 x_dim = x_grid[-1] - x_grid[0]
43 
44 ny = 20
45 y_grid = np.linspace(0.0, 1.0, ny)
46 y_half_pad = 0.5 * (y_grid[1] - y_grid[0])
47 y_bins = np.digitize(Y, y_grid + y_half_pad)
48 y_dim = y_grid[-1] - y_grid[0]
49 
50 point_bins = y_bins * nx + x_bins
51 heights = np.zeros((ny, nx))
52 np.maximum.at(heights.ravel(), point_bins, Z)
53 
54 point_cloud = fcl.BVHModelOBBRSS()
55 point_cloud.beginModel(0, num_points)
56 point_cloud.addVertices(points.T)
57 
58 height_field = fcl.HeightFieldOBBRSS(x_dim, y_dim, heights, min(Z))
59 height_field_placement = point_cloud_placement * pin.SE3(
60  np.eye(3), 0.5 * np.array([x_grid[0] + x_grid[-1], y_grid[0] + y_grid[-1], 0.0])
61 )
62 
63 go_point_cloud = pin.GeometryObject(
64  "point_cloud", 0, point_cloud_placement, point_cloud
65 )
66 go_point_cloud.meshColor = np.ones((4))
67 collision_model.addGeometryObject(go_point_cloud)
68 visual_model.addGeometryObject(go_point_cloud)
69 
70 go_height_field = pin.GeometryObject(
71  "height_field", 0, height_field_placement, height_field
72 )
73 go_height_field.meshColor = np.ones((4))
74 height_field_collision_id = collision_model.addGeometryObject(go_height_field)
75 visual_model.addGeometryObject(go_height_field)
76 
77 # Add colllision pair between the height field and the panda_hand geometry
78 panda_hand_collision_id = collision_model.getGeometryId("panda_hand_0")
79 go_panda_hand = collision_model.geometryObjects[panda_hand_collision_id]
80 go_panda_hand.geometry.buildConvexRepresentation(False)
81 go_panda_hand.geometry = (
82  go_panda_hand.geometry.convex
83 ) # We need to work with the convex hull of the real mesh
84 
85 collision_pair = pin.CollisionPair(height_field_collision_id, panda_hand_collision_id)
86 collision_model.addCollisionPair(collision_pair)
87 
88 # Start a new MeshCat server and client.
89 # Note: the server can also be started separately using the "meshcat-server" command in a terminal:
90 # this enables the server to remain active after the current script ends.
91 #
92 # Option open=True pens the visualizer.
93 # Note: the visualizer can also be opened seperately by visiting the provided URL.
94 try:
95  viz = MeshcatVisualizer(model, collision_model, visual_model)
96  viz.initViewer(open=True)
97 except ImportError as err:
98  print(
99  "Error while initializing the viewer. It seems you should install Python meshcat"
100  )
101  print(err)
102  sys.exit(0)
103 
104 # Load the robot in the viewer.
105 viz.loadViewerModel()
106 
107 # Display a robot configuration.
108 q0 = pin.neutral(model)
109 viz.display(q0)
110 
111 is_collision = False
112 data = model.createData()
113 collision_data = collision_model.createData()
114 while not is_collision:
115  q = pin.randomConfiguration(model)
116 
117  is_collision = pin.computeCollisions(
118  model, data, collision_model, collision_data, q, True
119  )
120 
121 print("Found a configuration in collision:", q)
122 viz.display(q)
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
pinocchio::cholesky::min
JointCollectionTpl const DataTpl< Scalar, Options, JointCollectionTpl > const Eigen::MatrixBase< Mat > & min
Definition: cholesky.hpp:91
pinocchio.visualize.meshcat_visualizer.MeshcatVisualizer
Definition: meshcat_visualizer.py:532
path


pinocchio
Author(s):
autogenerated on Sat Jun 1 2024 02:40:33