Skip to content

Commit ecdf4c8

Browse files
saran-tcopybara-github
authored andcommitted
Implement plugin mechanism for actuators and sensors.
PiperOrigin-RevId: 474874088 Change-Id: I1bbcc20439dc59de7e18327a2cdc6fad0a57201e
1 parent d05e627 commit ecdf4c8

3 files changed

Lines changed: 85 additions & 3 deletions

File tree

dm_control/autowrap/codegen_util.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ class UniqueOrderedDict(collections.OrderedDict):
6969
"""Subclass of `OrderedDict` that enforces the uniqueness of keys."""
7070

7171
def __setitem__(self, k, v):
72-
if k in self:
72+
existing_v = self.get(k)
73+
if existing_v is None:
74+
super().__setitem__(k, v)
75+
elif v != existing_v:
7376
raise ValueError("Key '{}' already exists.".format(k))
74-
super().__setitem__(k, v)
7577

7678

7779
def macro_struct_name(name, suffix=None):

dm_control/mjcf/schema.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,30 @@
783783
</element>
784784
</children>
785785
</element>
786+
<element name="extension">
787+
<children>
788+
<element name="required" repeated="true" namespace="plugin">
789+
<attributes>
790+
<attribute name="plugin" type="identifier" required="true"/>
791+
</attributes>
792+
<children>
793+
<element name="instance" repeated="true" namespace="plugin_instance">
794+
<attributes>
795+
<attribute name="name" type="identifier" required="true"/>
796+
</attributes>
797+
<children>
798+
<element name="config" repeated="true">
799+
<attributes>
800+
<attribute name="key" type="string"/>
801+
<attribute name="value" type="string"/>
802+
</attributes>
803+
</element>
804+
</children>
805+
</element>
806+
</children>
807+
</element>
808+
</children>
809+
</element>
786810
<element name="custom">
787811
<children>
788812
<element name="numeric" repeated="true">
@@ -1729,6 +1753,37 @@
17291753
<attribute name="gain" type="float"/>
17301754
</attributes>
17311755
</element>
1756+
<element name="plugin" repeated="true" namespace="actuator">
1757+
<attributes>
1758+
<attribute name="name" type="identifier"/>
1759+
<attribute name="class" type="reference" reference_namespace="default"/>
1760+
<attribute name="plugin" type="reference" reference_namespace="plugin"/>
1761+
<attribute name="instance" type="reference" reference_namespace="plugin_instance"/>
1762+
<attribute name="group" type="int"/>
1763+
<attribute name="ctrllimited" type="keyword" valid_values="false true auto"/>
1764+
<attribute name="forcelimited" type="keyword" valid_values="false true auto"/>
1765+
<attribute name="ctrlrange" type="array" array_type="float" array_size="2"/>
1766+
<attribute name="forcerange" type="array" array_type="float" array_size="2"/>
1767+
<attribute name="lengthrange" type="array" array_type="float" array_size="2"/>
1768+
<attribute name="gear" type="array" array_type="float" array_size="6"/>
1769+
<attribute name="cranklength" type="float"/>
1770+
<attribute name="joint" type="reference"/>
1771+
<attribute name="jointinparent" type="reference" reference_namespace="joint"/>
1772+
<attribute name="site" type="reference"/>
1773+
<attribute name="tendon" type="reference"/>
1774+
<attribute name="cranksite" type="reference" reference_namespace="site"/>
1775+
<attribute name="slidersite" type="reference" reference_namespace="site"/>
1776+
<attribute name="user" type="array" array_type="float"/>
1777+
</attributes>
1778+
<children>
1779+
<element name="config" repeated="true">
1780+
<attributes>
1781+
<attribute name="key" type="string"/>
1782+
<attribute name="value" type="string"/>
1783+
</attributes>
1784+
</element>
1785+
</children>
1786+
</element>
17321787
</children>
17331788
</element>
17341789
<element name="sensor">
@@ -2084,6 +2139,25 @@
20842139
<attribute name="dim" type="int" required="true"/>
20852140
</attributes>
20862141
</element>
2142+
<element name="plugin" repeated="true" namespace="sensor">
2143+
<attributes>
2144+
<attribute name="name" type="identifier"/>
2145+
<attribute name="plugin" type="reference" reference_namespace="plugin"/>
2146+
<attribute name="instance" type="reference" reference_namespace="plugin_instance"/>
2147+
<attribute name="cutoff" type="float"/>
2148+
<attribute name="objtype" type="keyword" required="true" valid_values="body joint geom site camera light mesh skin hfield texture material equality tendon actuator sensor numeric text tuple contact keyframe"/>
2149+
<attribute name="objname" type="reference" required="true" reference_namespace="attrib:objtype"/>
2150+
<attribute name="user" type="array" array_type="float"/>
2151+
</attributes>
2152+
<children>
2153+
<element name="config" repeated="true">
2154+
<attributes>
2155+
<attribute name="key" type="string"/>
2156+
<attribute name="value" type="string"/>
2157+
</attributes>
2158+
</element>
2159+
</children>
2160+
</element>
20872161
</children>
20882162
</element>
20892163
<element name="keyframe">

dm_control/mujoco/index_test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
"""Tests for index."""
1717

18+
import collections
19+
1820
from absl.testing import absltest
1921
from absl.testing import parameterized
2022
from dm_control.mujoco import index
@@ -311,10 +313,14 @@ def testFieldIndexerDir(self):
311313

312314
def _iter_indexers(model, data):
313315
size_to_axis_indexer = index.make_axis_indexers(model)
316+
all_fields = collections.OrderedDict()
314317
for struct, struct_name in ((model, 'mjmodel'), (data, 'mjdata')):
315318
indexer = index.struct_indexer(struct, struct_name, size_to_axis_indexer)
316319
for field_name, field_indexer in indexer._asdict().items():
317-
yield field_name, field_indexer
320+
if field_name not in all_fields:
321+
all_fields[field_name] = field_indexer
322+
for field_name, field_indexer in all_fields.items():
323+
yield field_name, field_indexer
318324

319325

320326
class AllFieldsTest(parameterized.TestCase):

0 commit comments

Comments
 (0)