Alex Mikhalev
6 years ago
6 changed files with 69 additions and 57 deletions
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
#include "lat_long.hh" |
||||
|
||||
float LatLong::distance_to(const LatLong &target) const { |
||||
float lat1 = latitude * RAD_PER_DEG; |
||||
float lat2 = target.latitude * RAD_PER_DEG; |
||||
float long1 = longitude * RAD_PER_DEG; |
||||
float long2 = target.longitude * RAD_PER_DEG; |
||||
float clat1 = cosf(lat1); |
||||
float clat2 = cosf(lat2); |
||||
float a = powf(sinf((long2 - long1) / 2.f), 2.f) * clat1 * clat2 + |
||||
powf(sinf((lat2 - lat1) / 2.f), 2.f); |
||||
float d_over_r = 2 * atan2f(sqrtf(a), sqrtf(1 - a)); |
||||
return d_over_r * EARTH_RAD; |
||||
} |
||||
|
||||
float LatLong::bearing_toward(const LatLong &target) const { |
||||
float dlong = (target.longitude - longitude) * RAD_PER_DEG; |
||||
float sdlong = sinf(dlong); |
||||
float cdlong = cosf(dlong); |
||||
float lat1 = latitude * RAD_PER_DEG; |
||||
float lat2 = target.latitude * RAD_PER_DEG; |
||||
float slat1 = sinf(lat1); |
||||
float clat1 = cosf(lat1); |
||||
float slat2 = sinf(lat2); |
||||
float clat2 = cosf(lat2); |
||||
float num = sdlong * clat2; |
||||
float denom = (clat1 * slat2) - (slat1 * clat2 * cdlong); |
||||
float course = atan2f(num, denom); |
||||
if (course < 0.0) { |
||||
course += 2 * PI; |
||||
} |
||||
return course / RAD_PER_DEG; |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
#pragma once |
||||
|
||||
#include <cmath> |
||||
#include "ugv_comms.hh" |
||||
|
||||
struct LatLong { |
||||
static constexpr float PI = |
||||
3.1415926535897932384626433832795028841971693993751058209749445923078164062; |
||||
|
||||
static constexpr float RAD_PER_DEG = PI / 180.f; |
||||
// Radius of earth in meters
|
||||
static constexpr float EARTH_RAD = 6372795.f; |
||||
|
||||
public: |
||||
float latitude; |
||||
float longitude; |
||||
|
||||
inline LatLong() : LatLong(0., 0.) {} |
||||
|
||||
inline LatLong(double latitude_, double longitude_) |
||||
: latitude(latitude_), longitude(longitude_) {} |
||||
|
||||
inline LatLong(const ugv::comms::messages::TargetLocation &loc) |
||||
: latitude(loc.latitude()), longitude(loc.longitude()) {} |
||||
|
||||
/**
|
||||
* Return distance from this LatLong to target, in meters |
||||
*/ |
||||
float distance_to(const LatLong &target) const; |
||||
float bearing_toward(const LatLong &target) const; |
||||
}; |
Loading…
Reference in new issue