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


pinocchio
Author(s):
autogenerated on Mon Dec 16 2024 03:41:00