1

I am attempting to make a simple Vehicle to Vehicle V2V commutation system using one nRF24L01 as a transmitter and another one for a receiver connected to Arduino Uno. Then I connected 2 GPS devices for each Arduino board type Adafruit Ultimate GPS Breakout Version 3. Is there a mathematical equation or a simple code that can be used to deal with the longitude and latitude obtained from the two devices to know the distance between the two vehicles as well as determine the direction, for example, cars approaching or moving away from depending on the updated values sent from the sender.

I tried to calculate the distance within the coverage area provided by nRF24L01 which provide 500 meters, for example, a straight road with 2 vehicles when the 1st vehicle enter the range of the 2nd vehicle I can get the approximation distances in real time between these vehicles by taking the advantages for the GPS located in both vehicles. Actually I read about different methods but I looking for something simple since I try to apply it in my simple suggested scenario and I successfully transmitted the longitude and latitude of the 2nd vehicle over the nRF24L01 to the 1st vehicle

below the data that I got when I connect my GPS to Arduino enter image description here

7ussein_3li
  • 33
  • 1
  • 5
  • If you can use a math library with trigonometry functions then [these StackOverflow answers](https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates) might be of help. – mmixLinus Sep 16 '20 at 09:28
  • I’m voting to close this question because not a programming question. how to get the distance between two CGS coordinates can be obtained through websearch – Piglet Sep 16 '20 at 09:32
  • @Piglet, a few things that people look up in the web, can even be calculated locally. Of course, if you also need information about shops on your way between those coordinates, that's what the web was invented for (google's opinion). But I agree, response shows it isn't an **arduino** question – datafiddler Sep 16 '20 at 09:45
  • @datafiddler I'm talking about the formula and code. it is 3 websearch keywords away. no need for a SO post. Not sure what that has to do with shops on your way. – Piglet Sep 16 '20 at 11:39

2 Answers2

0

For computing the distance between two points using GPS coordinates, you need to use the Haversine formula

The piece of code below (coming from here) shows you how to do it (in C++, but you should be able to adapt it easily in the language you want).

#include <bits/stdc++.h> 
using namespace std; 
  
// Utility function for converting degrees to radians 
long double toRadians(const long double degree) 
{ 
    long double one_deg = (M_PI) / 180; 
    return (one_deg * degree); 
} 
  
long double distance(long double lat1, long double long1,  
                     long double lat2, long double long2) 
{ 
    // Convert the latitudes and longitudes from degree to radians. 
    lat1 = toRadians(lat1); 
    long1 = toRadians(long1); 
    lat2 = toRadians(lat2); 
    long2 = toRadians(long2); 
      
    // Haversine Formula 
    long double dlong = long2 - long1; 
    long double dlat = lat2 - lat1; 
  
    long double ans = pow(sin(dlat / 2), 2) + cos(lat1) * cos(lat2) * pow(sin(dlong / 2), 2);
    ans = 2 * asin(sqrt(ans)); 
  
    // Radius of Earth in Kilometers, R = 6371 
    // Use R = 3956 for miles 
    long double R = 6371; 
      
    // Calculate the result 
    ans = ans * R; 
  
    return ans; 
}
  
// Driver Code 
int main() 
{ 
    long double lat1 = 53.32055555555556; 
    long double long1 = -1.7297222222222221; 
    long double lat2 = 53.31861111111111; 
    long double long2 = -1.6997222222222223; 
      
    // call the distance function 
    cout << setprecision(15) << fixed; 
    cout << distance(lat1, long1,  
                     lat2, long2) << " K.M"; 
  
    return 0; 
} 

mr.mams
  • 425
  • 4
  • 10
0

According to the Arduino Tag:

If it's for short distances, not too close to the poles, and if you prefer to steer a straight course rather than the shortest arch on the surface of a globe...

... you can use euclidean geometry, take a constant lat/dist_NS ratio and a fixed
lon/dist_WE ratio, depending on cos(lat)

On usual vessels, especially when equipped with float32 Arduinos, cos(lat) can safely be calculated once daily or even at power reset only.

Rather be sure not to lose precision when doing the substractions for dlat and dlon. Your (32.0512N 44.4497E) seems to be rounded to about 10m accuracy only.

Sailor's nautical miles are defined as 60 miles = 1° lat = 1° lon @ equator.

Here we return km for convenience.

const float kmpdeg = 111.195;  // km per degree
float coslat; // 1.000 @ 0° ,  0.500 @ 60°

typedef struct {float lat; float lon; } pos;  // S or W are negative
// eventually define something more accurate

pos p1 {32.0512, 44.4497};
pos p2 {32.0305, 44.4015}; // Kufa mosque, a few km southwest

float dist(const pos& a, const pos& b) {
  if (coslat == 0.0) {  // no need to calculate every time
   float lat = (a.lat + b.lat) / 2;
   coslat = cos(lat * PI / 180);
  } 
  
  float dlat = a.lat - b.lat;
  float dlon = (a.lon - b.lon) * coslat;

  return sqrt(dlat*dlat + dlon*dlon) * kmpdeg;
}

You won't notice a significant difference compared with the precise Haversine formula or online calculators, if it's for small but still GPS-significant distances.

datafiddler
  • 1,755
  • 3
  • 17
  • 30