-
Notifications
You must be signed in to change notification settings - Fork 4
/
link.py
494 lines (414 loc) · 14.9 KB
/
link.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from compas.colors import Color
from compas.data import Data
from compas.geometry import Box
from compas.geometry import Capsule
from compas.geometry import Cylinder
from compas.geometry import Frame
from compas.geometry import Sphere
from compas.geometry import Transformation
from compas_robots.files import URDFElement
from compas_robots.files import URDFParser
from .base import ColorProxy
from .base import FrameProxy
from .base import _attr_from_data
from .base import _attr_to_data
from .geometry import BoxProxy
from .geometry import CapsuleProxy
from .geometry import CylinderProxy
from .geometry import LinkGeometry
from .geometry import Material
from .geometry import MeshDescriptor
from .geometry import SphereProxy
from .geometry import Texture
class Mass(Data):
"""Represents a value of mass usually related to a link."""
def __init__(self, value):
super(Mass, self).__init__()
self.value = float(value)
def __str__(self):
return str(self.value)
def get_urdf_element(self):
attributes = {"value": self.value}
return URDFElement("mass", attributes)
@property
def __data__(self):
return {"value": self.value}
class Inertia(Data):
"""Rotational inertia matrix (3x3) represented in the inertia frame.
Since the rotational inertia matrix is symmetric, only 6 above-diagonal
elements of this matrix are specified here, using the attributes
``ixx``, ``ixy``, ``ixz``, ``iyy``, ``iyz``, ``izz``.
"""
def __init__(self, ixx=0.0, ixy=0.0, ixz=0.0, iyy=0.0, iyz=0.0, izz=0.0):
super(Inertia, self).__init__()
self.ixx = float(ixx)
self.ixy = float(ixy)
self.ixz = float(ixz)
self.iyy = float(iyy)
self.iyz = float(iyz)
self.izz = float(izz)
def get_urdf_element(self):
attributes = {
"ixx": self.ixx,
"ixy": self.ixy,
"ixz": self.ixz,
"iyy": self.iyy,
"iyz": self.iyz,
"izz": self.izz,
}
return URDFElement("inertia", attributes)
@property
def __data__(self):
return {
"ixx": self.ixx,
"ixy": self.ixy,
"ixz": self.ixz,
"iyy": self.iyy,
"iyz": self.iyz,
"izz": self.izz,
}
@classmethod
def __from_data__(cls, data):
return cls(
ixx=data.get("ixx", 0.0),
ixy=data.get("ixy", 0.0),
ixz=data.get("ixz", 0.0),
iyy=data.get("iyy", 0.0),
iyz=data.get("iyz", 0.0),
izz=data.get("izz", 0.0),
)
class Inertial(Data):
"""Inertial properties of a link.
Attributes
----------
origin
This is the pose of the inertial reference frame,
relative to the link reference frame.
mass
Mass of the link.
inertia
3x3 rotational inertia matrix, represented in the inertia frame.
"""
def __init__(self, origin=None, mass=None, inertia=None):
super(Inertial, self).__init__()
self.origin = origin
self.mass = mass
self.inertia = inertia
@property
def origin(self):
return self._origin
@origin.setter
def origin(self, value):
self._origin = FrameProxy.create_proxy(value)
def get_urdf_element(self):
elements = [self.origin, self.mass, self.inertia]
return URDFElement("inertial", elements=elements)
@property
def __data__(self):
return {
"origin": self.origin.__data__ if self.origin else None,
"mass": self.mass.__data__ if self.mass else None,
"inertia": self.inertia.__data__ if self.inertia else None,
}
@classmethod
def __from_data__(cls, data):
origin = Frame.__from_data__(data["origin"]) if data["origin"] else None
mass = Mass.__from_data__(data["mass"]) if data["mass"] else None
inertia = Inertia.__from_data__(data["inertia"]) if data["inertia"] else None
return cls(origin, mass, inertia)
class LinkItem(object):
def __init__(self):
self.init_transformation = None # to store the init transformation
self.current_transformation = None # to store the current transformation
self.native_geometry = None # to store the link's CAD native geometry
class Visual(LinkItem, Data):
"""Visual description of a link.
Attributes
----------
geometry
Shape of the visual element.
origin
Reference frame of the visual element with respect
to the reference frame of the link.
name
Name of the visual element.
material
Material of the visual element.
attr
Non-standard attributes.
"""
def __init__(self, geometry, origin=None, name=None, material=None, **kwargs):
super(Visual, self).__init__()
self.geometry = geometry
self.origin = origin
self.name = name
self.material = material
self.attr = kwargs
@property
def origin(self):
return self._origin
@origin.setter
def origin(self, value):
self._origin = FrameProxy.create_proxy(value)
def get_urdf_element(self):
attributes = {}
if self.name is not None:
attributes["name"] = self.name
attributes.update(self.attr)
elements = [self.origin, self.geometry, self.material]
return URDFElement("visual", attributes, elements)
# Overriding the default name property, because sometimes the name really is `None`.
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@property
def __data__(self):
return {
"geometry": self.geometry.__data__,
"origin": self.origin.__data__ if self.origin else None,
"name": self.name,
"material": self.material.__data__ if self.material else None,
"attr": _attr_to_data(self.attr),
"init_transformation": self.init_transformation.__data__ if self.init_transformation else None,
"current_transformation": self.current_transformation.__data__ if self.current_transformation else None,
}
@classmethod
def __from_data__(cls, data):
visual = cls(
geometry=LinkGeometry.__from_data__(data["geometry"]),
origin=Frame.__from_data__(data["origin"]) if data["origin"] else None,
name=data["name"],
material=Material.__from_data__(data["material"]) if data["material"] else None,
**_attr_from_data(data["attr"]),
)
if data["init_transformation"]:
visual.init_transformation = Transformation.__from_data__(data["init_transformation"])
if data["current_transformation"]:
visual.current_transformation = Transformation.__from_data__(data["current_transformation"])
return visual
def get_color(self):
"""Get the RGBA color array assigned to the link.
Only if the link has a material assigned.
Returns
-------
:class:`~compas.colors.Color`
If the link has a material assigned, return its color.
"""
if self.material:
return self.material.get_color()
else:
return None
@classmethod
def from_primitive(cls, primitive, **kwargs):
"""Create visual link from a primitive shape.
Parameters
----------
primitive : :class:`compas.geometry.Shape`
A primitive shape.
**kwargs : dict[str, Any], optional
The keyword arguments (kwargs) collected in a dict.
These allow using non-standard attributes absent in the URDF specification.
Returns
-------
:class:`~compas.datastructures.Mesh`
A visual description object.
"""
geometry = LinkGeometry()
geometry.shape = primitive
return cls(geometry, **kwargs)
class Collision(LinkItem, Data):
"""Collidable description of a link.
Attributes
----------
geometry
Shape of the collidable element.
origin
Reference frame of the collidable element with respect
to the reference frame of the link.
name
Name of the collidable element.
attr
Non-standard attributes.
"""
def __init__(self, geometry, origin=None, name=None, **kwargs):
super(Collision, self).__init__()
self.geometry = geometry
self.origin = origin
self.name = name
self.attr = kwargs
@property
def origin(self):
return self._origin
@origin.setter
def origin(self, value):
self._origin = FrameProxy.create_proxy(value)
def get_urdf_element(self):
attributes = {}
if self.name is not None:
attributes["name"] = self.name
attributes.update(self.attr)
elements = [self.origin, self.geometry]
return URDFElement("collision", attributes, elements)
# Overriding the default name property, because sometimes the name really is `None`.
@property
def name(self):
return self._name
@name.setter
def name(self, name):
self._name = name
@property
def __data__(self):
return {
"geometry": self.geometry.__data__,
"origin": self.origin.__data__ if self.origin else None,
"name": self.name,
"attr": _attr_to_data(self.attr),
"init_transformation": self.init_transformation.__data__ if self.init_transformation else None,
"current_transformation": self.current_transformation.__data__ if self.current_transformation else None,
}
@classmethod
def __from_data__(cls, data):
collision = cls(
geometry=LinkGeometry.__from_data__(data["geometry"]),
origin=Frame.__from_data__(data["origin"]) if data["origin"] else None,
name=data["name"],
attr=_attr_from_data(data["attr"]),
)
if data["init_transformation"]:
collision.init_transformation = Transformation.__from_data__(data["init_transformation"])
if data["current_transformation"]:
collision.current_transformation = Transformation.__from_data__(data["current_transformation"])
return collision
@classmethod
def from_primitive(cls, primitive, **kwargs):
"""Create collision link from a primitive shape.
Parameters
----------
primitive : :class:`compas.geometry.Shape`
A primitive shape.
**kwargs : dict[str, Any], optional
The keyword arguments (kwargs) collected in a dict.
These allow using non-standard attributes absent in the URDF specification.
Returns
-------
:class:`~compas.datastructures.Mesh`
A collision description object.
"""
geometry = LinkGeometry()
geometry.shape = primitive
return cls(geometry, **kwargs)
class Link(Data):
"""Link represented as a rigid body with an inertia, visual, and collision features.
Attributes
----------
name
Name of the link itself.
type
Link type. Undocumented in URDF, but used by PR2.
visual
Visual properties of the link.
collision
Collision properties of the link. This can be different
from the visual properties of a link.
inertial
Inertial properties of the link.
attr
Non-standard attributes.
joints
A list of joints that are the link's children
parent_joint
The reference to a parent joint if it exists
"""
def __init__(self, name, type=None, visual=(), collision=(), inertial=None, **kwargs):
super(Link, self).__init__()
self.name = name
self.type = type
self.visual = list(visual or [])
self.collision = list(collision or [])
self.inertial = inertial
self.attr = kwargs
self.joints = []
self.parent_joint = None
def get_urdf_element(self):
attributes = {"name": self.name}
if self.type is not None:
attributes["type"] = self.type
attributes.update(self.attr)
elements = self.visual + self.collision + [self.inertial]
return URDFElement("link", attributes, elements)
@property
def __data__(self):
return {
"name": self.name,
"type": self.type,
"visual": [visual.__data__ for visual in self.visual],
"collision": [collision.__data__ for collision in self.collision],
"inertial": self.inertial.__data__ if self.inertial else None,
"attr": _attr_to_data(self.attr),
"joints": [joint.__data__ for joint in self.joints],
}
@classmethod
def __from_data__(cls, data):
from .joint import Joint
link = cls(
name=data["name"],
type=data["type"],
visual=[Visual.__from_data__(d) for d in data["visual"]],
collision=[Collision.__from_data__(d) for d in data["collision"]],
inertial=Inertial.__from_data__(data["inertial"]) if data["inertial"] else None,
**_attr_from_data(data["attr"]),
)
link.joints = [Joint.__from_data__(d) for d in data["joints"]]
return link
URDFParser.install_parser(Link, "robot/link")
URDFParser.install_parser(Inertial, "robot/link/inertial")
URDFParser.install_parser(Mass, "robot/link/inertial/mass")
URDFParser.install_parser(Inertia, "robot/link/inertial/inertia")
URDFParser.install_parser(Visual, "robot/link/visual")
URDFParser.install_parser(Collision, "robot/link/collision")
URDFParser.install_parser(
Frame,
"robot/link/inertial/origin",
"robot/link/visual/origin",
"robot/link/collision/origin",
proxy_type=FrameProxy,
)
URDFParser.install_parser(LinkGeometry, "robot/link/visual/geometry", "robot/link/collision/geometry")
URDFParser.install_parser(
MeshDescriptor,
"robot/link/visual/geometry/mesh",
"robot/link/collision/geometry/mesh",
)
URDFParser.install_parser(
Box,
"robot/link/visual/geometry/box",
"robot/link/collision/geometry/box",
proxy_type=BoxProxy,
)
URDFParser.install_parser(
Cylinder,
"robot/link/visual/geometry/cylinder",
"robot/link/collision/geometry/cylinder",
proxy_type=CylinderProxy,
)
URDFParser.install_parser(
Sphere,
"robot/link/visual/geometry/sphere",
"robot/link/collision/geometry/sphere",
proxy_type=SphereProxy,
)
URDFParser.install_parser(
Capsule,
"robot/link/visual/geometry/capsule",
"robot/link/collision/geometry/capsule",
proxy_type=CapsuleProxy,
)
URDFParser.install_parser(Material, "robot/link/visual/material")
URDFParser.install_parser(Color, "robot/link/visual/material/color", proxy_type=ColorProxy)
URDFParser.install_parser(Texture, "robot/link/visual/material/texture")