QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
StructureScanComplexItem.cc
Go to the documentation of this file.
2
3#include <cmath>
4
5#include <QtCore/QJsonArray>
6
7#include "AppMessages.h"
8#include "AppSettings.h"
9#include "FirmwarePlugin.h"
10#include "FlightPathSegment.h"
11#include "JsonParsing.h"
12#include "MissionController.h"
14#include "QGCApplication.h"
15#include "QGCLoggingCategory.h"
16#include "QGCMath.h"
17#include "SettingsManager.h"
18#include "Vehicle.h"
19
20QGC_LOGGING_CATEGORY(StructureScanComplexItemLog, "Plan.StructureScanComplexItem")
21
24 , _metaDataMap (FactMetaData::createMapFromJsonFile(QStringLiteral(":/json/StructureScan.SettingsGroup.json"), this /* QObject parent */))
25 , _sequenceNumber (0)
26 , _entryVertex (0)
27, _ignoreRecalc (false)
28 , _scanDistance (0.0)
29 , _cameraShots (0)
30 , _cameraCalc (masterController, settingsGroup)
31 , _scanBottomAltFact (settingsGroup, _metaDataMap[scanBottomAltName])
32 , _structureHeightFact (settingsGroup, _metaDataMap[structureHeightName])
33 , _layersFact (settingsGroup, _metaDataMap[layersName])
34 , _gimbalPitchFact (settingsGroup, _metaDataMap[gimbalPitchName])
35 , _startFromTopFact (settingsGroup, _metaDataMap[startFromTopName])
36 , _entranceAltFact (settingsGroup, _metaDataMap[_entranceAltName])
37{
38 _editorQml = "qrc:/qml/QGroundControl/PlanView/StructureScanEditor.qml";
39
40 _entranceAltFact.setRawValue(SettingsManager::instance()->appSettings()->defaultMissionItemAltitude()->rawValue());
41
42 connect(&_entranceAltFact, &Fact::rawValueChanged, this, &StructureScanComplexItem::_setDirty);
43 connect(&_scanBottomAltFact, &Fact::rawValueChanged, this, &StructureScanComplexItem::_setDirty);
44 connect(&_layersFact, &Fact::rawValueChanged, this, &StructureScanComplexItem::_setDirty);
45 connect(&_gimbalPitchFact, &Fact::rawValueChanged, this, &StructureScanComplexItem::_setDirty);
46 connect(&_startFromTopFact, &Fact::rawValueChanged, this, &StructureScanComplexItem::_setDirty);
47
48 connect(&_startFromTopFact, &Fact::valueChanged, this, &StructureScanComplexItem::_signalTopBottomAltChanged);
49 connect(&_layersFact, &Fact::valueChanged, this, &StructureScanComplexItem::_signalTopBottomAltChanged);
50
51 connect(&_structureHeightFact, &Fact::rawValueChanged, this, &StructureScanComplexItem::_recalcLayerInfo);
52 connect(&_scanBottomAltFact, &Fact::rawValueChanged, this, &StructureScanComplexItem::_recalcLayerInfo);
53 connect(_cameraCalc.adjustedFootprintFrontal(), &Fact::rawValueChanged, this, &StructureScanComplexItem::_recalcLayerInfo);
54
55 connect(&_entranceAltFact, &Fact::rawValueChanged, this, &StructureScanComplexItem::_updateCoordinateAltitudes);
56
57 connect(&_structurePolygon, &QGCMapPolygon::dirtyChanged, this, &StructureScanComplexItem::_polygonDirtyChanged);
58 connect(&_structurePolygon, &QGCMapPolygon::pathChanged, this, &StructureScanComplexItem::_rebuildFlightPolygon);
60 connect(&_structurePolygon, &QGCMapPolygon::isValidChanged, this, &StructureScanComplexItem::_updateWizardMode);
61 connect(&_structurePolygon, &QGCMapPolygon::traceModeChanged, this, &StructureScanComplexItem::_updateWizardMode);
62
63 connect(&_structurePolygon, &QGCMapPolygon::countChanged, this, &StructureScanComplexItem::_updateLastSequenceNumber);
64 connect(&_layersFact, &Fact::valueChanged, this, &StructureScanComplexItem::_updateLastSequenceNumber);
66 &StructureScanComplexItem::_updateLastSequenceNumber);
67
68 connect(&_flightPolygon, &QGCMapPolygon::pathChanged, this, &StructureScanComplexItem::_flightPathChanged);
69 connect(&_flightPolygon, &QGCMapPolygon::countChanged, this, &StructureScanComplexItem::_updateLastSequenceNumber);
71
72 connect(_cameraCalc.distanceToSurface(), &Fact::rawValueChanged, this, &StructureScanComplexItem::_rebuildFlightPolygon);
73
74 connect(&_flightPolygon, &QGCMapPolygon::pathChanged, this, &StructureScanComplexItem::_recalcCameraShots);
75 connect(_cameraCalc.adjustedFootprintSide(), &Fact::rawValueChanged, this, &StructureScanComplexItem::_recalcCameraShots);
76 connect(&_layersFact, &Fact::valueChanged, this, &StructureScanComplexItem::_recalcCameraShots);
77
78 connect(&_cameraCalc, &CameraCalc::isManualCameraChanged, this, &StructureScanComplexItem::_updateGimbalPitch);
79
80 connect(&_layersFact, &Fact::valueChanged, this, &StructureScanComplexItem::_recalcScanDistance);
81 connect(&_flightPolygon, &QGCMapPolygon::pathChanged, this, &StructureScanComplexItem::_recalcScanDistance);
82
84
87
88 connect(&_entranceAltFact, &Fact::valueChanged, this, &StructureScanComplexItem::_amslEntryAltChanged);
90
97
102 connect(_cameraCalc.adjustedFootprintFrontal(), &Fact::valueChanged, this, &StructureScanComplexItem::_updateFlightPathSegmentsSignal);
106
107 // The follow is used to compress multiple recalc calls in a row to into a single call.
108 connect(this, &StructureScanComplexItem::_updateFlightPathSegmentsSignal, this, &StructureScanComplexItem::_updateFlightPathSegmentsDontCallDirectly, Qt::QueuedConnection);
109 qgcApp()->addCompressedSignal(QMetaMethod::fromSignal(&StructureScanComplexItem::_updateFlightPathSegmentsSignal));
110
111 _recalcLayerInfo();
112
113 if (!kmlOrShpFile.isEmpty()) {
114 _structurePolygon.loadKMLOrSHPFile(kmlOrShpFile);
115 _structurePolygon.setDirty(false);
116 }
117
118 setDirty(false);
119}
120
121void StructureScanComplexItem::_setCameraShots(int cameraShots)
122{
123 if (_cameraShots != cameraShots) {
124 _cameraShots = cameraShots;
125 emit cameraShotsChanged(this->cameraShots());
126 }
127}
128
129void StructureScanComplexItem::_clearInternal(void)
130{
131 setDirty(true);
132
135}
136
137void StructureScanComplexItem::_updateLastSequenceNumber(void)
138{
140}
141
143{
144 const bool wpNextOffsetROI = _wpNextOffsetROISupported();
145
146 // Each structure layer contains:
147 // 1 waypoint for each polygon vertex + 1 to go back to first polygon vertex for each layer
148 // Two commands for camera trigger start/stop
149 // Gimbal pitch and heading commands for each leg if ROI_WPNEXT_OFFSET is not supported
150 int layerItemCount = _flightPolygon.count() + 1 + 2 + (wpNextOffsetROI ? 0 : 2 * _flightPolygon.count());
151
152 // Take into account the number of layers
153 int multiLayerItemCount = layerItemCount * _layersFact.rawValue().toInt();
154
155 // +2 for pointing the camera at the structure and returning it to neutral
156 // +2 for entrance/exit waypoints
157 int itemCount = multiLayerItemCount + 2 + 2;
158
159 return _sequenceNumber + itemCount - 1;
160}
161
162bool StructureScanComplexItem::_wpNextOffsetROISupported(void) const
163{
164 // An empty list means every command is supported
165 const QList<MAV_CMD> supportedCommands =
167 return supportedCommands.isEmpty() || supportedCommands.contains(MAV_CMD_DO_SET_ROI_WPNEXT_OFFSET);
168}
169
170MissionItem* StructureScanComplexItem::_createGimbalPitchItem(int seqNum, double pitch,
171 QObject* missionItemParent) const
172{
173 return new MissionItem(seqNum, MAV_CMD_DO_MOUNT_CONTROL, MAV_FRAME_MISSION, pitch,
174 0, // Roll stays in standard orientation
175 0, // Yaw follows the vehicle heading
176 0, 0, 0, // param 4-6 not used
177 MAV_MOUNT_MODE_MAVLINK_TARGETING,
178 true, // autoContinue
179 false, // isCurrentItem
180 missionItemParent);
181}
182
183void StructureScanComplexItem::_appendLegCameraItems(QList<MissionItem*>& items, int& seqNum,
184 const QGeoCoordinate& legStart, const QGeoCoordinate& legEnd,
185 QObject* missionItemParent) const
186{
187 // ArduCopter resets the heading when it starts the next waypoint, which happens after it runs the first command
188 // following this waypoint, so the heading must not be that first command
189 items.append(_createGimbalPitchItem(seqNum++, _gimbalPitchFact.rawValue().toDouble(), missionItemParent));
190
191 // Face the structure, which is on the inside of the flight path, square on to the leg
192 const double structureSide = _flightPolygon.isClockwise() ? 90.0 : -90.0;
193 const double heading = std::fmod(legStart.azimuthTo(legEnd) + structureSide + 360.0, 360.0);
194 items.append(new MissionItem(seqNum++, MAV_CMD_CONDITION_YAW, MAV_FRAME_MISSION, heading,
195 0, // Default turn rate
196 0, // Shortest turn
197 0, // Absolute heading
198 0, 0, 0, // param 5-7 not used
199 true, // autoContinue
200 false, // isCurrentItem
201 missionItemParent));
202}
203
205{
206 if (_dirty != dirty) {
207 _dirty = dirty;
208 emit dirtyChanged(_dirty);
209 }
210}
211
212void StructureScanComplexItem::save(QJsonArray& missionItems)
213{
214 QJsonObject saveObject;
215
216 // Header
217 saveObject[JsonParsing::jsonVersionKey] = 3;
220
221 saveObject[_entranceAltName] = _entranceAltFact.rawValue().toDouble();
222 saveObject[scanBottomAltName] = _scanBottomAltFact.rawValue().toDouble();
223 saveObject[structureHeightName] = _structureHeightFact.rawValue().toDouble();
224 saveObject[layersName] = _layersFact.rawValue().toDouble();
225 saveObject[gimbalPitchName] = _gimbalPitchFact.rawValue().toDouble();
226 saveObject[startFromTopName] = _startFromTopFact.rawValue().toBool();
227
228 QJsonObject cameraCalcObject;
229 _cameraCalc.save(cameraCalcObject);
230 saveObject[_jsonCameraCalcKey] = cameraCalcObject;
231
232 _structurePolygon.saveToJson(saveObject);
233
234 missionItems.append(saveObject);
235}
236
245
246bool StructureScanComplexItem::load(const QJsonObject& complexObject, int sequenceNumber, QString& errorString)
247{
248 QList<JsonParsing::KeyValidateInfo> keyInfoList = {
249 { JsonParsing::jsonVersionKey, QJsonValue::Double, true },
250 { VisualMissionItem::jsonTypeKey, QJsonValue::String, true },
251 { ComplexMissionItem::jsonComplexItemTypeKey, QJsonValue::String, true },
252 { QGCMapPolygon::jsonPolygonKey, QJsonValue::Array, true },
253 { scanBottomAltName, QJsonValue::Double, true },
254 { structureHeightName, QJsonValue::Double, true },
255 { layersName, QJsonValue::Double, true },
256 { _jsonCameraCalcKey, QJsonValue::Object, true },
257 { _entranceAltName, QJsonValue::Double, true },
258 { gimbalPitchName, QJsonValue::Double, true },
259 { startFromTopName, QJsonValue::Bool, true },
260 };
261 if (!JsonParsing::validateKeys(complexObject, keyInfoList, errorString)) {
262 return false;
263 }
264
265 _structurePolygon.clear();
266
267 QString itemType = complexObject[VisualMissionItem::jsonTypeKey].toString();
268 QString complexType = complexObject[ComplexMissionItem::jsonComplexItemTypeKey].toString();
270 errorString = tr("%1 does not support loading this complex mission item type: %2:%3").arg(qgcApp()->applicationName()).arg(itemType).arg(complexType);
271 return false;
272 }
273
274 int version = complexObject[JsonParsing::jsonVersionKey].toInt();
275 if (version != 3) {
276 errorString = tr("%1 version %2 not supported").arg(jsonComplexItemTypeValue).arg(version);
277 return false;
278 }
279
281
282 // Load CameraCalc first since it will trigger camera name change which will trounce gimbal angles
283 if (!_cameraCalc.load(complexObject[_jsonCameraCalcKey].toObject(), false /* v1FollowTerrain */, errorString, false /* forPresets */)) {
284 return false;
285 }
286
287 _scanBottomAltFact.setRawValue (complexObject[scanBottomAltName].toDouble());
288 _layersFact.setRawValue (complexObject[layersName].toDouble());
289 _structureHeightFact.setRawValue (complexObject[structureHeightName].toDouble());
290 _startFromTopFact.setRawValue (complexObject[startFromTopName].toBool());
291 _entranceAltFact.setRawValue (complexObject[_entranceAltName].toDouble());
292 _gimbalPitchFact.setRawValue (complexObject[gimbalPitchName].toDouble());
293
294 if (!_structurePolygon.loadFromJson(complexObject, true /* required */, errorString)) {
295 _structurePolygon.clear();
296 return false;
297 }
298
299 return true;
300}
301
302void StructureScanComplexItem::_flightPathChanged(void)
303{
304 // Calc bounding cubetopFlightAlt
305 double north = 0.0;
306 double south = 180.0;
307 double east = 0.0;
308 double west = 360.0;
309 double bottom = 100000.;
310 double top = 0.;
311 QList<QGeoCoordinate> vertices = _flightPolygon.coordinateList();
312 for (int i = 0; i < vertices.count(); i++) {
313 QGeoCoordinate vertex = vertices[i];
314 double lat = vertex.latitude() + 90.0;
315 double lon = vertex.longitude() + 180.0;
316 north = fmax(north, lat);
317 south = fmin(south, lat);
318 east = fmax(east, lon);
319 west = fmin(west, lon);
320 bottom = fmin(bottom, vertex.altitude());
321 top = fmax(top, vertex.altitude());
322 }
323 //-- Update bounding cube for airspace management control
324 _setBoundingCube(QGCGeoBoundingCube(
325 QGeoCoordinate(north - 90.0, west - 180.0, bottom),
326 QGeoCoordinate(south - 90.0, east - 180.0, top)));
327
330
331 if (_isIncomplete) {
332 _isIncomplete = false;
333 emit isIncompleteChanged();
334 }
335}
336
337double StructureScanComplexItem::greatestDistanceTo(const QGeoCoordinate &other) const
338{
339 double greatestDistance = 0.0;
340 QList<QGeoCoordinate> vertices = _flightPolygon.coordinateList();
341
342 for (int i=0; i<vertices.count(); i++) {
343 QGeoCoordinate vertex = vertices[i];
344 double distance = vertex.distanceTo(other);
345 if (distance > greatestDistance) {
346 greatestDistance = distance;
347 }
348 }
349
350 return greatestDistance;
351}
352
353void StructureScanComplexItem::appendMissionItems(QList<MissionItem*>& items, QObject* missionItemParent)
354{
355 int seqNum = _sequenceNumber;
356 bool startFromTop = _startFromTopFact.rawValue().toBool();
357 double startAltitude = (startFromTop ? _structureHeightFact.rawValue().toDouble() : _scanBottomAltFact.rawValue().toDouble());
358 const bool wpNextOffsetROI = _wpNextOffsetROISupported();
359
360 MissionItem* item = nullptr;
361
362 // Entrance waypoint
363 QGeoCoordinate entranceExitCoord = _flightPolygon.vertexCoordinate(_entryVertex);
364 item = new MissionItem(seqNum++,
365 MAV_CMD_NAV_WAYPOINT,
366 MAV_FRAME_GLOBAL_RELATIVE_ALT,
367 0, // No hold time
368 0.0, // No acceptance radius specified
369 0.0, // Pass through waypoint
370 std::numeric_limits<double>::quiet_NaN(), // Yaw unchanged
371 entranceExitCoord.latitude(),
372 entranceExitCoord.longitude(),
373 _entranceAltFact.rawValue().toDouble(),
374 true, // autoContinue
375 false, // isCurrentItem
376 missionItemParent);
377 items.append(item);
378
379 // Point camera at structure. Without ROI_WPNEXT_OFFSET only the gimbal pitch is set here, and each leg
380 // sets the heading.
381 if (wpNextOffsetROI) {
382 item = new MissionItem(seqNum++, MAV_CMD_DO_SET_ROI_WPNEXT_OFFSET, MAV_FRAME_MISSION, 0, 0, 0,
383 0, // param 1-4 not used
384 _gimbalPitchFact.rawValue().toDouble(),
385 0, // Roll stays in standard orientation
386 90, // 90 degree yaw offset to point to structure
387 true, // autoContinue
388 false, // isCurrentItem
389 missionItemParent);
390 items.append(item);
391 } else {
392 items.append(_createGimbalPitchItem(seqNum++, _gimbalPitchFact.rawValue().toDouble(), missionItemParent));
393 }
394
395 // Set up for the first layer
396 double layerAltitude = startAltitude;
397 double halfLayerHeight = _cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble() / 2.0;
398 if (startFromTop) {
399 layerAltitude -= halfLayerHeight;
400 } else {
401 layerAltitude += halfLayerHeight;
402 }
403
404 for (int layer=0; layer<_layersFact.rawValue().toInt(); layer++) {
405 bool addTriggerStart = true;
406
407 bool done = false;
408 int currentVertex = _entryVertex;
409 int processedVertices = 0;
410 do {
411 QGeoCoordinate vertexCoord = _flightPolygon.vertexCoordinate(currentVertex);
412
413 item = new MissionItem(seqNum++,
414 MAV_CMD_NAV_WAYPOINT,
415 MAV_FRAME_GLOBAL_RELATIVE_ALT,
416 0, // No hold time
417 0.0, // No acceptance radius specified
418 0.0, // Pass through waypoint
419 std::numeric_limits<double>::quiet_NaN(), // Yaw unchanged
420 vertexCoord.latitude(),
421 vertexCoord.longitude(),
422 layerAltitude,
423 true, // autoContinue
424 false, // isCurrentItem
425 missionItemParent);
426 items.append(item);
427
428 // Start camera triggering after first waypoint in layer
429 if (addTriggerStart) {
430 addTriggerStart = false;
431 item = new MissionItem(seqNum++,
432 MAV_CMD_DO_SET_CAM_TRIGG_DIST,
433 MAV_FRAME_MISSION,
434 _cameraCalc.adjustedFootprintSide()->rawValue().toDouble(), // trigger distance
435 0, // shutter integration (ignore)
436 1, // trigger immediately when starting
437 0, 0, 0, 0, // param 4-7 unused
438 true, // autoContinue
439 false, // isCurrentItem
440 missionItemParent);
441 items.append(item);
442 }
443
444 // Move to next vertext
445 currentVertex++;
446 if (currentVertex >= _flightPolygon.count()) {
447 currentVertex = 0;
448 }
449
450 // Have we processed all the vertices
451 processedVertices++;
452 done = processedVertices == _flightPolygon.count() + 1;
453
454 if (!wpNextOffsetROI && !done) {
455 _appendLegCameraItems(items, seqNum, vertexCoord, _flightPolygon.vertexCoordinate(currentVertex),
456 missionItemParent);
457 }
458 } while (!done);
459
460 // Stop camera triggering after last waypoint in layer
461 item = new MissionItem(seqNum++,
462 MAV_CMD_DO_SET_CAM_TRIGG_DIST,
463 MAV_FRAME_MISSION,
464 0, // stop triggering
465 0, // shutter integration (ignore)
466 0, // trigger immediately when starting
467 0, 0, 0, 0, // param 4-7 unused
468 true, // autoContinue
469 false, // isCurrentItem
470 missionItemParent);
471 items.append(item);
472
473 // Move to next layer altitude
474 if (startFromTop) {
475 layerAltitude -= halfLayerHeight * 2;
476 } else {
477 layerAltitude += halfLayerHeight * 2;
478 }
479 }
480
481 // Return camera to neutral position
482 if (wpNextOffsetROI) {
483 // ROI_NONE takes no parameters
484 item = new MissionItem(seqNum++, MAV_CMD_DO_SET_ROI_NONE, MAV_FRAME_MISSION, 0, 0, 0, 0, 0, 0, 0,
485 true, // autoContinue
486 false, // isCurrentItem
487 missionItemParent);
488 } else {
489 item = _createGimbalPitchItem(seqNum++, 0, missionItemParent);
490 }
491 items.append(item);
492
493 // Exit waypoint
494 item = new MissionItem(seqNum++,
495 MAV_CMD_NAV_WAYPOINT,
496 MAV_FRAME_GLOBAL_RELATIVE_ALT,
497 0, // No hold time
498 0.0, // No acceptance radius specified
499 0.0, // Pass through waypoint
500 std::numeric_limits<double>::quiet_NaN(), // Yaw unchanged
501 entranceExitCoord.latitude(),
502 entranceExitCoord.longitude(),
503 _entranceAltFact.rawValue().toDouble(),
504 true, // autoContinue
505 false, // isCurrentItem
506 missionItemParent);
507 items.append(item);
508
509}
510
512{
513 return _cameraShots;
514}
515
517{
519 if (!QGC::fuzzyCompare(_vehicleSpeed, missionFlightStatus.vehicleSpeed)) {
520 _vehicleSpeed = missionFlightStatus.vehicleSpeed;
522 }
523}
524
525void StructureScanComplexItem::_setDirty(void)
526{
527 setDirty(true);
528}
529
531{
532 _entranceAltFact.setRawValue(newAltitude);
533}
534
536{
537 // Entrance altitude is always relative to home
539}
540
542{
543 const QGeoCoordinate oldCoordinate = this->coordinate();
544 if (!oldCoordinate.isValid() || !coordinate.isValid() || _structurePolygon.count() < 3) {
545 return;
546 }
547
548 const double distanceMeters = oldCoordinate.distanceTo(coordinate);
549 const double azimuthDegrees = oldCoordinate.azimuthTo(coordinate);
550 const QList<QGeoCoordinate> vertices = _structurePolygon.coordinateList();
551
552 QList<QGeoCoordinate> translatedVertices;
553 translatedVertices.reserve(vertices.count());
554 for (const QGeoCoordinate& vertex: vertices) {
555 translatedVertices.append(vertex.atDistanceAndAzimuth(distanceMeters, azimuthDegrees));
556 }
557
558 _structurePolygon.setPath(translatedVertices);
559}
560
561void StructureScanComplexItem::_polygonDirtyChanged(bool dirty)
562{
563 if (dirty) {
564 setDirty(true);
565 }
566}
567
569{
570 return _vehicleSpeed == 0 ? 0 : _cameraCalc.adjustedFootprintSide()->rawValue().toDouble() / _vehicleSpeed;
571}
572
573QGeoCoordinate StructureScanComplexItem::coordinate(void) const
574{
575 if (_flightPolygon.count() > 0) {
576 QGeoCoordinate coord = _flightPolygon.vertexCoordinate(_entryVertex);
577 return coord;
578 } else {
579 return QGeoCoordinate();
580 }
581}
582
583void StructureScanComplexItem::_updateCoordinateAltitudes(void)
584{
586}
587
589{
590 _entryVertex++;
591 if (_entryVertex >= _flightPolygon.count()) {
592 _entryVertex = 0;
593 }
595}
596
597void StructureScanComplexItem::_rebuildFlightPolygon(void)
598{
599 // While this is happening all hell breaks loose signal-wise which can cause a bad vertex reference.
600 // So we reset to a safe value first and then double check validity when putting it back
601 int savedEntryVertex = _entryVertex;
602 _entryVertex = 0;
603
604 _flightPolygon = _structurePolygon;
605 const bool offsetSucceeded = _flightPolygon.offset(_cameraCalc.distanceToSurface()->rawValue().toDouble());
606 if (!offsetSucceeded) {
607 // Never leave the structure outline itself as the flight path
608 _flightPolygon.clear();
609 }
610
611 const bool offsetFailed = !offsetSucceeded && _structurePolygon.isValid();
612 if (offsetFailed && !_flightPolygonOffsetFailed) {
614 tr("Structure Scan: Unable to calculate a flight path at the specified distance from the structure."));
615 }
616 _flightPolygonOffsetFailed = offsetFailed;
617
618 if (savedEntryVertex >= _flightPolygon.count()) {
619 _entryVertex = 0;
620 } else {
621 _entryVertex = savedEntryVertex;
622 }
623
625}
626
627void StructureScanComplexItem::_recalcCameraShots(void)
628{
629 double triggerDistance = _cameraCalc.adjustedFootprintSide()->rawValue().toDouble();
630 if (triggerDistance == 0) {
631 _setCameraShots(0);
632 return;
633 }
634
635 if (_flightPolygon.count() < 3) {
636 _setCameraShots(0);
637 return;
638 }
639
640 // Determine the distance for each polygon traverse
641 double distance = 0;
642 for (int i=0; i<_flightPolygon.count(); i++) {
643 QGeoCoordinate coord1 = _flightPolygon.vertexCoordinate(i);
644 QGeoCoordinate coord2 = _flightPolygon.vertexCoordinate(i + 1 == _flightPolygon.count() ? 0 : i + 1);
645 distance += coord1.distanceTo(coord2);
646 }
647 if (distance == 0.0) {
648 _setCameraShots(0);
649 return;
650 }
651
652 int cameraShots = static_cast<int>(distance / triggerDistance);
653 _setCameraShots(cameraShots * _layersFact.rawValue().toInt());
654}
655
656void StructureScanComplexItem::_recalcLayerInfo(void)
657{
658 double surfaceHeight = qMax(_structureHeightFact.rawValue().toDouble() - _scanBottomAltFact.rawValue().toDouble(), 0.0);
659
660 // Layer count is calculated from surface and layer heights
661 _layersFact.setRawValue(qMax(qCeil(surfaceHeight / _cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble()), 1));
662}
663
664void StructureScanComplexItem::_updateGimbalPitch(void)
665{
666 if (!_cameraCalc.isManualCamera()) {
667 _gimbalPitchFact.setRawValue(0);
668 }
669}
670
672{
673 if (_startFromTopFact.rawValue().toBool()) {
674 // Structure Height minus the topmost layers
675 double layerIncrement = (_cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble() / 2.0) + ((_layersFact.rawValue().toInt() - 1) * _cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble());
676 return _structureHeightFact.rawValue().toDouble() - layerIncrement;
677 } else {
678 // Bottom alt plus half the height of a layer
679 double layerIncrement = _cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble() / 2.0;
680 return _scanBottomAltFact.rawValue().toDouble() + layerIncrement;
681 }
682}
683
685{
686 if (_startFromTopFact.rawValue().toBool()) {
687 // Structure Height minus half the layer height
688 double layerIncrement = _cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble() / 2.0;
689 return _structureHeightFact.rawValue().toDouble() - layerIncrement;
690 } else {
691 // Bottom alt plus all layers
692 double layerIncrement = (_cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble() / 2.0) + ((_layersFact.rawValue().toInt() - 1) * _cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble());
693 return _scanBottomAltFact.rawValue().toDouble() + layerIncrement;
694 }
695}
696
697void StructureScanComplexItem::_signalTopBottomAltChanged(void)
698{
699 emit topFlightAltChanged();
701}
702
703void StructureScanComplexItem::_recalcScanDistance()
704{
705 double scanDistance = 0;
706
707 if (_flightPolygon.count() > 2) {
708 QList<QGeoCoordinate> vertices = _flightPolygon.coordinateList();
709 for (int i=0; i<vertices.count() - 1; i++) {
710 scanDistance += vertices[i].distanceTo(vertices[i+1]);
711 }
712 scanDistance += vertices.last().distanceTo(vertices.first());
713
714 scanDistance *= _layersFact.rawValue().toInt();
715
716 double surfaceHeight = qMax(_structureHeightFact.rawValue().toDouble() - _scanBottomAltFact.rawValue().toDouble(), 0.0);
717 scanDistance += surfaceHeight;
718
719 qCDebug(StructureScanComplexItemLog) << "StructureScanComplexItem--_recalcScanDistance layers: "
720 << _layersFact.rawValue().toInt() << " structure height: " << surfaceHeight
721 << " scanDistance: " << _scanDistance;
722 }
723
724 if (!QGC::fuzzyCompare(_scanDistance, scanDistance)) {
725 _scanDistance = scanDistance;
727 }
728
729}
730
732{
733 return _structurePolygon.isValid() && _flightPolygon.isValid() && !_wizardMode ? ReadyForSave : NotReadyForSaveData;
734}
735
736void StructureScanComplexItem::_updateWizardMode(void)
737{
738 if (_structurePolygon.isValid() && !_structurePolygon.traceMode()) {
739 setWizardMode(false);
740 }
741}
742
744{
745 return _entranceAltFact.rawValue().toDouble();
746}
747
749{
750 return _entranceAltFact.rawValue().toDouble() + _missionController->plannedHomePosition().altitude();
751}
752
753// Never call this method directly. If you want to update the flight segments you emit _updateFlightPathSegmentsSignal()
754void StructureScanComplexItem::_updateFlightPathSegmentsDontCallDirectly(void)
755{
756 // Generation of flight segments depends on the following values:
757 // _flightPolygon,
758 // _startFromTopFact
759 // _structureHeightFact,
760 // _scanBottomAltFact
761 // _cameraCalc.adjustedFootprintFrontal()
762 // _missionController->plannedHomePosition().altitude()
763 // _entranceAltFact
764 // _layersFact
765 // Any changes to these values must rebuild the segments
766
769 emit terrainCollisionChanged(false);
770 _structurePolygon.setShowAltColor(false);
771 }
772
773 _flightPathSegments.beginResetModel();
774 _flightPathSegments.clearAndDeleteContents();
775
776 if (_flightPolygon.count() > 2) {
777
778 bool startFromTop = _startFromTopFact.rawValue().toBool();
779 double startAltitude = (startFromTop ? _structureHeightFact.rawValue().toDouble() : _scanBottomAltFact.rawValue().toDouble());
780
781 // Set up for the first layer
782 double prevLayerAltitude = 0;
783 double layerAltitude = startAltitude;
784 double halfLayerHeight = _cameraCalc.adjustedFootprintFrontal()->rawValue().toDouble() / 2.0;
785 if (startFromTop) {
786 layerAltitude -= halfLayerHeight;
787 } else {
788 layerAltitude += halfLayerHeight;
789 }
790 layerAltitude += _missionController->plannedHomePosition().altitude();
791
792 QGeoCoordinate layerEntranceCoord = _flightPolygon.vertexCoordinate(_entryVertex);
793
794 // Entrance to first layer entrance
795 double entranceAlt = _entranceAltFact.rawValue().toDouble() + _missionController->plannedHomePosition().altitude();
796 _appendFlightPathSegment(FlightPathSegment::SegmentTypeGeneric, layerEntranceCoord, entranceAlt, layerEntranceCoord, layerAltitude);
797
798 // Segments for each layer
799 for (int layerIndex=0; layerIndex<_layersFact.rawValue().toInt(); layerIndex++) {
800 // Move from one layer to the next
801 if (layerIndex != 0) {
802 _appendFlightPathSegment(FlightPathSegment::SegmentTypeGeneric, layerEntranceCoord, prevLayerAltitude, layerEntranceCoord, layerAltitude);
803 }
804
805 QGeoCoordinate prevCoord = QGeoCoordinate();
806 for (const QGeoCoordinate& coord: _flightPolygon.coordinateList()) {
807 if (prevCoord.isValid()) {
808 _appendFlightPathSegment(FlightPathSegment::SegmentTypeGeneric, prevCoord, layerAltitude, coord, layerAltitude);
809 }
810 prevCoord = coord;
811 }
812 _appendFlightPathSegment(FlightPathSegment::SegmentTypeGeneric, _flightPolygon.coordinateList().last(), layerAltitude, _flightPolygon.coordinateList().first(), layerAltitude);
813
814 // Move to next layer altitude
815 prevLayerAltitude = layerAltitude;
816 if (startFromTop) {
817 layerAltitude -= halfLayerHeight * 2;
818 } else {
819 layerAltitude += halfLayerHeight * 2;
820 }
821 }
822
823 // Last layer exit back to entrance
824 _appendFlightPathSegment(FlightPathSegment::SegmentTypeGeneric, layerEntranceCoord, prevLayerAltitude, layerEntranceCoord, entranceAlt);
825 }
826
827 _flightPathSegments.endResetModel();
828
830 emit terrainCollisionChanged(true);
831 }
832
833 _masterController->missionController()->recalcTerrainProfile();
834}
835
837{
838 double minAlt = qMin(bottomFlightAlt(), _entranceAltFact.rawValue().toDouble());
839 return minAlt + _missionController->plannedHomePosition().altitude();
840}
841
843{
844 double maxAlt = qMax(topFlightAlt(), _entranceAltFact.rawValue().toDouble());
845 return maxAlt + _missionController->plannedHomePosition().altitude();
846}
847
848void StructureScanComplexItem::_segmentTerrainCollisionChanged(bool terrainCollision)
849{
851 _structurePolygon.setShowAltColor(_cTerrainCollisionSegments != 0);
852}
#define qgcApp()
#define QGC_LOGGING_CATEGORY(name, categoryStr)
Fact * distanceToSurface(void)
Definition CameraCalc.h:53
void isManualCameraChanged(void)
void isIncompleteChanged(void)
static constexpr const char * jsonComplexItemTypeKey
This mission item attribute specifies the type of the complex item.
void _appendFlightPathSegment(FlightPathSegment::SegmentType segmentType, const QGeoCoordinate &coord1, double coord1AMSLAlt, const QGeoCoordinate &coord2, double coord2AMSLAlt)
QmlObjectListModel _flightPathSegments
void greatestDistanceToChanged(void)
void maxAMSLAltitudeChanged(void)
virtual bool terrainCollision(void) const
void minAMSLAltitudeChanged(void)
void complexDistanceChanged(void)
ComplexMissionItem(PlanMasterController *masterController, bool flyView)
void terrainCollisionChanged(bool terrainCollision)
virtual void _segmentTerrainCollisionChanged(bool terrainCollision)
Holds the meta data associated with a Fact.
void rawValueChanged(const QVariant &value)
QVariant rawValue() const
Definition Fact.h:87
void valueChanged(const QVariant &value)
This signal is only meant for use by the QT property system. It should not be connected to by client ...
virtual QList< MAV_CMD > supportedMissionCommands(QGCMAVLinkTypes::VehicleClass_t) const
List of supported mission commands. Empty list for all commands supported.
void plannedHomePositionChanged(QGeoCoordinate plannedHomePosition)
Master controller for mission, fence, rally.
void dirtyChanged(bool dirty)
Q_INVOKABLE bool offset(double distance)
bool isValid(void) const
bool traceMode(void) const
QList< QGeoCoordinate > coordinateList(void) const
Returns the path in a list of QGeoCoordinate's format.
void traceModeChanged(bool traceMode)
static constexpr const char * jsonPolygonKey
Q_INVOKABLE void clear(void)
bool isValidChanged(void)
void setShowAltColor(bool showAltColor)
void pathChanged(void)
void countChanged(int count)
static SettingsManager * instance()
int lastSequenceNumber(void) const final
void setCoordinate(const QGeoCoordinate &coordinate) final
void bottomFlightAltChanged(void)
void setMissionFlightStatus(MissionFlightStatus_t &missionFlightStatus) final
void applyNewAltitude(double newAltitude) final
Adjust the altitude of the item if appropriate to the new altitude.
bool load(const QJsonObject &complexObject, int sequenceNumber, QString &errorString) final
bool usesAltitudeFrame(QGroundControlQmlGlobal::AltitudeFrame altitudeFrame) const final
QGeoCoordinate coordinate(void) const final
Q_INVOKABLE void rotateEntryPoint(void)
void save(QJsonArray &missionItems) final
static constexpr const char * structureHeightName
void timeBetweenShotsChanged(void)
void cameraShotsChanged(int cameraShots)
static constexpr const char * layersName
static constexpr const char * jsonComplexItemTypeValue
void topFlightAltChanged(void)
void setSequenceNumber(int sequenceNumber) final
static constexpr const char * scanBottomAltName
ReadyForSaveState readyForSaveState(void) const final
double editableAlt(void) const final
static constexpr const char * gimbalPitchName
double minAMSLAltitude(void) const final
StructureScanComplexItem(PlanMasterController *masterController, bool flyView, const QString &kmlOrSHPFile)
void _updateFlightPathSegmentsSignal(void)
double greatestDistanceTo(const QGeoCoordinate &other) const final
double amslEntryAlt(void) const final
double maxAMSLAltitude(void) const final
void appendMissionItems(QList< MissionItem * > &items, QObject *missionItemParent) final
static constexpr const char * startFromTopName
static constexpr const char * settingsGroup
int sequenceNumber(void) const final
void firmwareTypeChanged()
FirmwarePlugin * firmwarePlugin()
Provides access to the Firmware Plugin for this Vehicle.
Definition Vehicle.h:465
PlanMasterController * _masterController
void _setBoundingCube(QGCGeoBoundingCube bc)
bool flyView(void) const
static constexpr const char * jsonTypeComplexItemValue
Item type is Complex Item.
void amslExitAltChanged(double alt)
QString _editorQml
Qml resource for editing item.
void setWizardMode(bool wizardMode)
void lastSequenceNumberChanged(int sequenceNumber)
void entryCoordinateChanged(const QGeoCoordinate &entryCoordinate)
void specifiesCoordinateChanged(void)
void coordinateChanged(const QGeoCoordinate &coordinate)
void readyForSaveStateChanged(void)
MissionController * _missionController
double distance(void) const
void amslEntryAltChanged(double alt)
bool _wizardMode
true: Item editor is showing wizard completion panel
static constexpr const char * jsonTypeKey
Json file attribute which specifies the item type.
void _amslEntryAltChanged(void)
void dirtyChanged(bool dirty)
void sequenceNumberChanged(int sequenceNumber)
void exitCoordinateChanged(const QGeoCoordinate &exitCoordinate)
PlanMasterController * masterController(void)
void wizardModeChanged(bool wizardMode)
virtual void setMissionFlightStatus(MissionFlightStatus_t &missionFlightStatus)
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
double surfaceHeight(const QList< float > &heights, int gridSize, const LodDeltas &lodDeltas, double row, double col)
Definition PatchMesh.h:69
bool fuzzyCompare(double value1, double value2)
Returns true if the two values are equal or close. Correctly handles 0 and NaN values.
Definition QGCMath.cc:71
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Definition AppMessages.cc:9
double vehicleSpeed
Either cruise or hover speed based on vehicle type and vtol state.
static constexpr VehicleClass_t VehicleClassGeneric