 # Calculating heading distance, and direction

Hi,

I'm having an extremely difficult time getting heading calculations correctly.

For example: I have a rover with a current heading of 270 degrees I want it to face 100 degrees. How do I calculate -What is the shortest distance, in degrees, to get to 100? -Which direction should the rover turn to get to 100?

I know it should be very simple, but for some reason the math is really throwing me off.

First compute the difference of the two numbers (270-100 --> 170 degrees). Now subtract 360 from the largest number and do it again ((270-360)-100 --> -190 degrees). The smaller number in magnitude tells you how far apart they are in angle: 170 degrees.

To figure out the direction, figure out whether you need to add or subtract 170 degrees to get from your current angle to target angle: the answer tells you which way you have to turn. If you subtract you're going counter-clockwise, else clockwise.

-- The MegaRAM shield: add 128 kilobytes of external RAM to your Arduino Mega/Mega2560

OK!

It had me stumped for a bit as well!

Mainly headings around due north, 0/360 degrees.

Thanks for the help!

But it doesn't quite work.

num1=355-5=350 num2=5-360=-355-355=-710

350 is selected as our distance

350+5= 355, so we go clockwise.

When infact, to get to 355 from 5, we should be going counterclockwise 10 degrees.

Any ideas?

TarlSS:
I’m having an extremely difficult time getting heading calculations correctly.

This is quite simple once you realize that headings first need to be normalized to +/- 180 degrees (as opposed to 0-360). Shortest turn is then the normalized difference between the new and current heading. Direction to turn is left for a negative difference and right otherwise.

Example:
Current heading: 270 (normalized to -90)
New heading: 100 (normalized to 100)

Turn = 100 – (-90) = 190 (normalized to -170)

Shortest turn to reach 100 from 270 is then to turn 170 degrees left.

To convert from 0-360 to +/-180 can be done as follows:

``````While (heading > 180) heading -= 360
``````

This is quite simple once you realize that headings first need to be normalized to +/- 180 degrees (as opposed to 0-360). Shortest turn is then the normalized difference between the new and current heading. Direction to turn is left for a negative difference and right otherwise.

Example: Current heading: 270 (normalized to -90) New heading: 100 (normalized to 100)

Turn = 100 – (-90) = 190 (normalized to -170)

Shortest turn to reach 100 from 270 is then to turn 170 degrees left.

To convert from 0-360 to +/-180 can be done as follows:

groan... OK, just stopped banging my head on the bench!

Thank you!

I am so glad for his forum!

I'll try that after my walk.

Note a turn left and a turn right together allways sum up to 360 degrees ( in flatland That insight makes the code simple.

``````// assume new and heading are both 0..359 normalized (e.g. compass readings)

int left = heading - new;   // calculate left turn, will allways be 0..359

// take the smallest turn
if (left < 180)
{
// Turn left : left degrees
}
else
{
// Turn right : 360-left degrees
}
``````

some samples

270 & 100
left = 270 - 100 = 170 => smaller than 180 so turn left 170

5 & 355
5< 355 => 5 → 365
left = 365 - 355 = 10 => smaller than 180 so turn left 10

5 & 10
5 < 10 => 365
left = 365 - 10 = 355 => larger than 180 so turn right (360-355)

270 & 10
left = 270 - 10 = 260 => larger than 180 so turn right (360-100)

Note a turn left and a turn right together allways sum up to 360 degrees ( in flatland smiley-wink

NO wonder I had problem, I live up here in dem hills!

It works now! I got heading hold of a sort!

Doing the rest of the chassis build now, finally got my second Pololu Motor controller!

Insert Quote Quote Note a turn left and a turn right together allways sum up to 360 degrees ( in flatland smiley-wink

NO wonder I had problem, I live up here in dem hills!

In fact on the top of a hill the sum is less than 360 and down in a valley the sum is more than 360. It resembles the curved space equations of Einstein.

A thought experiment can make this clear:

Imagine you are on the top of a sphere (North pole) and you go 100 miles south* (call this place A). Turn 90 degrees and walk 2* PI * 100 miles East. You make a circle following a latitude line. In a flat land you would be on exact the same place A again. But because you are on a sphere this circle is a fraction shorter than its diameter (100 miles) suggests, so after 200 PI miles you will have passed place A. How many miles is left as an exercise ;)

• if you are on the north pole the only possible heading is south ;)

yes!

and in the northern hemisphere going south is going up!

we're not "down under", we are on top!

:P