Skip to content

Commit 143b416

Browse files
committed
added composer flag, and small adjustements to code for code review
1 parent bf855d6 commit 143b416

9 files changed

Lines changed: 534 additions & 671 deletions

File tree

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ def add_markers(model):
4747
4848
Args:
4949
model: The model to add markers to.
50-
51-
Returns:
52-
None
5350
"""
5451
bodies = model.find_all("body")
5552

@@ -71,7 +68,7 @@ def add_markers(model):
7168

7269
marker_idx += 1
7370
total_markers += 1
74-
71+
7572
for i in range(total_markers):
7673
marker_body = model.worldbody.add(
7774
"body", name="marker_" + str(i), mocap=True)

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,7 @@ def add_muscles(
218218
bones_meshes[bone[4:-4]] = bone_mesh
219219

220220
muscle = model.default.default["muscle"]
221-
222-
flexors = muscle.add("default", dclass="flexors")
223-
flexors.tendon.rgba = [0.5, 0, 0, 1]
224-
225-
extensors = muscle.add("default", dclass="extensors")
226-
extensors.tendon.rgba = [0.5, 0, 0, 1]
221+
muscle.tendon.rgba = [0.5, 0, 0, 1]
227222

228223
length_range = model.compiler.lengthrange
229224
length_range.inttotal = 5
@@ -276,19 +271,9 @@ def add_muscles(
276271
)
277272

278273
paths = slices2paths(mtu, slices, muscle_length)
279-
spatial = model.tendon.add("spatial", name=f"{mtu}_tendon")
274+
spatial = model.tendon.add("spatial", name=f"{mtu}_tendon", dclass="muscle")
280275
spatials.append(spatial)
281276
used_muscles.append(mtu)
282-
if (
283-
mtu in muscles_constants.FLEXORS_BACK
284-
or mtu in muscles_constants.FLEXORS_FRONT
285-
):
286-
spatial.dclass = "flexors"
287-
elif (
288-
mtu in muscles_constants.EXTENSORS_BACK
289-
or mtu in muscles_constants.EXTENSORS_FRONT
290-
):
291-
spatial.dclass = "extensors"
292277

293278
counter = 0 # used for site naming
294279
prev_body = None
@@ -447,15 +432,10 @@ def add_muscles(
447432
continue
448433

449434
physics.forward()
450-
451435
vector_min = np.minimum(vector_min, physics.data.ten_length[:])
452436
vector_max = np.maximum(vector_max, physics.data.ten_length[:])
453-
454437
physics.reset()
455438

456-
# physics.named.model.actuator_lengthrange[:, 0] = vector_min
457-
# physics.named.model.actuator_lengthrange[:, 1] = vector_max
458-
459439
for tend_idx, spatial in enumerate(spatials):
460440
if muscle_dynamics in ["Millard", "Sigmoid"]:
461441
muscle = model.actuator.add(

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ def add_wrapping_geoms(model):
2020
2121
Args:
2222
model: The model to which the wrapping geometries and sites will be added.
23-
24-
Returns:
25-
None
2623
"""
2724
pelvis = model.find("body", "pelvis")
2825
pelvis.add(

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
1,
7474
"Number of degrees of freedom vertebra in caudal spine.",
7575
)
76+
flags.DEFINE_boolean(
77+
"composer",
78+
False,
79+
"Make slight adjustments to the model to make it compatible for the composer.",
80+
)
81+
7682

7783
FLAGS = flags.FLAGS
7884

@@ -174,6 +180,7 @@ def main(argv):
174180
muscle_dynamics = FLAGS.muscle_dynamics
175181
add_markers = FLAGS.add_markers
176182
lengthrange_from_joints = FLAGS.lengthrange_from_joints
183+
composer = FLAGS.composer
177184
else:
178185
lumbar_dofs_per_vert = FLAGS["lumbar_dofs_per_vertebra"].default
179186
cervical_dofs_per_vertebra = FLAGS["cervical_dofs_per_vertebra"].default
@@ -185,11 +192,25 @@ def main(argv):
185192
muscle_dynamics = FLAGS["muscle_dynamics"].default
186193
add_markers = FLAGS["add_markers"].default
187194
lengthrange_from_joints = FLAGS["lengthrange_from_joints"].default
195+
composer = FLAGS["composer"].default
188196

189197
print("Load base model.")
190198
with open(os.path.join(CURRENT_DIR, BASE_MODEL), "r") as f:
191199
model = mjcf.from_file(f)
192200

201+
202+
if not composer:
203+
floor = model.worldbody.add(
204+
"body", name="floor", pos=(0, 0, 0)
205+
)
206+
floor.add(
207+
"geom",
208+
name="floor",
209+
type="plane",
210+
conaffinity=1,
211+
size=[10, 10, 0.1],
212+
material="grid"
213+
)
193214
# Helper constants:
194215
side_sign = {
195216
"_L": np.array((1.0, -1.0, 1.0)),
@@ -253,6 +274,7 @@ def main(argv):
253274
lumbar_dofs_per_vert,
254275
side_sign,
255276
parent=model.worldbody,
277+
composer=composer,
256278
)
257279

258280
print("Neck, skull, jaw.")
@@ -344,7 +366,7 @@ def main(argv):
344366

