QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GPSDecodedData.cc
Go to the documentation of this file.
1#include "GPSDecodedData.h"
2
3#include <algorithm>
4#include <cmath>
5#include <span>
6
7#include "MonotonicClock.h"
8
9namespace GPSDecodedData {
10
11GPSPositionReport position(const GPSDecodedPosition& source, const GPSIntegrityReport& diagnostic, uint64_t nowUs)
12{
13 GPSPositionReport result{.navigation = source.navigation, .integrity = diagnostic};
14 auto& navigation = result.navigation;
15 navigation.fixType = gpsFixQualityFromValue(static_cast<int>(navigation.fixType));
16 if (!source.velocityValid) {
17 navigation.speedMetersPerSecond = NAN;
18 navigation.courseRadians = NAN;
19 }
20 auto& integrity = result.integrity;
21 integrity.jamming.state = GPSIntegrityReport::jammingStateFromValue(static_cast<int>(diagnostic.jamming.state));
22 integrity.spoofing.state = GPSIntegrityReport::spoofingStateFromValue(static_cast<int>(diagnostic.spoofing.state));
23 // Navigation epochs retain their first receipt; diagnostics may arrive before that epoch is published.
24 integrity = integrity.freshAt(nowUs ? nowUs : std::max(navigation.timestampUs, diagnostic.timestampUs));
25 return result;
26}
27
29{
30 if (source.fullSnapshot) {
31 for (auto& [constellation, counts] : _constellations) {
32 counts = {};
33 }
34 }
36 const auto reported =
37 std::span(source.constellations).first(std::min<size_t>(source.count, source.constellations.size()));
38 for (const auto& native : reported) {
39 auto& counts = _constellations[native.constellation];
40 _accept(counts.inView, native.inViewTimestampUs, std::clamp(native.inView, 0, LIMIT), nowUs);
41 _accept(counts.used, native.inUseTimestampUs,
42 native.inUse ? std::optional<int>(std::clamp(*native.inUse, 0, LIMIT)) : std::nullopt, nowUs);
43 }
44 (void) _expire(nowUs);
45 return _report(false);
46}
47
49{
50 _countOnlyUsed = source.usedCount;
51 (void) _expire(nowUs);
52 return _report(true);
53}
54
55std::optional<GPSSatelliteReport> SatelliteCounts::expire(uint64_t nowUs)
56{
57 if (!_expire(nowUs)) {
58 return std::nullopt;
59 }
60 return _report(false);
61}
62
63void SatelliteCounts::_accept(Count& count, uint64_t receivedAtUs, std::optional<int> value, uint64_t nowUs)
64{
65 if (receivedAtUs >= count.receivedAtUs &&
67 count = {receivedAtUs, value};
68 }
69}
70
71bool SatelliteCounts::_expire(uint64_t nowUs)
72{
73 bool expired = false;
74 for (auto& [constellation, counts] : _constellations) {
75 for (Count* count : {&counts.inView, &counts.used}) {
76 if (count->receivedAtUs &&
77 !MonotonicClock::fresh(count->receivedAtUs, nowUs, SatelliteCounts::FRESHNESS_TIMEOUT)) {
78 *count = {};
79 expired = true;
80 }
81 }
82 }
83 return expired;
84}
85
86GPSSatelliteReport SatelliteCounts::_report(bool countOnlyUsed) const
87{
88 GPSSatelliteReport report;
89 int inView = 0;
90 int used = 0;
91 bool usedKnown = true;
92 for (const auto& [constellation, counts] : _constellations) {
93 if (!counts.inView.receivedAtUs) {
94 continue;
95 }
96 report.timestampUs = std::max(report.timestampUs, counts.inView.receivedAtUs);
97 const int visible = counts.inView.value.value_or(0);
98 inView += visible;
99 if (visible == 0) {
100 continue;
101 }
102 if (counts.used.receivedAtUs && counts.used.value) {
103 used += *counts.used.value;
104 } else {
105 usedKnown = false;
106 }
107 }
108 std::optional<int> viewUsed;
109 if (report.timestampUs) {
110 report.inView = inView;
111 if (inView == 0) {
112 viewUsed = 0;
113 } else if (usedKnown) {
114 viewUsed = used;
115 }
116 }
117 report.used = countOnlyUsed || !viewUsed ? _countOnlyUsed : viewUsed;
118 return report;
119}
120} // namespace GPSDecodedData
constexpr GPSFixQuality gpsFixQualityFromValue(int value)
GPSSatelliteReport update(const GPSDecodedSatellites &source, uint64_t nowUs)
std::optional< GPSSatelliteReport > expire(uint64_t nowUs)
static constexpr std::chrono::milliseconds FRESHNESS_TIMEOUT
How GPSDriver adapts decoded events to public reports.
GPSPositionReport position(const GPSDecodedPosition &source, const GPSIntegrityReport &diagnostic, uint64_t nowUs)
constexpr bool fresh(uint64_t timestampUs, uint64_t nowUs, std::chrono::microseconds lifetime)
Whether a receipt is younger than lifetime.
Decoded navigation plus whether the producer accepted its velocity solution.
GPSNavigationValues navigation
std::optional< int > usedCount
static constexpr uint16_t SAT_INFO_MAX_SATELLITES
std::array< Constellation, MAX_CONSTELLATIONS > constellations
static constexpr SpoofingState spoofingStateFromValue(int value)
static constexpr JammingState jammingStateFromValue(int value)
GPSIntegrityReport integrity
GPSNavigationValues navigation
std::optional< int > used
std::optional< int > inView