QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
VehicleGPSAggregateFactGroup.cc
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * (c) 2009-2024 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 *
5 * QGroundControl is licensed according to the terms in the file
6 * COPYING.md in the root of the source code directory.
7 *
8 ****************************************************************************/
9
11#include "VehicleGPSFactGroup.h"
12#include "QGCLoggingCategory.h"
13#include <QtMath>
14
15VehicleGPSAggregateFactGroup::VehicleGPSAggregateFactGroup(QObject *parent)
16 : FactGroup(1000, ":/json/Vehicle/GPSFact.json", parent)
17{
18 _addFact(&_spoofingStateFact);
19 _addFact(&_jammingStateFact);
20 _addFact(&_authenticationStateFact);
21 _addFact(&_isStaleFact);
22
23 _spoofingStateFact.setRawValue(255);
24 _jammingStateFact.setRawValue(255);
25 _authenticationStateFact.setRawValue(255);
26 _isStaleFact.setRawValue(true);
27
28 _staleTimer.setSingleShot(true);
29 _staleTimer.setInterval(GNSS_INTEGRITY_STALE_TIMEOUT_MS);
30 connect(&_staleTimer, &QTimer::timeout, this, &VehicleGPSAggregateFactGroup::_onStaleTimeout);
31}
32
33void VehicleGPSAggregateFactGroup::bindToGps(VehicleGPSFactGroup* gps1, VehicleGPSFactGroup* gps2)
34{
35 _clearConnections();
36 _gps1 = gps1;
37 _gps2 = gps2;
38
39 if (_gps1) {
40 _connections << connect(_gps1, &VehicleGPSFactGroup::gnssIntegrityReceived, this, &VehicleGPSAggregateFactGroup::_onIntegrityUpdated);
41 }
42 if (_gps2) {
43 _connections << connect(_gps2, &VehicleGPSFactGroup::gnssIntegrityReceived, this, &VehicleGPSAggregateFactGroup::_onIntegrityUpdated);
44 }
45 _updateAggregates();
46}
47
48void VehicleGPSAggregateFactGroup::_updateAggregates()
49{
50 updateFromGps(_gps1, _gps2);
51}
52
53void VehicleGPSAggregateFactGroup::_onIntegrityUpdated()
54{
55 _isStaleFact.setRawValue(false);
56 _staleTimer.start();
57 _updateAggregates();
58}
59
60void VehicleGPSAggregateFactGroup::_onStaleTimeout()
61{
62 _spoofingStateFact.setRawValue(255);
63 _jammingStateFact.setRawValue(255);
64 _authenticationStateFact.setRawValue(255);
65 _isStaleFact.setRawValue(true);
66}
67
68void VehicleGPSAggregateFactGroup::_clearConnections()
69{
70 for (const auto& c : _connections) {
71 QObject::disconnect(c);
72 }
73 _connections.clear();
74}
75
76int VehicleGPSAggregateFactGroup::_valueOrInvalid(Fact* fact)
77{
78 if (!fact) {
79 return -1;
80 }
81 const QVariant v = fact->rawValue();
82 if (!v.isValid()) {
83 return -1;
84 }
85 bool ok = false;
86 const int val = v.toInt(&ok);
87 if (!ok) {
88 return -1;
89 }
90 return (val == 255) ? -1 : val;
91}
92
93int VehicleGPSAggregateFactGroup::_mergeWorst(int a, int b)
94{
95 return qMax(a, b);
96}
97
98int VehicleGPSAggregateFactGroup::_mergeAuthentication(int a, int b)
99{
100 // Priority: Unknown < Disabled < Initializing < OK < Error
101 auto getWeight = [](int val) {
102 switch (val) {
103 case AUTH_INVALID: return -1;
104 case AUTH_UNKNOWN: return 0; // lowest priority)
105 case AUTH_DISABLED: return 1;
106 case AUTH_INITIALIZING: return 2;
107 case AUTH_OK: return 3;
108 case AUTH_ERROR: return 4; // highest priority
109 default: return -1;
110 }
111 };
112
113 return (getWeight(a) >= getWeight(b)) ? a : b;
114}
115
116void VehicleGPSAggregateFactGroup::updateFromGps(VehicleGPSFactGroup* gps1, VehicleGPSFactGroup* gps2)
117{
118 const int spoof1 = _valueOrInvalid(gps1 ? gps1->spoofingState() : nullptr);
119 const int spoof2 = _valueOrInvalid(gps2 ? gps2->spoofingState() : nullptr);
120 const int jam1 = _valueOrInvalid(gps1 ? gps1->jammingState() : nullptr);
121 const int jam2 = _valueOrInvalid(gps2 ? gps2->jammingState() : nullptr);
122 const int auth1 = _valueOrInvalid(gps1 ? gps1->authenticationState() : nullptr);
123 const int auth2 = _valueOrInvalid(gps2 ? gps2->authenticationState() : nullptr);
124
125 const int spoofMerged = _mergeWorst(spoof1, spoof2);
126 const int jamMerged = _mergeWorst(jam1, jam2);
127 const int authMerged = _mergeAuthentication(auth1, auth2);
128
129 _spoofingStateFact.setRawValue(spoofMerged == -1 ? 255 : spoofMerged);
130 _jammingStateFact.setRawValue(jamMerged == -1 ? 255 : jamMerged);
131 _authenticationStateFact.setRawValue(authMerged == -1 ? 255 : authMerged);
132}
Used to group Facts together into an object hierarachy.
Definition FactGroup.h:19
A Fact is used to hold a single value within the system.
Definition Fact.h:19
void gnssIntegrityReceived()