A robot pose replay may need to show an orientation between two logged samples. If yaw is stored in degrees within a wrapped range, adjacent headings may be recorded with opposite signs. Yaw values of 170 and minus 170 degrees look far apart when compared as ordinary numbers. Their orientations are only 20 degrees apart. A direct midpoint of the two displayed numbers is zero degrees, which heads in the opposite direction from the short-arc midpoint.

This example uses two synthetic rotations about the z-axis at timestamps 0 and 2 seconds. It checks angles and transformed vectors in SciPy 1.18.1 with NumPy 2.5.2. It models a replay operation on previously recorded orientations. It does not model joint limits, whole-arm configurations, or the path a particular robot took between samples.

Check the relative orientation first

Choose a common convention for both samples, including the coordinate frame, direction of positive rotation, and units. The fixture uses active rotations about z and tests them by applying a rotation to the unit vector along x. The relative rotation between these two samples has a magnitude of 20 degrees. The midpoint on the shorter arc turns that test vector to minus x. Checking a vector or rotation matrix avoids treating plus 180 and minus 180 on an angle display as different outcomes.

import numpy as np

from scipy.spatial.transform import Rotation, Slerp

angles = np.array([170., -170.])

r = Rotation.from_euler('z', angles[:, None], degrees=True)

q = r.as_quat()

t = [0., 2.]

m = Slerp(t, r)(1.)

opposite = q.copy()

opposite[1] *= -1

mf = Slerp(t, Rotation.from_quat(opposite))(1.)

assert np.allclose(m.apply([1, 0, 0]), [-1, 0, 0])

assert np.allclose(m.as_matrix(), mf.as_matrix())

assert np.isclose((r[0].inv() * r[1]).magnitude(), np.deg2rad(20))

assert np.allclose(Rotation.from_quat(q[:, [3, 0, 1, 2]], scalar_first=True).as_matrix(), r.as_matrix())

rr = Slerp(t, r)([0., .5, 1., 1.5, 2.])

steps = np.rad2deg((rr[:-1].inv() * rr[1:]).magnitude())

assert np.allclose(steps, [5, 5, 5, 5])

try:

Slerp(t, r)(2.25)

except ValueError:

pass

else:

raise AssertionError('Out-of-range request accepted')

The list of five times divides the interval into four equal steps. Each adjacent pair differs by five degrees in the fixture. The first and last rotations still match the supplied samples. This regular step size follows from the spherical interpolation within this interval. A replay with many intervals can have different axes and angular speeds on either side of a keyframe. It can therefore look continuous in orientation without having a smooth angular velocity throughout.

Keep the representation and meaning separate

A quaternion and its negation represent the same physical orientation. The fixture changes the sign of one complete quaternion and confirms that the resulting midpoint has the same rotation matrix. This would be a poor test if it compared quaternion components directly. A representation can change signs while the transformed vector stays the same. When comparing two replays, use their relative rotation or an application-level direction under a documented tolerance.

Component order is a separate source of error. A log may put the scalar component first, whereas the default SciPy import takes it last. The fixture reorders the test data and explicitly sets scalar_first when importing it. Two rows with four finite values can both look like valid quaternions even when a reader has interpreted their components incorrectly. Where possible, add a known non-identity rotation to an import check so an incorrect convention has a clear observable consequence.

Distinguish interpolation from a recovered motion

There are two useful tests here. The replay should hit its known orientations and the selected intermediate orientations should follow the interpolation rule. Neither result recovers an unobserved physical path. If a joint made an extra turn between samples, its orientation endpoints can be identical to endpoints from a move without the extra turn. Use suitable joint position records or denser measurements when that distinction matters.

A timestamp beyond the last keyframe raises a ValueError in this example. Decide how the replay caller handles an absent bracketing sample before building a timeline view. Leaving a gap and holding the last observation express different meanings. If a view holds an observation at a later displayed time, keep its original time alongside it so a reviewer can still tell when it was measured.

For an acceptance check, record the input frame and quaternion order with the source data, include a case that crosses the angle wrap, and test the endpoint and midpoint rotations independently. Preserve the original timestamps and mark which displayed poses were interpolated. If the timeline is later used to compare two log streams, the reviewer can distinguish recording boundaries from the poses rendered between them.

Sources

SciPy, Slerp

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Slerp.html

SciPy, Rotation.from_quat

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.from_quat.html

Illustration. AI-generated concept showing a short amber interpolation arc between two nearby orientation pointers.