CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

floating point with GPS

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ryan.reeve



Joined: 23 Jul 2006
Posts: 20

View user's profile Send private message

floating point with GPS
PostPosted: Thu Sep 28, 2006 10:11 pm     Reply with quote

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

View user's profile Send private message Visit poster's website AIM Address MSN Messenger

PostPosted: Thu Sep 28, 2006 11:07 pm     Reply with quote

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

View user's profile Send private message Visit poster's website AIM Address MSN Messenger

PostPosted: Thu Sep 28, 2006 11:21 pm     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Fri Sep 29, 2006 10:26 am     Reply with quote

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







PostPosted: Fri Sep 29, 2006 11:04 pm     Reply with quote

The trig functions are in radians, not degrees.
bsodmike



Joined: 05 Aug 2006
Posts: 52

View user's profile Send private message Visit poster's website AIM Address MSN Messenger

PostPosted: Sat Sep 30, 2006 12:17 am     Reply with quote

fyi, 1 degree = 2(pi)/360 radians = 0.01745329252 rad
ryan.reeve



Joined: 23 Jul 2006
Posts: 20

View user's profile Send private message

thnx....
PostPosted: Sat Sep 30, 2006 12:56 am     Reply with quote

bls wrote:
The trig functions are in radians, not degrees.


thanks a lot...
ryan.reeve



Joined: 23 Jul 2006
Posts: 20

View user's profile Send private message

PostPosted: Sat Sep 30, 2006 1:41 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Sun Oct 01, 2006 7:36 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Oct 01, 2006 11:45 pm     Reply with quote

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 ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group