A robot joint encoder may report angles wrapped into a fixed interval. A smooth motion through 180 degrees can therefore appear as a jump from 179 to -179. NumPy's `unwrap` can restore a continuous trace, but only when the period and units describe the same representation as the samples. Passing degree values while relying on the default radian period is a unit error, not a filter choice.

The fixture in this article is synthetic. It represents a joint moving forward through a wrap boundary and does not claim a particular motor, encoder, calibration, or safety limit. The purpose is to make the unit contract visible before a controller computes velocity or detects a stop.

Start with wrapped degree samples

Consider the sequence:

wrapped_deg = np.array([170., 179., -179., -170., -160.])

The physical-looking progression is 170, 179, 181, 190, 200 degrees, but the encoder representation has wrapped values into a 360-degree cycle. The raw first differences are [9, -358, 9, 10]. A derivative computed directly from those values would report a large negative spike at the wrap.

Use radians with the default period

NumPy documents `unwrap` as changing elements by integer multiples of a period when adjacent differences exceed a discontinuity threshold. Its default period is 2*pi, so the unit-safe radian call is:

wrapped_rad = np.deg2rad(wrapped_deg)

continuous_rad = np.unwrap(wrapped_rad)

continuous_deg = np.rad2deg(continuous_rad)

The executed fixture returns [170, 179, 181, 190, 200] degrees to displayed precision. The jump from -179 to the previous 179 is corrected by adding one full 2*pi period. The first differences after unwrapping are [9, 2, 9, 10] degrees, so the apparent -358-degree event is gone.

The conversion calls are part of the algorithm, not presentation decoration. `unwrap` operates in the input unit, and `rad2deg` converts the resulting radians back to degrees. Keep the stored trace in one declared unit until the final display or interface boundary.

Use degrees directly only with a degree period

The same result can be obtained without conversion:

continuous_deg_direct = np.unwrap(wrapped_deg, period=360.)

The explicit period tells NumPy that a complete cycle is 360 in the input's units. The returned values match the radian path. This is useful when a telemetry file or a control API is explicitly defined in degrees, but the period must remain attached to the code and metadata.

The discontinuity argument does not override the period contract

`unwrap` also accepts `discont`. It is tempting to set a small threshold to suppress every jump, but NumPy clamps a threshold below half the period to the half-period value. For a 360-degree cycle, a requested discontinuity of 120 degrees still cannot force a correction for an ordinary 90-degree move; the effective boundary remains at least 180 degrees under the documented rule. A test should therefore include both a wrap crossing and a legitimate move below the half-period.

In the fixture, `np.unwrap(wrapped_deg, period=360., discont=120.)` returns the same [170, 179, 181, 190, 200] result as the default discontinuity for that period. The explicit period is doing the unit work. The threshold controls which jumps are eligible within the period model; it does not turn degree data into radians.

Why this matters for robot control

Velocity, acceleration, jerk, and limit checks often consume differences of successive positions. A wrapped trace can create an artificial spike that triggers a diagnostic or a false overspeed response. An incorrectly unwrapped trace can be worse: it may smooth a real motion or interpret a multi-turn joint as a single-turn signal. The application must document whether the encoder is single-turn, multi-turn, or already continuous, and whether the period is 360 degrees, 2*pi radians, or another mechanical cycle.

Do not unwrap across a data gap without a policy. A dropped packet, a mode switch, or a commanded discontinuous reset can be a real boundary rather than a periodic crossing. Split the trace or mark the gap before unwrapping when continuity is not physically valid. Preserve the raw wrapped samples so an auditor can distinguish a corrected periodic boundary from an imputed sample.

Test the unit contract explicitly

An auditable fixture can assert both equivalent paths:

assert np.allclose(continuous_deg, continuous_deg_direct)

assert np.allclose(np.diff(continuous_deg), [9., 2., 9., 10.])

assert np.allclose(np.rad2deg(np.unwrap(np.deg2rad(wrapped_deg))),

[170., 179., 181., 190., 200.])

Also test a sequence that contains a real large move near the half-period and a trace with a deliberate reset. Those cases prevent a passing wrap example from being mistaken for a complete motion model.

The rule is simple: period, discontinuity threshold, and sample units form one contract. Convert degrees to radians before using the default, or pass an explicit degree period when the samples are degrees. Record the raw trace, the chosen period, and the gap policy next to every derived velocity. That keeps a joint-angle plot, a controller limit, and a later investigation on the same physical interpretation.

If the joint can rotate through more than one mechanical turn, the period is not automatically the encoder's complete travel. A multi-turn counter may already contain an accumulated turn count, in which case unwrapping it again can add a false cycle. Conversely, a single-turn absolute sensor may require unwrapping only after a mode transition has been identified. Preserve that sensor contract beside the array so a future maintainer knows whether the periodic correction is expected or forbidden.

Sources

[1] NumPy, numpy.unwrap: https://numpy.org/doc/stable/reference/generated/numpy.unwrap.html

[2] NumPy, numpy.rad2deg: https://numpy.org/doc/stable/reference/generated/numpy.rad2deg.html