Skip to content

Commit 63c7db6

Browse files
committed
Add example candlewick-viewer-solo.py
1 parent 2589f85 commit 63c7db6

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

examples/candlewick-viewer-solo.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
See also: meshcat-viewer-solo.py
3+
"""
4+
5+
import time
6+
from pathlib import Path
7+
8+
import numpy as np
9+
import pinocchio as pin
10+
from candlewick.multibody import Visualizer, VisualizerConfig
11+
12+
# Load the URDF model.
13+
# Conversion with str seems to be necessary when executing this file with ipython
14+
pinocchio_model_dir = Path(__file__).parent.parent / "models"
15+
16+
model_path = pinocchio_model_dir / "example-robot-data/robots"
17+
mesh_dir = pinocchio_model_dir
18+
urdf_filename = "solo12.urdf"
19+
urdf_model_path = model_path / "solo_description/robots" / urdf_filename
20+
21+
model, collision_model, visual_model = pin.buildModelsFromUrdf(
22+
urdf_model_path, mesh_dir, pin.JointModelFreeFlyer()
23+
)
24+
visual_model: pin.GeometryModel
25+
26+
config = VisualizerConfig()
27+
config.width = 1920
28+
config.height = 1080
29+
30+
31+
def ground(xy):
32+
return (
33+
np.sin(xy[0] * 3) / 5
34+
+ np.cos(xy[1] ** 2 * 3) / 20
35+
+ np.sin(xy[1] * xy[0] * 5) / 10
36+
)
37+
38+
39+
def vizGround(elevation_fn, space, name="ground", color=np.array([1.0, 1.0, 0.6, 0.8])):
40+
xg = np.arange(-2, 2, space)
41+
nx = xg.shape[0]
42+
xy_g = np.meshgrid(xg, xg)
43+
xy_g = np.stack(xy_g)
44+
elev_g = np.zeros((nx, nx))
45+
elev_g[:, :] = elevation_fn(xy_g)
46+
47+
sx = xg[-1] - xg[0]
48+
sy = xg[-1] - xg[0]
49+
elev_g[:, :] = elev_g[::-1, :]
50+
import coal
51+
52+
heightField = coal.HeightFieldAABB(sx, sy, elev_g, np.min(elev_g))
53+
pl = pin.SE3.Identity()
54+
obj = pin.GeometryObject(name, 0, pl, heightField)
55+
obj.meshColor[:] = color
56+
obj.overrideMaterial = True
57+
visual_model.addGeometryObject(obj)
58+
59+
60+
colorrgb = [128, 149, 255, 200]
61+
colorrgb = np.array(colorrgb) / 255.0
62+
vizGround(ground, 0.02, color=colorrgb)
63+
64+
viz = Visualizer(config, model, geomModel=visual_model)
65+
print(
66+
"Candlewick visualizer: opened on device driver", viz.renderer.device.driverName()
67+
)
68+
69+
q_ref = np.array(
70+
[
71+
[0.09906518],
72+
[0.20099078],
73+
[0.32502457],
74+
[0.19414175],
75+
[-0.00524735],
76+
[-0.97855773],
77+
[0.06860185],
78+
[0.00968163],
79+
[0.60963582],
80+
[-1.61206407],
81+
[-0.02543309],
82+
[0.66709088],
83+
[-1.50870083],
84+
[0.32405118],
85+
[-1.15305599],
86+
[1.56867351],
87+
[-0.39097222],
88+
[-1.29675892],
89+
[1.39741073],
90+
]
91+
)
92+
93+
while not viz.shouldExit:
94+
viz.display(q_ref)
95+
96+
time.sleep(1.0)

examples/meshcat-viewer-solo.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ def ground(xy):
6060
)
6161

6262

63-
def vizGround(viz, elevation_fn, space, name="ground", color=[1.0, 1.0, 0.6, 0.8]):
63+
def vizGround(
64+
viz, elevation_fn, space, name="ground", color=np.array([1.0, 1.0, 0.6, 0.8])
65+
):
6466
xg = np.arange(-2, 2, space)
6567
nx = xg.shape[0]
6668
xy_g = np.meshgrid(xg, xg)
@@ -75,7 +77,7 @@ def vizGround(viz, elevation_fn, space, name="ground", color=[1.0, 1.0, 0.6, 0.8
7577

7678
heightField = hppfcl.HeightFieldAABB(sx, sy, elev_g, np.min(elev_g))
7779
pl = pin.SE3.Identity()
78-
obj = pin.GeometryObject("ground", 0, pl, heightField)
80+
obj = pin.GeometryObject(name, 0, pl, heightField)
7981
obj.meshColor[:] = color
8082
viz.addGeometryObject(obj)
8183

0 commit comments

Comments
 (0)