QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GPSReceiver.h
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <functional>
5#include <memory>
6#include <optional>
7
8#include <QtCore/QHash>
9#include <QtCore/QList>
10#include <QtCore/QObject>
11#include <QtCore/QPointer>
12#include <QtCore/QString>
13#include <QtQmlIntegration/QtQmlIntegration>
14
15#include "GPSObservation.h"
18#include "GPSReceiverReports.h"
19#include "ScheduledTask.h"
20
25class GPSSourceHealth;
26class PositionManager;
29
32{
37#ifndef QGC_NO_SERIAL_LINK
41#endif
42};
43
46{
47 Q_GADGET
48 QML_VALUE_TYPE(gpsSerialPortEntry)
49 QML_STRUCTURED_VALUE
50 Q_PROPERTY(QString value MEMBER value FINAL)
51 Q_PROPERTY(QString label MEMBER label FINAL)
52
53public:
55 QString value;
57 QString label;
58
59 bool operator==(const GPSSerialPortEntry&) const = default;
60};
61
66class GPSReceiver : public QObject, private GPSReceiverConnector
67{
68 Q_OBJECT
69 QML_ELEMENT
70 QML_UNCREATABLE("Managed by GPSManager")
71 Q_MOC_INCLUDE("GPSReceiverFactGroup.h")
72
73 Q_PROPERTY(GPSReceiverFactGroup* facts READ facts CONSTANT FINAL)
74 Q_PROPERTY(QList<GPSSerialPortEntry> serialPortEntries READ serialPortEntries NOTIFY serialPortEntriesChanged FINAL)
75 Q_PROPERTY(QList<int> serialBaudRates READ serialBaudRates CONSTANT FINAL)
76 Q_PROPERTY(bool hasReceiver READ hasReceiver NOTIFY receiverChanged FINAL)
77 Q_PROPERTY(bool serialSupported READ serialSupported CONSTANT FINAL)
78 Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged FINAL)
79 Q_PROPERTY(int activeBaseMode READ activeBaseMode NOTIFY receiverChanged FINAL)
80 Q_PROPERTY(QString activeEndpoint READ activeEndpoint NOTIFY receiverChanged FINAL)
81 Q_PROPERTY(QString receiverIdentity READ receiverIdentity NOTIFY receiverChanged FINAL)
82 Q_PROPERTY(QString detectedReceiver READ detectedReceiver NOTIFY receiverChanged FINAL)
83 Q_PROPERTY(bool reconnecting READ reconnecting NOTIFY receiverChanged FINAL)
87 Q_PROPERTY(bool forwardingCorrections READ forwardingCorrections NOTIFY receiverChanged FINAL)
93 Q_PROPERTY(bool connectionSupported READ connectionSupported NOTIFY configurationChanged FINAL)
95 Q_PROPERTY(QString summaryLabel READ summaryLabel NOTIFY summaryLabelChanged FINAL)
97 Q_PROPERTY(QString statusText READ statusText NOTIFY statusTextChanged FINAL)
99 Q_PROPERTY(bool baseModeSupported READ baseModeSupported NOTIFY configurationChanged FINAL)
101 Q_PROPERTY(bool canConnect READ canConnect NOTIFY configurationChanged FINAL)
102
103 friend class GPSReceiverTest;
106
107public:
110
112 static constexpr std::chrono::seconds POLL_INTERVAL{1};
114 static constexpr std::chrono::seconds OUTPUT_OVERFLOW_WARNING_DURATION{30};
116 static constexpr std::chrono::seconds SHUTDOWN_TIMEOUT{2};
117
119
120 explicit GPSReceiver(QObject* parent = nullptr, RuntimeScheduler* scheduler = nullptr,
121 const Dependencies& dependencies = {});
122 ~GPSReceiver();
123
126 void setWorkerFactory(WorkerFactory factory);
127
128 const Configuration& configuration() const { return _configuration; }
129
134 QList<GPSSerialPortEntry> serialPortEntries() const { return _serialPortEntries; }
135
137 static QList<int> serialBaudRates();
138
140 std::optional<GPSObservation> acceptedPositionObservation(GPSObservation::PositionUse use) const;
144 Q_INVOKABLE bool connectReceiver();
146 Q_INVOKABLE void disconnectReceiver();
148 void startConnectionPolling(std::function<bool()> suspended = {});
150 void tick();
153 void shutdown();
154
156 GPSReceiverFactGroup* facts() const { return _facts; }
157
158 bool hasReceiver() const override;
159
160 bool serialSupported() const
161 {
162#ifdef QGC_NO_SERIAL_LINK
163 return false;
164#else
165 return true;
166#endif
167 }
168
170 QString errorMessage() const;
171
172 int activeBaseMode() const;
173
175 QString activeEndpoint() const;
176
177 QString receiverIdentity() const;
178
181 QString detectedReceiver() const;
182
183 QString summaryLabel() const { return _summaryLabel; }
184
185 QString statusText() const { return _statusText; }
186
188 bool reconnecting() const;
189
191 Q_INVOKABLE GPSReceiverPresentation capabilitiesFor(int role, int manufacturer) const;
192
195
197
198 bool forwardingCorrections() const;
199
200 bool persistentChangesAllowed() const { return _persistentChangesAllowed; }
201
203 void setPersistentChangesAllowed(bool allowed);
204
205 RTKSettings::ConnectionType effectiveConnectionType() const { return _configuration.effectiveConnectionType(); }
206
208 bool connectionSupported() const;
209
210 bool baseModeSupported() const;
211
212 bool canConnect() const;
213
214signals:
222 void serialDeviceRenamed(const QString& device);
225
226private slots:
227 void _onSatelliteInfoUpdated(const GPSSatelliteReport& msg);
228 void _onPositionUpdated(const GPSPositionReport& report);
229 void _onGPSConnect();
230 void _onReceiverDetected();
231 void _onInputProblem(GPSInputProblem problem);
232 void _onGPSSurveyReport(const GPSSurveyReport& status);
233
234private:
236 struct ReceiverState
237 {
238 bool hasReceiver = false;
239 int baseMode = -1;
240 QString endpoint;
241 QString identity;
242 QString detected;
243 bool reconnecting = false;
244 GPSReceiverPresentation presentation;
246 bool forwardingCorrections = false;
247 bool operator==(const ReceiverState&) const = default;
248 };
249
251 struct Notified
252 {
253 quint64 configurationRevision = 0;
254 bool persistentChangesAllowed = false;
255 QString errorMessage;
256 ReceiverState receiver;
257 };
258
259 int _activeManufacturer() const;
260
261 void setConnectionError(const QString& message) override { _setError(message); }
262
263 void endConnection(bool clearError) override;
264 bool connectTcp(const QString& host, quint16 port, GPSType type, bool allowPersistentChanges) override;
265 bool connectUdp(quint16 port, GPSType type) override;
266#ifndef QGC_NO_SERIAL_LINK
267 bool connectSerial(const QString& device, GPSType type, uint32_t baudRate, bool allowPersistentChanges,
268 GPSSerialClaim claim) override;
269 bool serialPortAvailable(const QString& device) const override;
270 QString serialPortLabel(const QString& device) const override;
271 bool serialPortAllowed(const QString& device) const override;
272 QStringList rtkReceiverPorts() const override;
273 GPSSerialClaim claimSerialPort(const QString& device) override;
276 void _onSerialPortsEnumerated(const QStringList& devices);
277 void _updateSerialPortEntries();
280 QString _serialLocation(const QString& device) const;
283 void _usePersistentSerialDevice();
284#endif
285
286 bool _connectReceiver(GPSType type, GPSReceiverWorker::TransportFactory transportFactory,
287 const QString& sourceInstance, uint32_t baudRate, bool allowPersistentChanges,
288 const QString& serialDevice = {}, const QString& endpoint = {});
289 void _endSession(GPSConnectionError error, const QString& detail, bool portRemoved);
291 void _retireSession();
293 void _joinRetiredWorkers();
294
295 void _setError(const QString& message = {}) { _errorMessage = message; }
296
298 void _updateOutputOverflow(uint64_t receiptUs);
299 void _setPersistentChangesAllowed(bool allowed);
300 void _resetStatus();
301 void _updateLabels();
303 void _clearStaleSolution();
304 ReceiverState _receiverState() const;
306 void _emitChanges();
307
308 Configuration _configuration;
309 GPSReceiverSession* _session = nullptr;
311 QList<QPointer<GPSReceiverWorker>> _retiredWorkers;
312 const QPointer<GPSCorrectionManager> _correctionManager;
313 const QPointer<PositionManager> _positionManager;
314 RuntimeScheduler* const _scheduler;
315 GPSSourceHealth* const _positionHealth;
316 ScheduledTask _outputOverflowTask;
317 ScheduledTask _pollTask;
318 quint64 _sessionCount = 0;
319 QString _errorMessage;
320 QString _summaryLabel;
321 QString _statusText;
322 QString _outputOverflowWarning;
323 uint64_t _outputOverflowUs = 0;
324 std::unique_ptr<GPSReceiverConnectionPolicy> _connection;
325 GPSReceiverFactGroup* _facts = nullptr;
326 WorkerFactory _workerFactory;
327 quint64 _configurationRevision = 0;
328 Notified _notified;
329 bool _destroying = false;
330 bool _persistentChangesAllowed = false;
331 QList<GPSSerialPortEntry> _serialPortEntries;
332#ifndef QGC_NO_SERIAL_LINK
333 const QPointer<SerialPortManager> _serialPorts;
336 QHash<QString, std::weak_ptr<const SerialPortManager::Reservation>> _serialClaims;
337 std::function<std::unique_ptr<GPSTransport>(const QString&, GPSCancelToken)> _serialTransportFactory;
338#endif
339};
GPSConnectionErrors::GPSInputProblem GPSInputProblem
GPSConnectionErrors::GPSConnectionError GPSConnectionError
std::function< GPSReceiverWorker *(GPSReceiverWorker::TransportFactory, GPSType, const GPSReceiverConfig &, QObject *)> GPSReceiverWorkerFactory
Creates a receiver session's worker; tests inject scripted workers.
SerialPortManager::ReservationPtr GPSSerialClaim
Holds a serial device for one receiver session until the last copy is released.
GPSTypes::GPSType GPSType
Definition GPSType.h:25
virtual QStringList rtkReceiverPorts() const =0
std::function< std::unique_ptr< GPSTransport >(GPSCancelToken)> TransportFactory
Consumed on the worker thread, so transport construction, I/O and destruction share that thread.
void summaryLabelChanged()
friend class GPSReceiverConnectionPolicyTest
static constexpr std::chrono::seconds POLL_INTERVAL
How often connection polling ticks the receiver.
void startConnectionPolling(std::function< bool()> suspended={})
Runs tick() every POLL_INTERVAL until shutdown(), skipping ticks while suspended returns true.
RTKSettings::ConnectionType effectiveConnectionType() const
QString detectedReceiver() const
bool persistentChangesAllowed() const
friend class GPSReceiverSettingsBindingTest
static QList< int > serialBaudRates()
The application's serial rates that a receiver accepts as an explicit rate; empty without serial link...
GPSReceiver(QObject *parent=nullptr, RuntimeScheduler *scheduler=nullptr, const Dependencies &dependencies={})
const Configuration & configuration() const
void receiverChanged()
RTKSettings::ReceiverRole activeRole() const
bool connectionSupported() const
UDP only receives, so a receiver QGroundControl configures needs serial or TCP.
QString statusText() const
QString activeEndpoint() const
Serial device or TCP host:port of the active receiver.
void configurationChanged()
Q_INVOKABLE GPSReceiverPresentation capabilitiesFor(int role, int manufacturer) const
Editable settings for a role; configured bases also depend on the manufacturer.
bool hasReceiver() const override
void persistentChangesAllowedChanged()
void serialPortEntriesChanged()
bool forwardingCorrections() const
friend class GPSReceiverTest
The active receiver's RTCM output is a correction source.
GPSReceiverWorkerFactory WorkerFactory
void shutdown()
QList< GPSSerialPortEntry > serialPortEntries() const
static constexpr std::chrono::seconds SHUTDOWN_TIMEOUT
How long shutdown() waits for the workers of retired sessions.
int activeBaseMode() const
GPSReceiverFactGroup * facts() const
The receiver's reported state; with no receiver, every Fact is at its metadata default.
bool reconnecting() const
The saved receiver is being connected automatically, at startup or after its connection was lost.
void setWorkerFactory(WorkerFactory factory)
Inject before connecting; an empty factory restores the real worker.
void serialDeviceRenamed(const QString &device)
GPSReceiverPresentation activePresentation() const
Presentation capabilities of the connected receiver family.
GPSReceiverDependencies Dependencies
QString summaryLabel() const
void statusTextChanged()
void errorMessageChanged()
static constexpr std::chrono::seconds OUTPUT_OVERFLOW_WARNING_DURATION
How long the warning about output the receiver dropped stays after the last report of it.
std::optional< GPSObservation > acceptedPositionObservation(GPSObservation::PositionUse use) const
Latest receiver solution that passes the consumer's gates; empty without a fresh fix.
QString errorMessage() const
The connection or input problem, else a recent report that the receiver dropped output.
GPSReceiverConfiguration Configuration
void setConfiguration(const Configuration &configuration)
bool serialSupported() const
Q_INVOKABLE bool connectReceiver()
void tick()
One step of automatic connection: connections due, reconnections, and USB discovery.
bool baseModeSupported() const
void setPersistentChangesAllowed(bool allowed)
Granting permission only lasts until the configuration changes, a session starts or ends,...
bool canConnect() const
Q_INVOKABLE void disconnectReceiver()
Disconnects and pauses automatic connection until the user connects again.
QString receiverIdentity() const
Session health is independent of transport readiness and RTK survey-in validity.
ConnectionType
Persisted values of connectionType. The storage type keeps any saved value a valid enum value.
Definition RTKSettings.h:44
ReceiverRole
Persisted values of receiverRole. The storage type keeps any saved value a valid enum value.
Definition RTKSettings.h:36
Shared serial inventory and exclusive claims. Called on the application thread.
The receiver configuration the application's settings binding supplies as values.
Services a GPSReceiver uses, which must outlive it.
Definition GPSReceiver.h:32
SerialPortManager * serialPorts
Definition GPSReceiver.h:40
GPSCorrectionManager * corrections
Receives a forwarding receiver's RTCM output as the local correction source.
Definition GPSReceiver.h:34
PositionManager * positions
Ready receivers publish their solution here as the GCS "RTK receiver" position source.
Definition GPSReceiver.h:36
A serial device the receiver settings offer.
Definition GPSReceiver.h:46
QString value
The name the device setting stores for the port.
Definition GPSReceiver.h:55
QString label
What the picker shows.
Definition GPSReceiver.h:57
bool operator==(const GPSSerialPortEntry &) const =default