headingError = currentHeading - holdHeading
Negative headingError means go right.
Positive headingError means go left.
If holdHeading is 359 and currentHeading is 10 then headingError should be 11
headingError = currentHeading - holdHeading
headingError = 10 - 359
headingError = -349
From 10 degrees go right 349 degrees to get to 359. This is correct. Obviously, you'd prefer to take the shorter route.
if ( headingError < -180 )
{
headingError = 360 + headingError;
}
headingError = currentHeading - holdHeading
headingError = 359 - 10
headingError = +349
From 359 degrees go left 349 degrees to get to 10. This is correct. Obviously, you'd prefer to take the shorter route.
if ( headingError > +180 )
{
headingError = 360 - headingError;
}