hello
i got a bit of code calculating distance between 2 points
now i know that the formula uses only the first 3 numbers of the lat and long also in the formula itself is it only using the first 3 numbers :S
i would like to know a way to set the precision or something so i can get the formula to work.
thank you verry much.
int distc(float Lattarget, float Lontarget, float Latloc, float Lonloc){ // latT lonT latL lonL
r = 6371;
Serial.println(Lattarget, 20); // here i see 20 decimals as needed
Serial.println(Lontarget); // here i don't ( that is logically ) but the //formula neets to put out around 8000 it doesnt now :( so i think it //uses only the first 3 numbers :S
Serial.println(Latloc);
Serial.println(Lonloc);
dlat = (Lattarget-Latloc);
dlon = (Lontarget-Lonloc);
A = (sin(dlat/2)) * (sin(dlat/2)) + (cos(Latloc)) * (cos(Lattarget)) * (sin(dlon/2)) * (sin(dlon/2));
C = 2 * (atan2(sqrt(A), sqrt(1-A)));
D = r * C;
lcd.setCursor(5,3);
lcd.print(D);
return D;
aha so if i write ie 53.123456789 is a float the 789 may be different since the float variable is only precise for 6 decimals.?
if i make them all a double then it will work better and preciser since then it will be double and precise for 12 decimals.
That is C trig functions expect radians. If you pass degrees as arguments you're not likely to get any result that makes sense irrespective of precision.
i get degrees adn decimal minutes from my gps the i convert them to degrees(i convert them i am not cutting the minutes off) then i convert them to decimals
latXXX = (latD + (latX / 60) + ( (latXX / 10000)/ 60)); // to degrees
lonXXX = (lonD + (lonX / 60) + ( (lonXX / 10000)/ 60)); // to degrees it is a bit wierd i know but the latD is the degrees the lat X is the decimal minutes(whole minutes)
// and the latXX is the deciamal minutes ( wich are not whole minutes)
lat = ((latXXX * PI) / 180); // to decimal
lon = ((lonXXX * PI) / 180); // to decimal
If you're still struggling, perhaps you wantn to share two sets of coordinates and show us what the output (distance) is? It should be possible then to see if your issue is down to rounding, implementation or other.
double targetlat = 53.22015354288012;
double targetlon = 6.572809517383575;
// first get the numbers from the array and store them in my own array ( yes it is kind of a detour
// but still it is easyer this way i think since i was getting errors i could not transform
// the variables and stuff..
// everything in the GPS.arguments sentence is exploded so GPS.arguments[2][0] is the first char wich is 5 and so on
// GPS.arguments[2] contains: 5317.7652
//GPS.arguments[4] contains: 00635.9691
// lat deg
tijdelijk[0][0] = GPS.arguments[2][0];
tijdelijk[0][1] = GPS.arguments[2][1];
//lon deg
tijdelijk[1][0] = GPS.arguments[4][0];
tijdelijk[1][1] = GPS.arguments[4][1];
tijdelijk[1][2] = GPS.arguments[4][2];
// decimal minutes of lat
tijdelijk[2][0] = GPS.arguments[2][2];
tijdelijk[2][1] = GPS.arguments[2][3];
// the rest of the decimal lat minutes (the precise kind:P)
tijdelijk[3][0] = GPS.arguments[2][5];
tijdelijk[3][1] = GPS.arguments[2][6];
tijdelijk[3][2] = GPS.arguments[2][7];
tijdelijk[3][3] = GPS.arguments[2][8];
// decimal minutes of lon
tijdelijk[4][0] = GPS.arguments[4][3];
tijdelijk[4][1] = GPS.arguments[4][4];
// the rest of the decimal lon minutes
tijdelijk[5][0] = GPS.arguments[4][6];
tijdelijk[5][1] = GPS.arguments[4][7];
tijdelijk[5][2] = GPS.arguments[4][8];
tijdelijk[5][3] = GPS.arguments[4][9];
// then store my array in floats latD is degrees latX is whole minutes
// latXX is decimal minutes and latXX is the degrees form then the lat form is the radial form wich is needed for the distance
latD = atol(tijdelijk[0]);
lonD = atol(tijdelijk[1]);
latX = atoi(tijdelijk[2]);
latXX =atoi(tijdelijk[3]);
lonX = atoi(tijdelijk[4]);
lonXX =atoi(tijdelijk[5]);
latXXX = (latD + (latX / 60) + ( (latXX / 10000)/ 60)); // to degrees
lonXXX = (lonD + (lonX / 60) + ( (lonXX / 10000)/ 60)); // to degrees
lat = ((latXXX * PI) / 180); // to radials
lon = ((lonXXX * PI) / 180); // to radials
int distc(float Lattarget, float Lontarget, float Latloc, float Lonloc){ // latT lonT latL lonL
r = 6371;
Serial.println(Lattarget);
Serial.println(Lontarget);
Serial.println(Latloc);
Serial.println(Lonloc);
dlat = (Lattarget-Latloc);
dlon = (Lontarget-Lonloc);
A = (sin(dlat/2)) * (sin(dlat/2)) + (cos(Latloc)) * (cos(Lattarget)) * (sin(dlon/2)) * (sin(dlon/2));
C = 2 * (atan2(sqrt(A), sqrt(1-A)));
D = r * C;
lcd.setCursor(5,3);
lcd.print(D);
return D;
}
distc(targetlat, targetlon, lat, lon);
and eventiually it puts out 12834.72 and that is in meters
but i know the distance between me and my school is 8 km ( in a straight line)
thank you verry much :)
im sorry the starting point is as my gps puts it out in the GPS.arguments string in the comments i pasted : // GPS.arguments[2] contains: 5317.7652
//GPS.arguments[4] contains: 00635.9691
then the values are exploded in the GPS.arguments string and then written in my string
then they are written in floats using atol
then the degrees are calculated
then the radials are calculated
then the radials are put in the calculator
writing this i know what i did wrong
i put degrees into my distance calculator from the target point not radials.... ::)