Skip to main content

Angle Wrapping

Easydifficulty

This is a short lesson that fixes a confusing bug. Anything that turns the robot to a heading needs it: turnTo, a heading-hold PID, and the rotation part of PID-to-point.

The robot spins 358° to turn 2°​

Your robot faces 179°179° and you ask it to turn to −179°-179°. That's a 2°2° nudge across "straight back." The controller computes the error the obvious way:

e=θtarget−θcurrent=−179°−179°=−358°e = \theta_\text{target} - \theta_\text{current} = -179° - 179° = -358°

and commands a huge −358°-358° turn. The robot spins almost all the way around the wrong way to reach a heading 2°2° away. A subtler version of the same bug: near that seam the error flips sign from loop to loop, and the robot wobbles instead of settling.

Angles are on a circle, not a number line​

Adding or subtracting 360°360° doesn't change a direction. So −179°-179° and +181°+181° are the same heading, and 181°−179°=+2°181° - 179° = +2°. The subtraction was fine. The answer just needs to be read as a turn around a circle, picking the shorter way.

0°90°−180° seam-90°current = 179°target = −179°wrapped error = +2°naive error = −358°canonical result((e + π) mod 2π) − π
The ±180° seam: short way vs. long way — Current heading 179° and target −179° are 2° apart across the seam (green). Subtracting them naively gives −358°, sending the controller the long way around (red). Modulo wrapping into [−π, π) fixes it; the exact tie is represented as −π.
Live demoAngle wrapping: compare raw subtraction with shortest signed rotation
0 deg90 deg-180 deg (canonical seam)-90 degtarget - current-358.0 degwrap(raw error)+2.0 degcontroller should use green
raw error: -358.0 degwrapped error: +2.0 degwrong-way extra rotation: 356.0 deg
wrapped shortest rotationraw subtraction pathcurrent headingtarget heading

The fix is to wrap every angle difference into the range [−π,π)[-\pi, \pi), which is −180°-180° up to (but not including) +180°+180°:

wrap⁡(e)=((e+π) mod 2π+2π) mod 2π−π\operatorname{wrap}(e) = ((e+\pi) \bmod 2\pi + 2\pi) \bmod 2\pi - \pi

Try it in degrees on the −358°-358° error. Add 180°180° to get −178°-178°. Reduce into one full turn, 0°0° to 360°360°, by adding 360°360°: that's 182°182°. Subtract 180°180°: +2°+2°. The shift by 180°180° at the start and end is what makes the result land between −180°-180° and +180°+180° instead of between 0°0° and 360°360°.

An error of exactly 180°180° is a tie, since both ways around are the same distance. This version always picks −180°-180°, so the controller behaves the same way every time.

Warn
Wrap the error, not just the target

Wrap the difference, every loop, right before it goes into the controller. Wrapping only the target or only the measurement still leaves a difference that can land outside [-π, π).

One helper, used everywhere a heading appears​

FTCAngle wrapping and a heading hold
// The angle in [-pi, pi) pointing the same way as a.
static double wrap(double a) {
double turn = 2 * Math.PI;
return ((a + Math.PI) % turn + turn) % turn - Math.PI;
}

double headingPower(double target, double heading) {
double error = wrap(target - heading); // this is the entire lesson
return clamp(kP * error, -1, +1);
}
ExerciseRead the wrap right to left

Each step has a job:

  • (a + pi) shifts so the range starts at 0 instead of −π-\pi
  • % turn reduces to one revolution, but in Java the result can be negative
  • + turn then % turn forces it positive
  • - pi shifts back

Why is the second % turn there? Try −3.0-3.0 rad by hand in Java.

Show solution

In Java, C, and JavaScript, % keeps the sign of the number being divided, so -1.0 % 6.283 is -1.0, not 5.283. With only one %, results can land anywhere in (−2π,2π)(-2\pi, 2\pi), and after subtracting π\pi some fall outside the range you wanted. Python's % is always non-negative, so the second step does nothing there. That's why code copied from Python without it breaks on the robot.

ExerciseThe 340-degree spin

The target is 170° and the heading is −170°-170°. What's target - heading, and what does wrap turn it into? Which way does the robot turn, and how far? Now answer again without wrap.

Show solution

target - heading is 340°340°. Wrapped, that's −20°-20°, so the robot turns 20° clockwise, the short way. Without the wrap, kP * 340° maxes out the command and the robot spins 340° counter-clockwise to end up in the same place, wasting a second or more of the match.

Use the same wrap anywhere two headings are compared: the rotation part of a go-to-pose controller, a turnTo, or the heading feedback in a path follower. Make it a static helper and use it every time you subtract two angles.

Wrapping vs. unwrapping

Wrapping keeps an angle in [−π,π)[-\pi, \pi), which is what you want for a control error. The opposite, unwrapping, removes the jumps to make a continuous signal. You want that before computing a turn rate from heading, or the 2π2\pi jump at the seam looks like an enormous spin.

Know your IMU's range

Some IMUs report heading from −180°-180° to 180°180°, some from 0°0° to 360°360°, and some keep counting past a full turn. Check the docs, convert to radians once where you read it, and wrap at every comparison rather than assuming the sensor did it.

Sources & references
  • WPILib: Pose and geometry - competition-robotics geometry reference for representing headings and poses.
  • E. Eade, Lie Groups for 2D and 3D Transformations - background for angle, rotation, and pose representations used in 2-D robotics.
  • Mecanum Kinematics - Control Lab's main downstream use case for wrapped heading error.