QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GPSProtocolMath.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5#include <ctime>
6#include <limits>
7#include <numbers>
8#include <optional>
9
10#include <QtCore/QByteArray>
11
12#include "GPSReceiverConfig.h"
13
15namespace GPSProtocolMath {
16
17inline constexpr double DEG_TO_RAD = std::numbers::pi / 180.0;
18inline constexpr double RAD_TO_DEG = 180.0 / std::numbers::pi;
20inline constexpr time_t UTC_PLAUSIBILITY_FLOOR_SECS = static_cast<time_t>(1234567890ULL);
21
23[[nodiscard]] constexpr double degreesFromE7(int32_t value)
24{
25 return value * 1e-7;
26}
27
28[[nodiscard]] constexpr double metersFromMillimeters(int32_t value)
29{
30 return value / 1000.0;
31}
32
34[[nodiscard]] inline double roundedToMillimeters(double meters)
35{
36 return static_cast<double>(std::llround(meters * 1000.0)) / 1000.0;
37}
38
40[[nodiscard]] inline std::optional<uint32_t> tenthMillimeters(double meters)
41{
42 const double units = meters * 10000.0;
43 if (!std::isfinite(units) || units < 0 || units > (std::numeric_limits<uint32_t>::max)()) {
44 return std::nullopt;
45 }
46 return static_cast<uint32_t>(units);
47}
48
49struct Ecef
50{
51 double x = 0;
52 double y = 0;
53 double z = 0;
54};
55
56[[nodiscard]] Ecef toEcef(const GPSEllipsoidPosition& position);
57[[nodiscard]] GPSEllipsoidPosition fromEcef(const Ecef& position);
58
60[[nodiscard]] inline double distance(const Ecef& left, const Ecef& right)
61{
62 return std::hypot(left.x - right.x, left.y - right.y, left.z - right.z);
63}
64
67[[nodiscard]] bool nearEarthSurface(const Ecef& position);
68
72[[nodiscard]] uint64_t utcMicroseconds(int year, int month, int day, int hour, int minute, int second, int32_t nsec);
73
74} // namespace GPSProtocolMath
75
78[[nodiscard]] QByteArray gpsFixedDecimal(double value, int decimals);
QByteArray gpsFixedDecimal(double value, int decimals)
Unit conversions and geodesy shared by receiver families and their callers.
Ecef toEcef(const GPSEllipsoidPosition &position)
constexpr double metersFromMillimeters(int32_t value)
constexpr time_t UTC_PLAUSIBILITY_FLOOR_SECS
Receiver dates before this instant (2009-02-13) are implausible and reported as unknown.
bool nearEarthSurface(const Ecef &position)
constexpr double DEG_TO_RAD
constexpr double degreesFromE7(int32_t value)
Degrees of a coordinate in 1e-7 degree units, as u-blox and MAVLink send them.
double roundedToMillimeters(double meters)
meters rounded to whole millimetres.
double distance(const Ecef &left, const Ecef &right)
Straight-line distance between left and right, in metres.
std::optional< uint32_t > tenthMillimeters(double meters)
meters in 0.1 mm, truncated toward zero. Empty when not finite, negative or beyond a 32-bit unsigned ...
GPSEllipsoidPosition fromEcef(const Ecef &position)
uint64_t utcMicroseconds(int year, int month, int day, int hour, int minute, int second, int32_t nsec)
constexpr double RAD_TO_DEG
WGS84 geodetic position with ellipsoid height, not mean-sea-level altitude.