View previous topic :: View next topic |
Author |
Message |
ryan.reeve
Joined: 23 Jul 2006 Posts: 20
|
floating point with GPS |
Posted: Thu Sep 28, 2006 10:11 pm |
|
|
Hi
I am using the Haversine formula, but value is too much erronious.Not even near the expected value.
float lat1,lat2,dlat,long1,long2,dlong,tmp1,tmp2,d;
lat1=31.000100;
lat2=31.000000;
long1=74.260000;
long2=74.260000;
dlat=lat1-lat2;
dlong=long1-long2;
tmp1=(sin(dlat/2)*sin(dlat/2))+cos(lat1)*cos(lat2)*(sin(dlong/2*sin(dlong/2)));
tmp2=2*atan2(sqrt(tmp1),sqrt(1-tmp1));
d=EARTH*tmp2;
Any ideas ? |
|
|
bsodmike
Joined: 05 Aug 2006 Posts: 52
|
|
Posted: Thu Sep 28, 2006 11:07 pm |
|
|
silly idea but would this work,
tmp1=(sin(dlat/2)*sin(dlat/2));
tmp2=cos(lat1)*cos(lat2);
tmp3=(sin(dlong/2)*sin(dlong/2)); //missing bracket as well, error in formula.
tmp=tmp1+(tmp2*tmp3);
or
tmp4 = tmp2 * tmp3;
tmp = tmp1 + tmp4;
this might work...
Last edited by bsodmike on Thu Sep 28, 2006 11:41 pm; edited 2 times in total |
|
|
bsodmike
Joined: 05 Aug 2006 Posts: 52
|
|
Posted: Thu Sep 28, 2006 11:21 pm |
|
|
Yea, you've made a mistake in the last part,
Quote: | dlon = lon2 - lon1
dlat = lat2 - lat1
a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2
c = 2 * atan2(sqrt(a), sqrt(1-a))
d = R * c |
|
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Fri Sep 29, 2006 10:26 am |
|
|
You wish to get the surface distance that separates two points on the earths surface. The points are corners of an imaginary rectangle constructed within the earth and whose corners touch the surface. The Diagonal is computed and the arc distance inferred from the cord the diagonal subtends. Look on the web and there is a good explanation of haversines a derivation of half sine.
Plugging code into a PIC without looking into the underlying theory is gambling at best. CCS is a bit quirky on casting etc but that having been said it is often many times more likely that the actual PIC code is wrong than an error be due to rounding. |
|
|
bls Guest
|
|
Posted: Fri Sep 29, 2006 11:04 pm |
|
|
The trig functions are in radians, not degrees. |
|
|
bsodmike
Joined: 05 Aug 2006 Posts: 52
|
|
Posted: Sat Sep 30, 2006 12:17 am |
|
|
fyi, 1 degree = 2(pi)/360 radians = 0.01745329252 rad |
|
|
ryan.reeve
Joined: 23 Jul 2006 Posts: 20
|
thnx.... |
Posted: Sat Sep 30, 2006 12:56 am |
|
|
bls wrote: | The trig functions are in radians, not degrees. |
thanks a lot... |
|
|
ryan.reeve
Joined: 23 Jul 2006 Posts: 20
|
|
Posted: Sat Sep 30, 2006 1:41 am |
|
|
Douglas Kennedy wrote: |
Plugging code into a PIC without looking into the underlying theory is gambling at best. |
All i want to do is to calculate distance and bearing in an efficient way.
math functions eat up all the rom.
any ideas ? |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sun Oct 01, 2006 7:36 pm |
|
|
I put some 32bit cordic routines in the code library but they may not be for you. They are scaled in degrees but you have to be careful of the quadrant you are in. They are for 0 to 90 degrees but like all circular functions they have symmetrical representations in the other quadrants. They won't work directly for a rotation of 110 degrees but if you can visualize it as a rotation of 90 ( x morphs to y and y to -x in cartesians) followed by a rotation of 20 then you'll have some idea as to how to modify it. Since the cordic avoids floating pt and does the calculation by shifting it will avoid floating point overhead and will deliver greater accuracy. |
|
|
ryan.reeve
Joined: 23 Jul 2006 Posts: 20
|
|
Posted: Sun Oct 01, 2006 11:45 pm |
|
|
Douglas Kennedy wrote: | I put some 32bit cordic routines in the code library but they may not be for you. They are scaled in degrees but you have to be careful of the quadrant you are in. They are for 0 to 90 degrees but like all circular functions they have symmetrical representations in the other quadrants. They won't work directly for a rotation of 110 degrees but if you can visualize it as a rotation of 90 ( x morphs to y and y to -x in cartesians) followed by a rotation of 20 then you'll have some idea as to how to modify it. Since the cordic avoids floating pt and does the calculation by shifting it will avoid floating point overhead and will deliver greater accuracy. |
so in a nut shell do u advise me to go for CORDIC as longitude can go up to 180 degree and bearing up to 360 ? |
|
|
|