A mobile robot may finish several docking trials with almost the same orientation even when half of the logged headings are positive and half are negative. Consider two stops reported as 179 degrees and -179 degrees in the same map frame. Their headings are two degrees apart. An ordinary average of the two printed numbers yields zero degrees, a heading opposite the two measured stops.

The problem comes from using a wrapped representation as though it were a straight number line. A log can show 179 degrees for one sample and -179 degrees for the next as the orientation moves through the wrap boundary. The sign change in the report is much larger than the change in physical orientation.

Compute the center before converting back to degrees

SciPy's Rotation.mean computes a rotation that minimizes a weighted sum of squared differences between rotation matrices. With no weights, each supplied rotation contributes equally. This is a chordal mean, a particular definition of a center for orientations. In the two-stop example, it places the center at the half turn.

The fixture builds two rotations about the map's vertical axis. It compares their rotation mean with a known half turn and then computes the residual angle of each docking trial relative to that mean.

import numpy as np

from scipy.spatial.transform import Rotation

headings = np.array([179.0, -179.0])

stops = Rotation.from_euler("z", headings[:, None], degrees=True)

center = stops.mean()

half_turn = Rotation.from_euler("z", 180.0, degrees=True)

center_error = (half_turn.inv() * center).magnitude()

residuals = np.degrees((center.inv() * stops).magnitude())

np.testing.assert_allclose(center_error, 0.0, atol=1e-12)

np.testing.assert_allclose(residuals, [1.0, 1.0], atol=1e-12)

The residuals are unsigned angular distances. For this simple case they show that both stops are one degree from the center. Subtracting printed headings would produce different numbers depending on whether the report prints the half turn as 180 degrees or -180 degrees. The test compares orientations directly so that either display choice describes the same result.

Make sure the trials belong to one group

Use samples expressed in the same frame and angle convention. For docking results, verify that each row refers to the completed stop at the same station. If one record describes the robot relative to the dock and another describes the dock relative to the robot, put them in a common convention before summarizing the run.

Decide what a row represents, too. A mean over intermediate time samples gives longer runs more influence because they contribute more rows. A mean over one final heading per trial gives each completed trial one entry. If reporting uses weights, record what they represent and check that their ordering matches the rotations.

The center alone gives no indication of how closely the runs agree. Report the number of trials and a spread statistic based on the relative rotations, such as the largest residual. If stops cluster around different headings, inspect the clusters and verify the grouping before presenting a single center. In that case an average can describe a direction that few or none of the trials actually approached.

For display, use a documented Euler sequence and keep degrees or radians explicit. SciPy's Euler-angle reference gives the output ranges and explains why some orientations do not have a unique set of Euler angles in a chosen sequence. The underlying rotation and the residual angles can still be used for comparison.

Hartley and colleagues distinguish the chordal distance between rotation matrices from the angular distance between orientations. The fixture uses the chordal objective for the center and angular distances to report the spread.

Sources

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

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

Hartley et al., Rotation Averaging: https://users.cecs.anu.edu.au/~hongdong/rotationaveraging.pdf