forked from gzz2000/robolite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjects.py
309 lines (258 loc) · 9.77 KB
/
objects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import copy
import xml.etree.ElementTree as ET
import numpy as np
from robosuite.models.base import MujocoXML
from robosuite.utils.mjcf_utils import string_to_array, array_to_string
class MujocoObject:
"""
Base class for all objects.
We use Mujoco Objects to implement all objects that
1) may appear for multiple times in a task
2) can be swapped between different tasks
Typical methods return copy so the caller can all joints/attributes as wanted
Attributes:
asset (TYPE): Description
"""
def __init__(self):
self.asset = ET.Element("asset")
def get_bottom_offset(self):
"""
Returns vector from object center to object bottom
Helps us put objects on a surface
Returns:
np.array: eg. np.array([0, 0, -2])
Raises:
NotImplementedError: Description
"""
raise NotImplementedError
def get_top_offset(self):
"""
Returns vector from object center to object top
Helps us put other objects on this object
Returns:
np.array: eg. np.array([0, 0, 2])
Raises:
NotImplementedError: Description
"""
raise NotImplementedError
def get_horizontal_radius(self):
"""
Returns scalar
If object a,b has horizontal distance d
a.get_horizontal_radius() + b.get_horizontal_radius() < d
should mean that a, b has no contact
Helps us put objects programmatically without them flying away due to
a huge initial contact force
Returns:
Float: radius
Raises:
NotImplementedError: Description
"""
raise NotImplementedError
# return 2
def get_collision(self, name=None, site=False):
"""
Returns a ET.Element
It is a <body/> subtree that defines all collision related fields
of this object.
Return is a copy
Args:
name (None, optional): Assign name to body
site (False, optional): Add a site (with name @name
when applicable) to the returned body
Returns:
ET.Element: body
Raises:
NotImplementedError: Description
"""
raise NotImplementedError
def get_visual(self, name=None, site=False):
"""
Returns a ET.Element
It is a <body/> subtree that defines all visualization related fields
of this object.
Return is a copy
Args:
name (None, optional): Assign name to body
site (False, optional): Add a site (with name @name
when applicable) to the returned body
Returns:
ET.Element: body
Raises:
NotImplementedError: Description
"""
raise NotImplementedError
def get_site_attrib_template(self):
"""
Returns attribs of spherical site used to mark body origin
Returns:
Dictionary of default site attributes
"""
return {
"pos": "0 0 0",
"size": "0.002 0.002 0.002",
"rgba": "1 0 0 1",
"type": "sphere",
}
class MujocoXMLObject(MujocoXML, MujocoObject):
"""
MujocoObjects that are loaded from xml files
"""
def __init__(self, fname):
"""
Args:
fname (TYPE): XML File path
"""
MujocoXML.__init__(self, fname)
def get_bottom_offset(self):
bottom_site = self.worldbody.find("./body/site[@name='bottom_site']")
return string_to_array(bottom_site.get("pos"))
def get_top_offset(self):
top_site = self.worldbody.find("./body/site[@name='top_site']")
return string_to_array(top_site.get("pos"))
def get_horizontal_radius(self):
horizontal_radius_site = self.worldbody.find(
"./body/site[@name='horizontal_radius_site']"
)
return string_to_array(horizontal_radius_site.get("pos"))[0]
def get_collision(self, name=None, site=False):
collision = copy.deepcopy(self.worldbody.find("./body/body[@name='collision']"))
collision.attrib.pop("name")
if name is not None:
collision.attrib["name"] = name
geoms = collision.findall("geom")
if len(geoms) == 1:
geoms[0].set("name", name)
else:
for i in range(len(geoms)):
geoms[i].set("name", "{}-{}".format(name, i))
if site:
# add a site as well
template = self.get_site_attrib_template()
template["rgba"] = "1 0 0 0"
if name is not None:
template["name"] = name
collision.append(ET.Element("site", attrib=template))
return collision
def get_visual(self, name=None, site=False):
visual = copy.deepcopy(self.worldbody.find("./body/body[@name='visual']"))
visual.attrib.pop("name")
if name is not None:
visual.attrib["name"] = name
if site:
# add a site as well
template = self.get_site_attrib_template()
template["rgba"] = "1 0 0 0"
if name is not None:
template["name"] = name
visual.append(ET.Element("site", attrib=template))
return visual
class MujocoGeneratedObject(MujocoObject):
"""
Base class for all programmatically generated mujoco object
i.e., every MujocoObject that does not have an corresponding xml file
"""
def __init__(
self,
size=None,
rgba=None,
density=None,
friction=None,
density_range=None,
friction_range=None,
):
"""
Provides default initialization of physical attributes:
also supports randomization of (rgba, density, friction).
- rgb is randomly generated if rgba='random' (alpha will be 1 in this case)
- If density is None and density_range is not:
Density is chosen uniformly at random specified from density range,
i.e. density_range = [50, 100, 1000]
- If friction is None and friction_range is not:
Tangential Friction is chosen uniformly at random from friction_range
Args:
size ([float], optional): of size 1 - 3
rgba (([float, float, float, float]), optional): Color
density (float, optional): Density
friction (float, optional): tangentia friction
see http://www.mujoco.org/book/modeling.html#geom for details
density_range ([float,float], optional): range for random choice
friction_range ([float,float], optional): range for random choice
"""
super().__init__()
if size is None:
self.size = [0.05, 0.05, 0.05]
else:
self.size = size
if rgba is None:
self.rgba = [1, 0, 0, 1]
elif rgba == "random":
self.rgba = np.array([np.random.uniform(0, 1) for i in range(3)] + [1])
else:
assert len(rgba) == 4, "rgba must be a length 4 array"
self.rgba = rgba
if density is None:
if density_range is not None:
self.density = np.random.choice(density_range)
else:
self.density = 1000 # water
else:
self.density = density
if friction is None:
if friction_range is not None:
self.friction = [np.random.choice(friction_range), 0.005, 0.0001]
else:
self.friction = [1, 0.005, 0.0001] # MuJoCo default
elif hasattr(type(friction), "__len__"):
assert len(friction) == 3, "friction must be a length 3 array or a float"
self.friction = friction
else:
self.friction = [friction, 0.005, 0.0001]
self.sanity_check()
def sanity_check(self):
"""
Checks if data provided makes sense.
Called in __init__()
For subclasses to inherit from
"""
pass
def get_collision_attrib_template(self):
return {"pos": "0 0 0", "group": "1"}
def get_visual_attrib_template(self):
return {"conaffinity": "0", "contype": "0", "group": "1"}
def _get_collision(self, name=None, site=False, ob_type="box"):
main_body = ET.Element("body")
if name is not None:
main_body.set("name", name)
template = self.get_collision_attrib_template()
if name is not None:
template["name"] = name
template["type"] = ob_type
template["rgba"] = array_to_string(self.rgba)
template["size"] = array_to_string(self.size)
template["density"] = str(self.density)
template["friction"] = array_to_string(self.friction)
main_body.append(ET.Element("geom", attrib=template))
if site:
# add a site as well
template = self.get_site_attrib_template()
if name is not None:
template["name"] = name
main_body.append(ET.Element("site", attrib=template))
return main_body
def _get_visual(self, name=None, site=False, ob_type="box"):
main_body = ET.Element("body")
if name is not None:
main_body.set("name", name)
template = self.get_visual_attrib_template()
template["type"] = ob_type
template["rgba"] = array_to_string(self.rgba)
template["size"] = array_to_string(self.size)
main_body.append(ET.Element("geom", attrib=template))
if site:
# add a site as well
template = self.get_site_attrib_template()
if name is not None:
template["name"] = name
main_body.append(ET.Element("site", attrib=template))
return main_body