Hi all
I am attempting to make a GPS device that returns "distance traveled". I am using the itead GPS shield and arduino UNO. So far, I have the GPS working, but I am having trouble calculating the distance between different latitude and longitude.
Attached are two codes, the first is the sketch that is working and the second is the code I found online, which is suppose to calculate the distance traveled. I have made an attempt to merge the two together but it will not compile. Could someone help me figure this out or guide me in the right direction.
Thanks
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
int RXPin = 2; //conntec to tx pin of the gps
int TXPin = 3; //conntect to RX pin of the gps
// The TinyGPS++ object
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
Serial.begin(115200);
ss.begin(9600);
Serial.println();
Serial.println(F("Sats Latitude Longitude Date Time Altitude Speed"));
Serial.println(F(" (deg) (deg) (UTC) (m) (mph)"));
Serial.println(F("________________________________________________________________________"));
}
void loop()
{
printInt(gps.satellites.value(), gps.satellites.isValid(), 9);
printFloat(gps.location.lat(), gps.location.isValid(), 12, 3);
printFloat(gps.location.lng(), gps.location.isValid(), 12, 3);
printDateTime(gps.date, gps.time);
printFloat(gps.altitude.meters(), gps.altitude.isValid(), 12, 2);
printFloat(gps.speed.mph(), gps.speed.isValid(), 6, 2);
Serial.println();
smartDelay(1000);
if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
}
// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
static void printFloat(float val, bool valid, int len, int prec)
{
if (!valid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
Serial.print(' ');
}
smartDelay(0);
}
static void printInt(unsigned long val, bool valid, int len)
{
char sz[32] = "*****************";
if (valid)
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len-1] = ' ';
Serial.print(sz);
smartDelay(0);
}
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
if (!d.isValid())
{
Serial.print(F("********** "));
}
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
Serial.print(sz);
}
if (!t.isValid())
{
Serial.print(F("******** "));
}
else
{
char sz[32];
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
Serial.print(sz);
}
smartDelay(0);
}
static void printStr(const char *str, int len)
{
int slen = strlen(str);
for (int i=0; i<len; ++i)
Serial.print(i<slen ? str[i] : ' ');
smartDelay(0);
}
// get distance example
// using TinyGPS
// Written by Bill Heaster
#include <TinyGPS.h> //include for the GPS handeling.
// These would be saved in the EEPROM for your GeoCache
// For this example we will name them as const variables
const long TargetLat = 43164622;
const long TargetLon = 77609596;
TinyGPS gps;
struct //structure to organize our variables.
{
float miles;
float feet;
float km;
}distance;
void setup()
{
Serial.begin(115200); //start serial for the GPS module
}
void loop()
{
if(isData())
{
getDistance(gps,TargetLat, TargetLon);
}
Serial.print("Miles: ");
Serial.println(distance.miles);
Serial.print("KM: ");
Serial.println(distance.km);
Serial.print("Feet: ");
Serial.println(distance.feet);
}
bool isData() // feed the gps object with serial info and check if we are recieving a valid packet
{
while (Serial.available())
{
if (gps.encode(Serial.read()))
return true;
}
return false;
}
void getDistance(TinyGPS &gps, long targetLat, long targetLon)
{
long lat, lon;
unsigned long age;
float latRad, lonRad;
float flat, flon;
float tlat, tlon;
float tlatRad, tlonRad;
float midLat, midLon;
int samples=0;
//----------------------------------------
for (samples; samples< 20; samples++) //collect a few samples to make the data more accurate.Not sure if this is the best soloution.
{
gps.get_position(&lat, &lon, &age); //get the coords from the tinygps object
if(age >= 5000) //if the data is old
{
delay(3000);//wait before we make another attempt
isData(); //refresh the GPS object.
}
else
{
samples = 20;
}
}
//convert to decimal degree
flat = lat /100000.0;
flon = lon /100000.0;
tlat = targetLat / 100000.0;
tlon = targetLon / 100000.0;
//convert decimal degree into radian
latRad = flat * 0.017453293;
lonRad = flon * 0.017453293;
tlatRad = tlat * 0.017453293;
tlonRad = tlon * 0.017453293;
midLat = tlatRad - latRad;
midLon = tlonRad - lonRad;
//Calculate the distance in KM
float latSin = sin((latRad - tlatRad)/2);
float lonSin = sin((lonRad - tlonRad)/2);
distance.km = 2 * asin(sqrt((latSin*latSin) + cos(latRad) * cos(tlatRad) * (lonSin * lonSin)));
distance.km = distance.km * 6371;
//convert to miles
distance.miles = distance.km * 0.621371192;
distance.feet = distance.miles *5280;
isData(); //refresh GPS during long calculations
// --------------------------------
}