Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haversine Formula Implementation on Arduino

I'm working on creating a geofence using GPS & Arduino. I want to implement the Haversine Formula to find the distance between two points, in order to compute it with the radius. The problem is I'm finding it hard to implement the Haversine Formula on the Arduino IDE, I'm relatively beginner on Arduino.

a = sin²(Δφ/2) + cos(φ1).cos(φ2).sin²(Δλ/2)
c = 2.atan2(√a, √(1−a))
d = R.c

How can I implement the above formula on Arduino IDE?

like image 931
user3286992 Avatar asked May 05 '14 08:05

user3286992


1 Answers

To use Haversine or Spherical Law of Cosines you require either atan2() or acos() functions which do not seem to be available in Arduino.

Pythagoras’ theorem can be used on an equirectangular projection to calculate distance. This is less complicated than Haversine or Spherical Law of Cosines but still allows for the convergence towards poles and uses the trig functions available in Arduino.

Theory

The circumference of the earth at equator = 40,076 km. The equator is divided into 360 degrees of longitude, so each degree at the equator represents approximately 111.32 km. Moving away from the equator towards a pole this distance decreases to zero at the pole.

1 degrees aproximates to 111.32 km at equator.
96.41km at 30 degrees N/S
78.71 km at 45 degrees N/S
55.66 km at 60 degrees N/S
28.82 km at 75 degrees N/S

Application

In pseudo code as I don't know Arduino

R = 6371;km
lat/lng in radians

var x = (lng2-lng1) * cos((lat1+lat2)/2);
var y = (lat2-lat1);
var dist = sqrt(x*x + y*y) * R;

Result

coordinates = (0,0) to (1,0) Haversine 157.293809 km Equirectangular 157.294807 km
coordinates = (10,0) to (11,0) Haversine 155.985273 km Equirectangular 155.986379 km
coordinates = (20,0) to (21,0) Haversine 152.397156 km Equirectangular 152.39855 km
coordinates = (30,0) to (31,0) Haversine 146.81715 km Equirectangular 146.818975 km
coordinates = (40,0) to (41,0) Haversine 139.728101 km Equirectangular 139.730447 km
coordinates = (50,0) to (51,0) Haversine 131.817706 km Equirectangular 131.820603 km
coordinates = (60,0) to (61,0) Haversine 123.976838 km Equirectangular 123.980256 km
coordinates = (70,0) to (71,0) Haversine 117.255674 km Equirectangular 117.259525 km
coordinates = (80,0) to (81,0) Haversine 112.726966 km Equirectangular 112.731106 km
like image 131
david strachan Avatar answered Sep 19 '22 15:09

david strachan