Pages: [1]   Go Down
Author Topic: GPS Distance Calculation  (Read 2720 times)
0 Members and 1 Guest are viewing this topic.
Minneapolis, Minnesota
Offline Offline
Full Member
***
Karma: 0
Posts: 128
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to calculate the distance between two sets of coordinates, one set coming from a GPS module.  I'm using the Haversine formula, which produces the correct answer when I enter it directly into Wolfram Alpha.  I'm also using the TinyGPS library.  Unfortunately, my code on the Arduino Duemilanove is not producing the correct answer, and I can't figure out why.  

Code:
 float flat, flon;
    {
    gps.f_get_position(&flat, &flon);            
  
    long dest_latitude = xx.xxxxxxxx;
    long dest_longitude = -yy.yyyyyyyy;                  
    int radius = 3956.6;                              
    const float two = 2.0;
    float delta_lat = radians(dest_latitude - flat);
    float delta_lon = radians(dest_longitude - flon);
    float a = square(sin(delta_lat/two)) + cos(radians (flat)) * cos(radians (dest_latitude)) * square(sin(delta_lon/two));
    float c = two * asin(sqrt(a));
    float d = radius * c;  

(it goes on from here)

When I'm approximately 15.5 miles away from the destination coordinates, the Arduino board and GPS tell me I'm only 12.75 miles away.  When I am at the destination coordinates, it tells me I'm 17.79 miles away.  All of my calculations are floating point.  What am I doing wrong?
« Last Edit: December 05, 2009, 09:55:51 am by alligator » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For starters, you're declaring the earth's radius as an int, not a float.  Are radians() and square() your functions?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
long dest_latitude = 45.001867;
    long dest_longitude = -93.364265;                  
    int radius = 3956.6;  

Those are all going to give you problems.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Minneapolis, Minnesota
Offline Offline
Full Member
***
Karma: 0
Posts: 128
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, thanks.  I've made them all "float."

I've included the math.h library in my code, but for some reason, it isn't colored orange like any of the other libraries I've included.

Code:
#include <EEPROM.h>
#include <NewSoftSerial.h>
#include <TinyGPS.h>
#include <math.h>

When I simulate what my code would have done in Wolfram Alpha, I get 12.64 miles - very close to the 12.75 I've been getting.  I'll try out the new code tonight.  Thanks.
« Last Edit: December 04, 2009, 01:50:35 pm by alligator » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it doesn't turn orange because it's not one of the Arduino provided libraries listed in the keywords files.  As long as it doesn't complain when you compile, don't worry about it.  You could try changing all of your floats to doubles to see if that makes any difference in the results
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 572
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's mine if you are looking to compare different alogrithms. It's pretty much cut'n'paste from the Ardupilot project

Quote
/*************************************************************************
 * //Function to calculate the distance between two waypoints
 *************************************************************************/
float calc_dist(float flat1, float flon1, float flat2, float flon2)
{
float dist_calc=0;
float dist_calc2=0;
float diflat=0;
float diflon=0;

//I've to spplit all the calculation in several steps. If i try to do it in a single line the arduino will explode.
diflat=radians(flat2-flat1);
flat1=radians(flat1);
flat2=radians(flat2);
diflon=radians((flon2)-(flon1));

dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
dist_calc2= cos(flat1);
dist_calc2*=cos(flat2);
dist_calc2*=sin(diflon/2.0);
dist_calc2*=sin(diflon/2.0);
dist_calc +=dist_calc2;

dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));

dist_calc*=6371000.0; //Converting to meters
//Serial.println(dist_calc);
return dist_calc;
}

Logged

Minneapolis, Minnesota
Offline Offline
Full Member
***
Karma: 0
Posts: 128
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I just wanted to report back that the problem has been solved.  Changing the variables from Int to Float worked just fine.  Thanks!
Logged

Boulder, CO
Offline Offline
Newbie
*
Karma: 0
Posts: 5
SparkFun Tech Support
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Care to share the rest of the code that worked?
Logged

Pages: [1]   Go Up
Jump to: