-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathrevolve_module.py
More file actions
648 lines (539 loc) · 21.1 KB
/
revolve_module.py
File metadata and controls
648 lines (539 loc) · 21.1 KB
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
"""
Class containing the body parts to compose a Robogen robot
"""
import math
from collections import OrderedDict
from enum import Enum
import numpy as np
from pyrevolve import SDF
# MEASUREMENT CONVERSION
def mm(x):
return x / 1000.0
def cm(x):
return x / 100.0
def grams(x):
return x / 1000.0
# Module Orientation
class Orientation(Enum):
BACK = 0 # This is the parent attachment point (if any)
FORWARD = 1
RIGHT = 2
LEFT = 3
def opposite(self):
if self == self.BACK:
return self.FORWARD
elif self == self.FORWARD:
return self.BACK
elif self == self.RIGHT:
return self.LEFT
elif self == self.LEFT:
return self.RIGHT
else:
assert False
def short_repr(self):
if self == self.BACK:
return 'B'
elif self == self.FORWARD:
return 'F'
elif self == self.RIGHT:
return 'R'
elif self == self.LEFT:
return 'L'
else:
assert False
def get_slot_rotation_matrix(self):
if self == self.BACK:
return rotate_matrix_z_axis(math.pi) # 180
elif self == self.FORWARD:
return rotate_matrix_z_axis(0.0)
elif self == self.RIGHT:
return rotate_matrix_z_axis(math.pi / 2.0) # 90
elif self == self.LEFT:
return rotate_matrix_z_axis(math.pi / -2.0) # -90
def rotate_matrix_z_axis(angle):
z_rotation_matrix = np.array([
[round(np.cos(angle)), -1*round(np.sin(angle)), 0],
[round(np.sin(angle)), round(np.cos(angle)), 0],
[0, 0, 1]
])
return z_rotation_matrix
def rotate_matrix_x_axis(angle):
x_rotation_matrix = np.array([
[1, 0, 0],
[0, round(np.cos(angle)), -1*round(np.sin(angle))],
[0, round(np.sin(angle)), round(np.cos(angle))]
])
return x_rotation_matrix
class RevolveModule:
"""
Base class allowing for constructing Robogen components in an overviewable manner
"""
DEFAULT_COLOR = (0.5, 0.5, 0.5)
TYPE = None
VISUAL_MESH = None
COLLISION_BOX = None
MASS = None
INERTIA = None
def __init__(self):
self.id = None
self.orientation = None
self.rgb = None # RevolveModule.DEFAULT_COLOR
self.substrate_coordinates = None
self.children = [None, None, None, None]
self.info = None
def color(self):
return self.rgb if self.rgb is not None else self.DEFAULT_COLOR
@staticmethod
def FromYaml(yaml_object):
"""
From a yaml object, creates a data struture of interconnected body modules.
Standard names for modules are:
CoreComponent
ActiveHinge
FixedBrick
FixedBrickSensor
"""
mod_type = yaml_object['type']
if mod_type == 'CoreComponent' or mod_type == 'Core':
module = CoreModule()
elif mod_type == 'ActiveHinge':
module = ActiveHingeModule()
elif mod_type == 'FixedBrick':
module = BrickModule()
elif mod_type == 'FixedBrickSensor':
module = BrickSensorModule()
elif mod_type == 'TouchSensor':
module = TouchSensorModule()
else:
raise NotImplementedError('"{}" module not yet implemented'.format(mod_type))
module.id = yaml_object['id']
try:
module.orientation = yaml_object['orientation']
except KeyError:
module.orientation = 0
try:
module.rgb = (
yaml_object['params']['red'],
yaml_object['params']['green'],
yaml_object['params']['blue'],
)
except KeyError:
pass
if 'children' in yaml_object:
for parent_slot in yaml_object['children']:
module.children[parent_slot] = RevolveModule.FromYaml(
yaml_object=yaml_object['children'][parent_slot])
return module
def to_yaml(self):
if self.TYPE is None:
raise RuntimeError('Module TYPE is not implemented for "{}",'
' this should be defined.'.format(self.__class__))
yaml_dict_object = OrderedDict()
yaml_dict_object['id'] = self.id
yaml_dict_object['type'] = self.TYPE
yaml_dict_object['orientation'] = self.orientation
if self.rgb is not None:
yaml_dict_object['params'] = {
'red': self.rgb[0],
'green': self.rgb[1],
'blue': self.rgb[2],
}
children = self._generate_yaml_children()
if children is not None:
yaml_dict_object['children'] = children
return yaml_dict_object
def iter_children(self):
return enumerate(self.children)
def _generate_yaml_children(self):
has_children = False
children = {}
for i, child in self.iter_children():
if child is not None:
children[i] = child.to_yaml()
has_children = True
return children if has_children else None
def validate(self):
"""
Tests if the robot tree is valid (recursively)
:return: True if the robot tree is valid
"""
raise RuntimeError("Robot tree validation not yet implemented")
def to_sdf(self, tree_depth='', parent_link=None, child_link=None):
"""
Transform the module in sdf elements.
IMPORTANT: It does not append VISUAL and COLLISION elements to the parent link
automatically. It does append automatically the SENSOR element.
TODO: make the append automatic for VISUAL AND COLLISION AS WELL.
:param tree_depth: current tree depth as string (for naming)
:param parent_link: link of the parent (may be needed for certain modules)
:param child_link: link of the child (may be needed for certain modules, like hinges)
:return: visual SDF element, collision SDF element, sensor SDF element.
Sensor SDF element may be None.
"""
name = 'component_{}_{}__box'.format(tree_depth, self.TYPE)
visual = SDF.Visual(name, self.rgb)
geometry = SDF.MeshGeometry(self.VISUAL_MESH)
visual.append(geometry)
collision = SDF.Collision(name, self.MASS)
geometry = SDF.BoxGeometry(self.COLLISION_BOX)
collision.append(geometry)
return visual, collision, None
def boxslot(self, orientation=None):
orientation = Orientation.BACK if orientation is None else orientation
return BoxSlot(self.possible_slots(), orientation)
def possible_slots(self):
box_geometry = self.COLLISION_BOX
return (
(box_geometry[0] / -2.0, box_geometry[0] / 2.0), # X
(box_geometry[1] / -2.0, box_geometry[1] / 2.0), # Y
(box_geometry[2] / -2.0, box_geometry[2] / 2.0), # Z
)
def has_children(self) -> bool:
"""
Check whether module has children
:return: True if module has children
"""
for i, child in self.iter_children():
if child is not None:
return True
return False
def count_module_connections(self) -> int:
"""
Count how many connections the module has.
Connected TouchSensor and BrickSensor Modules are ignored
:return: number of connections
"""
if not self.has_children():
return 1
children_count = 0
for core_slot, child in self.iter_children():
if child is not None:
continue
if not isinstance(child, TouchSensorModule) and \
not isinstance(child, BrickSensorModule):
children_count += 1
return children_count + 1
def is_folding(self) -> bool:
"""
Checks if the module is a folding point.
The module is a folding point if and only if it has one child and that child is not
attached opposite to the parent.
:return: True if the module is a folding point
"""
if not self.has_children():
return False
# Default parent slot is BACK
parent_slot = Orientation.BACK
target_slot = None
for slot, child in self.iter_children():
if slot == parent_slot:
continue
# is the child slot occupied?
if child is None:
continue
# ignore TouchSensor and BrickSensor modules
if isinstance(child, TouchSensorModule) or \
isinstance(child, BrickSensorModule):
continue
# second slot found, not a fold
if target_slot is not None:
return False
target_slot = slot
target_slot = Orientation(target_slot)
return parent_slot.opposite() != target_slot
class CoreModule(RevolveModule):
"""
Inherits class RevolveModule. Creates Robogen core module
"""
TYPE = "CoreComponent"
VISUAL_MESH = 'model://rg_robot/meshes/CoreComponent.dae'
SLOT_COORDINATES = 0.089 / 2.0
COLLISION_BOX = (0.089, 0.089, 0.045)
MASS = grams(90)
def __init__(self):
super().__init__()
self.substrate_coordinates = (0, 0, 0)
def possible_slots(self):
return (
(-self.SLOT_COORDINATES, self.SLOT_COORDINATES), # X
(-self.SLOT_COORDINATES, self.SLOT_COORDINATES), # Y
(-self.SLOT_COORDINATES, self.SLOT_COORDINATES), # Z
)
def to_sdf(self, tree_depth='', parent_link=None, child_link=None):
imu_sensor = SDF.IMUSensor('core-imu_sensor', parent_link, self)
visual, collision, _ = super().to_sdf(tree_depth, parent_link, child_link)
parent_link.append(imu_sensor)
return visual, collision, imu_sensor
def count_module_connections(self) -> int:
"""
Count how many connections the module has.
Connected TouchSensor and BrickSensor Modules are ignored
:return: number of connections
"""
# The CoreModule does not have a parent module
return super(CoreModule, self).count_module_connections() - 1
def is_folding(self) -> bool:
# parent slot is not supposed to be set
parent_slot = None
target_slot = None
for slot, child in self.iter_children():
# is the child slot occupied?
if child is None:
continue
# ignore TouchSensor and BrickSensor modules
if isinstance(child, TouchSensorModule) or \
isinstance(child, BrickSensorModule):
continue
if parent_slot is None:
parent_slot = slot
elif target_slot is None:
target_slot = slot
else:
# More than 2 children, this is not a fold
return False
if parent_slot is None or target_slot is None:
return False
return parent_slot.opposite() != target_slot
class ActiveHingeModule(RevolveModule):
"""
Inherits class RevolveModule. Creates Robogen joint module
"""
TYPE = 'ActiveHinge'
VISUAL_MESH_FRAME = 'model://rg_robot/meshes/ActiveHinge_Frame.dae'
VISUAL_MESH_SERVO = 'model://rg_robot/meshes/ActiveCardanHinge_Servo_Holder.dae'
COLLISION_BOX_FRAME = (2.20e-02, 3.575e-02, 1.0e-02)
COLLISION_BOX_SERVO = (2.45e-02, 2.575e-02, 1.5e-02)
COLLISION_BOX_SERVO_2 = (1.0e-3, 3.4e-2, 3.4e-02)
COLLISION_BOX_SERVO_OFFSET = (
SDF.math.Vector3(0, 0, 0),
SDF.math.Vector3(-0.0091, 0, 0),
)
MASS_FRAME = grams(1.7)
MASS_SERVO = grams(9)
def __init__(self):
super().__init__()
self.children = {1: None}
def iter_children(self):
return self.children.items()
def _generate_yaml_children(self):
child = self.children[1]
if child is None:
return None
else:
return {1: child.to_yaml()}
def to_sdf(self, tree_depth='', parent_link=None, child_link=None):
assert(parent_link is not None)
assert(child_link is not None)
name_frame = 'component_{}_{}__frame'.format(tree_depth, self.TYPE)
name_joint = 'component_{}_{}__joint'.format(tree_depth, self.TYPE)
name_servo = 'component_{}_{}__servo'.format(tree_depth, self.TYPE)
name_servo2 = 'component_{}_{}__servo2'.format(tree_depth, self.TYPE)
visual_frame = SDF.Visual(name_frame, self.rgb)
geometry = SDF.MeshGeometry(self.VISUAL_MESH_FRAME)
visual_frame.append(geometry)
collision_frame = SDF.Collision(name_frame, self.MASS_FRAME)
geometry = SDF.BoxGeometry(self.COLLISION_BOX_FRAME)
collision_frame.append(geometry)
visual_servo = SDF.Visual(name_servo, self.rgb)
geometry = SDF.MeshGeometry(self.VISUAL_MESH_SERVO)
visual_servo.append(geometry)
collision_servo = SDF.Collision(name_servo, self.MASS_SERVO)
collision_servo.translate(SDF.math.Vector3(0.002375, 0, 0))
geometry = SDF.BoxGeometry(self.COLLISION_BOX_SERVO)
collision_servo.append(geometry)
collision_servo_2 = SDF.Collision(name_servo2, 0)
collision_servo_2.translate(SDF.math.Vector3(0.01175, 0.001, 0))
geometry = SDF.BoxGeometry(self.COLLISION_BOX_SERVO_2)
collision_servo_2.append(geometry)
joint = SDF.Joint(self.id,
name_joint,
parent_link,
child_link,
axis=SDF.math.Vector3(0, 1, 0),
coordinates=self.substrate_coordinates,
motorized=True)
joint.set_position(SDF.math.Vector3(-0.0085, 0, 0))
return visual_frame, \
[collision_frame], \
visual_servo, \
[collision_servo, collision_servo_2], \
joint
def possible_slots_frame(self):
box_geometry = self.COLLISION_BOX_FRAME
return (
(box_geometry[0] / -2.0, box_geometry[0] / 2.0 - 0.001), # X
(0, 0), # Y
(0, 0), # Z
)
def possible_slots_servo(self):
box_geometry = self.COLLISION_BOX_SERVO
return (
(box_geometry[0] / -2.0, box_geometry[0] / 2.0), # X
(0, 0), # Y
(0, 0), # Z
)
def boxslot_frame(self, orientation=None):
orientation = Orientation.BACK if orientation is None else orientation
boundaries = self.possible_slots_frame()
return BoxSlotJoints(
boundaries,
orientation,
self.COLLISION_BOX_SERVO_OFFSET
)
def boxslot_servo(self, orientation=None):
orientation = Orientation.BACK if orientation is None else orientation
boundaries = self.possible_slots_servo()
return BoxSlotJoints(boundaries, orientation)
def boxslot(self, orientation=None):
orientation = Orientation.BACK if orientation is None else orientation
if orientation is Orientation.BACK:
return self.boxslot_frame(orientation)
elif orientation is Orientation.FORWARD:
return self.boxslot_servo(orientation)
else:
raise RuntimeError("Invalid orientation")
class BrickModule(RevolveModule):
"""
Inherits class RevolveModule. Creates Robogen brick module
"""
TYPE = "FixedBrick"
VISUAL_MESH = 'model://rg_robot/meshes/FixedBrick.dae'
SLOT_COORDINATES = 3.8e-2 / 2.0
COLLISION_BOX = (4.1e-2, 4.1e-2, 3.55e-02)
MASS = grams(10.2)
def __init__(self):
super().__init__()
def possible_slots(self):
return (
(-self.SLOT_COORDINATES, self.SLOT_COORDINATES), # X
(-self.SLOT_COORDINATES, self.SLOT_COORDINATES), # Y
(-self.SLOT_COORDINATES, self.SLOT_COORDINATES), # Z
)
class BrickSensorModule(RevolveModule):
"""
TODO not finished
Inherits class RevolveModule. Creates Robogen brick sensor module
"""
TYPE = "FixedBrickSensor"
VISUAL_MESH = 'model://rg_robot/meshes/FixedBrick.dae'
COLLISION_BOX = (4.1e-2, 4.1e-2, 3.55e-02)
def __init__(self):
super().__init__()
raise RuntimeError("Not implemented yet")
class TouchSensorModule(RevolveModule):
"""
Inherits class RevolveModule. Creates Robogen sensor module
"""
TYPE = "TouchSensor"
VISUAL_MESH = 'model://rg_robot/meshes/TouchSensor.dae'
SLOT_COORDINATES = 1e-2 / 2.0
COLLISION_BOX = (4.1e-3, 3.1e-2, 1.55e-02)
MASS = grams(3)
def __init__(self):
super().__init__()
self.children = {}
def boxslot(self, orientation=None):
orientation = Orientation.BACK if orientation is None else orientation
assert (orientation is Orientation.BACK)
return BoxSlotTouchSensor(self.possible_slots())
def possible_slots(self):
return (
(-self.SLOT_COORDINATES, 0), # X
(0, 0), # Y
(0, 0), # Z
)
def to_sdf(self, tree_depth='', parent_link=None, child_link=None):
assert(parent_link is not None)
name = 'component_{}_{}'.format(tree_depth, self.TYPE)
name_sensor = 'sensor_{}_{}'.format(tree_depth, self.TYPE)
visual = SDF.Visual(name, self.rgb)
geometry = SDF.MeshGeometry(self.VISUAL_MESH)
visual.append(geometry)
collision = SDF.Collision(name, self.MASS)
geometry = SDF.BoxGeometry(self.COLLISION_BOX)
# collision.translate(SDF.math.Vector3(0.01175, 0.001, 0))
collision.append(geometry)
sensor = SDF.TouchSensor(name_sensor, collision, parent_link, self)
parent_link.append(sensor)
return visual, collision, sensor
class BoxSlot:
"""
Helper class for modules connection slots
"""
def __init__(self, boundaries, orientation: Orientation):
self.orientation = orientation
self.pos = self._calculate_box_slot_pos(boundaries, orientation)
self.normal = self.pos.normalized()
self.tangent = self._calculate_box_slot_tangent(orientation)
def _calculate_box_slot_pos(self, boundaries, slot: Orientation):
# boundaries = collision_elem.boundaries
if slot == Orientation.BACK:
return SDF.math.Vector3(0, boundaries[1][0], 0)
elif slot == Orientation.FORWARD:
return SDF.math.Vector3(0, boundaries[1][1], 0)
elif slot == Orientation.RIGHT:
return SDF.math.Vector3(boundaries[0][1], 0, 0)
elif slot == Orientation.LEFT:
return SDF.math.Vector3(boundaries[0][0], 0, 0)
else:
raise RuntimeError('invalid module orientation: {}'.format(slot))
@staticmethod
def _calculate_box_slot_tangent(slot: Orientation):
"""
Return slot tangent
"""
if slot == Orientation.BACK:
return SDF.math.Vector3(0, 0, 1)
elif slot == Orientation.FORWARD:
return SDF.math.Vector3(0, 0, 1)
elif slot == Orientation.RIGHT:
return SDF.math.Vector3(0, 0, 1)
elif slot == Orientation.LEFT:
return SDF.math.Vector3(0, 0, 1)
# elif slot == 4:
# # Right face tangent: back face
# return SDF.math.Vector3(0, 1, 0)
# elif slot == 5:
# # Left face tangent: back face
# return SDF.math.Vector3(0, 1, 0)
else:
raise RuntimeError("Invalid orientation")
class BoxSlotJoints(BoxSlot):
def __init__(self, boundaries, orientation: Orientation, offset=(SDF.math.Vector3(), SDF.math.Vector3())):
self.offset = offset
super().__init__(boundaries, orientation)
def _calculate_box_slot_pos(self, boundaries, slot: Orientation):
if slot == Orientation.BACK:
return SDF.math.Vector3(boundaries[0][0], 0, 0) + self.offset[0]
elif slot == Orientation.FORWARD:
return SDF.math.Vector3(boundaries[0][1], 0, 0) + self.offset[1]
else:
raise RuntimeError('invalid module orientation: {}'.format(slot))
@staticmethod
def _calculate_box_slot_tangent(slot: Orientation):
"""
Return slot tangent
"""
if slot == Orientation.BACK:
return SDF.math.Vector3(0, 0, 1)
elif slot == Orientation.FORWARD:
return SDF.math.Vector3(0, 0, 1)
else:
raise RuntimeError("Invalid orientation")
class BoxSlotTouchSensor(BoxSlot):
def __init__(self, boundaries):
super().__init__(boundaries, Orientation.BACK)
def _calculate_box_slot_pos(self, boundaries, slot: Orientation):
if slot == Orientation.BACK:
return SDF.math.Vector3(boundaries[0][0], 0, 0)
else:
raise RuntimeError('invalid module orientation: {}'.format(slot))
@staticmethod
def _calculate_box_slot_tangent(slot: Orientation):
"""
Return slot tangent
"""
if slot == Orientation.BACK:
return SDF.math.Vector3(0, 1, 0)
else:
raise RuntimeError("Invalid orientation")