0

I have been tying to make a device with arduino that would use two gps modules. So one sends gps cords and the other receives and compares its current location to the location of the sender and once the receiver gets a few meters from the sender an indicator led would turn on indicating the send is nearby and if you were to walk away from the sender the led would turn off. So my problem is i cant figure out how to compare the received and current values of cords to work accurately. This is what i have been trying to get to work i dont know if im going completing the wrong way with this i dont know if you can do some sort of radius and use that if someone could help me solve this i would greatly appreciate it. Example data received from sender Lat: 12.76433041 Lon: -54.74539143 from receiver Lat: 12.76433854 Lon: -54.74539435.

 int destLat;
 int currLat;
 int destLon;
 int currLng;

  currLat = gps.location.lat();
  destLat = Lat;
  currLng = gps.location.lng();
  destLon = Lon; 

  bool AreSame(float currLat, float destLat)
    if fabs(currLat - destLat) < EPSILON;
    {
      if fabs(currLng - destLon) < EPSILON;
      digitalWrite (4, HIGH);
    }
Ismar Perez
  • 19
  • 1
  • 5
  • You haven't described the actual behavior you are seeing. Also, I don't think your GPS receiver can resolve "a few meters from the sender" with much reliability. – Jim Garrison May 02 '15 at 19:52
  • I have seen it work the gps can resolve a few meters from the sender. The data i am receiving has 8 decimal points of precision so lets say for example from sender Lat: 12.76433041 Lon: -54.74539143 from receiver Lat: 12.76433854 Lon: -54.74539435 – Ismar Perez May 02 '15 at 20:01

3 Answers3

1

What you are looking for is to find the "distance" between two geographic coordinates.

This not a simple task. There are several formulas that can approximate this but I think you will not find something that could give you an error smaller than 1 meter.

Also, as Jim mentioned, the error of the GPS measurement itself will be in most cases higher than few meters. Some GPS receivers will provide the estimated horizontal error in run-time. I assume you are using a cheap one. You should take into consideration that the accuracy is dependent also on the quality of the receiver, so do not expect great results.

The exact formula you need to use is beyond my knowledge, I can only give some keywords as references, you might want to research "haversine" and "great circle distance".

You could nevertheless "cheat" a little. Assuming that all the usages of the device will be performed in the same local area, you could determine a value of "epsilon" experimentally.

Start your arduino, go north X meters record the difference in Latitudes, this is your E1. Go back to starting point, go X meters east, record the difference in Longitudes, this is E2.

Then a super sketchy formula to test if one device is more than X meters away than the second device is:

sqrt(ΔLat**2 + ΔLong**2) < sqrt(E1**2 + E2**2). 

This is of course assuming that the error of the receiver is not too high.

Artium
  • 5,147
  • 8
  • 39
  • 60
0

This:

   if fabs(currLat - destLat) < EPSILON;
    {
      if fabs(currLng - destLon) < EPSILON;
      digitalWrite (4, HIGH);
    }

should probably be more like:

   if (fabs(currLat - destLat) < EPSILON) /* no semicolon needed here */
    {
      if (fabs(currLng - destLon) < EPSILON)  /* or here */
          digitalWrite (4, HIGH); /* runs when both conditions are true */
    }
Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • I dont think my approach on this will work do you know of any radius functions on c that i could use to solve my problem thank you so so much. – Ismar Perez May 02 '15 at 19:50
  • @Ismar, please see the answers on the question I marked as a duplicate of yours. – Jim Lewis May 03 '15 at 01:02
0

In Python, with the pyproj module it is easy.

from pyproj import Geod  # sudo pip3 install pyproj
geoid = Geod(ellps='WGS84')  # See pyproj documentation for other fun options
def odometer(start, end):
    """Calculate bearings and distance between two longitude/latitude tuples.
    Arguments:
      start (tuple of float): longitude and latitude of start position e.g., (-146.241122, -15.560615) Apataki Carenage
      end (tuple of float): longitude and latitude of end position, e.g., (150.142778,-30.290556) Mt. Kaputar
    Returns:
      float: True North bearing start -> end
      float: True North bearing end -> start
      float: distance start <-> end
    Raises:
      some error, I know not what.
    """
    try:
        bearing_to, bearing_fro, distance = geoid.inv(*start + end)
    except Exception as error:  # try to be more specific here!
        print("Can't calculate bearing or distance because:", error)
        return None
    bearing_to %= 360
    bearing_fro %= 360
    return bearing_to, bearing_fro, distance

A point to remember is 0.000001 degree longitude at the equator is approx. 10cm. while your gps units can return the appearance of this accuracy(or greater), the probability of this accuracy is not there.

Nodak
  • 929
  • 1
  • 8
  • 14