Skip to content

Commit 7ec5780

Browse files
committed
Change Rotation Attribute to Orientation for Sensor and Module (#548)
1 parent 34bf56a commit 7ec5780

File tree

13 files changed

+81
-60
lines changed

13 files changed

+81
-60
lines changed

ci_group/revolve2/ci_group/morphological_measures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __init__(self, body: Body) -> None:
133133
@classmethod
134134
def __calculate_is_2d_recur(cls, module: Module) -> bool:
135135
return all(
136-
[np.isclose(module.rotation, 0.0)]
136+
[np.isclose(module.orientation.angle, 0.0)]
137137
+ [cls.__calculate_is_2d_recur(child) for child in module.children.values()]
138138
)
139139

ci_group/revolve2/ci_group/planar_robot_representation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _draw_module(
107107
context.set_source_rgb(255, 255, 0) # Yellow
108108
case ActiveHinge():
109109
context.set_source_rgb(1, 0, 0) # Red
110-
if module.rotation == 0:
110+
if np.isclose(module.orientation.angle, 0.0):
111111
context.set_source_rgb(1.0, 0.4, 0.4) # Flesh Color
112112
case Brick():
113113
context.set_source_rgb(0, 0, 1) # Blue

modular_robot/revolve2/modular_robot/body/_module.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import uuid
44

5+
from pyrr import Quaternion
6+
57
from ._attachment_point import AttachmentPoint
68
from ._color import Color
7-
from ._right_angles import RightAngles
89
from .sensors import ActiveHingeSensor, CameraSensor, IMUSensor, Sensor
910

1011

@@ -99,7 +100,7 @@ class Module:
99100

100101
_attachment_points: dict[int, AttachmentPoint]
101102
_children: dict[int, Module]
102-
_rotation: float
103+
_orientation: Quaternion
103104

104105
_parent: Module | None
105106
"""
@@ -120,15 +121,15 @@ class Module:
120121

121122
def __init__(
122123
self,
123-
rotation: float | RightAngles,
124+
orientation: Quaternion,
124125
color: Color,
125126
attachment_points: dict[int, AttachmentPoint],
126127
sensors: list[Sensor],
127128
) -> None:
128129
"""
129130
Initialize this object.
130131
131-
:param rotation: Orientation of this model relative to its parent.
132+
:param orientation: Orientation of this model relative to its parent.
132133
:param color: The color of the module.
133134
:param attachment_points: The attachment points available on a module.
134135
:param sensors: The sensors associated with the module.
@@ -144,9 +145,7 @@ def __init__(
144145
for sensor in sensors: # Add all desired sensors to the module.
145146
self._sensors.add_sensor(sensor)
146147
self._attachment_points = attachment_points
147-
self._rotation = (
148-
rotation if isinstance(rotation, (float, int)) else rotation.value
149-
)
148+
self._orientation = orientation
150149
self._color = color
151150

152151
@property
@@ -159,13 +158,13 @@ def uuid(self) -> uuid.UUID:
159158
return self._uuid
160159

161160
@property
162-
def rotation(self) -> float:
161+
def orientation(self) -> Quaternion:
163162
"""
164163
Get the orientation of this model relative to its parent.
165164
166165
:returns: The orientation.
167166
"""
168-
return self._rotation
167+
return self._orientation
169168

170169
@property
171170
def parent(self) -> Module | None:

modular_robot/revolve2/modular_robot/body/base/_active_hinge.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,16 @@ def __init__(
102102
orientation=Quaternion.from_eulers([0.0, 0.0, 0.0]),
103103
),
104104
}
105-
105+
"""
106+
The base module only has orientation as its parameter since not all modules are square.
107+
108+
Here we covert the angle of the module to its orientation in space.
109+
"""
110+
orientation = Quaternion.from_eulers(
111+
[rotation if isinstance(rotation, float) else rotation.value, 0, 0]
112+
)
106113
super().__init__(
107-
rotation, Color(255, 255, 255, 255), attachment_points, sensors
114+
orientation, Color(255, 255, 255, 255), attachment_points, sensors
108115
)
109116

110117
self._sensor = None

modular_robot/revolve2/modular_robot/body/base/_attachment_face.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pyrr import Quaternion
2+
13
from .._attachment_point import AttachmentPoint
24
from .._color import Color
35
from .._module import Module
@@ -22,8 +24,16 @@ def __init__(
2224
:param rotation: Orientation of this model relative to its parent.
2325
:param attachment_points: The attachment points available on a module.
2426
"""
27+
"""
28+
The base module only has orientation as its parameter since not all modules are square.
29+
30+
Here we covert the angle of the module to its orientation in space.
31+
"""
32+
orientation = Quaternion.from_eulers(
33+
[rotation if isinstance(rotation, float) else rotation.value, 0, 0]
34+
)
2535
super().__init__(
26-
rotation=rotation,
36+
orientation=orientation,
2737
attachment_points=attachment_points,
2838
color=Color(255, 255, 255, 255),
2939
sensors=[],

modular_robot/revolve2/modular_robot/body/base/_body.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ def grid_position(cls, module: Module) -> Vector3:
4444
while parent is not None and child_index is not None:
4545
child = parent.children.get(child_index)
4646
assert child is not None
47-
assert np.isclose(child.rotation % (math.pi / 2.0), 0.0)
47+
assert np.isclose(child.orientation.angle % (math.pi / 2.0), 0.0)
4848

49-
position = Quaternion.from_eulers((child.rotation, 0.0, 0.0)) * position
49+
position = child.orientation * position
5050
position += Vector3([1, 0, 0])
5151

5252
attachment_point = parent.attachment_points.get(child_index)
@@ -143,11 +143,9 @@ def _make_grid_recur(
143143
for child_index, attachment_point in module.attachment_points.items():
144144
child = module.children.get(child_index)
145145
if child is not None:
146-
assert np.isclose(child.rotation % (math.pi / 2.0), 0.0)
146+
assert np.isclose(child.orientation.angle % (math.pi / 2.0), 0.0)
147147
rotation = (
148-
orientation
149-
* attachment_point.orientation
150-
* Quaternion.from_eulers([child.rotation, 0, 0])
148+
orientation * attachment_point.orientation * child.orientation
151149
)
152150
self._make_grid_recur(
153151
child, position + rotation * Vector3([1.0, 0.0, 0.0]), rotation

modular_robot/revolve2/modular_robot/body/base/_brick.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,18 @@ def __init__(
5252
}
5353
self._mass = mass
5454
self._bounding_box = bounding_box
55-
super().__init__(rotation, Color(50, 50, 255, 255), attachment_points, sensors)
55+
56+
"""
57+
The base module only has orientation as its parameter since not all modules are square.
58+
59+
Here we covert the angle of the module to its orientation in space.
60+
"""
61+
orientation = Quaternion.from_eulers(
62+
[rotation if isinstance(rotation, float) else rotation.value, 0, 0]
63+
)
64+
super().__init__(
65+
orientation, Color(50, 50, 255, 255), attachment_points, sensors
66+
)
5667

5768
@property
5869
def front(self) -> Module | None:

modular_robot/revolve2/modular_robot/body/base/_core.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ def __init__(
5959
),
6060
}
6161

62-
super().__init__(rotation, Color(255, 50, 50, 255), attachment_points, sensors)
63-
self._parent = None
64-
self._parent_child_index = None
62+
"""
63+
The base module only has orientation as its parameter since not all modules are square.
64+
65+
Here we covert the angle of the module to its orientation in space.
66+
"""
67+
orientation = Quaternion.from_eulers(
68+
[rotation if isinstance(rotation, float) else rotation.value, 0, 0]
69+
)
70+
super().__init__(
71+
orientation, Color(255, 50, 50, 255), attachment_points, sensors
72+
)
6573

6674
@property
6775
def mass(self) -> float:
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
from pyrr import Vector3
1+
from pyrr import Quaternion, Vector3
22

33
from ._sensor import Sensor
44

55

66
class ActiveHingeSensor(Sensor):
77
"""A sensors for an active hinge that measures its angle."""
88

9-
def __init__(self, rotation: float = 0.0) -> None:
10-
"""
11-
Initialize the ActiveHinge sensor.
12-
13-
:param rotation: The rotation of the IMU.
14-
"""
15-
super().__init__(rotation, Vector3([0, 0, 0]))
9+
def __init__(self) -> None:
10+
"""Initialize the ActiveHinge sensor."""
11+
super().__init__(Quaternion(), Vector3())

modular_robot/revolve2/modular_robot/body/sensors/_camera_sensor.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pyrr import Vector3
1+
from pyrr import Quaternion, Vector3
22

33
from ._sensor import Sensor
44

@@ -11,7 +11,7 @@ class CameraSensor(Sensor):
1111
def __init__(
1212
self,
1313
position: Vector3,
14-
rotation: float = 0.0,
14+
orientation: Quaternion = Quaternion(),
1515
camera_size: tuple[int, int] = (50, 50),
1616
) -> None:
1717
"""
@@ -21,21 +21,12 @@ def __init__(
2121
For evolution related work stick to 10x10 for fast results.
2222
2323
:param position: The position of the camera.
24-
:param rotation: The rotation of the camera.
24+
:param orientation: The rotation of the camera.
2525
:param camera_size: The size of the camera image.
2626
"""
27-
super().__init__(rotation, position)
27+
super().__init__(orientation, position)
2828
self._camera_size = camera_size
2929

30-
@property
31-
def position(self) -> Vector3:
32-
"""
33-
Get the position of the camera.
34-
35-
:return: The position.
36-
"""
37-
return self._position
38-
3930
@property
4031
def camera_size(self) -> tuple[int, int]:
4132
"""
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pyrr import Vector3
1+
from pyrr import Quaternion, Vector3
22

33
from ._sensor import Sensor
44

@@ -10,11 +10,13 @@ class IMUSensor(Sensor):
1010
Reports specific force(closely related to acceleration), angular rate(closely related to angular velocity), and orientation.
1111
"""
1212

13-
def __init__(self, position: Vector3, rotation: float = 0.0) -> None:
13+
def __init__(
14+
self, position: Vector3, orientation: Quaternion = Quaternion()
15+
) -> None:
1416
"""
1517
Initialize the IMU sensor.
1618
17-
:param rotation: The rotation of the IMU.
19+
:param orientation: The rotation of the IMU.
1820
:param position: The position of the IMU.
1921
"""
20-
super().__init__(rotation, position)
22+
super().__init__(orientation, position)

modular_robot/revolve2/modular_robot/body/sensors/_sensor.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import uuid
22
from abc import ABC
33

4-
from pyrr import Vector3
4+
from pyrr import Quaternion, Vector3
55

66

77
class Sensor(ABC):
88
"""An abstract Sensor Class."""
99

1010
_uuid: uuid.UUID
11-
_rotation: float
11+
_orientation: Quaternion
1212
_position: Vector3
1313

14-
def __init__(self, rotation: float, position: Vector3) -> None:
14+
def __init__(self, orientation: Quaternion, position: Vector3) -> None:
1515
"""
1616
Initialize the sensor.
1717
18-
:param rotation: The rotation of the sensor.
18+
:param orientation: The rotation of the sensor.
1919
:param position: The position of the sensor.
2020
"""
21-
self._rotation = rotation
21+
self._orientation = orientation
2222
self._uuid = uuid.uuid1()
2323
self._position = position
2424

@@ -32,13 +32,13 @@ def uuid(self) -> uuid.UUID:
3232
return self._uuid
3333

3434
@property
35-
def rotation(self) -> float:
35+
def orientation(self) -> Quaternion:
3636
"""
37-
Return the rotation of the sensor.
37+
Return the orientation of the sensor.
3838
39-
:return: The rotation.
39+
:return: The orientation.
4040
"""
41-
return self._rotation
41+
return self._orientation
4242

4343
@property
4444
def position(self) -> Vector3:

modular_robot_simulation/revolve2/modular_robot_simulation/_build_multi_body_systems/_unbuilt_child.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def make_pose(
2424
:param position: The position argument from the parent.
2525
:param orientation: The orientation of the attachment on the parent.
2626
"""
27-
module_rot = Quaternion.from_eulers([self.child_object.rotation, 0.0, 0.0])
2827
self.pose = Pose(
2928
position,
30-
orientation * module_rot,
29+
orientation * self.child_object.orientation,
3130
)

0 commit comments

Comments
 (0)