PDA

View Full Version : Bearing calculation out of 2 nmea strings


RFsolution
- 3rd October 2008, 10:47
Did anyone ever tried to calculate bearing and distance between
set of 2 cordinates ?

1 fixed (lat, lon), 1 comming in from a GPS NMEA 4800,8N1 GMRC string ?

thansk you

mackrackit
- 3rd October 2008, 11:42
I have tried LatLon to UTM and gave up, I thought I could do it when LONGs came out, but not quite. Pretty much the same math, at least the same type.

I could not keep track of the decimal point.

Do you know the formula for this? If not, here it is
http://www.codeguru.com/Cpp/Cpp/algorithms/general/article.php/c5115/

scalerobotics
- 10th March 2009, 10:25
Did anyone ever tried to calculate bearing and distance between
set of 2 cordinates ?

1 fixed (lat, lon), 1 comming in from a GPS NMEA 4800,8N1 GMRC string ?

thansk you

I used the Cordic to get the heading and distance. To get this to work, you need to calculate the difference between the two waypoints

lathome_hi = the 37 part of the 37.12345 degrees
lathome_low = the 0.1234 (as a whole number 1234) part of 37.1234 degrees

This allows you to have a 3 degree difference in distance, or about 180 miles, although you could add some math division to allow further distances.


GPSMath:
latlow_dif = lathome_low - latdest_low 'find difference in present loc and destination
lathi_dif = lathome_hi - latdest_hi

lathi_dif = lathi_dif * 10000 'multiply integer degree difference by 10000
lat_dif = lathi_dif + latlow_dif 'so we can add decimal degrees to 16 bit number

lonlow_dif = lonhome_low - londest_low
lonhi_dif = lonhome_hi - londest_hi

lonhi_dif = lonhi_dif * 10000
lon_dif = lonhi_dif + lonlow_dif

if west = 0 then toggle lon_dif.15 'inverse negative bit for east europe
if north = 0 then toggle lat_dif.15 'inverse negative bit for southern hemisphere
quadrant = 1 'if lat_dif.15 = 1 and lon_dif.15 = 0 then quadrant =1
if lat_dif.15 = 0 and lon_dif.15 = 0 then quadrant =2 'see which results are negative
if lat_dif.15 = 0 and lon_dif.15 = 1 then quadrant =3
if lat_dif.15 = 1 and lon_dif.15 = 1 then quadrant =4
if north = 0 then toggle lat_dif.15 'get rid of falsing for S hem so we can do math


gosub do_cordic 'perform cos and atan functions to determine bearing and distance to waypoint


'adds angle of triange to quadrant angle. (Yeah, my quads don't match what you learned in school)
if quadrant = 1 then b2dest = 900 - b2dest
if quadrant = 2 then b2dest = 900 + b2dest
if quadrant = 3 then b2dest = 2700 - b2dest
if quadrant = 4 then b2dest = 2700 + b2dest

' my quadrants
' N
' |
' 4 | 1
'W______|________E
' |
' 3 | 2
' |
' S


The above code was for the cordic made for a Pic16f device. The PIC18 cordic allows positive and negative numbers, so it figures the quadrant for you.

See the cordic and the last post here:

http://www.picbasic.co.uk/forum/showthread.php?t=7502&highlight=cordic

scalerobotics
- 27th June 2009, 07:29
Here is the part that corrects for the length of longitude, as latitude increases. This was written for the PIC16, and was used in addition to above code.


do_cordic:
Z_ = lathome_hi 'input whole # latitude 37.xxx for longitude
'correction (lon smaller at higher latitude) in Z_ for calcs
i = Z_ * 128 / 45 '256 / 90 'changes Z_ to the right format
if (Z_*128)//45 >= 23 then i = i +1 'round number if remainder is =>0.5
Z_ = i 'put result in Z_
call sincos 'get cos(Z_) and sin(Z_)

i = X_ * lon_dif ' lon_ratio = cos(lat_current)
lon_dif = div32 10000 'get lon_dif * the fraction for lon correction

X_ = lon_dif 'load new lon_dif into X_ , prepare for ATAN function
Y_ = lat_dif 'load lat_dif into Y_ , prepare for ATAN function

call atan 'perform ATAN assembly function get angle of coordinates.

b2dest = Z_ * 225 '*900 / 256 = 225/64
b2dest = div32 64 'puts angle in degrees into the variable in dd.d