8 from pathlib
import Path
12 import pinocchio
as pin
17 pinocchio_model_dir = Path(__file__).parent.parent /
"models"
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
24 model, collision_model, visual_model = pin.buildModelsFromUrdf(
25 urdf_model_path, mesh_dir
30 points = np.random.rand(3, num_points)
31 point_cloud_placement = (
34 point_cloud_placement.translation = np.array([0.2, 0.2, -0.5])
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]
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]
52 point_bins = y_bins * nx + x_bins
53 heights = np.zeros((ny, nx))
54 np.maximum.at(heights.ravel(), point_bins, Z)
56 point_cloud = fcl.BVHModelOBBRSS()
57 point_cloud.beginModel(0, num_points)
58 point_cloud.addVertices(points.T)
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])
65 go_point_cloud = pin.GeometryObject(
66 "point_cloud", 0, point_cloud_placement, point_cloud
68 go_point_cloud.meshColor = np.ones(4)
69 collision_model.addGeometryObject(go_point_cloud)
70 visual_model.addGeometryObject(go_point_cloud)
72 go_height_field = pin.GeometryObject(
73 "height_field", 0, height_field_placement, height_field
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)
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
87 collision_pair = pin.CollisionPair(height_field_collision_id, panda_hand_collision_id)
88 collision_model.addCollisionPair(collision_pair)
99 viz.initViewer(open=
True)
100 except ImportError
as err:
102 "Error while initializing the viewer. "
103 "It seems you should install Python meshcat"
109 viz.loadViewerModel()
112 q0 = pin.neutral(model)
116 data = model.createData()
117 collision_data = collision_model.createData()
118 while not is_collision:
119 q = pin.randomConfiguration(model)
121 is_collision = pin.computeCollisions(
122 model, data, collision_model, collision_data, q,
True
125 print(
"Found a configuration in collision:", q)