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

433 add rotate for head #434

Merged
merged 9 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
66 changes: 65 additions & 1 deletion src/reachy2_sdk/parts/head.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from reachy2_sdk_api.kinematics_pb2 import ExtEulerAngles, Point, Quaternion, Rotation3d

from ..orbita.orbita3d import Orbita3d
from ..utils.utils import get_grpc_interpolation_mode
from ..utils.utils import get_grpc_interpolation_mode, quaternion_from_euler
from .goto_based_part import IGoToBasedPart
from .joints_based_part import JointsBasedPart

Expand Down Expand Up @@ -317,6 +317,70 @@ def look_at(
self._wait_goto(response)
return response

def rotate_by(
self,
roll: float = 0,
pitch: float = 0,
yaw: float = 0,
duration: float = 2,
wait: bool = False,
degrees: bool = True,
frame: str = "robot",
interpolation_mode: str = "minimum_jerk",
) -> GoToId:
"""Rotate the neck by the specified angles.

Args:
roll: The angle in degrees to rotate around the x-axis (roll). Defaults to 0.
pitch: The angle in degrees to rotate around the y-axis (pitch). Defaults to 0.
yaw: The angle in degrees to rotate around the z-axis (yaw). Defaults to 0.
duration: The time in seconds for the neck to reach the target posture. Defaults to 2.
wait: Whether to wait for the movement to complete before returning. Defaults to False.
degrees: Whether the angles are provided in degrees. If True, the angles will be converted to radians.
Defaults to True.
frame: The frame of reference for the rotation. Can be either "robot" or "head". Defaults to "robot".
interpolation_mode: The interpolation mode for the movement, either "minimum_jerk" or "linear".
Defaults to "minimum_jerk".


Raises:
ValueError: If the frame is not "robot" or "head".
ValueError: If the duration is set to 0.
ValueError: If the interpolation mode is not "minimum_jerk" or "linear".
"""
if frame not in ["robot", "head"]:
raise ValueError(f"Unknown frame {frame}! Should be 'robot' or 'head'")
if not degrees:
roll, pitch, yaw = np.rad2deg([roll, pitch, yaw])

try:
goto = self.get_goto_queue()[-1]
except IndexError:
goto = self.get_goto_playing()

if goto.id != -1:
joints_request = self._get_goto_joints_request(goto)
else:
joints_request = None

if joints_request is not None:
initial_orientation = joints_request.goal_positions
initial_orientation[1] += 10
ClaireHzl marked this conversation as resolved.
Show resolved Hide resolved
initial_quaternion = quaternion_from_euler(
initial_orientation[0], initial_orientation[1], initial_orientation[2], degrees=True
)
else:
initial_quaternion = self.get_current_orientation()

additional_quaternion = quaternion_from_euler(roll, pitch, yaw, degrees=True)

if frame == "head":
target_quaternion = initial_quaternion * additional_quaternion
elif frame == "robot":
target_quaternion = additional_quaternion * initial_quaternion

return self.goto(target_quaternion, duration, wait, interpolation_mode)

def goto_posture(
self,
duration: float = 2,
Expand Down
27 changes: 27 additions & 0 deletions src/reachy2_sdk/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,33 @@ def get_pose_matrix(position: List[float], rotation: List[float], degrees: bool
return pose


def quaternion_from_euler(roll: float, pitch: float, yaw: float, degrees: bool = True) -> Quaternion:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offline tests needed sorry :)

"""Convert Euler angles (intrinsic XYZ order) to a quaternion using the pyquaternion library.

Args:
roll (float): Rotation angle around the X-axis (roll), in degrees by default.
pitch (float): Rotation angle around the Y-axis (pitch), in degrees by default.
yaw (float): Rotation angle around the Z-axis (yaw), in degrees by default.
degrees (bool): If True, the input angles are interpreted as degrees. If False, they are
interpreted as radians. Defaults to True.

Returns:
Quaternion: The quaternion representing the combined rotation in 3D space.
"""
if degrees:
roll = np.deg2rad(roll)
pitch = np.deg2rad(pitch)
yaw = np.deg2rad(yaw)

qx = Quaternion(axis=[1, 0, 0], angle=roll)
qy = Quaternion(axis=[0, 1, 0], angle=pitch)
qz = Quaternion(axis=[0, 0, 1], angle=yaw)

quaternion = qx * qy * qz

return quaternion


def rotate_in_self(frame: npt.NDArray[np.float64], rotation: List[float], degrees: bool = True) -> npt.NDArray[np.float64]:
"""Return a new homogeneous 4x4 pose matrix that is the input matrix rotated in itself.

Expand Down
42 changes: 42 additions & 0 deletions tests/units/online/test_advanced_goto_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,3 +848,45 @@ def test_rotate_by_gripper_frame(reachy_sdk_zeroed: ReachySDK) -> None:

with pytest.raises(ValueError):
reachy_sdk_zeroed.r_arm.forward_kinematics(reachy_sdk_zeroed.get_goto_joints_request(req8).goal_positions)


@pytest.mark.online
def test_head_rotation(reachy_sdk_zeroed: ReachySDK) -> None:
reachy_sdk_zeroed.head.goto([0, 0, 90], duration=1, wait=True)
current_orientation = reachy_sdk_zeroed.head.get_current_positions()
assert np.allclose(current_orientation, [0, 0, 90], atol=5)

reachy_sdk_zeroed.head.rotate_by(roll=30, duration=1, frame="head", wait=True)
current_orientation = reachy_sdk_zeroed.head.get_current_positions()
assert np.allclose(current_orientation, [0, 30, 90], atol=5)

reachy_sdk_zeroed.head.rotate_by(roll=-30, duration=1, frame="head", wait=True)
current_orientation = reachy_sdk_zeroed.head.get_current_positions()
assert np.allclose(current_orientation, [0, 0, 90], atol=5)

reachy_sdk_zeroed.head.rotate_by(roll=30, duration=1, frame="robot", wait=True)
current_orientation = reachy_sdk_zeroed.head.get_current_positions()
assert np.allclose(current_orientation, [30, 0, 90], atol=5)

reachy_sdk_zeroed.head.rotate_by(roll=-30, duration=1, frame="robot", wait=True)
reachy_sdk_zeroed.head.rotate_by(pitch=30, duration=1, frame="head", wait=True)
current_orientation = reachy_sdk_zeroed.head.get_current_positions()
assert np.allclose(current_orientation, [-30, 0, 90], atol=5)

reachy_sdk_zeroed.head.rotate_by(pitch=-30, duration=1, frame="head", wait=True)
reachy_sdk_zeroed.head.rotate_by(pitch=30, duration=1, frame="robot", wait=True)
current_orientation = reachy_sdk_zeroed.head.get_current_positions()
assert np.allclose(current_orientation, [0, 30, 90], atol=5)

reachy_sdk_zeroed.head.rotate_by(yaw=30, duration=1, frame="head", wait=True)
current_orientation = reachy_sdk_zeroed.head.get_current_positions()
assert np.allclose(current_orientation, [0, 30, 120], atol=5)

with pytest.raises(ValueError):
reachy_sdk_zeroed.head.rotate_by(roll=30, frame="head", duration=0)

with pytest.raises(ValueError):
reachy_sdk_zeroed.head.rotate_by(roll=30, frame="coucou")

with pytest.raises(ValueError):
reachy_sdk_zeroed.head.rotate_by(roll=30, interpolation_mode="coucou")
Loading