|
| 1 | +"""Tests for pose interpolation, in particular the yaw_as_scalar routing fix.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from scipy.spatial.transform import Rotation as R |
| 5 | + |
| 6 | +from reachy_mini.utils import create_head_pose |
| 7 | +from reachy_mini.utils.interpolation import linear_pose_interpolation |
| 8 | + |
| 9 | + |
| 10 | +def _world_yaw_deg(pose): |
| 11 | + return R.from_matrix(pose[:3, :3]).as_euler("xyz", degrees=True)[2] |
| 12 | + |
| 13 | + |
| 14 | +def test_yaw_as_scalar_routes_through_front_not_back(): |
| 15 | + """look-left(+120) -> look-right(-120) must route through 0, never the back (+-180). |
| 16 | +
|
| 17 | + Regression for the SLERP geodesic taking the short 3D path around +-180 deg, which |
| 18 | + the bounded +-160 deg body yaw cannot follow (causing a discontinuous body_yaw flip). |
| 19 | + """ |
| 20 | + start = create_head_pose(yaw=120, degrees=True) |
| 21 | + end = create_head_pose(yaw=-120, degrees=True) |
| 22 | + |
| 23 | + yaws = [ |
| 24 | + _world_yaw_deg( |
| 25 | + linear_pose_interpolation(start, end, i / 60, yaw_as_scalar=True) |
| 26 | + ) |
| 27 | + for i in range(61) |
| 28 | + ] |
| 29 | + |
| 30 | + # never swings out toward +-180 (the back) |
| 31 | + assert max(abs(y) for y in yaws) <= 121.0 |
| 32 | + # passes through the front (~0 deg) |
| 33 | + assert min(abs(y) for y in yaws) <= 5.0 |
| 34 | + # smooth: no large per-frame jump from a back crossover |
| 35 | + assert max(abs(b - a) for a, b in zip(yaws, yaws[1:])) < 10.0 |
| 36 | + |
| 37 | + |
| 38 | +def test_yaw_as_scalar_matches_slerp_without_back_crossing(): |
| 39 | + """Outside the cross-the-back case, yaw_as_scalar is identical to the geodesic SLERP.""" |
| 40 | + # same-side sweep with pitch/roll, body must move but no back crossing |
| 41 | + start = create_head_pose(yaw=130, pitch=15, roll=10, degrees=True) |
| 42 | + end = create_head_pose(yaw=70, pitch=15, roll=10, degrees=True) |
| 43 | + |
| 44 | + for i in range(21): |
| 45 | + t = i / 20 |
| 46 | + np.testing.assert_allclose( |
| 47 | + linear_pose_interpolation(start, end, t, yaw_as_scalar=True), |
| 48 | + linear_pose_interpolation(start, end, t), |
| 49 | + atol=1e-9, |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def test_yaw_as_scalar_endpoints_exact(): |
| 54 | + """t=0 and t=1 reproduce the start and target poses exactly.""" |
| 55 | + start = create_head_pose(yaw=120, pitch=5, degrees=True) |
| 56 | + end = create_head_pose(yaw=-120, roll=8, degrees=True) |
| 57 | + |
| 58 | + np.testing.assert_allclose( |
| 59 | + linear_pose_interpolation(start, end, 0.0, yaw_as_scalar=True), start, atol=1e-9 |
| 60 | + ) |
| 61 | + np.testing.assert_allclose( |
| 62 | + linear_pose_interpolation(start, end, 1.0, yaw_as_scalar=True), end, atol=1e-9 |
| 63 | + ) |
0 commit comments