7namespace tinyrobotics {
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
55 float longitude = 360;
58 GPSCoordinate() =
default;
60 GPSCoordinate(
float lat,
float lon,
float alt = 0)
61 : latitude(lat), longitude(lon), altitude(alt) {}
65 return latitude >= -90 && latitude <= 90 && longitude >= -180 &&
76 Distance dist(distM, DistanceUnit::M);
77 return dist.getValue(unit);
85 Angle angle(bearingDeg, AngleUnit::DEG);
86 return angle.getValue(unit);
93 float angleDeg = elevationDeg(other);
94 Angle angle(angleDeg, AngleUnit::DEG);
95 return angle.getValue(unit);
101 return other.altitude - altitude;
106 Distance altDiff)
const {
107 float bearingDeg = bearing.getValue(AngleUnit::DEG);
108 float distanceM = distance.getValue(DistanceUnit::M);
109 float altDiffM = altDiff.getValue(DistanceUnit::M);
115 float alt_diff_m = 0)
const {
116 float R = 6371000.0f;
117 float bearing_rad = bearing_deg *
static_cast<
float>(M_PI) / 180.0f;
118 float lat1 = latitude *
static_cast<
float>(M_PI) / 180.0f;
119 float lon1 = longitude *
static_cast<
float>(M_PI) / 180.0f;
121 float lat2 = std::asin(std::sin(lat1) * std::cos(distance_m / R) +
122 std::cos(lat1) * std::sin(distance_m / R) *
123 std::cos(bearing_rad));
127 std::sin(bearing_rad) * std::sin(distance_m / R) * std::cos(lat1),
128 std::cos(distance_m / R) - std::sin(lat1) * std::sin(lat2));
130 return GPSCoordinate(lat2 * 180.0f /
static_cast<
float>(M_PI),
131 lon2 * 180.0f /
static_cast<
float>(M_PI),
132 altitude + alt_diff_m);
138 return distance(other) < limit;
144 float altLimit)
const {
145 return distance(other) < limit &&
146 std::fabs(altitudeDifference(other)) < altLimit;
152 snprintf(buf,
sizeof(buf),
"%s: %.8f, %.8f, %.2fm", getTypeName(), latitude,
153 longitude, altitude);
154 return std::string(buf);
164 sscanf(str.c_str(),
"%79[^:]: %f, %f, %fm", typeName, &lat, &lon, &alt);
166 if (strcmp(typeName, getTypeName()) != 0)
return false;
175 const char* getTypeName()
const {
return "GPSCoordinate"; }
181 float lat1 = latitude *
static_cast<
float>(M_PI) / 180.0f;
182 float lat2 = other.latitude *
static_cast<
float>(M_PI) / 180.0f;
184 (other.longitude - longitude) *
static_cast<
float>(M_PI) / 180.0f;
186 float y = std::sin(dLon) * std::cos(lat2);
187 float x = std::cos(lat1) * std::sin(lat2) -
188 std::sin(lat1) * std::cos(lat2) * std::cos(dLon);
189 float brng = std::atan2(y, x);
190 return normalizeAngleDeg(brng * 180.0f /
191 static_cast<
float>(M_PI));
195 float dz = other.altitude - altitude;
197 return std::atan2(dz, dxy) * 180.0f /
static_cast<
float>(M_PI);
202 float R = 6371000.0f;
203 float lat1 = latitude *
static_cast<
float>(M_PI) / 180.0f;
204 float lat2 = other.latitude *
static_cast<
float>(M_PI) / 180.0f;
206 (other.latitude - latitude) *
static_cast<
float>(M_PI) / 180.0f;
208 (other.longitude - longitude) *
static_cast<
float>(M_PI) / 180.0f;
210 float a = std::sin(dLat / 2.0f) * std::sin(dLat / 2.0f) +
211 std::cos(lat1) * std::cos(lat2) * std::sin(dLon / 2.0f) *
212 std::sin(dLon / 2.0f);
213 float c = 2.0f * std::atan2(std::sqrt(a), std::sqrt(1.0f - a));
Represents a geodetic GPS coordinate with latitude, longitude, and optional altitude.
Definition: GPSCoordinate.h:52
std::string toString() const
Serialize GPS coordinate to string representation.
Definition: GPSCoordinate.h:150
float bearingDegree(const GPSCoordinate &other) const
Definition: GPSCoordinate.h:180
float distance(const GPSCoordinate &other, DistanceUnit unit=DistanceUnit::M) const
Calculate distance to other GPS coordinate.
Definition: GPSCoordinate.h:73
bool isValid() const
Check if the values are valid.
Definition: GPSCoordinate.h:64
bool equalsWithAltitude(const GPSCoordinate &other, float limit, float altLimit) const
Definition: GPSCoordinate.h:143
operator bool() const
Check if the values are valid.
Definition: GPSCoordinate.h:70
float elevation(const GPSCoordinate &other, AngleUnit unit=AngleUnit::DEG) const
Definition: GPSCoordinate.h:91
bool fromString(const std::string &str)
Definition: GPSCoordinate.h:159
GPSCoordinate navigate(float distance_m, float bearing_deg, float alt_diff_m=0) const
Calculate a new GPS coordinate given a distance (in meters) and bearing.
Definition: GPSCoordinate.h:114
float altitudeDifference(const GPSCoordinate &other) const
Definition: GPSCoordinate.h:100
GPSCoordinate navigate(Distance distance, Angle bearing, Distance altDiff) const
Calculate a new GPS coordinate given a distance (in meters) and bearing.
Definition: GPSCoordinate.h:105
float distanceM(const GPSCoordinate &other) const
Distance in meters between this coordinate and another GPS coordinate.
Definition: GPSCoordinate.h:201
bool equals(const GPSCoordinate &other, float limit) const
Definition: GPSCoordinate.h:137
float bearing(const GPSCoordinate &other, AngleUnit unit=AngleUnit::DEG) const
Definition: GPSCoordinate.h:82