QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
RallyPointController.cc
Go to the documentation of this file.
2#include "RallyPoint.h"
3#include "Vehicle.h"
4#include "GeoJsonHelper.h"
5#include "JsonParsing.h"
6#include "SettingsManager.h"
7#include "AppSettings.h"
9#include "RallyPointManager.h"
10#include "Vehicle.h"
11#include "QGCLoggingCategory.h"
12
13#include <QtCore/QJsonArray>
14
15QGC_LOGGING_CATEGORY(RallyPointControllerLog, "PlanManager.RallyPointController")
16
19 , _managerVehicle (masterController->managerVehicle())
20 , _rallyPointManager (masterController->managerVehicle()->rallyPointManager())
21{
23 connect(&_points, &QmlObjectListModel::dirtyChanged, this, &RallyPointController::_pointsDirtyChanged);
24}
25
30
32{
33 qCDebug(RallyPointControllerLog) << "start flyView" << flyView;
34
35 _managerVehicleChanged(_masterController->managerVehicle());
36 connect(_masterController, &PlanMasterController::managerVehicleChanged, this, &RallyPointController::_managerVehicleChanged);
37
39}
40
41void RallyPointController::_managerVehicleChanged(Vehicle* managerVehicle)
42{
43 if (_managerVehicle) {
44 _rallyPointManager->disconnect(this);
45 _managerVehicle->disconnect(this);
46 _managerVehicle = nullptr;
47 _rallyPointManager = nullptr;
48 }
49
50 _managerVehicle = managerVehicle;
51 if (!_managerVehicle) {
52 qWarning() << "RallyPointController::managerVehicleChanged managerVehicle=NULL";
53 return;
54 }
55
56 _rallyPointManager = _managerVehicle->rallyPointManager();
57 connect(_rallyPointManager, &RallyPointManager::loadComplete, this, &RallyPointController::_managerLoadComplete);
58 connect(_rallyPointManager, &RallyPointManager::sendComplete, this, &RallyPointController::_managerSendComplete);
59 connect(_rallyPointManager, &RallyPointManager::removeAllComplete, this, &RallyPointController::_managerRemoveAllComplete);
61
62 (void) connect(_managerVehicle, &Vehicle::capabilityBitsChanged, this, [this](uint64_t capabilityBits) {
63 Q_UNUSED(capabilityBits);
65 });
66
68}
69
70bool RallyPointController::load(const QJsonObject& json, QString& errorString)
71{
72 removeAll();
73
74 errorString.clear();
75
76 if (json.contains(JsonParsing::jsonVersionKey) && json[JsonParsing::jsonVersionKey].toInt() == 1) {
77 // We just ignore old version 1 data
78 return true;
79 }
80
81 QList<JsonParsing::KeyValidateInfo> keyInfoList = {
82 { JsonParsing::jsonVersionKey, QJsonValue::Double, true },
83 { _jsonPointsKey, QJsonValue::Array, true },
84 };
85 if (!JsonParsing::validateKeys(json, keyInfoList, errorString)) {
86 return false;
87 }
88
89 QString errorStr;
90 QString errorMessage = tr("Rally: %1");
91
92 const int version = json[JsonParsing::jsonVersionKey].toInt();
93 if (version < _jsonOldestSupportedVersion || version > _jsonCurrentVersion) {
94 errorString =
95 tr("Rally Points supports versions %1 to %2").arg(_jsonOldestSupportedVersion).arg(_jsonCurrentVersion);
96 return false;
97 }
98
99 // Version 2 points are [lat, lon, alt] arrays with no frame, their altitudes are relative to home
100 auto loadPoint = [version](const QJsonValue& jsonPoint, QGeoCoordinate& coordinate,
101 QGroundControlQmlGlobal::AltitudeFrame& altitudeFrame, QString& error) {
103 if (version < 3) {
104 return GeoJsonHelper::loadGeoCoordinate(jsonPoint, true /* altitudeRequired */, coordinate, error);
105 }
106
107 if (!jsonPoint.isObject()) {
108 error = tr("Each point must be an object");
109 return false;
110 }
111 const QJsonObject jsonPointObject = jsonPoint.toObject();
112 const QList<JsonParsing::KeyValidateInfo> pointKeyInfoList = {
113 {_jsonCoordinateKey, QJsonValue::Array, true},
114 {_jsonFrameKey, QJsonValue::Double, true},
115 };
116 if (!JsonParsing::validateKeys(jsonPointObject, pointKeyInfoList, error) ||
117 !GeoJsonHelper::loadGeoCoordinate(jsonPointObject[_jsonCoordinateKey], true /* altitudeRequired */,
118 coordinate, error)) {
119 return false;
120 }
121
122 const int frame = jsonPointObject[_jsonFrameKey].toInt(-1);
123 if (frame != MAV_FRAME_GLOBAL && frame != MAV_FRAME_GLOBAL_RELATIVE_ALT &&
124 frame != MAV_FRAME_GLOBAL_TERRAIN_ALT) {
125 error = tr("Unsupported frame %1").arg(frame);
126 return false;
127 }
128 altitudeFrame = RallyPoint::altitudeFrameFromMavFrame(static_cast<MAV_FRAME>(frame));
129 return true;
130 };
131
132 QObjectList pointList;
133 const QJsonArray jsonPoints = json[_jsonPointsKey].toArray();
134 for (const QJsonValue& jsonPoint : jsonPoints) {
135 QGeoCoordinate coordinate;
137 if (!loadPoint(jsonPoint, coordinate, altitudeFrame, errorStr)) {
138 qDeleteAll(pointList);
139 errorString = errorMessage.arg(errorStr);
140 return false;
141 }
142 pointList.append(new RallyPoint(coordinate, altitudeFrame, this));
143 }
144 _points.swapObjectList(pointList);
145
146 setDirty(false);
147 _setFirstPointCurrent();
148
149 return true;
150}
151
152void RallyPointController::save(QJsonObject& json)
153{
154 json[JsonParsing::jsonVersionKey] = _jsonCurrentVersion;
155
156 QJsonArray rgPoints;
157 for (int i=0; i<_points.count(); i++) {
158 const RallyPoint* rallyPoint = qobject_cast<RallyPoint*>(_points[i]);
159 QJsonValue jsonCoordinate;
160 GeoJsonHelper::saveGeoCoordinate(rallyPoint->coordinate(), true /* writeAltitude */, jsonCoordinate);
161 QJsonObject jsonPoint;
162 jsonPoint[_jsonCoordinateKey] = jsonCoordinate;
163 jsonPoint[_jsonFrameKey] = static_cast<int>(RallyPoint::mavFrame(rallyPoint->altitudeFrame()));
164 rgPoints.append(jsonPoint);
165 }
166 json[_jsonPointsKey] = QJsonValue(rgPoints);
167}
168
170{
171 _points.clearAndDeleteContents();
172 setDirty(true);
173 setCurrentRallyPoint(nullptr);
174}
175
177{
178 if (_masterController->offline()) {
179 qCCritical(RallyPointControllerLog) << "RallyPointController::removeAllFromVehicle called while offline";
180 } else if (syncInProgress()) {
181 qCCritical(RallyPointControllerLog) << "RallyPointController::removeAllFromVehicle called while syncInProgress";
182 } else {
183 _rallyPointManager->removeAll();
184 }
185}
186
188{
189 if (_masterController->offline()) {
190 qCCritical(RallyPointControllerLog) << "RallyPointController::loadFromVehicle called while offline";
191 } else if (syncInProgress()) {
192 qCCritical(RallyPointControllerLog) << "RallyPointController::loadFromVehicle called while syncInProgress";
193 } else {
194 _itemsRequested = true;
195 _rallyPointManager->loadFromVehicle();
196 }
197}
198
200{
201 if (_masterController->offline()) {
202 qCCritical(RallyPointControllerLog) << "RallyPointController::sendToVehicle called while offline";
203 } else if (syncInProgress()) {
204 qCCritical(RallyPointControllerLog) << "RallyPointController::sendToVehicle called while syncInProgress";
205 } else {
206 qCDebug(RallyPointControllerLog) << "RallyPointController::sendToVehicle";
207 setDirty(false);
208 QList<RallyPointData> rgPoints;
209 for (int i=0; i<_points.count(); i++) {
210 const RallyPoint* rallyPoint = qobject_cast<RallyPoint*>(_points[i]);
211 rgPoints.append({rallyPoint->coordinate(), RallyPoint::mavFrame(rallyPoint->altitudeFrame())});
212 }
213 _rallyPointManager->sendToVehicle(rgPoints);
214 }
215}
216
218{
219 return _rallyPointManager->inProgress();
220}
221
223{
224 if (dirty != _dirty) {
225 _dirty = dirty;
226 if (!dirty) {
227 _points.setDirty(false);
228 }
229 emit dirtyChanged(dirty);
230 }
231}
232
233void RallyPointController::_pointsDirtyChanged(bool dirty)
234{
235 if (dirty) {
236 setDirty(true);
237 }
238}
239
241{
242 return _rallyPointManager->editorQml();
243}
244
245void RallyPointController::_managerLoadComplete(void)
246{
247 // Fly view always reloads on _loadComplete
248 // Plan view only reloads if:
249 // - Load was specifically requested
250 // - There is no current Plan
251 if (_flyView || _itemsRequested || isEmpty()) {
252 _points.clearAndDeleteContents();
253 QObjectList pointList;
254 for (const RallyPointData& pointData : _rallyPointManager->points()) {
255 pointList.append(
256 new RallyPoint(pointData.coordinate, RallyPoint::altitudeFrameFromMavFrame(pointData.frame), this));
257 }
258 _points.swapObjectList(pointList);
259 setDirty(false);
260 _setFirstPointCurrent();
261 emit loadComplete();
262 }
263 _itemsRequested = false;
264}
265
266void RallyPointController::_managerSendComplete(bool error)
267{
268 // Fly view always reloads after send
269 if (!error && _flyView) {
271 }
272}
273
274void RallyPointController::_managerRemoveAllComplete(bool error)
275{
276 if (!error) {
277 // Remove all from vehicle so we always update
279 }
280}
281
282void RallyPointController::addPoint(QGeoCoordinate point)
283{
284 Vehicle* const vehicle = _masterController->controllerVehicle();
285 const bool px4 = vehicle->px4Firmware();
286
287 double defaultAlt;
289 if (px4) {
290 // The altitude is the ground height at the point, it is seeded from terrain below
292 } else if (_points.count()) {
293 const RallyPoint* lastPoint = qobject_cast<RallyPoint*>(_points[_points.count() - 1]);
294 defaultAlt = lastPoint->coordinate().altitude();
295 altitudeFrame = lastPoint->altitudeFrame();
296 } else if (vehicle->apmFirmware() || vehicle->fixedWing()) {
297 defaultAlt = SettingsManager::instance()->appSettings()->defaultMissionItemAltitude()->rawValue().toDouble();
298 } else {
300 }
301 point.setAltitude(defaultAlt);
302 RallyPoint* newPoint = new RallyPoint(point, altitudeFrame, this);
303 _points.append(newPoint);
304 setCurrentRallyPoint(newPoint);
305 setDirty(true);
306
307 if (px4) {
308 // Stays relative with the default altitude if terrain data is not available
309 newPoint->useTerrainAltitude();
310 }
311}
312
314{
315 for (int i = 0; i < _points.count(); i++) {
316 const RallyPoint* const rallyPoint = qobject_cast<const RallyPoint*>(_points[i]);
317 if (rallyPoint && rallyPoint->terrainQueryPending()) {
318 return true;
319 }
320 }
321
322 return false;
323}
324
326{
327 return _managerVehicle->capabilityBits() & MAV_PROTOCOL_CAPABILITY_MISSION_RALLY;
328}
329
330void RallyPointController::removePoint(QObject* rallyPoint)
331{
332 int foundIndex = 0;
333 for (foundIndex=0; foundIndex<_points.count(); foundIndex++) {
334 if (_points[foundIndex] == rallyPoint) {
335 _points.removeOne(rallyPoint);
336 rallyPoint->deleteLater();
337 }
338 }
339
340 if (_points.count()) {
341 int newIndex = qMin(foundIndex, _points.count() - 1);
342 newIndex = qMax(newIndex, 0);
343 setCurrentRallyPoint(_points[newIndex]);
344 } else {
345 setCurrentRallyPoint(nullptr);
346 }
347}
348
350{
351 if (_currentRallyPoint != rallyPoint) {
352 _currentRallyPoint = rallyPoint;
353 emit currentRallyPointChanged(rallyPoint);
354 }
355}
356
357void RallyPointController::_setFirstPointCurrent(void)
358{
359 setCurrentRallyPoint(_points.count() ? _points[0] : nullptr);
360}
361
363{
364 return _points.count() > 0;
365}
366
368{
369 qCDebug(RallyPointControllerLog) << "showPlanFromManagerVehicle _flyView" << _flyView;
370 if (_masterController->offline()) {
371 qCCritical(RallyPointControllerLog) << "RallyPointController::showPlanFromManagerVehicle called while offline";
372 return true; // stops further propagation of showPlanFromManagerVehicle due to error
373 } else {
374 if (!_managerVehicle->initialPlanRequestComplete()) {
375 // The vehicle hasn't completed initial load, we can just wait for loadComplete to be signalled automatically
376 qCDebug(RallyPointControllerLog) << "showPlanFromManagerVehicle: !initialPlanRequestComplete, wait for signal";
377 return true;
378 } else if (syncInProgress()) {
379 // If the sync is already in progress, _loadComplete will be called automatically when it is done. So no need to do anything.
380 qCDebug(RallyPointControllerLog) << "showPlanFromManagerVehicle: syncInProgress wait for signal";
381 return true;
382 } else {
383 qCDebug(RallyPointControllerLog) << "showPlanFromManagerVehicle: sync complete";
384 _itemsRequested = true;
385 _managerLoadComplete();
386 return false;
387 }
388 }
389}
390
392{
393 return _points.count() == 0;
394}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void dirtyChanged(bool dirty)
void countChanged(int count)
void supportedChanged(bool supported)
PlanElementController(PlanMasterController *masterController, QObject *parent=nullptr)
void syncInProgressChanged(bool syncInProgress)
PlanMasterController * masterController(void)
PlanMasterController * _masterController
virtual void start(bool flyView)
Should be called immediately upon Component.onCompleted.
void dirtyChanged(bool dirty)
Master controller for mission, fence, rally.
void managerVehicleChanged(Vehicle *managerVehicle)
int count() const override final
void clearAndDeleteContents() override final
Clears the list and calls deleteLater on each entry.
bool containsItems(void) const final
void save(QJsonObject &json) final
RallyPointController(PlanMasterController *masterController, QObject *parent=nullptr)
void loadComplete(void)
Q_INVOKABLE void addPoint(QGeoCoordinate point)
bool supported(void) const final
true: controller is waiting for the current load to complete
void setDirty(bool dirty) final
QString editorQml(void) const
bool showPlanFromManagerVehicle(void) final
bool syncInProgress(void) const final
void setCurrentRallyPoint(QObject *rallyPoint)
bool dirty(void) const final
void start(bool flyView) final
Should be called immediately upon Component.onCompleted.
void removeAllFromVehicle(void) final
bool load(const QJsonObject &json, QString &errorString) final
Q_INVOKABLE void removePoint(QObject *rallyPoint)
void removeAll(void) final
Removes all from controller only.
bool terrainQueryPending(void) const
true: a rally point is still waiting on terrain data for its altitude
void currentRallyPointChanged(QObject *rallyPoint)
void loadFromVehicle(void) final
void inProgressChanged(bool inProgress)
void loadComplete(void)
void sendComplete(bool error)
QList< RallyPointData > points(void) const
void removeAllComplete(bool error)
This class is used to encapsulate the QGeoCoordinate associated with a Rally Point into a QObject suc...
Definition RallyPoint.h:21
QGeoCoordinate coordinate(void) const
static double getDefaultFactAltitude()
static QGroundControlQmlGlobal::AltitudeFrame altitudeFrameFromMavFrame(MAV_FRAME mavFrame)
bool terrainQueryPending(void) const
true: a terrain answer is on its way and will change the altitude. A failed query is not pending.
Definition RallyPoint.h:60
Q_INVOKABLE void useTerrainAltitude(bool switchToAbsoluteOnFailure=false)
QGroundControlQmlGlobal::AltitudeFrame altitudeFrame(void) const
Definition RallyPoint.h:53
static MAV_FRAME mavFrame(QGroundControlQmlGlobal::AltitudeFrame altitudeFrame)
static SettingsManager * instance()
AppSettings * appSettings() const
bool px4Firmware() const
Definition Vehicle.h:515
void capabilityBitsChanged(uint64_t capabilityBits)
bool apmFirmware() const
Definition Vehicle.h:516
RallyPointManager * rallyPointManager()
Definition Vehicle.h:599
bool fixedWing() const
Definition Vehicle.cc:1783
bool loadGeoCoordinate(const QJsonValue &jsonValue, bool altitudeRequired, QGeoCoordinate &coordinate, QString &errorString)
void saveGeoCoordinate(const QGeoCoordinate &coordinate, bool writeAltitude, QJsonValue &jsonValue)
Saves a QGeoCoordinate as [lat, lon, alt] array (QGC plan format).
bool validateKeys(const QJsonObject &jsonObject, const QList< KeyValidateInfo > &keyInfo, QString &errorString)
Validates that all required keys are present and that listed keys have the expected type.
constexpr const char * jsonVersionKey
Definition JsonParsing.h:14
A rally point as exchanged with the vehicle.