EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

GPS formulas

Started by Richard Sloan May 28, 2005
I have been looking for formulas to help me create a special GPS device, I
need to know distance from current position to a point (LAT,LONG) and also I
need a function to let me know when I am passing by a point and if its to
the left, right, ahead, or behind me, and it would be nice to know by how
much.

Below are formula I found on the web and thought would be at least part of 
my answer, these were referred to as great circle equations.

I am not a math wiz, I am very good at math, but not to the point I could 
come up with these equations myself :-)

Can anyone help with whats required?

Thanks!
Richard.

Now for the distance I have used successfully:
dLAT = LAT1 - LAT2;
dLONG = LONG1 - LONG2;
R = 6731000.0
double dist2(void) {
 return R*2*asin(sqrt((sin((dLAT)/2))*(sin((dLAT)/2)) + 
cos(LAT1)*cos(LAT2)*(sin((dLONG)/2))*(sin((dLONG)/2))));
}

And for nearing I have used rather unsuccessfully:
double bearing(void) {
 C = 
fmod(atan2(sin(dLONG)*cos(LAT2),cos(LAT1)*sin(LAT2)-sin(LAT1)*cos(LAT2)*cos(dLONG)), 
2*PI);
 C = ((C*180)/PI)

 return C;
}


In article <o7ame.17094$dZ5.1093342@news20.bellglobal.com>, rsloan2003
@hotmail.com says...
> I have been looking for formulas to help me create a special GPS device, I > need to know distance from current position to a point (LAT,LONG) and also I > need a function to let me know when I am passing by a point and if its to > the left, right, ahead, or behind me, and it would be nice to know by how > much. > > Below are formula I found on the web and thought would be at least part of > my answer, these were referred to as great circle equations. > > I am not a math wiz, I am very good at math, but not to the point I could > come up with these equations myself :-) > > Can anyone help with whats required? >
If the point is fixed, you can simplify your calculations by pre- calculating the sin and cosine of its latitude and longitude. If processing time is at all a concern, you can also assume that the sine and cosine of your own position are also the same as the sine and cosine of the point. There will be an error--that will disappear as you approach the point.
> > Now for the distance I have used successfully: > dLAT = LAT1 - LAT2; > dLONG = LONG1 - LONG2; > R = 6731000.0 > double dist2(void) { > return R*2*asin(sqrt((sin((dLAT)/2))*(sin((dLAT)/2)) + > cos(LAT1)*cos(LAT2)*(sin((dLONG)/2))*(sin((dLONG)/2)))); > } > > And for nearing I have used rather unsuccessfully: > double bearing(void) { > C = > fmod(atan2(sin(dLONG)*cos(LAT2),cos(LAT1)*sin(LAT2)-sin(LAT1)*cos(LAT2)*cos(dLONG)), > 2*PI); > C = ((C*180)/PI) >
If you want to avoid excessive trig, first calculate the distance as sqrt( mNorth *mNorth + mEast * mEast) where the north and east distances are in meters. You can get these numbers with very simple plane trignometric calculations based on latitude, longitude and the radius of the earth. When you have meters north and east to the point, the determination of bearing is a simple atan2() calculation. These assumptions work nicely if you only need bearing to the nearest degree and the distance is less than 40 or 50 kilometers. If the distance is much larger, you are probably stuck with the more complex spherical trignometric equations.
> return C; > }
Mark Borgerson
Richard Sloan wrote:
> I have been looking for formulas to help me create a special GPS device, I > need to know distance from current position to a point (LAT,LONG) and also I > need a function to let me know when I am passing by a point and if its to > the left, right, ahead, or behind me, and it would be nice to know by how > much. > > Below are formula I found on the web and thought would be at least part of > my answer, these were referred to as great circle equations. > > I am not a math wiz, I am very good at math, but not to the point I could > come up with these equations myself :-) > > Can anyone help with whats required? > > Thanks! > Richard. > > Now for the distance I have used successfully: > dLAT = LAT1 - LAT2; > dLONG = LONG1 - LONG2; > R = 6731000.0 > double dist2(void) { > return R*2*asin(sqrt((sin((dLAT)/2))*(sin((dLAT)/2)) + > cos(LAT1)*cos(LAT2)*(sin((dLONG)/2))*(sin((dLONG)/2)))); > } > > And for nearing I have used rather unsuccessfully: > double bearing(void) { > C = > fmod(atan2(sin(dLONG)*cos(LAT2),cos(LAT1)*sin(LAT2)-sin(LAT1)*cos(LAT2)*cos(dLONG)), > 2*PI); > C = ((C*180)/PI) > > return C; > }
The great circle formulae get increasingly inaccurate when you get near the waypoint. This is because the great circle distance is calculated as the sine or cosine of the route seen from Earth's center (6400 kilometers / 4000 miles away). The accuracy of the trigonometric functions will play a significant role in short distances. The shortest route (great circle route) will have a changing heading (direction) unless the route is north-south or along the equator. There is a little longer route with a constant heading called the rhumbline. One reference is <http://williams.best.vwh.net/avform.htm>. There are plenty of others, Google for 'great circle navigation'. HTH -- Tauno Voipio, CPL(A), aeronautic navigation instructor tauno voipio (at) iki fi
Richard Sloan <rsloan2003@hotmail.com> wrote:
> I have been looking for formulas to help me create a special GPS device, I > need to know distance from current position to a point (LAT,LONG) and also I > need a function to let me know when I am passing by a point and if its to > the left, right, ahead, or behind me, and it would be nice to know by how > much.
> Below are formula I found on the web and thought would be at least part of > my answer, these were referred to as great circle equations.
> I am not a math wiz, I am very good at math, but not to the point I could > come up with these equations myself :-)
> Can anyone help with whats required?
> Thanks! > Richard.
> Now for the distance I have used successfully: > dLAT = LAT1 - LAT2; > dLONG = LONG1 - LONG2;
Pretty much guaranteed to be wrong at this point already. Differences between longitudes don't have much of a useful meaning. First because longitude is circular, and difference across a wrap-around will yield wild results. Second because the meaning of a longitude difference changes with latitude. The best way to generate such formulae is usually to not use longitude and latitude at all, but 3D cartesian coordinates of points either on the unit sphere, or on the actual earth surface --- those will even be easier to extract from GPS raw signals, as an extra bonus. The distance between two such points along the earth's surface is then roughly arccos(dotproduct(point1, point2))/R the length of an arc along the great circle through the two points. I say "roughly" because earth isn't really a sphere. Generally, two points on the sphere, together with the center, define a plane in space. The intersection of that plane with the sphere is a great-circle, and the shortest path between the two points is part of it. -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.
On Sat, 28 May 2005 22:54:06 -0400, "Richard Sloan"
<rsloan2003@hotmail.com> wrote:

