QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
DeviceInfo.cc
Go to the documentation of this file.
1#include "DeviceInfo.h"
3
4#include <QtCore/QApplicationStatic>
5
6QGC_LOGGING_CATEGORY(QGCDeviceInfoLog, "Utilities.QGCDeviceInfo")
7
8namespace QGCDeviceInfo
9{
10
12
13Q_APPLICATION_STATIC(QGCAmbientTemperature, s_ambientTemperature);
14
15QGCAmbientTemperature* QGCAmbientTemperature::instance()
16{
17 return s_ambientTemperature();
18}
19
20QGCAmbientTemperature::QGCAmbientTemperature(QObject* parent)
21 : QObject(parent)
22 , _ambientTemperature(new QAmbientTemperatureSensor(this))
23 , _ambientTemperatureFilter(std::make_shared<QGCAmbientTemperatureFilter>())
24{
25 connect(_ambientTemperature, &QAmbientTemperatureSensor::sensorError, this, [](int error) {
26 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "QAmbientTemperature error:" << error;
27 });
28
29 if (!init()) {
30 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Error Initializing Ambient Temperature Sensor";
31 }
32
33 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
34}
35
36QGCAmbientTemperature::~QGCAmbientTemperature()
37{
38 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
39}
40
41bool QGCAmbientTemperature::init()
42{
43 _ambientTemperature->addFilter(_ambientTemperatureFilter.get());
44
45 const bool connected = _ambientTemperature->connectToBackend();
46 if(!connected) {
47 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Failed to connect to ambient temperature backend";
48 return false;
49 } else {
50 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Connected to ambient temperature backend:" << _ambientTemperature->identifier();
51 }
52
53 if (_ambientTemperature->isFeatureSupported(QSensor::SkipDuplicates)) {
54 _ambientTemperature->setSkipDuplicates(true);
55 }
56
57 const qrangelist dataRates = _ambientTemperature->availableDataRates();
58 if (!dataRates.isEmpty()) {
59 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Available Data Rates:" << dataRates;
60 // _ambientTemperature->setDataRate(dataRates.first().first);
61 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Selected Data Rate:" << _ambientTemperature->dataRate();
62 }
63
64 const qoutputrangelist outputRanges = _ambientTemperature->outputRanges();
65 if (!outputRanges.isEmpty()) {
66 // qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Output Ranges:" << outputRanges;
67 // _ambientTemperature->setOutputRange(outputRanges.first().first);
68 const int outputRangeIndex = _ambientTemperature->outputRange();
69 if (outputRangeIndex < outputRanges.size()) {
70 const qoutputrange outputRange = outputRanges.at(_ambientTemperature->outputRange());
71 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Selected Output Range:" << outputRange.minimum << outputRange.maximum << outputRange.accuracy;
72 }
73 }
74
75 _readingChangedConnection = connect(_ambientTemperature, &QAmbientTemperatureSensor::readingChanged, this, [this]() {
76 QAmbientTemperatureReading* reading = _ambientTemperature->reading();
77 if (!reading) {
78 return;
79 }
80
81 _temperatureC = reading->temperature();
82
83 emit temperatureUpdated(_temperatureC);
84 });
85
86 // _ambientTemperature->setActive(true);
87 const bool started = _ambientTemperature->start();
88 if (!started) {
89 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Failed to start ambient temperature";
90 return false;
91 }
92
93 return true;
94}
95
96void QGCAmbientTemperature::quit()
97{
98 // _ambientTemperature->setActive(false);
99 _ambientTemperature->stop();
100 _ambientTemperature->disconnect(_readingChangedConnection);
101}
102
103QGCAmbientTemperatureFilter::QGCAmbientTemperatureFilter()
104 : QAmbientTemperatureFilter()
105{
106 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
107}
108
109QGCAmbientTemperatureFilter::~QGCAmbientTemperatureFilter()
110{
111 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
112}
113
114bool QGCAmbientTemperatureFilter::filter(QAmbientTemperatureReading *reading)
115{
116 if (!reading) {
117 return false;
118 }
119
120 const qreal temperature = reading->temperature();
121 return ((temperature >= s_minValidTemperatureC) && (temperature <= s_maxValidTemperatureC));
122}
123
125
126Q_APPLICATION_STATIC(QGCPressure, s_pressure);
127
128QGCPressure* QGCPressure::instance()
129{
130 return s_pressure();
131}
132
133QGCPressure::QGCPressure(QObject* parent)
134 : QObject(parent)
135 , _pressure(new QPressureSensor(this))
136 , _pressureFilter(std::make_shared<QGCPressureFilter>())
137{
138 connect(_pressure, &QPressureSensor::sensorError, this, [](int error) {
139 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "QPressure error:" << error;
140 });
141
142 if (!init()) {
143 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Error Initializing Pressure Sensor";
144 }
145
146 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
147}
148
149QGCPressure::~QGCPressure()
150{
151 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
152}
153
154bool QGCPressure::init()
155{
156 _pressure->addFilter(_pressureFilter.get());
157
158 const bool connected = _pressure->connectToBackend();
159 if(!connected) {
160 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Failed to connect to pressure backend";
161 return false;
162 } else {
163 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Connected to pressure backend:" << _pressure->identifier();
164 }
165
166 if (_pressure->isFeatureSupported(QSensor::SkipDuplicates)) {
167 _pressure->setSkipDuplicates(true);
168 }
169
170 const qrangelist dataRates = _pressure->availableDataRates();
171 if (!dataRates.isEmpty()) {
172 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Available Data Rates:" << dataRates;
173 // _pressure->setDataRate(dataRates.first().first);
174 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Selected Data Rate:" << _pressure->dataRate();
175 }
176
177 const qoutputrangelist outputRanges = _pressure->outputRanges();
178 if (!outputRanges.isEmpty()) {
179 // qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Output Ranges:" << outputRanges;
180 // _pressure->setOutputRange(outputRanges.first().first);
181 const int outputRangeIndex = _pressure->outputRange();
182 if (outputRangeIndex < outputRanges.size()) {
183 const qoutputrange outputRange = outputRanges.at(_pressure->outputRange());
184 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Selected Output Range:" << outputRange.minimum << outputRange.maximum << outputRange.accuracy;
185 }
186 }
187
188 _readingChangedConnection = connect(_pressure, &QPressureSensor::readingChanged, this, [this]() {
189 QPressureReading* reading = _pressure->reading();
190 if (!reading) {
191 return;
192 }
193
194 _temperatureC = reading->temperature();
195 _pressurePa = reading->pressure();
196 emit pressureUpdated(_pressurePa, _temperatureC);
197 });
198
199 // _pressure->setActive(true);
200 const bool started = _pressure->start();
201 if (!started) {
202 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Failed to start pressure";
203 return false;
204 }
205
206 return true;
207}
208
209void QGCPressure::quit()
210{
211 // _pressure->setActive(false);
212 _pressure->stop();
213 _pressure->disconnect(_readingChangedConnection);
214}
215
216QGCPressureFilter::QGCPressureFilter()
217 : QPressureFilter()
218{
219 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
220}
221
222QGCPressureFilter::~QGCPressureFilter()
223{
224 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
225}
226
227bool QGCPressureFilter::filter(QPressureReading *reading)
228{
229 if (!reading) {
230 return false;
231 }
232
233 const qreal temperature = reading->temperature();
234 if ((temperature < s_minValidTemperatureC) || (temperature > s_maxValidTemperatureC)) {
235 return false;
236 }
237
238 const qreal pressure = reading->pressure();
239 if ((pressure < s_minValidPressurePa) || (pressure > s_maxValidPressurePa)) {
240 return false;
241 }
242
243 return true;
244}
245
247
248Q_APPLICATION_STATIC(QGCCompass, s_compass);
249
250QGCCompass* QGCCompass::instance()
251{
252 return s_compass();
253}
254
255QGCCompass::QGCCompass(QObject* parent)
256 : QObject(parent)
257 , _compass(new QCompass(this))
258 , _compassFilter(std::make_shared<QGCCompassFilter>())
259{
260 // qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
261
262 (void) connect(_compass, &QCompass::sensorError, this, [](int error) {
263 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Compass error:" << error;
264 });
265
266 if (!init()) {
267 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Error Initializing Compass Sensor";
268 }
269}
270
271QGCCompass::~QGCCompass()
272{
273 // qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
274}
275
276bool QGCCompass::init()
277{
278 _compass->addFilter(_compassFilter.get());
279
280 const bool connected = _compass->connectToBackend();
281 if (!connected) {
282 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Failed to connect to compass backend";
283 return false;
284 } else {
285 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Connected to compass backend:" << _compass->identifier();
286 }
287
288 if (_compass->isFeatureSupported(QSensor::SkipDuplicates)) {
289 _compass->setSkipDuplicates(true);
290 }
291
292 const qrangelist dataRates = _compass->availableDataRates();
293 if (!dataRates.isEmpty()) {
294 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Available Data Rates:" << dataRates;
295 // _compass->setDataRate(dataRates.first().first);
296 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Selected Data Rate:" << _compass->dataRate();
297 }
298
299 const qoutputrangelist outputRanges = _compass->outputRanges();
300 if (!outputRanges.isEmpty()) {
301 // qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Output Ranges:" << outputRanges;
302 // _compass->setOutputRange(outputRanges.first().first);
303 const int outputRangeIndex = _compass->outputRange();
304 if (outputRangeIndex < outputRanges.size()) {
305 const qoutputrange outputRange = outputRanges.at(_compass->outputRange());
306 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << "Selected Output Range:" << outputRange.minimum << outputRange.maximum << outputRange.accuracy;
307 }
308 }
309
310 _readingChangedConnection = connect(_compass, &QCompass::readingChanged, this, [this]() {
311 QCompassReading* const reading = _compass->reading();
312 if (!reading) {
313 return;
314 }
315
316 _calibrationLevel = reading->calibrationLevel();
317 _azimuth = reading->azimuth();
318
319 emit compassUpdated(_azimuth);
320
321 QGeoPositionInfo update;
322 update.setAttribute(QGeoPositionInfo::Attribute::Direction, _azimuth);
323 update.setAttribute(QGeoPositionInfo::Attribute::DirectionAccuracy, _calibrationLevel);
324 update.setTimestamp(QDateTime::currentDateTimeUtc());
325 emit positionUpdated(update);
326 });
327
328 // _compass->setActive(true);
329 const bool started = _compass->start();
330 if (!started) {
331 qCWarning(QGCDeviceInfoLog) << Q_FUNC_INFO << "Failed to start compass";
332 return false;
333 }
334
335 return true;
336}
337
338void QGCCompass::quit()
339{
340 // _compass->setActive(false);
341 _compass->stop();
342 _compass->disconnect(_readingChangedConnection);
343}
344
345QGCCompassFilter::QGCCompassFilter()
346 : QCompassFilter()
347{
348 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
349}
350
351QGCCompassFilter::~QGCCompassFilter()
352{
353 qCDebug(QGCDeviceInfoLog) << Q_FUNC_INFO << this;
354}
355
356bool QGCCompassFilter::filter(QCompassReading *reading)
357{
358 if (!reading) {
359 return false;
360 }
361
362 const qreal calibration = reading->calibrationLevel();
363 return (calibration >= s_minCompassCalibrationLevel);
364}
365
367
368} // namespace QGCDeviceInfo
Q_APPLICATION_STATIC(ADSBVehicleManager, _adsbVehicleManager, SettingsManager::instance() ->adsbVehicleManagerSettings())
Error error
#define QGC_LOGGING_CATEGORY(name, categoryStr)