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 viz = MeshcatVisualizer(model, collision_model, visual_model)
29 
30 # Start a new MeshCat server and client.
31 # Note: the server can also be started separately using the "meshcat-server" command in a terminal:
32 # this enables the server to remain active after the current script ends.
33 #
34 # Option open=True pens the visualizer.
35 # Note: the visualizer can also be opened seperately by visiting the provided URL.
36 try:
37  viz.initViewer(open=True)
38 except ImportError as err:
39  print(
40  "Error while initializing the viewer. It seems you should install Python meshcat"
41  )
42  print(err)
43  sys.exit(0)
44 
45 # Load the robot in the viewer.
46 viz.loadViewerModel()
47 
48 # Display a robot configuration.
49 q0 = pin.neutral(model)
50 viz.display(q0)
51 viz.displayCollisions(True)
52 viz.displayVisuals(False)
53 
54 mesh = visual_model.geometryObjects[0].geometry
55 mesh.buildConvexRepresentation(True)
56 convex = mesh.convex
57 
58 if convex is not None:
59  placement = pin.SE3.Identity()
60  placement.translation[0] = 2.0
61  geometry = pin.GeometryObject("convex", 0, convex, placement)
62  geometry.meshColor = np.ones((4))
63  visual_model.addGeometryObject(geometry)
64 
65 # Display another robot.
66 viz2 = MeshcatVisualizer(model, collision_model, visual_model)
67 viz2.initViewer(viz.viewer)
68 viz2.loadViewerModel(rootNodeName="pinocchio2")
69 q = q0.copy()
70 q[1] = 1.0
71 viz2.display(q)
72 
73 # standing config
74 q1 = np.array(
75  [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]
76 )
77 
78 v0 = np.random.randn(model.nv) * 2
79 data = viz.data
80 pin.forwardKinematics(model, data, q1, v0)
81 frame_id = model.getFrameId("HR_FOOT")
82 viz.display()
83 viz.drawFrameVelocities(frame_id=frame_id)
84 
85 model.gravity.linear[:] = 0.0
86 dt = 0.01
87 
88 
89 def sim_loop():
90  tau0 = np.zeros(model.nv)
91  qs = [q1]
92  vs = [v0]
93  nsteps = 100
94  for i in range(nsteps):
95  q = qs[i]
96  v = vs[i]
97  a1 = pin.aba(model, data, q, v, tau0)
98  vnext = v + dt * a1
99  qnext = pin.integrate(model, q, dt * vnext)
100  qs.append(qnext)
101  vs.append(vnext)
102  viz.display(qnext)
103  viz.drawFrameVelocities(frame_id=frame_id)
104  return qs, vs
105 
106 
107 qs, vs = sim_loop()
108 
109 fid2 = model.getFrameId("FL_FOOT")
110 
111 
112 def my_callback(i, *args):
113  viz.drawFrameVelocities(frame_id)
114  viz.drawFrameVelocities(fid2)
115 
116 
117 with viz.create_video_ctx("../leap.mp4"):
118  viz.play(qs, dt, callback=my_callback)
def my_callback(i, args)


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:32