Actual distance with TinyGPS

Hey.

So I'm looking to add a function to my home-brew GPS that gives actual distance covered - i.e. not straightline distance from the start point.

My first go went a bit like this
-create variable called "trip"
-record position (call it "pos1")
-delay for 1 second
-record position (call this one "pos2")
-calculate distance between pos1 and pos2 (call is "dist")
-let trip=trip+dist
-and repeat... (except creating the variable "trip")

However, this doesn't seem to work. I just get zero.

Any ideas on what's going wrong or if there's a more efficient method? (If needs be I can upload my code, but it's a helluva messy at the moment :~ )

Thanks!

My first go went a bit like this
-create variable called "trip"
-record position (call it "pos1")
-delay for 1 second
-record position (call this one "pos2")
-calculate distance between pos1 and pos2 (call is "dist")
-let trip=trip+dist
-and repeat... (except creating the variable "trip")

However, this doesn't seem to work. I just zero.

Matches the amount of code you posted. What a coincidence.

Well I'll try put some of the code here. It runs over multiple tabs in the main program, which makes it easier to see what's going on, but it makes it hard to copy individual bits of code.

long constlon;
long constlat;
unsigned long fix_age;
int trip;
float fltrip;

void setup()
{ 
 //The usual setup stuff... 
}

void trip1()
{
   gps.get_position(&constlat, &constlon, &fix_age);

}

void trip2()
{
  long constlon2;
  long constlat2;
  unsigned long fix_age2;
  gps.get_position(&constlat2, &constlon2, &fix_age2);
  
  float distkm;
  distkm=(sqrt(sq(constlat2-constlat)+sq(constlon2-constlon)))/1000; //My TinyGPS version doesn't have distance_to(), so I just use this.

  trip=trip+distkm; //this gives an interger trip
  fltrip=fltrip+distkm; //this gives a float trip  
 
}

void loop()
{
  trip1();
  delay(1000);
  trip2();
  Serial.print(trip);
}

This isn't the exact code, but it represents what I'm trying to achieve (I hope)

Your formula for calculating the distance between two lat/long positions is wrong. See, for example, Calculate distance and bearing between two Latitude/Longitude points using haversine formula in JavaScript
What units are used for the latitude and longitude that they are stored in long integers?

Pete

Are you moving fast enough that a second would give a measurable difference on the GPS?

Hi Pete.
I figured using the distance formula for a flat plane would be sufficient, since I'm not covering enough distance for the curvature of the earth to make a noticeable difference in results. I might make use of the Haversine formula at a later stage (when I feel like translating it into code XD ).
I don't really know why I'm using "long" values, but TinyGPS suggests I do. Also, using "int" seemed to produce incorrect data.

@Wildbill:
The GPS is meant for my car, so I've been testing it by driving (up to 60km/h... no free-ways yet...). I figured that would produce a measurable difference.

TinyGPS docs say it does this:

retrieves +/- lat/long in 100000ths of a degree

which is why you do have to store the result in a long integer. But your formula isn't going to work because lat and long are angles, not distances, so the Pythagorean theorem isn't going to give a meaningful result.

Pete

Aha, thanks for clarifying!

I'll have to give the Haversine a go then (next weekend... :drooling_face: )