Skip to content

Commit ffa2358

Browse files
committed
fixed code review issues
1 parent 2dc9e57 commit ffa2358

7 files changed

Lines changed: 118 additions & 94 deletions

File tree

dm_control/locomotion/walkers/assets/dog_v2/add_markers.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,23 @@
1515

1616
"""Add markers to the dog model, used for motion capture tracking."""
1717

18-
markers_per_body = {
19-
'torso': [[-0.25, 0, 0.13], [-0.1, 0, 0.13], [0.05, 0, 0.15],
20-
[-0.25, -0.06, -0.01], [-0.1, -0.08, -0.02], [0.05, -0.09, -0.02],
21-
[-0.25, 0.06, -0.01], [-0.1, 0.08, -0.02], [0.05, 0.09, -0.02]],
22-
'C_6': [[0.05, -0.05, 0], [0.05, 0.05, 0]],
23-
'upper_arm': [[0.04, 0, 0]],
24-
'lower_arm': [[0.025, 0, 0], [0.025, 0, -0.1]],
25-
'hand': [[0, -0.02, 0], [0, 0.02, 0], [0.02, -0.02, -0.08],
26-
[0.02, 0.02, -0.08]],
27-
'finger': [[0.065, 0, 0]],
28-
'pelvis': [[0.04, -0.07, 0.02], [0.04, 0.07, 0.02]],
29-
'upper_leg': [[-0.03, 0.04, 0]],
30-
'lower_leg': [[0.04, 0, 0], [-0.04, 0.035, 0]],
31-
'foot': [[0, -0.02, 0], [0, 0.02, 0], [0, -0.02, -0.1], [0, 0.03, -0.1]],
32-
'toe': [[0.055, 0, 0]],
33-
'Ca_10': [[0, 0, 0.01]],
34-
'skull': [[0.01, 0, 0.04], [0, -0.05, 0], [0, 0.05, 0], [-0.1, 0, 0.02]]}
18+
MARKERS_PER_BODY = {
19+
'torso': ((-0.25, 0, 0.13), (-0.1, 0, 0.13), (0.05, 0, 0.15),
20+
(-0.25, -0.06, -0.01), (-0.1, -0.08, -0.02), (0.05, -0.09, -0.02),
21+
(-0.25, 0.06, -0.01), (-0.1, 0.08, -0.02), (0.05, 0.09, -0.02)),
22+
'C_6': ((0.05, -0.05, 0), (0.05, 0.05, 0)),
23+
'upper_arm': ((0.04, 0, 0)),
24+
'lower_arm': ((0.025, 0, 0), (0.025, 0, -0.1)),
25+
'hand': ((0, -0.02, 0), (0, 0.02, 0), (0.02, -0.02, -0.08),
26+
(0.02, 0.02, -0.08)),
27+
'finger': ((0.065, 0, 0)),
28+
'pelvis': ((0.04, -0.07, 0.02), (0.04, 0.07, 0.02)),
29+
'upper_leg': ((-0.03, 0.04, 0)),
30+
'lower_leg': ((0.04, 0, 0), (-0.04, 0.035, 0)),
31+
'foot': ((0, -0.02, 0), (0, 0.02, 0), (0, -0.02, -0.1), (0, 0.03, -0.1)),
32+
'toe': ((0.055, 0, 0)),
33+
'Ca_10': ((0, 0, 0.01)),
34+
'skull': ((0.01, 0, 0.04), (0, -0.05, 0), (0, 0.05, 0), (-0.1, 0, 0.02))}
3535

3636
def add_markers(model):
3737
bodies = model.find_all('body')
@@ -53,5 +53,5 @@ def add_markers(model):
5353
for i in range(50):
5454
marker_body = model.worldbody.add('body', name="marker_" + str(i), mocap=True)
5555
marker_body.add('site', name="marker_" + str(i), dclass="mocap_marker")
56-
57-
print("TOT_MARKERS:", TOTAL_MARKERS)
56+
57+
print("TOTAL MARKERS ADDED:", TOTAL_MARKERS)

dm_control/locomotion/walkers/assets/dog_v2/add_muscles.py

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@
1919
from os.path import isfile, join
2020

2121
import numpy as np
22-
from scipy.spatial.transform import Rotation as Rot
22+
from dm_control.utils.transformations import *
2323
from pykdtree.kdtree import KDTree
2424
import trimesh
2525
import pyvista as pv
2626
from dm_control import mjcf as mjcf_module
2727
from dm_control.mujoco.wrapper import mjbindings
2828

29-
from muscles import extensors_back, extensors_front, flexors_back, \
30-
flexors_front, lateral, neck, tail, torso
29+
from muscles import EXTENSORS_BACK, EXTENSORS_FRONT, FLEXORS_BACK, \
30+
FLEXORS_FRONT, LATERAL, NECK, TAIL, TORSO
3131

3232
from utils import array_to_string, slices2paths
3333

34-
muscle_legs = extensors_back + extensors_front + \
35-
flexors_back + flexors_front
34+
MUSCLE_LEGS = EXTENSORS_BACK + EXTENSORS_FRONT + \
35+
FLEXORS_BACK + FLEXORS_FRONT
3636

37-
wrap_geoms_legs = ['shoulder_L_wrapping', 'elbow_L_wrapping',
37+
WRAP_GEOMS_LEGS = ('shoulder_L_wrapping', 'elbow_L_wrapping',
3838
'wrist_L_wrapping', 'finger_L_wrapping', 'hip_L_wrapping',
3939
'knee_L_wrapping', 'toe_L_wrapping',
4040

4141
'shoulder_R_wrapping', 'elbow_R_wrapping',
4242
'wrist_R_wrapping', 'finger_R_wrapping', 'hip_R_wrapping',
43-
'knee_R_wrapping', 'toe_R_wrapping']
43+
'knee_R_wrapping', 'toe_R_wrapping')
4444

4545

4646
def getClosestGeom(kd_tree, point, mjcf, mtu):
@@ -57,14 +57,14 @@ def getClosestGeom(kd_tree, point, mjcf, mtu):
5757
closest_geom_body: The closest geometric body (as a Mujoco body object)
5858
to the target point.
5959
"""
60-
dist = 100000000000
60+
dist = np.inf
6161
closest_geom_body = 0
6262
for bone, tree in kd_tree.items():
6363
dist_new, i = tree.query(np.array([point]), k=1)
6464
geom = mjcf.find("geom", bone)
6565
body = geom.parent
6666

67-
if mtu in muscle_legs:
67+
if mtu in MUSCLE_LEGS:
6868
if body.name not in ["upper_leg_L",
6969
"upper_leg_R", "lower_leg_L", "lower_leg_R",
7070
"foot_L", "foot_R", "toe_L", "toe_R",
@@ -116,9 +116,9 @@ def calculate_transformation(element):
116116
pos = np.zeros(3)
117117

118118
if element.quat is not None:
119-
rot = Rot.from_quat(element.quat).as_matrix()
119+
rot = quat_to_mat(element.get('quat'))
120120
else:
121-
rot = Rot.identity().as_matrix()
121+
rot = np.eye(3)
122122

123123
all_transformations.append(
124124
np.vstack(
@@ -137,11 +137,35 @@ def calculate_transformation(element):
137137

138138
def add_muscles(model, scale_multiplier, muscle_dynamics,
139139
asset_dir, lengthrange_from_joints):
140+
"""
141+
Add muscles to a Mujoco (MJCF) model.
142+
143+
This function adds muscles to a Mujoco model,
144+
creating MTUs (muscle-tendon units) and defining
145+
their properties.
146+
147+
Args:
148+
model (MJCFModel): The input Mujoco model to which muscles will be added.
149+
scale_multiplier (float): A scaling factor for muscle forces (0 to disable anatomical scaling).
150+
muscle_dynamics (str): Muscle dynamics model, either 'Millard', 'Sigmoid', or 'General'.
151+
asset_dir (str): The directory path containing muscle and bone assets.
152+
lengthrange_from_joints (bool): If True, compute length ranges from joint limits.
153+
154+
Returns:
155+
None
156+
157+
Note:
158+
- The function modifies the input `model` in place by adding muscles and related properties.
159+
- The specific muscles added depend on the provided `muscle_dynamics` and other parameters.
160+
161+
Raises:
162+
NameError: If an unsupported `muscle_dynamics` value is provided.
163+
"""
140164
physics = mjcf_module.Physics.from_mjcf_model(model)
141165

142166
mjcf = model
143167

144-
muscle_meshes_path = asset_dir + '/muscles/'
168+
muscle_meshes_path = join(asset_dir, 'muscles/')
145169
bones_path = asset_dir
146170

147171
bones = [f for f in listdir(bones_path)
@@ -171,8 +195,8 @@ def add_muscles(model, scale_multiplier, muscle_dynamics,
171195

172196
mjcf.option.timestep = 0.005
173197

174-
muscles = extensors_front + flexors_front + \
175-
extensors_back + flexors_back + torso + neck + tail
198+
muscles = EXTENSORS_FRONT + FLEXORS_FRONT + \
199+
EXTENSORS_BACK + FLEXORS_BACK + TORSO + NECK + TAIL
176200

177201
used_muscles = []
178202
volumes = []
@@ -184,7 +208,7 @@ def add_muscles(model, scale_multiplier, muscle_dynamics,
184208
pv_mesh = pv.read(muscle_meshes_path + mtu + '.stl')
185209
volumes.append(pv_mesh.volume)
186210
# check along which axis to slice
187-
if mtu not in lateral:
211+
if mtu not in LATERAL:
188212
plane_normal = [0, 0, -1]
189213
start = np.argmax(m.vertices[:, 2])
190214
end = np.argmin(m.vertices[:, 2])
@@ -213,9 +237,9 @@ def add_muscles(model, scale_multiplier, muscle_dynamics,
213237
used_muscles.append(mtu)
214238

215239
# print("adding spatial", spatial.name)
216-
if mtu in flexors_back or mtu in flexors_front:
240+
if mtu in FLEXORS_BACK or mtu in FLEXORS_FRONT:
217241
spatial.dclass = "flexors"
218-
elif mtu in extensors_back or mtu in extensors_front:
242+
elif mtu in EXTENSORS_BACK or mtu in EXTENSORS_FRONT:
219243
spatial.dclass = "extensors"
220244

221245
counter = 0 # used for site naming
@@ -229,9 +253,9 @@ def add_muscles(model, scale_multiplier, muscle_dynamics,
229253
# Add site to tendon
230254
min_dist = 0.05
231255
max_dist = 0.11
232-
if mtu in tail:
256+
if mtu in TAIL:
233257
max_dist = 0.05
234-
elif mtu in neck:
258+
elif mtu in NECK:
235259
min_dist = 0.11
236260
max_dist = 0.15
237261

@@ -266,28 +290,28 @@ def add_muscles(model, scale_multiplier, muscle_dynamics,
266290
# check for wrapping geoms
267291
if idx != len(paths) - 1:
268292
geoms = physics.named.data.geom_xpos.axes.row.names
269-
dist = 100000000000
293+
dist = np.inf
270294
closest_g = None
271295
closest_geom_pos = None
272296
for g_name in geoms:
273297
if 'wrapping' in g_name:
274298
pos = physics.named.data.geom_xpos[g_name]
275299
new_dist = np.linalg.norm(pos - point)
276-
if (mtu in lateral or mtu in neck) and \
277-
g_name not in wrap_geoms_legs and \
300+
if (mtu in LATERAL or mtu in NECK) and \
301+
g_name not in WRAP_GEOMS_LEGS and \
278302
pos[2] < point[2] and \
279303
new_dist < dist:
280304
closest_g = g_name
281305
dist = new_dist
282306
closest_geom_pos = pos
283-
elif (mtu in flexors_back or
284-
mtu in flexors_front or
285-
mtu in extensors_front or
286-
mtu in extensors_back) and \
307+
elif (mtu in FLEXORS_BACK or
308+
mtu in FLEXORS_FRONT or
309+
mtu in EXTENSORS_FRONT or
310+
mtu in EXTENSORS_BACK) and \
287311
pos[2] < point[2] and \
288312
dist > 0.01 and \
289313
new_dist < dist and \
290-
g_name in wrap_geoms_legs:
314+
g_name in WRAP_GEOMS_LEGS:
291315
# in the legs we are interested only in wrapping
292316
# geometries that are lower than the site we
293317
# are adding
@@ -296,15 +320,15 @@ def add_muscles(model, scale_multiplier, muscle_dynamics,
296320
closest_geom_pos = pos
297321

298322
if dist < 0.1:
299-
if (mtu in flexors_back or mtu in flexors_front) \
323+
if (mtu in FLEXORS_BACK or mtu in FLEXORS_FRONT) \
300324
and closest_geom_pos[2] < point[2]:
301325
spatial.add('geom', geom=closest_g,
302326
sidesite=closest_g + '_backward')
303-
elif (mtu in extensors_back or mtu in extensors_front) \
327+
elif (mtu in EXTENSORS_BACK or mtu in EXTENSORS_FRONT) \
304328
and closest_geom_pos[2] < point[2]:
305329
spatial.add('geom', geom=closest_g,
306330
sidesite=closest_g + '_forward')
307-
elif mtu in neck:
331+
elif mtu in NECK:
308332
pos1 = physics.named.data.site_xpos[closest_g + '_forward']
309333
d1 = np.linalg.norm(pos1 - point)
310334
pos2 = physics.named.data.site_xpos[closest_g + '_backward']
@@ -315,7 +339,7 @@ def add_muscles(model, scale_multiplier, muscle_dynamics,
315339
else:
316340
spatial.add('geom', geom=closest_g,
317341
sidesite=closest_g + '_backward')
318-
elif mtu in torso and dist < 0.05:
342+
elif mtu in TORSO and dist < 0.05:
319343
spatial.add('geom', geom=closest_g,
320344
sidesite=closest_g + '_forward')
321345

@@ -325,7 +349,7 @@ def add_muscles(model, scale_multiplier, muscle_dynamics,
325349
gaintype="muscle", biastype="muscle")
326350

327351
prms = [0] * 10
328-
prms[0] = 0.04
352+
prms[0] = 0.01
329353
prms[1] = 0.04
330354
if muscle_dynamics == 'Millard':
331355
muscle.dynprm = prms

dm_control/locomotion/walkers/assets/dog_v2/build_dog.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def main(argv):
341341

342342
if make_skin:
343343
create_skin.create(model=model, mesh_file=skin_msh, asset_dir=ASSET_DIR,
344-
name_mesh='dog_skin')
344+
mesh_name='dog_skin')
345345

346346
# Add skin from .skn
347347
print('Adding Skin.')
@@ -364,7 +364,7 @@ def main(argv):
364364
muscle_msh = model.asset.add('mesh', name=filename[:-4],
365365
file=ASSET_DIR + '/muscles/' + filename)
366366
create_skin.create(model=model, asset_dir=ASSET_DIR,
367-
mesh_file=muscle_msh, name_mesh=filename[:-4],
367+
mesh_file=muscle_msh, mesh_name=filename[:-4],
368368
tex_coords=False, transform=False)
369369

370370
# Add skin from .skn
@@ -392,7 +392,8 @@ def main(argv):
392392
add_wrapping_geoms.add_wrapping_geoms(model)
393393
# Add muscles
394394
add_muscles.add_muscles(model, muscle_strength_scale,
395-
muscle_dynamics, ASSET_DIR, lengthrange_from_joints)
395+
muscle_dynamics, ASSET_DIR,
396+
lengthrange_from_joints)
396397
else:
397398
actuated_joints = add_torque_actuators.add_motors(
398399
physics, model, lumbar_joints, cervical_joints, caudal_joints

dm_control/locomotion/walkers/assets/dog_v2/create_skin.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
from scipy import spatial
2424

2525

26-
def create(model, mesh_file, name_mesh, asset_dir, tex_coords=True, transform=True):
26+
def create(model, mesh_file, mesh_name, asset_dir, tex_coords=True, transform=True):
2727
"""Create and add skin in the dog model.
2828
2929
Args:
3030
model: model in which we want to add the skin.
3131
mesh_file: a binary mesh format of the skin.
32-
name_mesh: the name of the skin to use in the xml
32+
mesh_name: the name of the skin to use in the xml
3333
asset_dir: asset directory to load the .skn file
3434
tex_coords: boolean to indicate if the mesh has texture coordinates.
3535
transform: a boolean to rotate mesh orientation by 90 degrees along the z-axis.
@@ -39,8 +39,8 @@ def create(model, mesh_file, name_mesh, asset_dir, tex_coords=True, transform=Tr
3939
if transform:
4040
skinmesh = model.worldbody.add(
4141
'geom',
42-
name=name_mesh,
43-
mesh=name_mesh,
42+
name=mesh_name,
43+
mesh=mesh_name,
4444
type='mesh',
4545
contype=0,
4646
conaffinity=0,
@@ -50,28 +50,28 @@ def create(model, mesh_file, name_mesh, asset_dir, tex_coords=True, transform=Tr
5050
else:
5151
skinmesh = model.worldbody.add(
5252
'geom',
53-
name=name_mesh,
54-
mesh=name_mesh,
53+
name=mesh_name,
54+
mesh=mesh_name,
5555
type='mesh',
5656
contype=0,
5757
conaffinity=0)
5858
physics = mjcf.Physics.from_mjcf_model(model)
5959

6060
# Get skinmesh vertices in global coordinates
61-
vertadr = physics.named.model.mesh_vertadr[name_mesh]
62-
vertnum = physics.named.model.mesh_vertnum[name_mesh]
61+
vertadr = physics.named.model.mesh_vertadr[mesh_name]
62+
vertnum = physics.named.model.mesh_vertnum[mesh_name]
6363
skin_vertices = physics.model.mesh_vert[vertadr:vertadr + vertnum, :]
6464
skin_vertices = skin_vertices.dot(
65-
physics.named.data.geom_xmat[name_mesh].reshape(3, 3).T)
66-
skin_vertices += physics.named.data.geom_xpos[name_mesh]
65+
physics.named.data.geom_xmat[mesh_name].reshape(3, 3).T)
66+
skin_vertices += physics.named.data.geom_xpos[mesh_name]
6767
skin_normals = physics.model.mesh_normal[vertadr:vertadr + vertnum, :]
6868
skin_normals = skin_normals.dot(
69-
physics.named.data.geom_xmat[name_mesh].reshape(3, 3).T)
70-
skin_normals += physics.named.data.geom_xpos[name_mesh]
69+
physics.named.data.geom_xmat[mesh_name].reshape(3, 3).T)
70+
skin_normals += physics.named.data.geom_xpos[mesh_name]
7171

7272
# Get skinmesh faces
73-
faceadr = physics.named.model.mesh_faceadr[name_mesh]
74-
facenum = physics.named.model.mesh_facenum[name_mesh]
73+
faceadr = physics.named.model.mesh_faceadr[mesh_name]
74+
facenum = physics.named.model.mesh_facenum[mesh_name]
7575
skin_faces = physics.model.mesh_face[faceadr:faceadr + facenum, :]
7676

7777
# Make skin
@@ -191,7 +191,7 @@ def box(sx, sy, sz):
191191

192192
# Convert skin into *.skn file according to
193193
# https://mujoco.readthedocs.io/en/latest/XMLreference.html#asset-skin
194-
f = open(asset_dir + '/skins/' + name_mesh + '.skn', 'w+b')
194+
f = open(asset_dir + '/skins/' + mesh_name + '.skn', 'w+b')
195195

196196
nvert = skin.vertex.size // 3
197197

dm_control/locomotion/walkers/assets/dog_v2/dog_base.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@
5050
<joint group="3" pos="0.012 0 0.01" armature="0.01" damping="3.0"/>
5151
<general gainprm="40"/>
5252
<default class="lumbar_extend">
53-
<joint stiffness="30.0" range="-24.5 24.5"/>
53+
<joint stiffness="30.0" range="-13 13"/>
5454
<general gainprm="60"/>
5555
</default>
5656
<default class="lumbar_bend">
57-
<joint stiffness="45.0" range="-21 21"/>
57+
<joint stiffness="45.0" range="-15 15"/>
5858
</default>
5959
<default class="lumbar_twist">
60-
<joint stiffness="45.0" range="-16.333 16.333"/>
60+
<joint stiffness="45.0" range="-10 10"/>
6161
</default>
6262
</default>
6363
<default class="cervical">

0 commit comments

Comments
 (0)