Bearing calculation out of 2 nmea strings

# Thread: Bearing calculation out of 2 nmea strings

1. ## Bearing calculation out of 2 nmea strings

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

2. 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/algo...cle.php/c5115/

3. Originally Posted by RFsolution
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.

Code:
```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

'            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/show...ghlight=cordic
Last edited by ScaleRobotics; - 10th March 2009 at 09:50.

4. 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.

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```

5. ## Re: Bearing calculation out of 2 nmea strings

I have just started a similar project. I was thinking of few ideas. One of them was to trigger something in the system ( alert/alarm or whatever) if the latitude / longitude seconds differ by 2. For example, if the location is 000.000.2 & 00.00.5 >- let us assume that, the next location which comes from GPS are 000.00.4 & 00.00.5 or where 5 becomes a 7.

Will this approach be a good approach to know when the stationary vehicle is moved? or there is any flaw in this which I am missing!

6. ## Re: Bearing calculation out of 2 nmea strings

Sounds like a fine approach to me. The more complicated stuff is for if you want to know which direction and distance it is to something. If you just want to see if its been moved, and don't care which direction it's been moved to, it seems nice and simple.

7. ## Re: Bearing calculation out of 2 nmea strings

You should be able to cycle a list of waypoints,
measure the distance to all of them,
find the closest, calculate the bearing, and draw an arrow pointing to it.
Was that the idea?

I finally got PBP working with MPLab again today,
The trig functions should be fine to rotate points around a screen
(2D screen with X - Y coordinates) yes ?

This is how I'd rotate points about an origin in C, where 160x240 is the origin
this was to connect the points for a 3D cube, so has a faked z axis as well:
Code:
```    // do pre-calculations for rotation
theta = rotation;		// pre calculations
thetab = 360 - theta;
thetab = (3.141592 / 180) * theta;
ztheta = rotationz;
zthetab = 360 - ztheta;
zthetab = (3.141592 / 180) * ztheta;

for(cnt=0; cnt<8; cnt++) {	// 3D rotate points
xx = xpoints[cnt];
yy = ypoints[cnt];
x = xx - 160;
y = yy - 240;
xnew = cos(thetab)*x - sin(thetab)*y;
ynew = sin(thetab)*x + cos(thetab)*y;
xnew = xnew + 160;
ynew = ynew + 240;
if (cnt < 4) {zxx = 160 + 76;} else {zxx = 160 - 76;}
zyy = ynew;
zx = zxx - 160;
zy = zyy - 240;
zxnew = cos(zthetab)*zx - sin(zthetab)*zy;
zynew = sin(zthetab)*zx + cos(zthetab)*zy;
zxnew = zxnew + 160;
zynew = zynew + 240;
xcoords[cnt+1] = xnew;
ycoords[cnt+1] = zynew;
}```
The source and return data is coordinate integers, but it appears the trick you do
is multiplying out the fractions in the formula is to occupy integers and divide later?
Last edited by Art; - 5th October 2013 at 09:17.

8. ## Re: Bearing calculation out of 2 nmea strings

Art, I see you're catching up on some reading.

The last post was from August, 2 years ago.

Robert (being a smart-azz)

9. ## Re: Bearing calculation out of 2 nmea strings

It's cool, cordic trig would still be a good thing to get working.
I tried last time, and had no luck, but it's worth another shot.

Originally Posted by Demon
Art, I see you're catching up on some reading.

The last post was from August, 2 years ago.

Robert (being a smart-azz)

10. ## Re: Bearing calculation out of 2 nmea strings

double post :O