meshcat-viewer.py
Go to the documentation of this file.
1 # This examples shows how to load and move a robot in meshcat.
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 numpy as np
7 import sys
8 import os
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 = "talos_reduced.urdf"
20 # urdf_model_path = join(join(model_path,"talos_data/robots"),urdf_filename)
21 urdf_filename = "solo.urdf"
22 urdf_model_path = join(join(model_path, "solo_description/robots"), urdf_filename)
23 
24 model, collision_model, visual_model = pin.buildModelsFromUrdf(
25  urdf_model_path, mesh_dir, pin.JointModelFreeFlyer()
26 )
27 
28 # Start a new MeshCat server and client.
29 # Note: the server can also be started separately using the "meshcat-server" command in a terminal:
30 # this enables the server to remain active after the current script ends.
31 #
32 # Option open=True pens the visualizer.
33 # Note: the visualizer can also be opened seperately by visiting the provided URL.
34 try:
35  viz = MeshcatVisualizer(model, collision_model, visual_model)
36  viz.initViewer(open=True)
37 except ImportError as err:
38  print(
39  "Error while initializing the viewer. It seems you should install Python meshcat"
40  )
41  print(err)
42  sys.exit(0)
43 
44 # Load the robot in the viewer.
45 viz.loadViewerModel()
46 
47 # Display a robot configuration.
48 q0 = pin.neutral(model)
49 viz.display(q0)
50 viz.displayVisuals(True)
51 
52 # Create a convex shape from solo main body
53 mesh = visual_model.geometryObjects[0].geometry
54 mesh.buildConvexRepresentation(True)
55 convex = mesh.convex
56 
57 # Place the convex object on the scene and display it
58 if convex is not None:
59  placement = pin.SE3.Identity()
60  placement.translation[0] = 2.0
61  geometry = pin.GeometryObject("convex", 0, placement, convex)
62  geometry.meshColor = np.ones((4))
63  # Add a PhongMaterial to the convex object
64  geometry.overrideMaterial = True
65  geometry.meshMaterial = pin.GeometryPhongMaterial()
66  geometry.meshMaterial.meshEmissionColor = np.array([1.0, 0.1, 0.1, 1.0])
67  geometry.meshMaterial.meshSpecularColor = np.array([0.1, 1.0, 0.1, 1.0])
68  geometry.meshMaterial.meshShininess = 0.8
69  visual_model.addGeometryObject(geometry)
70  # After modifying the visual_model we must rebuild
71  # associated data inside the visualizer
72  viz.rebuildData()
73 
74 # Display another robot.
75 viz2 = MeshcatVisualizer(model, collision_model, visual_model)
76 viz2.initViewer(viz.viewer)
77 viz2.loadViewerModel(rootNodeName="pinocchio2")
78 q = q0.copy()
79 q[1] = 1.0
80 viz2.display(q)
81 
82 # standing config
83 q1 = np.array(
84  [0.0, 0.0, 0.235, 0.0, 0.0, 0.0, 1.0, 0.8, -1.6, 0.8, -1.6, -0.8, 1.6, -0.8, 1.6]
85 )
86 
87 v0 = np.random.randn(model.nv) * 2
88 data = viz.data
89 pin.forwardKinematics(model, data, q1, v0)
90 frame_id = model.getFrameId("HR_FOOT")
91 viz.display()
92 viz.drawFrameVelocities(frame_id=frame_id)
93 
94 model.gravity.linear[:] = 0.0
95 dt = 0.01
96 
97 
98 def sim_loop():
99  tau0 = np.zeros(model.nv)
100  qs = [q1]
101  vs = [v0]
102  nsteps = 100
103  for i in range(nsteps):
104  q = qs[i]
105  v = vs[i]
106  a1 = pin.aba(model, data, q, v, tau0)
107  vnext = v + dt * a1
108  qnext = pin.integrate(model, q, dt * vnext)
109  qs.append(qnext)
110  vs.append(vnext)
111  viz.display(qnext)
112  viz.drawFrameVelocities(frame_id=frame_id)
113  return qs, vs
114 
115 
116 qs, vs = sim_loop()
117 
118 fid2 = model.getFrameId("FL_FOOT")
119 
120 
121 def my_callback(i, *args):
122  viz.drawFrameVelocities(frame_id)
123  viz.drawFrameVelocities(fid2)
124 
125 
126 with viz.create_video_ctx("../leap.mp4"):
127  viz.play(qs, dt, callback=my_callback)
pinocchio.visualize
Definition: bindings/python/pinocchio/visualize/__init__.py:1
meshcat-viewer.sim_loop
def sim_loop()
Definition: meshcat-viewer.py:98
meshcat-viewer.my_callback
def my_callback(i, *args)
Definition: meshcat-viewer.py:121
pinocchio.visualize.meshcat_visualizer.MeshcatVisualizer
Definition: meshcat_visualizer.py:532
path


pinocchio
Author(s):
autogenerated on Sat Jun 22 2024 02:41:49