So in our previous episode, we discussed how to move a two wheeled robot. However, we mentioned that there’s a problem with computing the angle delta:

The problem is *angle wraparound.*

There are two primary ways of representing a heading: either as an angle in the closed interval [-π, π] or an angle in the closed interval [0, 2π]. If you compute the difference between two angles in either representation, you will likely end up with an angle that is outside the interval due to wraparound, and you will have to correct it. This correction can be tricky and fraught with peril, since you’ll likely be chasing after special cases.

So, let’s keep it simple, instead.

First, we’ll keep our headings in the interval [-π, π]. Why? Because there’s a nifty trick you can use with the four-quadrant arctangent function, atan2(), that forces any angle to this interval, to wit:

Once we’ve corrected and , we compute the difference as usual, plus an additional delta:

Now, if is greater than zero, will need to be negated. Once that is done, we return the appropriate result, based on whether the absolute value of is less than the absolute value of . If it is, we use , otherwise, use .

When we’re done, we’ll have a properly corrected that can be used in a controller, or for any other application that requires angle steering.

And that’s it! Later: all that “real” steering stuff.

Pingback: Moving a Robot Part 3: Two wheels, moving differently | Walking in the Faded Twilight