reachable-workspace.py
Go to the documentation of this file.
1 import meshcat.geometry as g
2 import pinocchio as pin
3 import sys
4 from os.path import dirname, join, abspath
5 import time
6 import numpy as np
7 
8 from pinocchio.visualize import MeshcatVisualizer
9 
10 # Load the URDF model.
11 pinocchio_model_dir = join(dirname(dirname(str(abspath(__file__)))), "models")
12 
13 model_path = join(pinocchio_model_dir, "example-robot-data/robots")
14 mesh_dir = pinocchio_model_dir
15 
16 urdf_filename = "panda.urdf"
17 urdf_model_path = join(join(model_path, "panda_description/urdf"), urdf_filename)
18 
19 robot, collision_model, visual_model = pin.buildModelsFromUrdf(
20  urdf_model_path, mesh_dir
21 )
22 data = robot.createData()
23 
24 # Start a new MeshCat server and client.
25 viz = MeshcatVisualizer(robot, collision_model, visual_model)
26 try:
27  viz.initViewer(open=True)
28 except ImportError as err:
29  print(
30  "Error while initializing the viewer. It seems you should install Python meshcat"
31  )
32  print(err)
33  sys.exit(0)
34 
35 # # Load the robot in the viewer.
36 viz.loadViewerModel()
37 # Display a robot configuration.
38 q0 = (robot.upperPositionLimit.T + robot.lowerPositionLimit.T) / 2
39 horizon = 0.2 # seconds
40 frame = robot.getFrameId(
41  robot.frames[-1].name
42 ) # for example the last frame of the robot
43 n_samples = 5
44 facet_dims = 2
45 
46 # To have convex hull computation or just the points of the reachable workspace and then compute it with cgal
47 convex = True
48 
49 if convex:
50  verts, faces = pin.reachableWorkspaceHull(
51  robot, q0, horizon, frame, n_samples, facet_dims
52  )
53  verts = verts.T
54 
55 else:
56  try:
57  from CGAL.CGAL_Alpha_wrap_3 import *
58  from CGAL.CGAL_Kernel import *
59  from CGAL.CGAL_Polyhedron_3 import Polyhedron_3
60  from CGAL.CGAL_Mesh_3 import *
61  except ModuleNotFoundError:
62  print("To compute non convex Polytope CGAL library needs to be installed.")
63  sys.exit(0)
64 
66  return v.x(), v.y(), v.z()
67 
69  p1 = he
70  p2 = p1.next()
71  p3 = p2.next()
72  return [
73  vertex_to_tuple(p1.vertex().point()),
74  vertex_to_tuple(p2.vertex().point()),
75  vertex_to_tuple(p3.vertex().point()),
76  ]
77 
78  def alpha_shape_with_cgal(coords, alpha=None):
79  """
80  Compute the alpha shape of a set of points. (Code thanks to A. Skuric)
81  Retrieved from http://blog.thehumangeo.com/2014/05/12/drawing-boundaries-in-python/
82 
83  :param coords : Coordinates of points
84  :param alpha: List of alpha values to influence the gooeyness of the border. Smaller numbers don't fall inward as much as larger numbers.
85  Too large, and you lose everything!
86  :return: Shapely.MultiPolygons which is the hull of the input set of points
87  """
88  if alpha is None:
89  bbox_diag = np.linalg.norm(np.max(coords, 0) - np.min(coords, 0))
90  alpha_value = bbox_diag / 5
91  else:
92  alpha_value = np.mean(alpha)
93  # Convert to CGAL point
94  points = [Point_3(pt[0], pt[1], pt[2]) for pt in coords]
95  # Compute alpha shape
96  Q = Polyhedron_3()
97  a = alpha_wrap_3(points, alpha_value, 0.01, Q)
98 
99  alpha_shape_vertices = np.array(
100  [vertex_to_tuple(vertex.point()) for vertex in Q.vertices()]
101  )
102  alpha_shape_faces = np.array(
103  [np.array(halfedge_to_triangle(face.halfedge())) for face in Q.facets()]
104  )
105 
106  return alpha_shape_vertices, alpha_shape_faces
107 
108  verts = pin.reachableWorkspace(robot, q0, horizon, frame, n_samples, facet_dims)
109  verts = verts.T
110 
111  alpha = 0.2
112  verts, faces = alpha_shape_with_cgal(verts, alpha)
113  verts = faces.reshape(-1, 3)
114  faces = np.arange(len(verts)).reshape(-1, 3)
115 
116 print("------------------- Display Vertex")
117 
118 
119 # meshcat triangulated mesh
120 poly = g.TriangularMeshGeometry(vertices=verts, faces=faces)
121 viz.viewer["poly"].set_object(
122  poly, g.MeshBasicMaterial(color=0x000000, wireframe=True, linewidth=12, opacity=0.2)
123 )
124 
125 while True:
126  viz.display(q0)
127  viz.viewer["poly"].set_object(
128  poly,
129  g.MeshBasicMaterial(color=0x000000, wireframe=True, linewidth=2, opacity=0.2),
130  )
131 
132  time.sleep(1e-2)
CGAL_Kernel
CGAL_Mesh_3
reachable-workspace.halfedge_to_triangle
def halfedge_to_triangle(he)
Definition: reachable-workspace.py:68
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
reachable-workspace.alpha_shape_with_cgal
def alpha_shape_with_cgal(coords, alpha=None)
Definition: reachable-workspace.py:78
CGAL_Alpha_wrap_3
reachable-workspace.vertex_to_tuple
def vertex_to_tuple(v)
Definition: reachable-workspace.py:65
pinocchio.visualize.meshcat_visualizer.MeshcatVisualizer
Definition: meshcat_visualizer.py:532
path


pinocchio
Author(s):
autogenerated on Tue Jun 25 2024 02:42:40