>I have been looking for formulas to help me create a special GPS device, I >need to know distance from current position to a point (LAT,LONG) and also I >need a function to let me know when I am passing by a point and if its to >the left, right, ahead, or behind me, and it would be nice to know by how >much. >
Have a look at http://williams.best.vwh.net/avform.htm -- Peter Bennett, VE7CEI peterbb4 (at) interchange.ubc.ca new newsgroup users info : http://vancouver-webpages.com/nnq GPS and NMEA info: http://vancouver-webpages.com/peter Vancouver Power Squadron: http://vancouver.powersquadron.ca
Hans-Bernhard Broeker <broeker@physik.rwth-aachen.de> writes:
> Richard Sloan <rsloan2003@hotmail.com> wrote: > > I have been looking for formulas to help me create a special GPS device, I > > need to know distance from current position to a point (LAT,LONG) and also I > > need a function to let me know when I am passing by a point and if its to > > the left, right, ahead, or behind me, and it would be nice to know by how > > much. > > > Below are formula I found on the web and thought would be at least part of > > my answer, these were referred to as great circle equations. > > > I am not a math wiz, I am very good at math, but not to the point I could > > come up with these equations myself :-) > > > Can anyone help with whats required? > > > Thanks! > > Richard. > > > Now for the distance I have used successfully: > > dLAT = LAT1 - LAT2; > > dLONG = LONG1 - LONG2; > > Pretty much guaranteed to be wrong at this point already. > > Differences between longitudes don't have much of a useful meaning. > First because longitude is circular, and difference across a > wrap-around will yield wild results. Second because the meaning of a > longitude difference changes with latitude. The best way to generate > such formulae is usually to not use longitude and latitude at all, but > 3D cartesian coordinates of points either on the unit sphere, or on > the actual earth surface --- those will even be easier to extract from > GPS raw signals, as an extra bonus. > > The distance between two such points along the earth's surface is then > roughly > > arccos(dotproduct(point1, point2))/R > > the length of an arc along the great circle through the two points. I > say "roughly" because earth isn't really a sphere.
The above formula will cause all sorts of grief for short distances. Due to (in)accuracy of the acos() function for small angles, the computed result will be quite erratic for two points that are very close to each other.
> Generally, two points on the sphere, together with the center, define > a plane in space. The intersection of that plane with the sphere is a > great-circle, and the shortest path between the two points is part of > it.
Everett M. Greene <mojaveg@mojaveg.iwvisp.com> wrote:
> Hans-Bernhard Broeker <broeker@physik.rwth-aachen.de> writes:
[...]
> > The distance between two such points along the earth's surface is then > > roughly > > > > arccos(dotproduct(point1, point2))/R
> The above formula will cause all sorts of grief for short > distances.
That grief is due to the problem being numerically tricky, though, not because of the formula being wrong. The formula cited by the OP will have very similar problems: differences among longitudes and latitudes of nearby points will lose up to 5 decimal digits to cancellation (GPS resolution vs. range), even before any trigonomtric function is called. -- Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.
Hans-Bernhard Broeker wrote:
> Everett M. Greene <mojaveg@mojaveg.iwvisp.com> wrote: > >>Hans-Bernhard Broeker <broeker@physik.rwth-aachen.de> writes: > > > [...] > > >>>The distance between two such points along the earth's surface is then >>>roughly >>> >>> arccos(dotproduct(point1, point2))/R > > >>The above formula will cause all sorts of grief for short >>distances. > > > That grief is due to the problem being numerically tricky, though, not > because of the formula being wrong. The formula cited by the OP will > have very similar problems: differences among longitudes and latitudes > of nearby points will lose up to 5 decimal digits to cancellation (GPS > resolution vs. range), even before any trigonomtric function is > called. >
That'a why a plain plane trigonometry formula with latitude correction gives the best results on short tracks. Take one degree to correspond 111.11 km and multiply the east-west (longitude) difference with the cosine of the avreage latitude of the track, then use Pythagoras' theorem for the rest. -- Tauno Voipio tauno voipio (at) iki fi
"Tauno Voipio" <tauno.voipio@iki.fi.NOSPAM.invalid> wrote in message 
news:dRIme.320$0T.38@read3.inet.fi...
> That'a why a plain plane trigonometry formula with latitude > correction gives the best results on short tracks. Take one > degree to correspond 111.11 km and multiply the east-west > (longitude) difference with the cosine of the avreage latitude > of the track, then use Pythagoras' theorem for the rest.
I've used a similar approach: calculate the km/degree of latitude factor for the average latitude of the two points, and then use Pythagoras. This approach is simple, gives increasingly accurate results with shorter distances, but doesn't work too well near the poles or over very large distances. Polar explorers and astronauts probably shouldn't use it. Steve http://www.fivetrees.com

The 2024 Embedded Online Conference