Pages: [1]   Go Down
 Author Topic: Calculating heading distance, and direction  (Read 1371 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 11
 « on: February 27, 2012, 09:10:51 pm » Bigger Smaller Reset

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.

 Logged

0
Offline
Karma: 12
Posts: 2857
ruggedcircuits.com
 « Reply #1 on: February 27, 2012, 09:36:36 pm » Bigger Smaller Reset

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
 Logged

Offline
Sr. Member
Karma: 2
Posts: 342
Arduino rocks
 « Reply #2 on: February 27, 2012, 11:22:39 pm » Bigger Smaller Reset

OK!

It had me stumped for a bit as well!

Mainly headings around due north, 0/360 degrees.
 Logged

Offline
Newbie
Karma: 0
Posts: 11
 « Reply #3 on: February 28, 2012, 12:51:19 am » Bigger Smaller Reset

Thanks for the help!

But it doesn't quite work.

Check out this example

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?
 Logged

Offline
God Member
Karma: 3
Posts: 985
Arduino rocks
 « Reply #4 on: February 28, 2012, 01:16:10 am » Bigger Smaller Reset

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:

Code:
 Logged

Offline
Sr. Member
Karma: 2
Posts: 342
Arduino rocks
 « Reply #5 on: February 28, 2012, 01:41:05 am » Bigger Smaller Reset

Quote
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.
 Logged

Netherlands
Online
Tesla Member
Karma: 87
Posts: 9366
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #6 on: February 28, 2012, 03:01:38 pm » Bigger Smaller Reset

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

That insight makes the code simple.

Code:
// 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)
 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline
Sr. Member
Karma: 2
Posts: 342
Arduino rocks
 « Reply #7 on: February 28, 2012, 05:44:28 pm » Bigger Smaller Reset

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!

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!
 Logged

Netherlands
Online
Tesla Member
Karma: 87
Posts: 9366
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #8 on: February 29, 2012, 03:52:23 pm » Bigger Smaller Reset

Quote
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
 Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline
Sr. Member
Karma: 2
Posts: 342
Arduino rocks
 « Reply #9 on: February 29, 2012, 05:57:22 pm » Bigger Smaller Reset

yes!

and in the northern hemisphere going south is going up!

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

 Logged

 Pages: [1]   Go Up