Pages: [1]   Go Down
Author Topic: Calculating heading distance, and direction  (Read 2903 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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 Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK!

It had me stumped for a bit as well!

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help!

But it doesn't quite work.

Check out this example
Init Heading: 5
Target Heading: 355

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 Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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:
While (heading > 180) heading -= 360
While (heading < - 180) heading += 360
Logged

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13717
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

That insight makes the code simple.

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

if (heading < new) heading += 360;  // denormalize ...
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 -
(Please do not PM for private consultancy)

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 217
Posts: 13717
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset 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  smiley-wink

* if you are on the north pole the only possible heading is south smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

field road, jupiter creek
Offline Offline
Sr. Member
****
Karma: 6
Posts: 354
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes!

and in the northern hemisphere going south is going up!

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

 smiley-razz
Logged

Pages: [1]   Go Up
Jump to: