Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

547 change camera sensor roataion to quaternion #550

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci_group/revolve2/ci_group/morphological_measures.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(self, body: Body) -> None:
@classmethod
def __calculate_is_2d_recur(cls, module: Module) -> bool:
return all(
[np.isclose(module.rotation, 0.0)]
[np.isclose(module.orientation.angle, 0.0)]
+ [cls.__calculate_is_2d_recur(child) for child in module.children.values()]
)

Expand Down
2 changes: 1 addition & 1 deletion ci_group/revolve2/ci_group/planar_robot_representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _draw_module(
context.set_source_rgb(255, 255, 0) # Yellow
case ActiveHinge():
context.set_source_rgb(1, 0, 0) # Red
if module.rotation == 0:
if np.isclose(module.orientation.angle, 0.0):
context.set_source_rgb(1.0, 0.4, 0.4) # Flesh Color
case Brick():
context.set_source_rgb(0, 0, 1) # Blue
Expand Down
17 changes: 8 additions & 9 deletions modular_robot/revolve2/modular_robot/body/_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import uuid

from pyrr import Quaternion

from ._attachment_point import AttachmentPoint
from ._color import Color
from ._right_angles import RightAngles
from .sensors import ActiveHingeSensor, CameraSensor, IMUSensor, Sensor


Expand Down Expand Up @@ -99,7 +100,7 @@ class Module:

_attachment_points: dict[int, AttachmentPoint]
_children: dict[int, Module]
_rotation: float
_orientation: Quaternion

_parent: Module | None
"""
Expand All @@ -120,15 +121,15 @@ class Module:

def __init__(
self,
rotation: float | RightAngles,
orientation: Quaternion,
color: Color,
attachment_points: dict[int, AttachmentPoint],
sensors: list[Sensor],
) -> None:
"""
Initialize this object.

:param rotation: Orientation of this model relative to its parent.
:param orientation: Orientation of this model relative to its parent.
:param color: The color of the module.
:param attachment_points: The attachment points available on a module.
:param sensors: The sensors associated with the module.
Expand All @@ -144,9 +145,7 @@ def __init__(
for sensor in sensors: # Add all desired sensors to the module.
self._sensors.add_sensor(sensor)
self._attachment_points = attachment_points
self._rotation = (
rotation if isinstance(rotation, (float, int)) else rotation.value
)
self._orientation = orientation
self._color = color

@property
Expand All @@ -159,13 +158,13 @@ def uuid(self) -> uuid.UUID:
return self._uuid

@property
def rotation(self) -> float:
def orientation(self) -> Quaternion:
"""
Get the orientation of this model relative to its parent.

:returns: The orientation.
"""
return self._rotation
return self._orientation

@property
def parent(self) -> Module | None:
Expand Down
11 changes: 9 additions & 2 deletions modular_robot/revolve2/modular_robot/body/base/_active_hinge.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,16 @@ def __init__(
orientation=Quaternion.from_eulers([0.0, 0.0, 0.0]),
),
}

"""
The base module only has orientation as its parameter since not all modules are square.

Here we covert the angle of the module to its orientation in space.
"""
orientation = Quaternion.from_eulers(
[rotation if isinstance(rotation, float) else rotation.value, 0, 0]
)
super().__init__(
rotation, Color(255, 255, 255, 255), attachment_points, sensors
orientation, Color(255, 255, 255, 255), attachment_points, sensors
)

self._sensor = None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pyrr import Quaternion

from .._attachment_point import AttachmentPoint
from .._color import Color
from .._module import Module
Expand All @@ -22,8 +24,16 @@ def __init__(
:param rotation: Orientation of this model relative to its parent.
:param attachment_points: The attachment points available on a module.
"""
"""
The base module only has orientation as its parameter since not all modules are square.

Here we covert the angle of the module to its orientation in space.
"""
orientation = Quaternion.from_eulers(
[rotation if isinstance(rotation, float) else rotation.value, 0, 0]
)
super().__init__(
rotation=rotation,
orientation=orientation,
attachment_points=attachment_points,
color=Color(255, 255, 255, 255),
sensors=[],
Expand Down
10 changes: 4 additions & 6 deletions modular_robot/revolve2/modular_robot/body/base/_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def grid_position(cls, module: Module) -> Vector3:
while parent is not None and child_index is not None:
child = parent.children.get(child_index)
assert child is not None
assert np.isclose(child.rotation % (math.pi / 2.0), 0.0)
assert np.isclose(child.orientation.angle % (math.pi / 2.0), 0.0)

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

attachment_point = parent.attachment_points.get(child_index)
Expand Down Expand Up @@ -143,11 +143,9 @@ def _make_grid_recur(
for child_index, attachment_point in module.attachment_points.items():
child = module.children.get(child_index)
if child is not None:
assert np.isclose(child.rotation % (math.pi / 2.0), 0.0)
assert np.isclose(child.orientation.angle % (math.pi / 2.0), 0.0)
rotation = (
orientation
* attachment_point.orientation
* Quaternion.from_eulers([child.rotation, 0, 0])
orientation * attachment_point.orientation * child.orientation
)
self._make_grid_recur(
child, position + rotation * Vector3([1.0, 0.0, 0.0]), rotation
Expand Down
13 changes: 12 additions & 1 deletion modular_robot/revolve2/modular_robot/body/base/_brick.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ def __init__(
}
self._mass = mass
self._bounding_box = bounding_box
super().__init__(rotation, Color(50, 50, 255, 255), attachment_points, sensors)

"""
The base module only has orientation as its parameter since not all modules are square.

Here we covert the angle of the module to its orientation in space.
"""
orientation = Quaternion.from_eulers(
[rotation if isinstance(rotation, float) else rotation.value, 0, 0]
)
super().__init__(
orientation, Color(50, 50, 255, 255), attachment_points, sensors
)

@property
def front(self) -> Module | None:
Expand Down
14 changes: 11 additions & 3 deletions modular_robot/revolve2/modular_robot/body/base/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,17 @@ def __init__(
),
}

super().__init__(rotation, Color(255, 50, 50, 255), attachment_points, sensors)
self._parent = None
self._parent_child_index = None
"""
The base module only has orientation as its parameter since not all modules are square.

Here we covert the angle of the module to its orientation in space.
"""
orientation = Quaternion.from_eulers(
[rotation if isinstance(rotation, float) else rotation.value, 0, 0]
)
super().__init__(
orientation, Color(255, 50, 50, 255), attachment_points, sensors
)

@property
def mass(self) -> float:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from pyrr import Vector3
from pyrr import Quaternion, Vector3

from ._sensor import Sensor


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

def __init__(self, rotation: float = 0.0) -> None:
"""
Initialize the ActiveHinge sensor.

:param rotation: The rotation of the IMU.
"""
super().__init__(rotation, Vector3([0, 0, 0]))
def __init__(self) -> None:
"""Initialize the ActiveHinge sensor."""
super().__init__(Quaternion(), Vector3())
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyrr import Vector3
from pyrr import Quaternion, Vector3

from ._sensor import Sensor

Expand All @@ -11,7 +11,7 @@ class CameraSensor(Sensor):
def __init__(
self,
position: Vector3,
rotation: float = 0.0,
orientation: Quaternion = Quaternion(),
camera_size: tuple[int, int] = (50, 50),
) -> None:
"""
Expand All @@ -21,21 +21,12 @@ def __init__(
For evolution related work stick to 10x10 for fast results.

:param position: The position of the camera.
:param rotation: The rotation of the camera.
:param orientation: The rotation of the camera.
:param camera_size: The size of the camera image.
"""
super().__init__(rotation, position)
super().__init__(orientation, position)
self._camera_size = camera_size

@property
def position(self) -> Vector3:
"""
Get the position of the camera.

:return: The position.
"""
return self._position

@property
def camera_size(self) -> tuple[int, int]:
"""
Expand Down
10 changes: 6 additions & 4 deletions modular_robot/revolve2/modular_robot/body/sensors/_imu_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pyrr import Vector3
from pyrr import Quaternion, Vector3

from ._sensor import Sensor

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

def __init__(self, position: Vector3, rotation: float = 0.0) -> None:
def __init__(
self, position: Vector3, orientation: Quaternion = Quaternion()
) -> None:
"""
Initialize the IMU sensor.

:param rotation: The rotation of the IMU.
:param orientation: The rotation of the IMU.
:param position: The position of the IMU.
"""
super().__init__(rotation, position)
super().__init__(orientation, position)
18 changes: 9 additions & 9 deletions modular_robot/revolve2/modular_robot/body/sensors/_sensor.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import uuid
from abc import ABC

from pyrr import Vector3
from pyrr import Quaternion, Vector3


class Sensor(ABC):
"""An abstract Sensor Class."""

_uuid: uuid.UUID
_rotation: float
_orientation: Quaternion
_position: Vector3

def __init__(self, rotation: float, position: Vector3) -> None:
def __init__(self, orientation: Quaternion, position: Vector3) -> None:
"""
Initialize the sensor.

:param rotation: The rotation of the sensor.
:param orientation: The rotation of the sensor.
:param position: The position of the sensor.
"""
self._rotation = rotation
self._orientation = orientation
self._uuid = uuid.uuid1()
self._position = position

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

@property
def rotation(self) -> float:
def orientation(self) -> Quaternion:
"""
Return the rotation of the sensor.
Return the orientation of the sensor.

:return: The rotation.
:return: The orientation.
"""
return self._rotation
return self._orientation

@property
def position(self) -> Vector3:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def make_pose(
:param position: The position argument from the parent.
:param orientation: The orientation of the attachment on the parent.
"""
module_rot = Quaternion.from_eulers([self.child_object.rotation, 0.0, 0.0])
self.pose = Pose(
position,
orientation * module_rot,
orientation * self.child_object.orientation,
)
Loading