Pages: [1]   Go Down
Author Topic: Expanding TinyGPS Library with distance function  (Read 816 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 90
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I want to expand the TinyGPS Library with a "distance and course to the next way point" function. I have never wrote my own Header file before, so I don't know what to do. I already took a look into the file and also in the cpp file. What do I have to do for adding a new function?

Greetings
Philipp
Logged

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

Hi,

I just tryed it, and it seems to work. I wrote this into the TinyGPS.h:
Code:
void get_distance(float lat1, float lat2, float lon1, float lon2, float *course_, float *distance){
        float lat_dif;
        float lon_dif;

        *distance = sin(lat1/180.0*PI)*sin(lat2/180.0*PI) + cos(lat1/180.0*PI)*cos(lat2/180.0*PI)*cos((lon2/180.0*PI)-(lon1/180.0*PI));
        *distance = acos(*distance);
        *distance *= 6378.137*1000;

        lat_dif = lat1 - lat2;
        if(lat_dif<0){
            lat_dif *= (-1.0);
        }
        lon_dif = lon1 - lon2;
        if(lon_dif<0){
            lon_dif *= (-1.0);
        }

        lat_dif *= 60.0 * 1852.0;

        lon_dif *= 60.0;
        lon_dif *= cos(lat2*PI/180);
        lon_dif *= 1852.0;

        *course_ = atan(lon_dif/lat_dif)*180/PI;

        if((lat2<lat1)&&(lon2>lon1)){
            *course_ = 180.0 - *course_;
        }else if((lat2<lat1)&&(lon2<lon1)){
            *course_ = *course_ + 180.0;
        }else if((lat2>lat1)&&(lon2<lon1)){
            *course_ = 360.0 - *course_;
        }
    }

Philipp
« Last Edit: June 29, 2010, 06:03:48 am by lord-maricek » Logged

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

Thanks for posting this here, Philipp! Great stuff! Works great!

Cheers,

Antonio Santos
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 42
Posts: 5612
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Philipp Maricek:

Philipp - how accurate is that code, by the way? Can it handle over-the-horizon navigation (earth's curvature and all that)? I am just curious, as such code could be useful to me in the future...
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Pages: [1]   Go Up
Jump to: