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 and you ask it to turn to . That's a nudge across "straight back." The controller computes the error the obvious way:
and commands a huge turn. The robot spins almost all the way around the wrong way to reach a heading 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 doesn't change a direction. So and are the same heading,
and . The subtraction was fine. The answer just needs to be read as a turn around a
circle, picking the shorter way.
The fix is to wrap every angle difference into the range , which is up to (but not including) :
Try it in degrees on the error. Add to get . Reduce into one full turn, to , by adding : that's . Subtract : . The shift by at the start and end is what makes the result land between and instead of between and .
An error of exactly is a tie, since both ways around are the same distance. This version always picks , so the controller behaves the same way every time.
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
// 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);
}
Each step has a job:
(a + pi)shifts so the range starts at 0 instead of% turnreduces to one revolution, but in Java the result can be negative+ turnthen% turnforces it positive- pishifts back
Why is the second In Java, C, and JavaScript, % turn there? Try rad by hand in Java.Show solution
% 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 , and after subtracting
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.
The target is 170° and the heading is . 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 . Wrapped, that's , 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 keeps an angle in , 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 jump at the seam looks like an enormous spin.
Some IMUs report heading from to , some from to , 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.
- 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.