QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
TransformPositionController.cc
Go to the documentation of this file.
2
3#include "Fact.h"
4#include "FactMetaData.h"
6#include "QGCGeo.h"
8#include "Vehicle.h"
9
10QGC_LOGGING_CATEGORY(TransformPositionControllerLog, "QMLControls.TransformPositionController")
11
12QMap<QString, FactMetaData*> TransformPositionController::_metaDataMap;
13
15 : QObject(parent)
16 , _latitudeFact(new Fact(0, _latitudeFactName, FactMetaData::valueTypeDouble, this))
17 , _longitudeFact(new Fact(0, _longitudeFactName, FactMetaData::valueTypeDouble, this))
18 , _zoneFact(new Fact(0, _zoneFactName, FactMetaData::valueTypeUint8, this))
19 , _hemisphereFact(new Fact(0, _hemisphereFactName, FactMetaData::valueTypeUint8, this))
20 , _eastingFact(new Fact(0, _eastingFactName, FactMetaData::valueTypeDouble, this))
21 , _northingFact(new Fact(0, _northingFactName, FactMetaData::valueTypeDouble, this))
22 , _mgrsFact(new Fact(0, _mgrsFactName, FactMetaData::valueTypeString, this))
23 , _offsetEastFact(new Fact(0, _offsetEastFactName, FactMetaData::valueTypeDouble, this))
24 , _offsetNorthFact(new Fact(0, _offsetNorthFactName, FactMetaData::valueTypeDouble, this))
25 , _offsetUpFact(new Fact(0, _offsetUpFactName, FactMetaData::valueTypeDouble, this))
26 , _rotateDegreesCWFact(new Fact(0, _rotateDegreesCWFactName, FactMetaData::valueTypeDouble, this))
27{
28 // qCDebug(TransformPositionControllerLog) << Q_FUNC_INFO << this;
29
30 if (_metaDataMap.isEmpty()) {
31 _metaDataMap = FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/TransformPositionController.FactMetaData.json"), nullptr /* QObject parent */);
32 }
33
34 _latitudeFact->setMetaData(_metaDataMap[_latitudeFactName]);
35 _longitudeFact->setMetaData(_metaDataMap[_longitudeFactName]);
36 _zoneFact->setMetaData(_metaDataMap[_zoneFactName]);
37 _hemisphereFact->setMetaData(_metaDataMap[_hemisphereFactName]);
38 _eastingFact->setMetaData(_metaDataMap[_eastingFactName]);
39 _northingFact->setMetaData(_metaDataMap[_northingFactName]);
40 _mgrsFact->setMetaData(_metaDataMap[_mgrsFactName]);
41 _offsetEastFact->setMetaData(_metaDataMap[_offsetEastFactName]);
42 _offsetNorthFact->setMetaData(_metaDataMap[_offsetNorthFactName]);
43 _offsetUpFact->setMetaData(_metaDataMap[_offsetUpFactName]);
44 _rotateDegreesCWFact->setMetaData(_metaDataMap[_rotateDegreesCWFactName]);
45}
46
47TransformPositionController::~TransformPositionController()
48{
49 // qCDebug(TransformPositionControllerLog) << Q_FUNC_INFO << this;
50}
51
52void TransformPositionController::setCoordinate(QGeoCoordinate coordinate)
53{
54 if (!coordinate.isValid()) {
55 qCWarning(TransformPositionControllerLog) << "Attempt to set invalid coordinate";
56 return;
57 }
58
59 if (coordinate == _coordinate) {
60 return;
61 }
62
63 const bool wasInvalid = !_coordinate.isValid();
64 _coordinate = coordinate;
65
66 // Do not emit on the initial invalid->valid transition to prevent
67 // onCoordinateChanged handlers from firing on initial dialog setup.
68 if (!wasInvalid) {
69 emit coordinateChanged(_coordinate);
70 }
71}
72
73void TransformPositionController::initValues()
74{
75 if (!_coordinate.isValid()) {
76 return;
77 }
78
79 _latitudeFact->setRawValue(_coordinate.latitude());
80 _longitudeFact->setRawValue(_coordinate.longitude());
81
82 double easting, northing;
83 const int zone = QGCGeo::convertGeoToUTM(_coordinate, easting, northing);
84 if ((zone >= 1) && (zone <= 60)) {
85 _zoneFact->setRawValue(zone);
86 _hemisphereFact->setRawValue(_coordinate.latitude() < 0);
87 _eastingFact->setRawValue(easting);
88 _northingFact->setRawValue(northing);
89 }
90
91 const QString mgrs = QGCGeo::convertGeoToMGRS(_coordinate);
92 if (!mgrs.isEmpty()) {
93 _mgrsFact->setRawValue(mgrs);
94 }
95}
96
97void TransformPositionController::setFromGeo()
98{
99 QGeoCoordinate newCoordinate = _coordinate;
100 newCoordinate.setLatitude(_latitudeFact->rawValue().toDouble());
101 newCoordinate.setLongitude(_longitudeFact->rawValue().toDouble());
102 setCoordinate(newCoordinate);
103}
104
105void TransformPositionController::setFromUTM()
106{
107 qCDebug(TransformPositionControllerLog) << _eastingFact->rawValue().toDouble() << _northingFact->rawValue().toDouble() << _zoneFact->rawValue().toInt() << (_hemisphereFact->rawValue().toInt() == 1);
108 QGeoCoordinate newCoordinate;
109 if (QGCGeo::convertUTMToGeo(_eastingFact->rawValue().toDouble(), _northingFact->rawValue().toDouble(), _zoneFact->rawValue().toInt(), _hemisphereFact->rawValue().toInt() == 1, newCoordinate)) {
110 qCDebug(TransformPositionControllerLog) << _eastingFact->rawValue().toDouble() << _northingFact->rawValue().toDouble() << _zoneFact->rawValue().toInt() << (_hemisphereFact->rawValue().toInt() == 1) << newCoordinate;
111 setCoordinate(newCoordinate);
112 } else {
113 initValues();
114 }
115}
116
117void TransformPositionController::setFromMGRS()
118{
119 QGeoCoordinate newCoordinate;
120 if (QGCGeo::convertMGRSToGeo(_mgrsFact->rawValue().toString(), newCoordinate)) {
121 setCoordinate(newCoordinate);
122 } else {
123 initValues();
124 }
125}
126
127void TransformPositionController::setFromVehicle()
128{
129 Vehicle* activeVehicle = MultiVehicleManager::instance()->activeVehicle();
130 if (!activeVehicle) {
131 qCWarning(TransformPositionControllerLog) << "Cannot set coordinate from vehicle: no active vehicle";
132 return;
133 }
134
135 setCoordinate(activeVehicle->coordinate());
136}
Geographic coordinate conversion utilities using GeographicLib.
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static QMap< QString, FactMetaData * > createMapFromJsonFile(const QString &jsonFilename, QObject *metaDataParent)
A Fact is used to hold a single value within the system.
Definition Fact.h:16
void coordinateChanged(QGeoCoordinate coordinate)
QGeoCoordinate coordinate()
Definition Vehicle.h:410
QString convertGeoToMGRS(const QGeoCoordinate &coord)
Definition QGCGeo.cc:179
int convertGeoToUTM(const QGeoCoordinate &coord, double &easting, double &northing)
Definition QGCGeo.cc:148
bool convertUTMToGeo(double easting, double northing, int zone, bool southhemi, QGeoCoordinate &coord)
Definition QGCGeo.cc:161
bool convertMGRSToGeo(const QString &mgrs, QGeoCoordinate &coord)
Definition QGCGeo.cc:205