345367
if make_skin:
346368
create_skin.create(
347-
model=model, mesh_file=skin_msh, asset_dir=ASSET_DIR, mesh_name="dog_skin"
369+
model=model, mesh_file=skin_msh, asset_dir=ASSET_DIR, mesh_name="dog_skin", composer=composer
348370
)
349371

350372
# Add skin from .skn
@@ -382,6 +404,7 @@ def main(argv):
382404
mesh_name=filename[:-4],
383405
tex_coords=False,
384406
transform=False,
407+
composer=composer,
385408
)
386409

387410
# Add skin from .skn
@@ -397,9 +420,9 @@ def main(argv):
397420
muscle_msh.remove()
398421

399422
model.option.timestep = 0.005
400-
model.option.integrator = "Euler"
423+
model.option.integrator = "implicitfast"
401424
model.option.noslip_iterations = 3
402-
model.option.cone = "pyramidal"
425+
model.option.cone = "elliptic"
403426

404427
if add_markers:
405428
print("Add Markers")
@@ -473,6 +496,10 @@ def main(argv):
473496
)
474497
)
475498

499+
if composer:
500+
torso = model.find("body", "torso")
501+
torso.pos = np.zeros(3)
502+
476503
print("Finalising and saving model.")
477504
xml_string = model.to_xml_string("float", precision=4, zero_threshold=1e-7)
478505
root = etree.XML(xml_string, etree.XMLParser(remove_blank_text=True))
@@ -513,11 +540,12 @@ def main(argv):
513540
newlines.append(b"")
514541
xml_string = b"\n".join(newlines)
515542

543+
name_prefix = "composer_" if composer else ""
516544
# Save to file.
517545
if not use_muscles:
518-
name = "dog.xml"
546+
name = name_prefix + "dog.xml"
519547
else:
520-
name = "dog_muscles_{}_{}.xml".format(
548+
name = name_prefix + "dog_muscles_{}_{}.xml".format(
521549
muscle_strength_scale, muscle_dynamics)
522550

523551
f = open(os.path.join(CURRENT_DIR, name), "wb")

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def create_torso(
26-
model, bones, bone_position, lumbar_dofs_per_vertebra, side_sign, parent
26+
model, bones, bone_position, lumbar_dofs_per_vertebra, side_sign, parent, composer
2727
):
2828
"""Add torso in the dog model.
2929
@@ -36,7 +36,7 @@ def create_torso(
3636
side_sign: a dictionary with two axis representing the signs of
3737
translations.
3838
parent: parent object on which we should start attaching new components.
39-
39+
composer: boolean to determine if a model used by the composer is being created.
4040
Returns:
4141
The tuple `(pelvic_bones, lumbar_joints)`.
4242
"""
@@ -55,7 +55,9 @@ def create_torso(
5555
sternum = [m for m in bones if "Sternum" in m]
5656
torso_bones = thoracic_spine + ribs + sternum # + ['Xiphoid_cartilage']
5757
torso = parent.add("body", name="torso")
58-
torso.add("freejoint", name="root")
58+
59+
if not composer:
60+
torso.add("freejoint", name="root")
5961

6062
torso.add("site", name="root", size=(0.01,), rgba=[0, 1, 0, 1])
6163
torso.add("light", name="light", mode="trackcom", pos=[0, 0, 3])
@@ -84,7 +86,6 @@ def create_torso(
8486
torso.pos = torso_pos
8587
for geom in torso_geoms:
8688
geom.pos = -torso_pos
87-
8889
# Collision primitive for torso
8990
torso.add(
9091
"geom",

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from dm_control.mujoco.wrapper.mjbindings import enums
2525

2626

27-
def create(model, mesh_file, mesh_name, asset_dir, tex_coords=True, transform=True):
27+
def create(model, mesh_file, mesh_name, asset_dir, tex_coords=True, transform=True, composer=False):
2828
"""Create and add skin in the dog model.
2929
3030
Args:
@@ -34,6 +34,7 @@ def create(model, mesh_file, mesh_name, asset_dir, tex_coords=True, transform=Tr
3434
asset_dir: asset directory to load the .skn file
3535
tex_coords: boolean to indicate if the mesh has texture coordinates.
3636
transform: a boolean to rotate mesh orientation by 90 degrees along the z-axis.
37+
composer: boolean to determine if a model used by the composer is being created.
3738
"""
3839
# Add skin mesh:
3940
if transform:
@@ -132,7 +133,8 @@ def box(sx, sy, sz):
132133
# Find smallest distance between
133134
# each skin vertex and vertices of all meshes in body i
134135
distance = np.ones((skin_vertices.shape[0], physics.model.nbody)) * 1e6
135-
for i in range(2, physics.model.nbody):
136+
start = 1 if composer else 2 # in composer mode we don't have the floor
137+
for i in range(start, physics.model.nbody):
136138
geom_id = np.argwhere(physics.model.geom_bodyid == i).ravel()
137139
mesh_id = physics.model.geom_dataid[geom_id]
138140
body_verts = []
@@ -175,7 +177,7 @@ def box(sx, sy, sz):
175177
weights[weights < threshold] = 0
176178
weights /= np.atleast_2d(np.sum(weights, axis=1)).T
177179

178-
for i in range(2, physics.model.nbody):
180+
for i in range(start, physics.model.nbody):
179181
vertweight = weights[weights[:, i - 1] >= threshold, i - 1]
180182
vertid = np.argwhere(weights[:, i - 1] >= threshold).ravel()
181183
if vertid.any():

0 commit comments

Comments
 (0)