QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
UBXEpochAssembler.cc
Go to the documentation of this file.
2
3#include <utility>
4
5#include "GPSFamilyProtocol.h"
6#include "GPSTime.h"
7
8namespace UBX {
9
11{
12 const uint64_t now = context.nowUs();
13 expire(context);
14 if (tow >= GPSTime::WEEK_MS || (_lastPublished && !GPSTime::towAdvances(tow, *_lastPublished))) {
15 return nullptr;
16 }
17 for (auto& epoch : _epochs) {
18 if (epoch && epoch->tow == tow) {
19 return &*epoch;
20 }
21 }
22 auto free = _epochs.begin();
23 while (free != _epochs.end() && *free) {
24 ++free;
25 }
26 if (free == _epochs.end()) {
27 free = GPSTime::towAdvances(_epochs[0]->tow, _epochs[1]->tow) ? _epochs.begin() + 1 : _epochs.begin();
28 if (!GPSTime::towAdvances(tow, (*free)->tow)) {
29 return nullptr;
30 }
31 _finish(*free, context);
32 }
33 *free = Epoch{};
34 (*free)->tow = tow;
35 (*free)->receipt = now;
36 return &**free;
37}
38
40{
41 const uint64_t now = context.nowUs();
42 _orderOldestFirst();
43 for (auto& epoch : _epochs) {
44 if (epoch && now >= epoch->receipt && std::chrono::microseconds(now - epoch->receipt) >= MAX_AGE) {
45 _finish(epoch, context);
46 }
47 }
48}
49
50void EpochAssembler::end(uint32_t tow, GPSDecodeContext& context)
51{
52 _orderOldestFirst();
53 for (auto& epoch : _epochs) {
54 if (epoch && (epoch->tow == tow || GPSTime::towAdvances(tow, epoch->tow))) {
55 _finish(epoch, context);
56 }
57 }
58}
59
60void EpochAssembler::_orderOldestFirst()
61{
62 if (_epochs[0] && _epochs[1] && GPSTime::towAdvances(_epochs[0]->tow, _epochs[1]->tow)) {
63 std::swap(_epochs[0], _epochs[1]);
64 }
65}
66
67void EpochAssembler::_finish(std::optional<Epoch>& epoch, GPSDecodeContext& context)
68{
69 if (epoch->hasPvt && (!_lastPublished || GPSTime::towAdvances(epoch->tow, *_lastPublished))) {
70 epoch->position.navigation.timestampUs = epoch->receipt;
71 context.publishPosition(epoch->position);
72 }
73 if (!_lastPublished || GPSTime::towAdvances(epoch->tow, *_lastPublished)) {
74 _lastPublished = epoch->tow;
75 }
76 epoch.reset();
77}
78
79} // namespace UBX
void publishPosition(const GPSDecodedPosition &report)
uint64_t nowUs() const
Epoch * find(uint32_t tow, GPSDecodeContext &context)
The epoch at tow, started now when new; nullptr for a time of week already published or too old.
void end(uint32_t tow, GPSDecodeContext &context)
Publishes the epoch at tow and the ones before it.
void expire(GPSDecodeContext &context)
Publishes epochs older than MAX_AGE.
static constexpr std::chrono::microseconds MAX_AGE
constexpr bool towAdvances(uint32_t later, uint32_t earlier)
Whether time of week later follows earlier by less than half a week, across the week rollover.
Definition GPSTime.h:18
constexpr uint32_t WEEK_MS
Milliseconds in a GPS week; every time of week is below it.
Definition GPSTime.h:9
u-blox receivers over UBX: CFG-VALSET configuration from protocol 27, CFG-MSG and CFG-TMODE3 before i...